From 2466923d9694b7d00a90619ae0109f26c4b149db Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 21 Mar 2021 11:48:45 -0400 Subject: [PATCH] Fix crash when opening Inserter/Redstone Emitter GUI in non-dev env --- .../phycon/block/inserter/InserterBlockEntity.kt | 7 ++++++- .../block/inserter/InserterScreenHandler.kt | 2 +- .../RedstoneEmitterBlockEntity.kt | 7 ++++++- .../RedstoneEmitterScreenHandler.kt | 2 +- .../net/shadowfacts/phycon/util/GhostSlot.kt | 15 +++++++++------ 5 files changed, 23 insertions(+), 10 deletions(-) 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 378742b..eeb2351 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/block/inserter/InserterBlockEntity.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/block/inserter/InserterBlockEntity.kt @@ -19,6 +19,7 @@ import net.shadowfacts.phycon.init.PhyBlockEntities import net.shadowfacts.phycon.packet.* import net.shadowfacts.phycon.util.ActivationMode import net.shadowfacts.phycon.util.ClientConfigurableDevice +import net.shadowfacts.phycon.util.GhostInv import kotlin.math.min /** @@ -27,7 +28,8 @@ import kotlin.math.min class InserterBlockEntity: DeviceBlockEntity(PhyBlockEntities.INSERTER), ItemStackPacketHandler, ActivationController.ActivatableDevice, - ClientConfigurableDevice { + ClientConfigurableDevice, + GhostInv { companion object { val SLEEP_TIME = 40L @@ -40,6 +42,9 @@ class InserterBlockEntity: DeviceBlockEntity(PhyBlockEntities.INSERTER), private var inventory: ItemInsertable? = null private var currentRequest: PendingExtractRequest? = null var stackToExtract: ItemStack = ItemStack.EMPTY + override var ghostSlotStack: ItemStack + get() = stackToExtract + set(value) { stackToExtract = value } 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 ba15769..6919774 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/block/inserter/InserterScreenHandler.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/block/inserter/InserterScreenHandler.kt @@ -37,7 +37,7 @@ class InserterScreenHandler( init { // fake slot - addSlot(GhostSlot(inserter::stackToExtract, 31, 20)) + addSlot(GhostSlot(inserter, 31, 20)) // player inv for (y in 0 until 3) { diff --git a/src/main/kotlin/net/shadowfacts/phycon/block/redstone_emitter/RedstoneEmitterBlockEntity.kt b/src/main/kotlin/net/shadowfacts/phycon/block/redstone_emitter/RedstoneEmitterBlockEntity.kt index 0329d43..6bacd6a 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/block/redstone_emitter/RedstoneEmitterBlockEntity.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/block/redstone_emitter/RedstoneEmitterBlockEntity.kt @@ -13,19 +13,24 @@ import net.shadowfacts.phycon.packet.DeviceRemovedPacket import net.shadowfacts.phycon.packet.ReadInventoryPacket import net.shadowfacts.phycon.packet.RequestInventoryPacket import net.shadowfacts.phycon.util.ClientConfigurableDevice +import net.shadowfacts.phycon.util.GhostInv import kotlin.math.round /** * @author shadowfacts */ class RedstoneEmitterBlockEntity: DeviceBlockEntity(PhyBlockEntities.REDSTONE_EMITTER), - ClientConfigurableDevice { + ClientConfigurableDevice, + GhostInv { private val inventoryCache = mutableMapOf() var cachedEmittedPower: Int = 0 private set var stackToMonitor: ItemStack = ItemStack.EMPTY + override var ghostSlotStack: ItemStack + get() = stackToMonitor + set(value) { stackToMonitor = value } var maxAmount = 64 var mode = Mode.ANALOG set(value) { diff --git a/src/main/kotlin/net/shadowfacts/phycon/block/redstone_emitter/RedstoneEmitterScreenHandler.kt b/src/main/kotlin/net/shadowfacts/phycon/block/redstone_emitter/RedstoneEmitterScreenHandler.kt index 36905e6..fcd0ea6 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/block/redstone_emitter/RedstoneEmitterScreenHandler.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/block/redstone_emitter/RedstoneEmitterScreenHandler.kt @@ -36,7 +36,7 @@ class RedstoneEmitterScreenHandler( init { // fake slot - addSlot(GhostSlot(emitter::stackToMonitor, 31, 20)) + addSlot(GhostSlot(emitter, 31, 20)) // player inv for (y in 0 until 3) { diff --git a/src/main/kotlin/net/shadowfacts/phycon/util/GhostSlot.kt b/src/main/kotlin/net/shadowfacts/phycon/util/GhostSlot.kt index 8c3c8c8..0f0d011 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/util/GhostSlot.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/util/GhostSlot.kt @@ -4,12 +4,11 @@ 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) { +class GhostSlot(inv: net.shadowfacts.phycon.util.GhostInv, x: Int, y: Int): Slot(GhostInv(inv), 0, x, y) { override fun canInsert(stack: ItemStack) = false @@ -18,17 +17,17 @@ class GhostSlot(prop: KMutableProperty, x: Int, y: Int): Slot(GhostIn override fun canTakeItems(player: PlayerEntity) = false - class GhostInv(private val prop: KMutableProperty): Inventory { + private class GhostInv(private val inv: net.shadowfacts.phycon.util.GhostInv): Inventory { override fun clear() { - prop.setter.call(ItemStack.EMPTY) + inv.ghostSlotStack = ItemStack.EMPTY } override fun size() = 1 - override fun isEmpty() = prop.getter.call().isEmpty + override fun isEmpty() = inv.ghostSlotStack.isEmpty override fun getStack(i: Int): ItemStack { - return if (i == 0) prop.getter.call() + return if (i == 0) inv.ghostSlotStack else ItemStack.EMPTY } @@ -43,3 +42,7 @@ class GhostSlot(prop: KMutableProperty, x: Int, y: Int): Slot(GhostIn override fun canPlayerUse(playerEntity: PlayerEntity?) = true } } + +interface GhostInv { + var ghostSlotStack: ItemStack +}