Extract GhostSlot to separate class

This commit is contained in:
Shadowfacts 2021-02-28 22:01:45 -05:00
parent f5268aef51
commit 4c2148b30f
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 48 additions and 39 deletions

View File

@ -37,7 +37,7 @@ class InserterBlockEntity: DeviceBlockEntity(PhyBlockEntities.INSERTER),
private var inventory: ItemInsertable? = null private var inventory: ItemInsertable? = null
private var currentRequest: PendingExtractRequest? = null private var currentRequest: PendingExtractRequest? = null
var stackToExtract = ItemStack.EMPTY var stackToExtract: ItemStack = ItemStack.EMPTY
var amountToExtract = 1 var amountToExtract = 1
override val controller = ActivationController(SLEEP_TIME, this) override val controller = ActivationController(SLEEP_TIME, this)

View File

@ -2,7 +2,6 @@ package net.shadowfacts.phycon.block.inserter
import net.minecraft.entity.player.PlayerEntity import net.minecraft.entity.player.PlayerEntity
import net.minecraft.entity.player.PlayerInventory import net.minecraft.entity.player.PlayerInventory
import net.minecraft.inventory.Inventory
import net.minecraft.item.ItemStack import net.minecraft.item.ItemStack
import net.minecraft.network.PacketByteBuf import net.minecraft.network.PacketByteBuf
import net.minecraft.screen.ScreenHandler import net.minecraft.screen.ScreenHandler
@ -12,6 +11,7 @@ import net.minecraft.util.Identifier
import net.shadowfacts.phycon.PhysicalConnectivity import net.shadowfacts.phycon.PhysicalConnectivity
import net.shadowfacts.phycon.init.PhyBlocks import net.shadowfacts.phycon.init.PhyBlocks
import net.shadowfacts.phycon.init.PhyScreens import net.shadowfacts.phycon.init.PhyScreens
import net.shadowfacts.phycon.util.GhostSlot
import kotlin.math.min import kotlin.math.min
/** /**
@ -27,8 +27,6 @@ class InserterScreenHandler(
val ID = Identifier(PhysicalConnectivity.MODID, "inserter") val ID = Identifier(PhysicalConnectivity.MODID, "inserter")
} }
private val fakeInv = FakeInventory(inserter)
constructor(syncId: Int, playerInv: PlayerInventory, buf: PacketByteBuf): constructor(syncId: Int, playerInv: PlayerInventory, buf: PacketByteBuf):
this( this(
syncId, syncId,
@ -38,7 +36,7 @@ class InserterScreenHandler(
init { init {
// fake slot // fake slot
addSlot(FakeSlot(fakeInv, 31, 20)) addSlot(GhostSlot(inserter::stackToExtract, 31, 20))
// player inv // player inv
for (y in 0 until 3) { for (y in 0 until 3) {
@ -85,38 +83,4 @@ class InserterScreenHandler(
return ItemStack.EMPTY 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
}
} }

View File

@ -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<ItemStack>, 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<ItemStack>): 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
}
}