Recheck the actual Auto Crafter input grid
Some checks failed
Planet DuneChest Build / dunechest (push) Successful in 51s
DuneRealms Plugin CI / plugins (push) Has been cancelled

This commit is contained in:
unimatrixgit 2026-11-30 18:02:23 +01:00
commit d2956cf349
3 changed files with 19 additions and 8 deletions

View file

@ -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;

View file

@ -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;
}

View file

@ -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;