Skip to content

Commit

Permalink
Add priority to patterns in (dual) interfaces (#673)
Browse files Browse the repository at this point in the history
Co-authored-by: Julia Dijkstra <[email protected]>
  • Loading branch information
Vlamonster and Julia Dijkstra authored Feb 12, 2025
1 parent acf0603 commit 35fb695
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
45 changes: 28 additions & 17 deletions src/main/java/appeng/helpers/DualityInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,26 +181,31 @@ public void onChangeInventory(final IInventory inv, final int slot, final InvOpe
if (this.isWorking) {
return;
}

if (inv == this.config) {
this.readConfig();
} else if (inv == this.patterns && (removed != null || added != null)) {
this.updateCraftingList();
} else if (inv == this.storage && slot >= 0) {
final boolean had = this.hasWorkToDo();
} else if (inv == this.patterns) {
if (removed != null || added != null) {
this.updateCraftingList();
}
} else if (inv == this.storage) {
if (slot >= 0) {
final boolean had = this.hasWorkToDo();

this.updatePlan(slot);
this.updatePlan(slot);

final boolean now = this.hasWorkToDo();
final boolean now = this.hasWorkToDo();

if (had != now) {
try {
if (now) {
this.gridProxy.getTick().alertDevice(this.gridProxy.getNode());
} else {
this.gridProxy.getTick().sleepDevice(this.gridProxy.getNode());
if (had != now) {
try {
if (now) {
this.gridProxy.getTick().alertDevice(this.gridProxy.getNode());
} else {
this.gridProxy.getTick().sleepDevice(this.gridProxy.getNode());
}
} catch (final GridAccessException e) {
// :P
}
} catch (final GridAccessException e) {
// :P
}
}
} else if (inv == this.upgrades) {
Expand Down Expand Up @@ -389,7 +394,7 @@ public void updateCraftingList() {

for (int x = 0; x < accountedFor.length; x++) {
if (!accountedFor[x]) {
this.addToCraftingList(this.patterns.getStackInSlot(x));
this.addToCraftingList(x);
}
}

Expand Down Expand Up @@ -469,7 +474,9 @@ public void notifyNeighbors() {
}
}

protected void addToCraftingList(final ItemStack is) {
protected void addToCraftingList(final int slot) {
final ItemStack is = this.patterns.getStackInSlot(slot);

if (is == null) {
return;
}
Expand All @@ -482,6 +489,7 @@ protected void addToCraftingList(final ItemStack is) {
this.craftingList = new LinkedList<>();
}

details.setPriority(slot - 36 * this.getPriority());
this.craftingList.add(details);
}
}
Expand Down Expand Up @@ -1118,7 +1126,6 @@ private static boolean acceptsItems(final InventoryAdaptor ad, final InventoryCr
public void provideCrafting(final ICraftingProviderHelper craftingTracker) {
if (this.gridProxy.isActive() && this.craftingList != null) {
for (final ICraftingPatternDetails details : this.craftingList) {
details.setPriority(this.priority);
craftingTracker.addCraftingOption(this, details);
}
}
Expand Down Expand Up @@ -1332,6 +1339,10 @@ public void setPriority(final int newValue) {
this.priority = newValue;
this.markDirty();

// Update the priority of stored patterns.
this.craftingList = null;
this.updateCraftingList();

try {
this.gridProxy.getGrid().postEvent(new MENetworkCraftingPatternChange(this, this.gridProxy.getNode()));
} catch (final GridAccessException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand All @@ -28,6 +29,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;
import java.util.function.IntConsumer;
import java.util.stream.IntStream;
Expand Down Expand Up @@ -121,8 +123,10 @@ public final class CraftingCPUCluster implements IAECluster, ICraftingCPU {
private final WorldCoord min;
private final WorldCoord max;
private final int[] usedOps = new int[3];
private final Map<ICraftingPatternDetails, TaskProgress> tasks = new HashMap<>();
private Map<ICraftingPatternDetails, TaskProgress> workableTasks = new HashMap<>();
private final Comparator<ICraftingPatternDetails> priorityComparator = Comparator
.comparing(ICraftingPatternDetails::getPriority).thenComparing(ICraftingPatternDetails::hashCode);
private final Map<ICraftingPatternDetails, TaskProgress> tasks = new TreeMap<>(priorityComparator);
private Map<ICraftingPatternDetails, TaskProgress> workableTasks = new TreeMap<>(priorityComparator);
private HashSet<ICraftingMedium> knownBusyMediums = new HashSet<>();
// INSTANCE sate
private final LinkedList<TileCraftingTile> tiles = new LinkedList<>();
Expand Down Expand Up @@ -682,7 +686,8 @@ public void updateCraftingLogic(final IGrid grid, final IEnergyGrid eg, final Cr
final int started = this.remainingOperations;

// Shallow copy tasks so we may remove them after visiting
this.workableTasks = new HashMap<>(this.tasks);
this.workableTasks.clear();
this.workableTasks.putAll(this.tasks);
this.knownBusyMediums.clear();
if (this.remainingOperations > 0) {
do {
Expand All @@ -694,7 +699,6 @@ public void updateCraftingLogic(final IGrid grid, final IEnergyGrid eg, final Cr
this.usedOps[1] = this.usedOps[0];
this.usedOps[0] = started - this.remainingOperations;

this.workableTasks.clear();
this.knownBusyMediums.clear();

if (this.remainingOperations > 0 && !this.somethingChanged) {
Expand Down

0 comments on commit 35fb695

Please sign in to comment.