diff --git a/src/main/kotlin/lu/kolja/expandedgt/common/machines/input/ExpMEInputBusPartMachine.kt b/src/main/kotlin/lu/kolja/expandedgt/common/machines/input/ExpMEInputBusPartMachine.kt index 20bbffc..4e4500c 100644 --- a/src/main/kotlin/lu/kolja/expandedgt/common/machines/input/ExpMEInputBusPartMachine.kt +++ b/src/main/kotlin/lu/kolja/expandedgt/common/machines/input/ExpMEInputBusPartMachine.kt @@ -1,20 +1,13 @@ package lu.kolja.expandedgt.common.machines.input -import appeng.api.stacks.GenericStack import com.gregtechceu.gtceu.api.machine.IMachineBlockEntity import com.gregtechceu.gtceu.api.machine.trait.NotifiableItemStackHandler -import com.gregtechceu.gtceu.common.item.IntCircuitBehaviour import com.gregtechceu.gtceu.integration.ae2.machine.MEInputBusPartMachine import com.gregtechceu.gtceu.integration.ae2.slot.ExportOnlyAEItemList -import com.lowdragmc.lowdraglib.gui.widget.LabelWidget -import com.lowdragmc.lowdraglib.gui.widget.Widget -import com.lowdragmc.lowdraglib.gui.widget.WidgetGroup import com.lowdragmc.lowdraglib.syncdata.field.ManagedFieldHolder -import com.lowdragmc.lowdraglib.utils.Position -import lu.kolja.expandedgt.common.machines.widgets.ae2.ExpAEItemConfigWidget -import net.minecraft.nbt.CompoundTag -open class ExpMEInputBusPartMachine(holder: IMachineBlockEntity, vararg args: Any): MEInputBusPartMachine(holder, args) { +open class ExpMEInputBusPartMachine(holder: IMachineBlockEntity, vararg args: Any) : + MEInputBusPartMachine(holder, args) { open val managedFieldHolder = ManagedFieldHolder(ExpMEInputBusPartMachine::class.java, MANAGED_FIELD_HOLDER) companion object { @@ -27,59 +20,4 @@ open class ExpMEInputBusPartMachine(holder: IMachineBlockEntity, vararg args: An this.aeItemHandler = ExportOnlyAEItemList(this, SIZE) return this.aeItemHandler } - - override fun createUIWidget(): Widget { - val group = WidgetGroup(Position(0, 0)) - - group.addWidget(LabelWidget(3, 0, - if (this.isOnline) "gtceu.gui.me_network.online" else "gtceu.gui.me_network.offline" - )) - val left = this.aeItemHandler.inventory.copyOfRange(0, SIZE / 2) - val right = this.aeItemHandler.inventory.copyOfRange(SIZE / 2, SIZE) - val firstWidget = ExpAEItemConfigWidget(3, 10, aeItemHandler, left) - val secondWidget = ExpAEItemConfigWidget(3, 10 + 76, aeItemHandler, right) - firstWidget.otherWidget = secondWidget - secondWidget.otherWidget = firstWidget - group.addWidget(firstWidget) - group.addWidget(secondWidget) - return group - } - - override fun writeConfigToTag(): CompoundTag { - val tag = CompoundTag() - val configStacks = CompoundTag() - tag.put("ConfigStacks", configStacks) - for (i in 0..? = null - - init { - this.autoPullTest = Predicate { false } - } - - override fun addedToController(controller: IMultiController) { - super.addedToController(controller) - super.addedToController(controller) - } - - override fun removedFromController(controller: IMultiController) { - super.removedFromController(controller) - super.removedFromController(controller) + companion object { + const val SIZE = 32 } override fun createInventory(vararg args: Any?): NotifiableItemStackHandler { @@ -75,252 +19,4 @@ class ExpMEStockingBusPartMachine(holder: IMachineBlockEntity, vararg args: Any) } override fun getFieldHolder() = managedFieldHolder - - override fun autoIO() { - super.autoIO() - if (ticksPerCycle == 0) ticksPerCycle = ConfigHolder.INSTANCE.compat.ae2.updateIntervals - if (offsetTimer.toInt() % ticksPerCycle == 0) { - if (autoPull) refreshList() - syncME() - } - } - - override fun syncME() { - this.mainNode.grid?.let { - val networkInv = it.storageService.inventory - for (slot in this.aeItemHandler.inventory) { - val config = slot.config - if (config != null) { - val key = config.what - val extracted = networkInv.extract(key, Long.MAX_VALUE, Actionable.SIMULATE, actionSource) - if (extracted >= minStackSize) { - slot.stock = GenericStack(key, extracted) - continue - } - } - slot.stock = null - } - } - } - - override fun attachSideTabs(sideTabs: TabsWidget) { - sideTabs.mainTab = this - } - - override fun flushInventory() { - // NO-OP - } - - override fun setDistinct(distinct: Boolean) { - super.setDistinct(distinct) - if (!isRemote && !isDistinct) validateConfig() - } - - override fun getSlotList(): IConfigurableSlotList = this.aeItemHandler - - override fun testConfiguredInOtherPart(config: GenericStack?): Boolean { - if (config == null) return false - // In distinct mode, we don't need to check other buses since only one bus can run a recipe at a time. - if (!isFormed || isDistinct) return false - - // Otherwise, we need to test for if the item is configured - // in any stocking bus in the multi (besides ourselves). - for (controller in controllers) { - for (part in controller.parts) { - if (part is ExpMEStockingBusPartMachine) { - // We don't need to check for this ourselves, as this case is handled elsewhere. - if (part == this || part.isDistinct) continue - if (part.aeItemHandler.hasStackInConfig(config, false)) { - return true - } - } - } - } - return false - } - - override fun getMinStackSize() = this.minStackSize - - override fun setMinStackSize(newSize: Int) { - this.minStackSize = newSize - } - - override fun getTicksPerCycle() = this.ticksPerCycle - - override fun setTicksPerCycle(newSize: Int) { - this.ticksPerCycle = newSize - } - - override fun isAutoPull() = autoPull - - override fun setAutoPull(autoPull: Boolean) { - this.autoPull = autoPull - if (!isRemote) { - if (!this.autoPull) this.aeItemHandler.clearInventory(0) - else if (updateMEStatus()) { - this.refreshList() - updateInventorySubscription() - } - } - } - - override fun setAutoPullTest(test: Predicate?) { - this.autoPullTest = test - } - - fun refreshList() { - val grid = this.mainNode.grid - if (grid == null) { - this.aeItemHandler.clearInventory(0) - return - } - - val networkStorage = grid.storageService.inventory - val counter = networkStorage.availableStacks - - val topItems = PriorityQueue>( - compareBy { it.longValue } - ) - - for (entry in counter) { - val amount = entry.longValue - val what = entry.key - if (amount <= 0) continue - if (what !is AEItemKey) continue - - val request = networkStorage.extract(what, amount, Actionable.SIMULATE, this.actionSource) - if (request == 0L) continue - - if (autoPullTest != null && !autoPullTest!!.test(GenericStack(what, amount))) continue - if (amount >= minStackSize) { - if (topItems.size < SIZE) { - topItems.offer(entry) - } else if (amount > topItems.peek().longValue) { - topItems.poll() - topItems.offer(entry) - } - } - } - - var index = 0 - val itemAmount = topItems.size - for (i in 0...attachConfigurators(configuratorPanel) - super.attachConfigurators(configuratorPanel) - configuratorPanel.attachConfigurators(AutoStockingFancyConfigurator(this)) - } - - override fun onScrewdriverClick( - playerIn: Player, - hand: InteractionHand, - gridSide: Direction, - hitResult: BlockHitResult - ): InteractionResult { - if (!isRemote) { - setAutoPull(!autoPull) - if (autoPull) playerIn.sendSystemMessage("gtceu.machine.me.stocking_auto_pull_enabled".translate()) - else playerIn.sendSystemMessage("gtceu.machine.me.stocking_auto_pull_disabled".translate()) - } - return InteractionResult.sidedSuccess(isRemote) - } - - override fun writeConfigToTag(): CompoundTag { - if (!autoPull) { - val tag = super.writeConfigToTag() - tag.putBoolean("AutoPull", false) - return tag; - } - // if in auto-pull, no need to write actual configured slots, but still need to write the ghost circuit - val tag = CompoundTag(); - tag.putBoolean("AutoPull", true) - tag.putByte("GhostCircuit", IntCircuitBehaviour.getCircuitConfiguration(circuitInventory.getStackInSlot(0)).toByte()) - return tag - } - - override fun readConfigFromTag(tag: CompoundTag) { - if (tag.getBoolean("AutoPull")) { - setAutoPull(true) - circuitInventory.setStackInSlot(0, IntCircuitBehaviour.stack(tag.getByte("GhostCircuit").toInt())) - return - } - setAutoPull(false) - super.readConfigFromTag(tag) - } - - - private inner class ExportOnlyAEStockingItemList(holder: MetaMachine, slots: Int) : ExportOnlyAEItemList(holder, slots, ::ExportOnlyAEStockingItemSlot) { - override fun isAutoPull() = autoPull - - override fun isStocking() = true - - override fun hasStackInConfig(stack: GenericStack?, checkExternal: Boolean): Boolean { - val inThisBus = super.hasStackInConfig(stack, false) - if (inThisBus) return true - if (checkExternal) return testConfiguredInOtherPart(stack) - return false - } - } - - private inner class ExportOnlyAEStockingItemSlot : ExportOnlyAEItemSlot { - constructor() : super() - - constructor(config: GenericStack?, stock: GenericStack?) : super(config, stock) - - override fun extractItem(slot: Int, amount: Int, simulate: Boolean): ItemStack { - if (slot == 0 && this.stock != null) { - if (this.config != null) { - // Extract the items from the real net to either validate (simulate) - // or extract (modulate) when this is called - if (!isOnline()) return ItemStack.EMPTY - mainNode.grid?.let { - val aeNetwork = it.storageService.inventory - - val action = if (simulate) Actionable.SIMULATE else Actionable.MODULATE - val key = config!!.what - val extracted = aeNetwork.extract(key, amount.toLong(), action, actionSource) - - if (extracted > 0) { - val resultStack = if (key is AEItemKey) key.toStack(extracted.toInt()) else ItemStack.EMPTY - if (!simulate) { - // may as well update the display here - this.stock = copy(stock, stock!!.amount - extracted) - if (this.stock!!.amount == 0L) { - this.stock = null - } - if (this.onContentsChanged != null) { - this.onContentsChanged.run() - } - } - return resultStack - } - } - } - } - return ItemStack.EMPTY - } - - override fun copy(): ExportOnlyAEStockingItemSlot { - return ExportOnlyAEStockingItemSlot( - if (this.config == null) null else copy(this.config), - if (this.stock == null) null else copy(this.stock) - ) - } - } -} \ No newline at end of file +} diff --git a/src/main/kotlin/lu/kolja/expandedgt/common/machines/input/stocking/ExpMEStockingHatchPartMachine.kt b/src/main/kotlin/lu/kolja/expandedgt/common/machines/input/stocking/ExpMEStockingHatchPartMachine.kt index 66cee93..d770256 100644 --- a/src/main/kotlin/lu/kolja/expandedgt/common/machines/input/stocking/ExpMEStockingHatchPartMachine.kt +++ b/src/main/kotlin/lu/kolja/expandedgt/common/machines/input/stocking/ExpMEStockingHatchPartMachine.kt @@ -1,75 +1,16 @@ package lu.kolja.expandedgt.common.machines.input.stocking -import appeng.api.config.Actionable -import appeng.api.stacks.AEFluidKey -import appeng.api.stacks.AEItemKey -import appeng.api.stacks.AEKey -import appeng.api.stacks.GenericStack -import com.gregtechceu.gtceu.api.gui.fancy.ConfiguratorPanel -import com.gregtechceu.gtceu.api.gui.fancy.TabsWidget import com.gregtechceu.gtceu.api.machine.IMachineBlockEntity -import com.gregtechceu.gtceu.api.machine.MetaMachine -import com.gregtechceu.gtceu.api.machine.fancyconfigurator.AutoStockingFancyConfigurator -import com.gregtechceu.gtceu.api.machine.feature.multiblock.IMultiController import com.gregtechceu.gtceu.api.machine.trait.NotifiableFluidTank -import com.gregtechceu.gtceu.common.item.IntCircuitBehaviour -import com.gregtechceu.gtceu.config.ConfigHolder -import com.gregtechceu.gtceu.integration.ae2.machine.feature.multiblock.IMEStockingPart -import com.gregtechceu.gtceu.integration.ae2.slot.ExportOnlyAEFluidList -import com.gregtechceu.gtceu.integration.ae2.slot.ExportOnlyAEFluidSlot -import com.gregtechceu.gtceu.integration.ae2.slot.IConfigurableSlotList -import com.gregtechceu.gtceu.integration.ae2.utils.AEUtil -import com.lowdragmc.lowdraglib.syncdata.annotation.DescSynced -import com.lowdragmc.lowdraglib.syncdata.annotation.DropSaved -import com.lowdragmc.lowdraglib.syncdata.annotation.Persisted +import com.gregtechceu.gtceu.integration.ae2.machine.MEInputHatchPartMachine +import com.gregtechceu.gtceu.integration.ae2.machine.MEStockingHatchPartMachine import com.lowdragmc.lowdraglib.syncdata.field.ManagedFieldHolder -import it.unimi.dsi.fastutil.objects.Object2LongMap -import lombok.Getter -import lombok.Setter -import lu.kolja.expandedgt.common.machines.input.ExpMEInputHatchPartMachine -import lu.kolja.expandedgt.util.translate -import net.minecraft.core.Direction -import net.minecraft.nbt.CompoundTag -import net.minecraft.world.InteractionHand -import net.minecraft.world.InteractionResult -import net.minecraft.world.entity.player.Player -import net.minecraft.world.phys.BlockHitResult -import net.minecraftforge.fluids.FluidStack -import net.minecraftforge.fluids.capability.IFluidHandler -import java.util.* -import java.util.function.Predicate +class ExpMEStockingHatchPartMachine(holder: IMachineBlockEntity, vararg args: Any): MEStockingHatchPartMachine(holder, args) { + open val managedFieldHolder = ManagedFieldHolder(MEStockingHatchPartMachine::class.java, MEInputHatchPartMachine.MANAGED_FIELD_HOLDER) -class ExpMEStockingHatchPartMachine(holder: IMachineBlockEntity, vararg args: Any): ExpMEInputHatchPartMachine(holder, args), IMEStockingPart { - override val managedFieldHolder = ManagedFieldHolder(ExpMEStockingHatchPartMachine::class.java, super.managedFieldHolder) - - @DescSynced - @Persisted - @Getter - private var autoPull: Boolean = false - - @Persisted - @DropSaved - private var minStackSize: Int = 1 - @Persisted - @DropSaved - private var ticksPerCycle: Int = 40 - - @Setter - private var autoPullTest: Predicate? = null - - init { - this.autoPullTest = Predicate { false } - } - - override fun addedToController(controller: IMultiController) { - super.addedToController(controller) - super.addedToController(controller) - } - - override fun removedFromController(controller: IMultiController) { - super.removedFromController(controller) - super.removedFromController(controller) + companion object { + const val SIZE = 32 } override fun createTank( @@ -82,239 +23,4 @@ class ExpMEStockingHatchPartMachine(holder: IMachineBlockEntity, vararg args: An } override fun getFieldHolder() = managedFieldHolder - - override fun autoIO() { - super.autoIO() - if (ticksPerCycle == 0) ticksPerCycle = ConfigHolder.INSTANCE.compat.ae2.updateIntervals - if (offsetTimer.toInt() % ticksPerCycle == 0) { - if (autoPull) refreshList() - syncME() - } - } - - override fun syncME() { - this.mainNode.grid?.let { - val networkInv = it.storageService.inventory - for (slot in this.aeFluidHandler.inventory) { - val config = slot.config - if (config != null) { - val key = config.what - val extracted = networkInv.extract(key, Long.MAX_VALUE, Actionable.SIMULATE, actionSource) - if (extracted >= minStackSize) { - slot.stock = GenericStack(key, extracted) - continue - } - } - slot.stock = null - } - } - } - - override fun attachSideTabs(sideTabs: TabsWidget) { - sideTabs.mainTab = this - } - - override fun flushInventory() { - // NO-OP - } - - override fun getSlotList(): IConfigurableSlotList = this.aeFluidHandler - - override fun testConfiguredInOtherPart(config: GenericStack?): Boolean { - if (config == null) return false - if (!isFormed) return false - - for (controller in controllers) { - for (part in controller.parts) { - if (part is ExpMEStockingHatchPartMachine) { - if (part == this) continue - if (part.aeFluidHandler.hasStackInConfig(config, false)) { - return true - } - } - } - } - return false - } - - override fun getMinStackSize() = this.minStackSize - - override fun setMinStackSize(newSize: Int) { - this.minStackSize = newSize - } - - override fun getTicksPerCycle() = this.ticksPerCycle - - override fun setTicksPerCycle(newSize: Int) { - this.ticksPerCycle = newSize - } - - override fun isAutoPull() = autoPull - - override fun setAutoPull(autoPull: Boolean) { - this.autoPull = autoPull - if (!isRemote) { - if (!this.autoPull) this.aeFluidHandler.clearInventory(0) - else if (updateMEStatus()) { - this.refreshList() - updateTankSubscription() - } - } - } - - override fun setAutoPullTest(test: Predicate?) { - this.autoPullTest = test - } - - fun refreshList() { - val grid = this.mainNode.grid - if (grid == null) { - this.aeFluidHandler.clearInventory(0) - return - } - - val networkStorage = grid.storageService.inventory - val counter = networkStorage.availableStacks - - val topItems = PriorityQueue>( - compareBy { it.longValue } - ) - - for (entry in counter) { - val amount = entry.longValue - val what = entry.key - if (amount <= 0) continue - if (what !is AEItemKey) continue - - val request = networkStorage.extract(what, amount, Actionable.SIMULATE, this.actionSource) - if (request == 0L) continue - - if (autoPullTest != null && !autoPullTest!!.test(GenericStack(what, amount))) continue - if (amount >= minStackSize) { - if (topItems.size < SIZE) { - topItems.offer(entry) - } else if (amount > topItems.peek().longValue) { - topItems.poll() - topItems.offer(entry) - } - } - } - - var index = 0 - val itemAmount = topItems.size - for (i in 0...attachConfigurators(configuratorPanel) - super.attachConfigurators(configuratorPanel) - configuratorPanel.attachConfigurators(AutoStockingFancyConfigurator(this)) - } - - override fun onScrewdriverClick( - playerIn: Player, - hand: InteractionHand, - gridSide: Direction, - hitResult: BlockHitResult - ): InteractionResult { - if (!isRemote) { - setAutoPull(!autoPull) - if (autoPull) playerIn.sendSystemMessage("gtceu.machine.me.stocking_auto_pull_enabled".translate()) - else playerIn.sendSystemMessage("gtceu.machine.me.stocking_auto_pull_disabled".translate()) - } - return InteractionResult.sidedSuccess(isRemote) - } - - override fun writeConfigToTag(): CompoundTag { - if (!autoPull) { - val tag = super.writeConfigToTag() - tag.putBoolean("AutoPull", false) - return tag; - } - // if in auto-pull, no need to write actual configured slots, but still need to write the ghost circuit - val tag = CompoundTag(); - tag.putBoolean("AutoPull", true) - tag.putByte("GhostCircuit", IntCircuitBehaviour.getCircuitConfiguration(circuitInventory.getStackInSlot(0)).toByte()) - return tag - } - - override fun readConfigFromTag(tag: CompoundTag) { - if (tag.getBoolean("AutoPull")) { - setAutoPull(true) - circuitInventory.setStackInSlot(0, IntCircuitBehaviour.stack(tag.getByte("GhostCircuit").toInt())) - return - } - setAutoPull(false) - super.readConfigFromTag(tag) - } - - private inner class ExportOnlyAEStockingFluidList (holder: MetaMachine, slots: Int): ExportOnlyAEFluidList(holder, slots, ::ExportOnlyAEStockingFluidSlot) { - override fun isAutoPull() = autoPull - - override fun isStocking() = true - - override fun hasStackInConfig(stack: GenericStack?, checkExternal: Boolean): Boolean { - val inThisHatch = super.hasStackInConfig(stack, false) - if (inThisHatch) return true - if (checkExternal) return testConfiguredInOtherPart(stack) - return false - } - } - - private inner class ExportOnlyAEStockingFluidSlot: ExportOnlyAEFluidSlot { - constructor() : super() - - constructor(config: GenericStack?, stock: GenericStack?) : super(config, stock) - - override fun drain( - maxDrain: Int, - action: IFluidHandler.FluidAction - ): FluidStack { - if (this.stock != null && this.config != null) { - if (!isOnline()) return FluidStack.EMPTY - mainNode.grid?.let { - val aeNetwork = it.storageService.inventory - - val key = config!!.what - val extracted = aeNetwork.extract(key, maxDrain.toLong(), Actionable.of(action), actionSource) - - if (extracted > 0) { - val resultStack = if (key is AEFluidKey) AEUtil.toFluidStack(key, extracted) else FluidStack.EMPTY - if (action.execute()) { - this.stock = copy(this.stock, stock!!.amount - extracted) - if (this.stock!!.amount == 0L) { - this.stock = null - } - if (this.onContentsChanged != null) { - this.onContentsChanged.run() - } - } - return resultStack - } - } - } - return FluidStack.EMPTY - } - - override fun copy(): ExportOnlyAEFluidSlot { - return ExportOnlyAEStockingFluidSlot( - if (this.config == null) null else copy(this.config), - if (this.stock == null) null else copy(this.stock) - ) - } - } } \ No newline at end of file