diff --git a/DuneChest/build.gradle.kts b/DuneChest/build.gradle.kts index 56a299c5..f695eab5 100644 --- a/DuneChest/build.gradle.kts +++ b/DuneChest/build.gradle.kts @@ -1,6 +1,6 @@ plugins { java } group = "com.example" -version = "1.0.0" +version = "1.0.1" repositories { mavenCentral() maven("https://repo.papermc.io/repository/maven-public/") diff --git a/DuneChest/src/main/java/com/example/dunechest/StorageModule.java b/DuneChest/src/main/java/com/example/dunechest/StorageModule.java index 03c63978..8160a190 100644 --- a/DuneChest/src/main/java/com/example/dunechest/StorageModule.java +++ b/DuneChest/src/main/java/com/example/dunechest/StorageModule.java @@ -334,14 +334,19 @@ public final class StorageModule implements Listener, CommandExecutor { Inventory src = containerAt(b.getRelative(BlockFace.UP)), dst = containerAt(b.getRelative(BlockFace.DOWN)); 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; - Map need = new EnumMap<>(Material.class); - for (ItemStack s : p.recipe) if (s != null) need.merge(s.getType(), 1, Integer::sum); - for (var n : need.entrySet()) if (!src.containsAtLeast(new ItemStack(n.getKey()), n.getValue())) return; + ItemStack[] ingredients = src.getStorageContents(); + int[] consumption = StorageRules.matchIngredients(p.recipe, ingredients); + if (consumption == null) return; ItemStack result = r.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; - for (var n : need.entrySet()) src.removeItem(new ItemStack(n.getKey(), n.getValue())); + for (int slot = 0; slot < consumption.length; slot++) { + if (consumption[slot] == 0) continue; + ItemStack stack = ingredients[slot]; + int remaining = stack.getAmount() - consumption[slot]; + src.setItem(slot, remaining == 0 ? null : stack.asQuantity(remaining)); + } dst.addItem(result); data.dirty = true; } diff --git a/DuneChest/src/main/java/com/example/dunechest/StorageRules.java b/DuneChest/src/main/java/com/example/dunechest/StorageRules.java index 71ef804f..a5c8a718 100644 --- a/DuneChest/src/main/java/com/example/dunechest/StorageRules.java +++ b/DuneChest/src/main/java/com/example/dunechest/StorageRules.java @@ -3,6 +3,7 @@ package com.example.dunechest; import java.util.*; import org.bukkit.Material; import org.bukkit.block.BlockFace; +import org.bukkit.inventory.ItemStack; /** Pure rules for the custom storage blocks: hopper tiers, filters, ender-chest colour frequencies and pad geometry. */ public final class StorageRules { @@ -71,6 +72,23 @@ 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) { + int[] consumption = new int[source.length]; + for (ItemStack ingredient : template) { + if (ingredient == null || ingredient.isEmpty()) continue; + int slot = 0; + while (slot < source.length) { + ItemStack stack = source[slot]; + if (stack != null && stack.getAmount() > consumption[slot] && ingredient.isSimilar(stack)) break; + slot++; + } + if (slot == source.length) return null; + consumption[slot]++; + } + return consumption; + } + static final Set BLOCKED_TARGETS = Set.of("BREWING", "CRAFTER", "JUKEBOX", "LECTERN", "COMPOSTER", "DECORATED_POT", "CHISELED_BOOKSHELF", "BEACON", "ENCHANTING", "ANVIL", "SMITHING"); public static boolean transferable(String inventoryType) { return !BLOCKED_TARGETS.contains(inventoryType); } } diff --git a/DuneChest/src/main/resources/plugin.yml b/DuneChest/src/main/resources/plugin.yml index a563d269..646847b5 100644 --- a/DuneChest/src/main/resources/plugin.yml +++ b/DuneChest/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: DuneChest -version: 1.0.0 +version: 1.0.1 main: com.example.dunechest.DuneChest api-version: '1.21.8' softdepend: [floodgate, DuneMenus, DuneEss] diff --git a/DuneChest/src/test/java/com/example/dunechest/StorageRulesTest.java b/DuneChest/src/test/java/com/example/dunechest/StorageRulesTest.java index 78a71ab9..db77617d 100644 --- a/DuneChest/src/test/java/com/example/dunechest/StorageRulesTest.java +++ b/DuneChest/src/test/java/com/example/dunechest/StorageRulesTest.java @@ -53,6 +53,30 @@ class StorageRulesTest { assertTrue(StorageRules.padCentre(BlockFace.NORTH, 0)[0] > StorageRules.padCentre(BlockFace.NORTH, 2)[0]); } + @Test void crafterReservesOnlyMatchingHopperTiersAndCountsEachIngredient() { + ItemStack mk1 = new HopperStack(1, 1); + 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})); + assertArrayEquals(new int[]{0, 0, 2}, StorageRules.matchIngredients( + new ItemStack[]{mk2, null, mk2}, new ItemStack[]{vanilla, mk1, mk2})); + assertEquals(2, mk2.getAmount()); + } + + // Model the Bukkit metadata comparison without booting a Paper server. + private static final class HopperStack extends ItemStack { + private final int tier, amount; + 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 int getAmount() { return amount; } + @Override public boolean isSimilar(ItemStack other) { + return other instanceof HopperStack hopper && tier == hopper.tier; + } + } + @Test void mostCommonItemPicksBiggestTotal() { var counts = new java.util.LinkedHashMap(); counts.put(Material.DIRT, 50); counts.put(Material.STONE, 40);