Match Auto Crafter ingredients by exact item identity
Some checks failed
Planet DuneChest Build / dunechest (push) Successful in 1m9s
DuneRealms Plugin CI / plugins (push) Has been cancelled

This commit is contained in:
unimatrixgit 2026-11-30 17:59:43 +01:00
commit 74c703789c
5 changed files with 53 additions and 6 deletions

View file

@ -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/")

View file

@ -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<Material, Integer> 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;
}

View file

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

View file

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

View file

@ -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<Material, Integer>();
counts.put(Material.DIRT, 50); counts.put(Material.STONE, 40);