From d2956cf3499e81e9b2a3ea83cae18312b094c6b0 Mon Sep 17 00:00:00 2001 From: unimatrixgit <95320811+unimatrixgit@users.noreply.github.com> Date: Mon, 30 Nov 2026 18:02:23 +0100 Subject: [PATCH] Recheck the actual Auto Crafter input grid --- .../java/com/example/dunechest/StorageModule.java | 8 ++++++-- .../main/java/com/example/dunechest/StorageRules.java | 8 +++++--- .../java/com/example/dunechest/StorageRulesTest.java | 11 ++++++++--- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/DuneChest/src/main/java/com/example/dunechest/StorageModule.java b/DuneChest/src/main/java/com/example/dunechest/StorageModule.java index 8160a190..e6115af3 100644 --- a/DuneChest/src/main/java/com/example/dunechest/StorageModule.java +++ b/DuneChest/src/main/java/com/example/dunechest/StorageModule.java @@ -335,9 +335,13 @@ public final class StorageModule implements Listener, CommandExecutor { if (src == null || dst == null || !StorageRules.transferable(src.getType().name()) || !StorageRules.transferable(dst.getType().name())) return; Recipe r = Bukkit.getCraftingRecipe(p.recipe.clone(), b.getWorld()); if (r == null) return; ItemStack[] ingredients = src.getStorageContents(); - int[] consumption = StorageRules.matchIngredients(p.recipe, ingredients); + ItemStack[] actualGrid = new ItemStack[p.recipe.length]; + int[] consumption = StorageRules.matchIngredients(p.recipe, ingredients, actualGrid); if (consumption == null) return; - ItemStack result = r.getResult().clone(); + Recipe actual = Bukkit.getCraftingRecipe(actualGrid, b.getWorld()); + if (!(r instanceof Keyed expected) || !(actual instanceof Keyed matched) + || !expected.getKey().equals(matched.getKey())) return; + ItemStack result = actual.getResult().clone(); Inventory probe = Bukkit.createInventory(null, Math.min(54, Math.max(9, (dst.getSize() + 8) / 9 * 9))); for (int i = 0; i < dst.getSize() && i < probe.getSize(); i++) probe.setItem(i, dst.getItem(i)); if (!probe.addItem(result.clone()).isEmpty()) return; diff --git a/DuneChest/src/main/java/com/example/dunechest/StorageRules.java b/DuneChest/src/main/java/com/example/dunechest/StorageRules.java index a5c8a718..943bfcc3 100644 --- a/DuneChest/src/main/java/com/example/dunechest/StorageRules.java +++ b/DuneChest/src/main/java/com/example/dunechest/StorageRules.java @@ -72,10 +72,11 @@ public final class StorageRules { return best; } - /** Reserve one exact item per template slot, without changing the source inventory. */ - static int[] matchIngredients(ItemStack[] template, ItemStack[] source) { + /** Build a grid of real inputs and reserve their source slots without changing the inventory. */ + static int[] matchIngredients(ItemStack[] template, ItemStack[] source, ItemStack[] actualGrid) { int[] consumption = new int[source.length]; - for (ItemStack ingredient : template) { + for (int gridSlot = 0; gridSlot < template.length; gridSlot++) { + ItemStack ingredient = template[gridSlot]; if (ingredient == null || ingredient.isEmpty()) continue; int slot = 0; while (slot < source.length) { @@ -85,6 +86,7 @@ public final class StorageRules { } if (slot == source.length) return null; consumption[slot]++; + actualGrid[gridSlot] = source[slot].asOne(); } return consumption; } diff --git a/DuneChest/src/test/java/com/example/dunechest/StorageRulesTest.java b/DuneChest/src/test/java/com/example/dunechest/StorageRulesTest.java index db77617d..7db79f98 100644 --- a/DuneChest/src/test/java/com/example/dunechest/StorageRulesTest.java +++ b/DuneChest/src/test/java/com/example/dunechest/StorageRulesTest.java @@ -58,10 +58,14 @@ class StorageRulesTest { ItemStack mk2 = new HopperStack(2, 2); ItemStack vanilla = new HopperStack(0, 64); - assertNull(StorageRules.matchIngredients(new ItemStack[]{mk1}, new ItemStack[]{vanilla, mk2})); - assertNull(StorageRules.matchIngredients(new ItemStack[]{mk1, mk1}, new ItemStack[]{vanilla, mk1})); + assertNull(StorageRules.matchIngredients(new ItemStack[]{mk1}, new ItemStack[]{vanilla, mk2}, new ItemStack[1])); + assertNull(StorageRules.matchIngredients(new ItemStack[]{mk1, mk1}, new ItemStack[]{vanilla, mk1}, new ItemStack[2])); + ItemStack[] actualGrid = new ItemStack[3]; assertArrayEquals(new int[]{0, 0, 2}, StorageRules.matchIngredients( - new ItemStack[]{mk2, null, mk2}, new ItemStack[]{vanilla, mk1, mk2})); + new ItemStack[]{mk2, null, mk2}, new ItemStack[]{vanilla, mk1, mk2}, actualGrid)); + assertTrue(mk2.isSimilar(actualGrid[0])); + assertNull(actualGrid[1]); + assertEquals(1, actualGrid[2].getAmount()); assertEquals(2, mk2.getAmount()); } @@ -71,6 +75,7 @@ class StorageRulesTest { HopperStack(int tier, int amount) { this.tier = tier; this.amount = amount; } @Override public Material getType() { return Material.HOPPER; } @Override public boolean isEmpty() { return false; } + @Override public ItemStack asOne() { return new HopperStack(tier, 1); } @Override public int getAmount() { return amount; } @Override public boolean isSimilar(ItemStack other) { return other instanceof HopperStack hopper && tier == hopper.tier;