Terminal: Define separate mode for buffer inventory slots

This commit is contained in:
Shadowfacts 2019-10-30 14:55:29 -04:00
parent 14125143dc
commit 8a20837f11
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 61 additions and 2 deletions

View File

@ -36,7 +36,7 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), Invento
} }
private val inventoryCache = mutableMapOf<MACAddress, GroupedItemInv>() private val inventoryCache = mutableMapOf<MACAddress, GroupedItemInv>()
val internalBuffer = BasicInventory(18) val internalBuffer = TerminalBufferInventory(18)
private val pendingRequests = LinkedList<StackLocateRequest>() private val pendingRequests = LinkedList<StackLocateRequest>()
@ -84,7 +84,7 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), Invento
} }
private fun handleItemStack(packet: ItemStackPacket) { private fun handleItemStack(packet: ItemStackPacket) {
val remaining = internalBuffer.insert(packet.stack) val remaining = internalBuffer.insertFromNetwork(packet.stack)
if (!remaining.isEmpty) { if (!remaining.isEmpty) {
// todo: calculate entity spawn point by finding non-obstructed location // todo: calculate entity spawn point by finding non-obstructed location
val entity = ItemEntity(world!!, pos.x.toDouble(), pos.y + 1.0, pos.z.toDouble(), remaining) val entity = ItemEntity(world!!, pos.x.toDouble(), pos.y + 1.0, pos.z.toDouble(), remaining)

View File

@ -0,0 +1,59 @@
package net.shadowfacts.phycon.network.block.terminal
import alexiil.mc.lib.attributes.item.ItemStackUtil
import net.minecraft.inventory.BasicInventory
import net.minecraft.item.ItemStack
import kotlin.math.min
/**
* @author shadowfacts
*/
class TerminalBufferInventory(size: Int): BasicInventory(size) {
enum class Mode {
TO_NETWORK, FROM_NETWORK, UNASSIGNED
}
private val modes = Array(size) { Mode.UNASSIGNED }
fun insertFromNetwork(stack: ItemStack): ItemStack {
var remaining = stack.copy()
for (slot in 0 until invSize) {
if (modes[slot] == Mode.TO_NETWORK) continue
remaining = insertFromNetwork(stack, slot)
if (remaining.isEmpty) {
break
}
}
return remaining
}
private fun insertFromNetwork(stack: ItemStack, slot: Int): ItemStack {
val mode = modes[slot]
if (mode == Mode.TO_NETWORK) return stack
val current = getInvStack(slot)
if (current.isEmpty) {
setInvStack(slot, stack)
modes[slot] = Mode.FROM_NETWORK
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
return stack
} else {
return stack
}
}
override fun setInvStack(slot: Int, stack: ItemStack) {
if (stack.isEmpty) {
modes[slot] = Mode.UNASSIGNED
}
super.setInvStack(slot, stack)
}
}