Skip to content

Commit

Permalink
Add configurable priority to the decompression module
Browse files Browse the repository at this point in the history
  • Loading branch information
62832 committed Dec 18, 2023
1 parent a0b2d76 commit 034056c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<AEKey> outputs = new Object2LongOpenHashMap<>();
private final Map<AEKey, Long> outputs = new HashMap<>();

private int priority = 0;

public DecompressionModulePart(IPartItem<?> partItem) {
super(partItem);
Expand All @@ -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<IPatternDetails> getAvailablePatterns() {
var grid = getMainNode().getGrid();
Expand All @@ -50,7 +76,7 @@ public List<IPatternDetails> getAvailablePatterns() {

@Override
public int getPatternPriority() {
return Integer.MAX_VALUE;
return priority;
}

@Override
Expand All @@ -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;
Expand Down Expand Up @@ -91,18 +117,51 @@ 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);
}
}

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();
}
}
4 changes: 2 additions & 2 deletions fabric/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 034056c

Please sign in to comment.