package net.shadowfacts.phycon.plugin.techreborn import alexiil.mc.lib.attributes.Simulation import alexiil.mc.lib.attributes.item.GroupedItemInv import alexiil.mc.lib.attributes.item.GroupedItemInvView import alexiil.mc.lib.attributes.item.ItemStackCollections import alexiil.mc.lib.attributes.item.ItemStackUtil import alexiil.mc.lib.attributes.item.filter.ItemFilter import net.minecraft.item.ItemStack import net.shadowfacts.phycon.util.copyWithCount import techreborn.blockentity.storage.item.StorageUnitBaseBlockEntity import kotlin.math.min /** * @author shadowfacts */ class StorageUnitWrapper( val be: StorageUnitBaseBlockEntity, ): GroupedItemInv { override fun getStoredStacks(): Set { val set = ItemStackCollections.set() if (!be.storedStack.isEmpty) { set.add(be.storedStack) } return set } override fun getTotalCapacity(): Int { return be.maxCapacity } override fun getStatistics(filter: ItemFilter): GroupedItemInvView.ItemInvStatistic { // todo: should spaceAddable really be zero? that's what SimpleGroupedItemInv does return if (be.storedStack.isEmpty) { GroupedItemInvView.ItemInvStatistic(filter, 0, 0, totalCapacity) } else if (filter.matches(be.storedStack)) { // don't use the storedAmount field, it's only used on the client for rendering val amount = be.getStoredAmount() GroupedItemInvView.ItemInvStatistic(filter, amount, 0, totalCapacity - amount) } else { GroupedItemInvView.ItemInvStatistic(filter, 0, 0, 0) } } override fun attemptInsertion(filter: ItemStack, simulation: Simulation): ItemStack { if (simulation.isAction) { return be.processInput(filter) } if (be.storedStack.isEmpty) { return ItemStack.EMPTY } if (!ItemStackUtil.areEqualIgnoreAmounts(be.storedStack, filter)) { return filter } val availableCapacity = totalCapacity - be.getStoredAmount() return if (availableCapacity >= filter.count) { ItemStack.EMPTY } else { filter.copyWithCount(filter.count - availableCapacity) } } override fun attemptExtraction(filter: ItemFilter, maxAmount: Int, simulation: Simulation): ItemStack { if (be.storedStack.isEmpty || !filter.matches(be.storedStack)) { return ItemStack.EMPTY } val extracted = min(maxAmount, be.getStoredAmount()) if (simulation.isAction) { be.storedStack.decrement(extracted) } return be.storedStack.copyWithCount(extracted) } }