From 034056c5c303ff76cc35522d40cd876b36756dc0 Mon Sep 17 00:00:00 2001 From: 90 Date: Mon, 18 Dec 2023 22:57:33 +0000 Subject: [PATCH] Add configurable priority to the decompression module --- .../item/part/DecompressionModulePart.java | 77 ++++++++++++++++--- fabric/build.gradle.kts | 4 +- 2 files changed, 70 insertions(+), 11 deletions(-) diff --git a/common/src/main/java/gripe/_90/megacells/item/part/DecompressionModulePart.java b/common/src/main/java/gripe/_90/megacells/item/part/DecompressionModulePart.java index 77b2a7d2..366ac697 100644 --- a/common/src/main/java/gripe/_90/megacells/item/part/DecompressionModulePart.java +++ b/common/src/main/java/gripe/_90/megacells/item/part/DecompressionModulePart.java @@ -1,9 +1,15 @@ package gripe._90.megacells.item.part; +import java.util.HashMap; import java.util.List; +import java.util.Map; -import it.unimi.dsi.fastutil.objects.Object2LongMap; -import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.phys.Vec3; import appeng.api.config.Actionable; import appeng.api.crafting.IPatternDetails; @@ -19,19 +25,27 @@ import appeng.api.parts.IPartModel; import appeng.api.stacks.AEKey; import appeng.api.stacks.KeyCounter; +import appeng.helpers.IPriorityHost; import appeng.items.parts.PartModels; +import appeng.menu.ISubMenu; +import appeng.menu.MenuOpener; +import appeng.menu.implementations.PriorityMenu; +import appeng.menu.locator.MenuLocators; import appeng.parts.AEBasePart; import appeng.parts.PartModel; import gripe._90.megacells.MEGACells; +import gripe._90.megacells.definition.MEGAItems; import gripe._90.megacells.misc.DecompressionPattern; import gripe._90.megacells.misc.DecompressionService; -public class DecompressionModulePart extends AEBasePart implements ICraftingProvider, IGridTickable { +public class DecompressionModulePart extends AEBasePart implements ICraftingProvider, IPriorityHost, IGridTickable { @PartModels public static final IPartModel MODEL = new PartModel(MEGACells.makeId("part/decompression_module")); - private final Object2LongMap outputs = new Object2LongOpenHashMap<>(); + private final Map outputs = new HashMap<>(); + + private int priority = 0; public DecompressionModulePart(IPartItem partItem) { super(partItem); @@ -42,6 +56,18 @@ public DecompressionModulePart(IPartItem partItem) { .setIdlePowerUsage(10.0); } + @Override + public void writeToNBT(CompoundTag data) { + super.writeToNBT(data); + data.putInt("priority", priority); + } + + @Override + public void readFromNBT(CompoundTag data) { + super.readFromNBT(data); + priority = data.getInt("priority"); + } + @Override public List getAvailablePatterns() { var grid = getMainNode().getGrid(); @@ -50,7 +76,7 @@ public List getAvailablePatterns() { @Override public int getPatternPriority() { - return Integer.MAX_VALUE; + return priority; } @Override @@ -60,7 +86,7 @@ public boolean pushPattern(IPatternDetails patternDetails, KeyCounter[] inputHol } var output = pattern.getPrimaryOutput(); - outputs.mergeLong(output.what(), output.amount(), Long::sum); + outputs.merge(output.what(), output.amount(), Long::sum); getMainNode().ifPresent((grid, node) -> grid.getTickManager().alertDevice(node)); return true; @@ -91,13 +117,13 @@ public TickingRequest getTickingRequest(IGridNode node) { public TickRateModulation tickingRequest(IGridNode node, int ticksSinceLastCall) { var storage = node.getGrid().getStorageService().getInventory(); - for (var output : outputs.object2LongEntrySet()) { + for (var output : new HashMap<>(outputs).entrySet()) { var what = output.getKey(); - var amount = output.getLongValue(); + var amount = output.getValue(); var inserted = storage.insert(what, amount, Actionable.MODULATE, IActionSource.ofMachine(this)); if (inserted >= amount) { - outputs.removeLong(what); + outputs.remove(what); } else if (inserted > 0) { outputs.put(what, amount - inserted); } @@ -105,4 +131,37 @@ public TickRateModulation tickingRequest(IGridNode node, int ticksSinceLastCall) return TickRateModulation.URGENT; } + + @Override + public boolean onPartActivate(Player player, InteractionHand hand, Vec3 pos) { + if (!player.getCommandSenderWorld().isClientSide()) { + MenuOpener.open(PriorityMenu.TYPE, player, MenuLocators.forPart(this)); + } + + return true; + } + + @Override + public int getPriority() { + return priority; + } + + @Override + public void setPriority(int newPriority) { + priority = newPriority; + getHost().markForSave(); + ICraftingProvider.requestUpdate(getMainNode()); + } + + @Override + public void returnToMainMenu(Player player, ISubMenu subMenu) { + if (player instanceof ServerPlayer serverPlayer) { + serverPlayer.closeContainer(); + } + } + + @Override + public ItemStack getMainMenuIcon() { + return MEGAItems.DECOMPRESSION_MODULE.stack(); + } } diff --git a/fabric/build.gradle.kts b/fabric/build.gradle.kts index 0ded5fb5..58094c17 100644 --- a/fabric/build.gradle.kts +++ b/fabric/build.gradle.kts @@ -66,8 +66,8 @@ dependencies { modImplementation(libs.appbot.fabric) { exclude(group = "dev.emi", module = "emi-fabric") } modRuntimeOnly(libs.botania.fabric) { exclude(group = "dev.emi", module = "emi-fabric") } - modRuntimeOnly(libs.ae2wtlib.fabric) - modRuntimeOnly(libs.cloth.fabric) + // modRuntimeOnly(libs.ae2wtlib.fabric) + // modRuntimeOnly(libs.cloth.fabric) modRuntimeOnly(libs.jei.fabric) modRuntimeOnly(libs.jade.fabric)