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-12-22 23:59:51 +00:00
|
|
|
import net.minecraft.nbt.NbtCompound
|
|
|
|
import net.minecraft.nbt.NbtIntArray
|
2021-02-11 05:10:24 +00:00
|
|
|
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
|
|
|
|
|
2021-12-22 23:59:51 +00:00
|
|
|
fun toTag(): NbtCompound {
|
|
|
|
val compound = NbtCompound()
|
2021-02-11 05:10:24 +00:00
|
|
|
compound.put("Inventory", (this as SimpleInventory).toTag())
|
2021-12-22 23:59:51 +00:00
|
|
|
compound.put("Modes", NbtIntArray(modes.map(Mode::ordinal)))
|
2021-02-11 05:10:24 +00:00
|
|
|
return compound
|
|
|
|
}
|
|
|
|
|
2021-12-22 23:59:51 +00:00
|
|
|
fun fromTag(tag: NbtCompound) {
|
2021-02-11 05:10:24 +00:00
|
|
|
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
|
|
|
|
2021-03-28 17:42:14 +00:00
|
|
|
fun insert(stack: ItemStack, mode: Mode): ItemStack {
|
2019-10-30 18:55:29 +00:00
|
|
|
var remaining = stack.copy()
|
2021-02-10 23:55:49 +00:00
|
|
|
for (slot in 0 until size()) {
|
2021-03-28 17:42:14 +00:00
|
|
|
if (modes[slot] != mode && modes[slot] != Mode.UNASSIGNED) continue
|
2019-10-30 18:55:29 +00:00
|
|
|
|
2021-03-28 17:42:14 +00:00
|
|
|
remaining = tryInsert(stack, slot, mode)
|
2019-10-30 18:55:29 +00:00
|
|
|
|
|
|
|
if (remaining.isEmpty) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return remaining
|
|
|
|
}
|
|
|
|
|
2021-03-28 17:42:14 +00:00
|
|
|
private fun tryInsert(stack: ItemStack, slot: Int, mode: Mode): ItemStack {
|
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)
|
2021-03-28 17:42:14 +00:00
|
|
|
markSlot(slot, mode)
|
2019-10-30 18:55:29 +00:00
|
|
|
return ItemStack.EMPTY
|
|
|
|
} else if (ItemStackUtil.areEqualIgnoreAmounts(stack, current)) {
|
|
|
|
val toTransfer = min(current.maxCount - current.count, stack.count)
|
|
|
|
current.count += toTransfer
|
|
|
|
stack.count -= toTransfer
|
2021-03-28 17:42:14 +00:00
|
|
|
markSlot(slot, mode)
|
2019-10-30 18:55:29 +00:00
|
|
|
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
|
|
|
}
|