Cleanup TerminalBufferInventory code

This commit is contained in:
Shadowfacts 2021-03-28 13:42:14 -04:00
parent 7286efcfc2
commit 4fa5a12746
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 7 additions and 9 deletions

View File

@ -109,7 +109,7 @@ abstract class AbstractTerminalBlockEntity(type: BlockEntityType<*>): DeviceBloc
}
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
// we "know" how much the count in the source inventory has changed

View File

@ -34,12 +34,12 @@ class TerminalBufferInventory(size: Int): SimpleInventory(size) {
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()
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) {
break
@ -48,19 +48,17 @@ class TerminalBufferInventory(size: Int): SimpleInventory(size) {
return remaining
}
private fun insertFromNetwork(stack: ItemStack, slot: Int): ItemStack {
val mode = modes[slot]
if (mode == Mode.TO_NETWORK) return stack
private fun tryInsert(stack: ItemStack, slot: Int, mode: Mode): ItemStack {
val current = getStack(slot)
if (current.isEmpty) {
setStack(slot, stack)
modes[slot] = Mode.FROM_NETWORK
markSlot(slot, mode)
return ItemStack.EMPTY
} else if (ItemStackUtil.areEqualIgnoreAmounts(stack, current)) {
val toTransfer = min(current.maxCount - current.count, stack.count)
current.count += toTransfer
stack.count -= toTransfer
modes[slot] = Mode.FROM_NETWORK
markSlot(slot, mode)
return stack
} else {
return stack