diff --git a/eco-api/src/main/java/com/willfp/eco/core/items/Items.java b/eco-api/src/main/java/com/willfp/eco/core/items/Items.java index 965b1b76..f3608138 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/items/Items.java +++ b/eco-api/src/main/java/com/willfp/eco/core/items/Items.java @@ -79,6 +79,8 @@ public final class Items { TestableItem item = null; + int stackAmount = 1; + String[] split = args[0].toLowerCase().split(":"); if (split.length == 1) { @@ -101,7 +103,8 @@ public final class Items { if (material == null || material == Material.AIR) { return new EmptyTestableItem(); } - item = new TestableStack(new MaterialTestableItem(material), Integer.parseInt(split[1])); + item = new MaterialTestableItem(material); + stackAmount = Integer.parseInt(split[1]); } else { item = part; } @@ -113,23 +116,24 @@ public final class Items { */ if (split.length == 3) { CustomItem part = REGISTRY.get(NamespacedKeyUtils.create(split[0], split[1])); - item = part == null ? new EmptyTestableItem() : new TestableStack(part, Integer.parseInt(split[2])); + if (part == null) { + return new EmptyTestableItem(); + } + item = part; + stackAmount = Integer.parseInt(split[2]); } boolean usingNewStackFormat = false; if (args.length >= 2) { - if (!(item instanceof TestableStack)) { - try { - int amount = Integer.parseInt(args[1]); - usingNewStackFormat = true; - item = item == null ? new EmptyTestableItem() : new TestableStack(item, amount); - } catch (NumberFormatException ignored) { - } + try { + stackAmount = Integer.parseInt(args[1]); + usingNewStackFormat = true; + } catch (NumberFormatException ignored) { } } - if (item == null || item instanceof EmptyTestableItem) { + if (item == null) { return new EmptyTestableItem(); } @@ -146,10 +150,6 @@ public final class Items { requiredEnchantments.put(enchantment, level); } - if (requiredEnchantments.isEmpty()) { - return item; - } - ItemStack example = item.getItem(); if (example.getItemMeta() instanceof EnchantmentStorageMeta storageMeta) { @@ -162,41 +162,49 @@ public final class Items { example.setItemMeta(meta); } - return new ModifiedTestableItem( - item, - itemStack -> { - if (!itemStack.hasItemMeta()) { - return false; - } + if (!requiredEnchantments.isEmpty()) { + item = new ModifiedTestableItem( + item, + itemStack -> { + if (!itemStack.hasItemMeta()) { + return false; + } - ItemMeta meta = itemStack.getItemMeta(); + ItemMeta meta = itemStack.getItemMeta(); - assert meta != null; + assert meta != null; - if (meta instanceof EnchantmentStorageMeta storageMeta) { - for (Map.Entry entry : requiredEnchantments.entrySet()) { - if (!storageMeta.hasStoredEnchant(entry.getKey())) { - return false; + if (meta instanceof EnchantmentStorageMeta storageMeta) { + for (Map.Entry entry : requiredEnchantments.entrySet()) { + if (!storageMeta.hasStoredEnchant(entry.getKey())) { + return false; + } + if (storageMeta.getStoredEnchantLevel(entry.getKey()) < entry.getValue()) { + return false; + } } - if (storageMeta.getStoredEnchantLevel(entry.getKey()) < entry.getValue()) { - return false; + } else { + for (Map.Entry entry : requiredEnchantments.entrySet()) { + if (!meta.hasEnchant(entry.getKey())) { + return false; + } + if (meta.getEnchantLevel(entry.getKey()) < entry.getValue()) { + return false; + } } } - } else { - for (Map.Entry entry : requiredEnchantments.entrySet()) { - if (!meta.hasEnchant(entry.getKey())) { - return false; - } - if (meta.getEnchantLevel(entry.getKey()) < entry.getValue()) { - return false; - } - } - } - return true; - }, - example - ); + return true; + }, + example + ); + } + + if (stackAmount == 1) { + return item; + } else { + return new TestableStack(item, stackAmount); + } } /** diff --git a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/recipes/ShapedRecipeListener.java b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/recipes/ShapedRecipeListener.java index 7f47562b..18fe37df 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/recipes/ShapedRecipeListener.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/recipes/ShapedRecipeListener.java @@ -98,14 +98,65 @@ public class ShapedRecipeListener extends PluginDependent implements return; } + boolean isStackedRecipe = false; + + int upperBound = 64; for (int i = 0; i < 9; i++) { ItemStack inMatrix = event.getInventory().getMatrix()[i]; TestableItem inRecipe = matched.getParts().get(i); if (inRecipe instanceof TestableStack testableStack) { - inMatrix.setAmount(inMatrix.getAmount() - (testableStack.getAmount() - 1)); + int max = Math.floorDiv(inMatrix.getAmount(), testableStack.getAmount()); + if (max < upperBound) { + upperBound = max; + } + isStackedRecipe = true; + } else if (inMatrix != null) { + int max = inMatrix.getAmount(); + if (max < upperBound) { + upperBound = max; + } } } + + if (!isStackedRecipe) { + return; + } + + int toGivePerRecipe = event.getRecipe().getResult().getAmount(); + int maxStackSize = event.getRecipe().getResult().getMaxStackSize(); + while (toGivePerRecipe * upperBound > maxStackSize) { + upperBound--; + } + + for (int i = 0; i < 9; i++) { + ItemStack inMatrix = event.getInventory().getMatrix()[i]; + TestableItem inRecipe = matched.getParts().get(i); + + if (inRecipe instanceof TestableStack testableStack) { + if (event.isShiftClick()) { + int amount = inMatrix.getAmount() + 1; + for (int j = 0; j < upperBound; j++) { + amount -= testableStack.getAmount(); + } + inMatrix.setAmount(amount); + } else { + inMatrix.setAmount(inMatrix.getAmount() - (testableStack.getAmount() - 1)); + } + } + } + + int finalUpperBound = upperBound; + + if (event.isShiftClick()) { + ItemStack result = event.getInventory().getResult(); + if (result == null) { + return; + } + + result.setAmount(result.getAmount() * finalUpperBound); + event.getInventory().setResult(result); + } } @EventHandler