diff --git a/src/main/kotlin/net/shadowfacts/phycon/block/inserter/InserterBlockEntity.kt b/src/main/kotlin/net/shadowfacts/phycon/block/inserter/InserterBlockEntity.kt index f6e9030..5170400 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/block/inserter/InserterBlockEntity.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/block/inserter/InserterBlockEntity.kt @@ -37,7 +37,7 @@ class InserterBlockEntity: DeviceBlockEntity(PhyBlockEntities.INSERTER), private var inventory: ItemInsertable? = null private var currentRequest: PendingExtractRequest? = null - var stackToExtract = ItemStack.EMPTY + var stackToExtract: ItemStack = ItemStack.EMPTY var amountToExtract = 1 override val controller = ActivationController(SLEEP_TIME, this) diff --git a/src/main/kotlin/net/shadowfacts/phycon/block/inserter/InserterScreenHandler.kt b/src/main/kotlin/net/shadowfacts/phycon/block/inserter/InserterScreenHandler.kt index 5a8ffdf..dbaa515 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/block/inserter/InserterScreenHandler.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/block/inserter/InserterScreenHandler.kt @@ -2,7 +2,6 @@ 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 @@ -12,6 +11,7 @@ import net.minecraft.util.Identifier import net.shadowfacts.phycon.PhysicalConnectivity import net.shadowfacts.phycon.init.PhyBlocks import net.shadowfacts.phycon.init.PhyScreens +import net.shadowfacts.phycon.util.GhostSlot import kotlin.math.min /** @@ -27,8 +27,6 @@ class InserterScreenHandler( val ID = Identifier(PhysicalConnectivity.MODID, "inserter") } - private val fakeInv = FakeInventory(inserter) - constructor(syncId: Int, playerInv: PlayerInventory, buf: PacketByteBuf): this( syncId, @@ -38,7 +36,7 @@ class InserterScreenHandler( init { // fake slot - addSlot(FakeSlot(fakeInv, 31, 20)) + addSlot(GhostSlot(inserter::stackToExtract, 31, 20)) // player inv for (y in 0 until 3) { @@ -85,38 +83,4 @@ class InserterScreenHandler( 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 - - } - } diff --git a/src/main/kotlin/net/shadowfacts/phycon/util/GhostSlot.kt b/src/main/kotlin/net/shadowfacts/phycon/util/GhostSlot.kt new file mode 100644 index 0000000..8c3c8c8 --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/phycon/util/GhostSlot.kt @@ -0,0 +1,45 @@ +package net.shadowfacts.phycon.util + +import net.minecraft.entity.player.PlayerEntity +import net.minecraft.inventory.Inventory +import net.minecraft.item.ItemStack +import net.minecraft.screen.slot.Slot +import kotlin.reflect.KMutableProperty + +/** + * @author shadowfacts + */ +class GhostSlot(prop: KMutableProperty, x: Int, y: Int): Slot(GhostInv(prop), 0, x, y) { + + override fun canInsert(stack: ItemStack) = false + + override fun setStack(itemStack: ItemStack?) { + } + + override fun canTakeItems(player: PlayerEntity) = false + + class GhostInv(private val prop: KMutableProperty): Inventory { + override fun clear() { + prop.setter.call(ItemStack.EMPTY) + } + + override fun size() = 1 + + override fun isEmpty() = prop.getter.call().isEmpty + + override fun getStack(i: Int): ItemStack { + return if (i == 0) prop.getter.call() + 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 + } +}