Cleanup TerminalBufferInventory code
This commit is contained in:
parent
7286efcfc2
commit
4fa5a12746
|
@ -109,7 +109,7 @@ abstract class AbstractTerminalBlockEntity(type: BlockEntityType<*>): DeviceBloc
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun doHandleItemStack(packet: ItemStackPacket): ItemStack {
|
override fun doHandleItemStack(packet: ItemStackPacket): ItemStack {
|
||||||
val remaining = internalBuffer.insertFromNetwork(packet.stack)
|
val remaining = internalBuffer.insert(packet.stack, TerminalBufferInventory.Mode.FROM_NETWORK)
|
||||||
|
|
||||||
// this happens outside the normal update loop because by receiving the item stack packet
|
// this happens outside the normal update loop because by receiving the item stack packet
|
||||||
// we "know" how much the count in the source inventory has changed
|
// we "know" how much the count in the source inventory has changed
|
||||||
|
|
|
@ -34,12 +34,12 @@ class TerminalBufferInventory(size: Int): SimpleInventory(size) {
|
||||||
tag.getIntArray("Modes").forEachIndexed { i, it -> modes[i] = Mode.values()[it] }
|
tag.getIntArray("Modes").forEachIndexed { i, it -> modes[i] = Mode.values()[it] }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun insertFromNetwork(stack: ItemStack): ItemStack {
|
fun insert(stack: ItemStack, mode: Mode): ItemStack {
|
||||||
var remaining = stack.copy()
|
var remaining = stack.copy()
|
||||||
for (slot in 0 until size()) {
|
for (slot in 0 until size()) {
|
||||||
if (modes[slot] == Mode.TO_NETWORK) continue
|
if (modes[slot] != mode && modes[slot] != Mode.UNASSIGNED) continue
|
||||||
|
|
||||||
remaining = insertFromNetwork(stack, slot)
|
remaining = tryInsert(stack, slot, mode)
|
||||||
|
|
||||||
if (remaining.isEmpty) {
|
if (remaining.isEmpty) {
|
||||||
break
|
break
|
||||||
|
@ -48,19 +48,17 @@ class TerminalBufferInventory(size: Int): SimpleInventory(size) {
|
||||||
return remaining
|
return remaining
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun insertFromNetwork(stack: ItemStack, slot: Int): ItemStack {
|
private fun tryInsert(stack: ItemStack, slot: Int, mode: Mode): ItemStack {
|
||||||
val mode = modes[slot]
|
|
||||||
if (mode == Mode.TO_NETWORK) return stack
|
|
||||||
val current = getStack(slot)
|
val current = getStack(slot)
|
||||||
if (current.isEmpty) {
|
if (current.isEmpty) {
|
||||||
setStack(slot, stack)
|
setStack(slot, stack)
|
||||||
modes[slot] = Mode.FROM_NETWORK
|
markSlot(slot, mode)
|
||||||
return ItemStack.EMPTY
|
return ItemStack.EMPTY
|
||||||
} else if (ItemStackUtil.areEqualIgnoreAmounts(stack, current)) {
|
} else if (ItemStackUtil.areEqualIgnoreAmounts(stack, current)) {
|
||||||
val toTransfer = min(current.maxCount - current.count, stack.count)
|
val toTransfer = min(current.maxCount - current.count, stack.count)
|
||||||
current.count += toTransfer
|
current.count += toTransfer
|
||||||
stack.count -= toTransfer
|
stack.count -= toTransfer
|
||||||
modes[slot] = Mode.FROM_NETWORK
|
markSlot(slot, mode)
|
||||||
return stack
|
return stack
|
||||||
} else {
|
} else {
|
||||||
return stack
|
return stack
|
||||||
|
|
Loading…
Reference in New Issue