package net.shadowfacts.phycon.block.inserter import net.minecraft.entity.player.PlayerEntity import net.minecraft.entity.player.PlayerInventory import net.minecraft.inventory.Inventory import net.minecraft.item.ItemStack import net.minecraft.network.PacketByteBuf import net.minecraft.screen.ScreenHandler import net.minecraft.screen.slot.Slot import net.minecraft.screen.slot.SlotActionType import net.minecraft.util.Identifier import net.shadowfacts.phycon.PhysicalConnectivity import net.shadowfacts.phycon.init.PhyBlocks import net.shadowfacts.phycon.init.PhyScreens import kotlin.math.min /** * @author shadowfacts */ class InserterScreenHandler( syncId: Int, playerInv: PlayerInventory, val inserter: InserterBlockEntity, ): ScreenHandler(PhyScreens.INSERTER_SCREEN_HANDLER, syncId) { companion object { val ID = Identifier(PhysicalConnectivity.MODID, "inserter") } private val fakeInv = FakeInventory(inserter) constructor(syncId: Int, playerInv: PlayerInventory, buf: PacketByteBuf): this( syncId, playerInv, PhyBlocks.INSERTER.getBlockEntity(playerInv.player.world, buf.readBlockPos())!! ) init { // fake slot addSlot(FakeSlot(fakeInv, 31, 20)) // player inv for (y in 0 until 3) { for (x in 0 until 9) { addSlot(Slot(playerInv, x + y * 9 + 9, 8 + x * 18, 51 + y * 18)) } } // hotbar for (x in 0 until 9) { addSlot(Slot(playerInv, x, 8 + x * 18, 109)) } } private fun stackToExtractChanged() { inserter.amountToExtract = min(inserter.stackToExtract.maxCount, inserter.amountToExtract) } override fun canUse(player: PlayerEntity): Boolean { return true } override fun onSlotClick(slotId: Int, clickData: Int, actionType: SlotActionType, player: PlayerEntity): ItemStack { // fake slot if (slotId == 0) { if (player.inventory.cursorStack.isEmpty) { inserter.stackToExtract = ItemStack.EMPTY } else { val copy = player.inventory.cursorStack.copy() copy.count = 1 inserter.stackToExtract = copy } stackToExtractChanged() } return super.onSlotClick(slotId, clickData, actionType, player) } override fun transferSlot(player: PlayerEntity, slotId: Int): ItemStack { val slot = slots[slotId] val copy = slot.stack.copy() copy.count = 1 inserter.stackToExtract = copy stackToExtractChanged() return ItemStack.EMPTY } class FakeSlot(inv: FakeInventory, x: Int, y: Int): Slot(inv, 0, x, y) { override fun canInsert(stack: ItemStack) = false override fun setStack(stack: ItemStack) { } override fun canTakeItems(player: PlayerEntity) = false } class FakeInventory(val inserter: InserterBlockEntity): Inventory { override fun clear() { inserter.stackToExtract = ItemStack.EMPTY } override fun size() = 1 override fun isEmpty() = inserter.stackToExtract.isEmpty override fun getStack(i: Int): ItemStack { return if (i == 0) inserter.stackToExtract else ItemStack.EMPTY } override fun removeStack(i: Int, j: Int) = ItemStack.EMPTY override fun removeStack(i: Int) = ItemStack.EMPTY override fun setStack(i: Int, itemStack: ItemStack?) {} override fun markDirty() {} override fun canPlayerUse(playerEntity: PlayerEntity?) = true } }