2021-02-28 18:48:39 +00:00
|
|
|
package net.shadowfacts.phycon.block.terminal
|
2019-10-30 18:55:29 +00:00
|
|
|
|
|
|
|
import alexiil.mc.lib.attributes.item.ItemStackUtil
|
2021-02-10 23:55:49 +00:00
|
|
|
import net.minecraft.inventory.SimpleInventory
|
2019-10-30 18:55:29 +00:00
|
|
|
import net.minecraft.item.ItemStack
|
2021-02-11 05:10:24 +00:00
|
|
|
import net.minecraft.nbt.CompoundTag
|
|
|
|
import net.minecraft.nbt.IntArrayTag
|
|
|
|
import net.shadowfacts.phycon.util.fromTag
|
|
|
|
import net.shadowfacts.phycon.util.toTag
|
2019-10-30 18:55:29 +00:00
|
|
|
import kotlin.math.min
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author shadowfacts
|
|
|
|
*/
|
2021-02-10 23:55:49 +00:00
|
|
|
class TerminalBufferInventory(size: Int): SimpleInventory(size) {
|
2019-10-30 18:55:29 +00:00
|
|
|
|
|
|
|
enum class Mode {
|
|
|
|
TO_NETWORK, FROM_NETWORK, UNASSIGNED
|
|
|
|
}
|
|
|
|
|
2021-02-11 05:10:24 +00:00
|
|
|
var modes = Array(size) { Mode.UNASSIGNED }
|
|
|
|
private set
|
|
|
|
|
|
|
|
fun toTag(): CompoundTag {
|
|
|
|
val compound = CompoundTag()
|
|
|
|
compound.put("Inventory", (this as SimpleInventory).toTag())
|
|
|
|
compound.put("Modes", IntArrayTag(modes.map(Mode::ordinal)))
|
|
|
|
return compound
|
|
|
|
}
|
|
|
|
|
|
|
|
fun fromTag(tag: CompoundTag) {
|
|
|
|
val inventory = tag.getList("Inventory", 10)
|
|
|
|
(this as SimpleInventory).fromTag(inventory)
|
|
|
|
tag.getIntArray("Modes").forEachIndexed { i, it -> modes[i] = Mode.values()[it] }
|
|
|
|
}
|
2019-10-30 18:55:29 +00:00
|
|
|
|
|
|
|
fun insertFromNetwork(stack: ItemStack): ItemStack {
|
|
|
|
var remaining = stack.copy()
|
2021-02-10 23:55:49 +00:00
|
|
|
for (slot in 0 until size()) {
|
2019-10-30 18:55:29 +00:00
|
|
|
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
|
2021-02-10 23:55:49 +00:00
|
|
|
val current = getStack(slot)
|
2019-10-30 18:55:29 +00:00
|
|
|
if (current.isEmpty) {
|
2021-02-10 23:55:49 +00:00
|
|
|
setStack(slot, stack)
|
2019-10-30 18:55:29 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 23:55:49 +00:00
|
|
|
override fun setStack(slot: Int, stack: ItemStack) {
|
2019-10-30 18:55:29 +00:00
|
|
|
if (stack.isEmpty) {
|
|
|
|
modes[slot] = Mode.UNASSIGNED
|
|
|
|
}
|
2021-02-10 23:55:49 +00:00
|
|
|
super.setStack(slot, stack)
|
2019-10-30 18:55:29 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 05:10:24 +00:00
|
|
|
override fun clear() {
|
|
|
|
super.clear()
|
|
|
|
this.modes = Array(size()) { Mode.UNASSIGNED }
|
|
|
|
}
|
|
|
|
|
2021-02-11 03:40:01 +00:00
|
|
|
fun getMode(slot: Int): Mode {
|
|
|
|
return modes[slot]
|
|
|
|
}
|
|
|
|
|
|
|
|
fun markSlot(slot: Int, mode: Mode) {
|
|
|
|
modes[slot] = mode
|
|
|
|
}
|
|
|
|
|
2019-10-30 18:55:29 +00:00
|
|
|
}
|