Skip to content

Commit

Permalink
feat: use checksum instead of hashcode
Browse files Browse the repository at this point in the history
  • Loading branch information
ybw0014 committed Mar 23, 2024
1 parent dc2c631 commit cafe8e0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public final class FastMachineCache {
private final AbstractFastMachine machine;
private final BlockMenu menu;
private final BlockPosition blockPosition;
private int inputHash;
private int inputChecksum;
private Map<IRecipe, Integer> outputs;
private int page = -1;
private ItemStack choice;
Expand Down Expand Up @@ -100,11 +100,11 @@ public void tick() {
*/
private void findAvailableOutputs() {
Map<ItemStack, Integer> machineInputs = MachineUtils.getMachineInputAmount(menu, INPUT_SLOTS);
int currentInputHash = machineInputs.hashCode();
if (currentInputHash == inputHash) {
int currentInputChecksum = MachineUtils.checksum(machineInputs);
if (currentInputChecksum == inputChecksum) {
return;
}
inputHash = currentInputHash;
inputChecksum = currentInputChecksum;
outputs = new LinkedHashMap<>();

if (machineInputs.isEmpty()) {
Expand Down Expand Up @@ -218,10 +218,10 @@ private void craft(final Player p, final int amount, int tries) {
List<Map.Entry<IRecipe, Integer>> outputRecipes;
try {
outputRecipes = new LinkedHashMap<>(outputs).entrySet().stream().toList();
} catch (ConcurrentModificationException e) {
} catch (Exception e) {
// sometimes player crafts when the machine is calculating outputs,
// so we just delay this one tick and try again
if (tries < 3) {
if (tries < 5) {
FastMachines.getScheduler().run(() -> craft(p, amount, tries + 1));
}
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ public static Map<ItemStack, Integer> getMachineInputAmount(BlockMenu menu, int[
return RecipeUtils.calculateItems(getItems(menu, slots));
}

@ParametersAreNonnullByDefault
public static int checksum(Map<ItemStack, Integer> map) {
int checksum = 0;
for (var entry : map.entrySet()) {
checksum ^= entry.getKey().hashCode() * entry.getValue();
}
return checksum;
}

/**
* Retrive all the {@link ItemStack} inside given machine slots.
*
Expand Down

0 comments on commit cafe8e0

Please sign in to comment.