Recheck the actual Auto Crafter input grid
This commit is contained in:
parent
74c703789c
commit
d2956cf349
3 changed files with 19 additions and 8 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff