From 9ad0e41c6b4cab52e839b7542f0d22c044cf97d0 Mon Sep 17 00:00:00 2001 From: James Peters Date: Mon, 20 Dec 2021 16:23:08 +0000 Subject: [PATCH] Fix Complex Recipes Complex recipes were using the new Bukkit.craftItem() method which modifies the input recipe. So a copy of the matrix needs to made before hand to prevent it being modified. (The result is available in the CraftingResult record) --- .../minecraft/chests/CraftingResult.java | 28 +------------------ .../minecraft/chests/latest/Crafting.java | 12 ++++---- .../interfaces/VirtualCraftingHolder.java | 10 +++---- .../src/main/resources/lang/en_GB.properties | 2 +- 4 files changed, 14 insertions(+), 38 deletions(-) diff --git a/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/CraftingResult.java b/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/CraftingResult.java index 79f518e..db018c0 100644 --- a/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/CraftingResult.java +++ b/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/CraftingResult.java @@ -2,30 +2,4 @@ package com.jamesdpeters.minecraft.chests; import org.bukkit.inventory.ItemStack; -public class CraftingResult { - ItemStack result; - ItemStack[] matrixResult; - - public CraftingResult(ItemStack result, ItemStack[] matrixResult) { - this.result = result; - this.matrixResult = matrixResult; - } - - public CraftingResult() {} - - public ItemStack[] getMatrixResult() { - return matrixResult; - } - - public void setMatrixResult(ItemStack[] matrixResult) { - this.matrixResult = matrixResult; - } - - public ItemStack getResult() { - return result; - } - - public void setResult(ItemStack result) { - this.result = result; - } -} +public record CraftingResult(ItemStack result, ItemStack[] matrixResult) {} diff --git a/ChestsPlusPlus_LatestMC/src/main/java/com/jamesdpeters/minecraft/chests/latest/Crafting.java b/ChestsPlusPlus_LatestMC/src/main/java/com/jamesdpeters/minecraft/chests/latest/Crafting.java index 685747d..26da3db 100644 --- a/ChestsPlusPlus_LatestMC/src/main/java/com/jamesdpeters/minecraft/chests/latest/Crafting.java +++ b/ChestsPlusPlus_LatestMC/src/main/java/com/jamesdpeters/minecraft/chests/latest/Crafting.java @@ -8,15 +8,17 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.Recipe; +import java.util.Arrays; + public class Crafting implements CraftingProvider { @Override public CraftingResult craft(Player player, World world, ItemStack[] items) { - var item = Bukkit.craftItem(items, world, player); - CraftingResult result = new CraftingResult(); - result.setResult(item); - result.setMatrixResult(items); - return result; + // Create copy of array since Bukkit#craftItem modifies the input array. + ItemStack[] itemsCopy = Arrays.copyOf(items, items.length); + + var item = Bukkit.craftItem(itemsCopy, world, player); + return new CraftingResult(item, itemsCopy); } @Override diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/interfaces/VirtualCraftingHolder.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/interfaces/VirtualCraftingHolder.java index da74d8c..2c71572 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/interfaces/VirtualCraftingHolder.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/interfaces/VirtualCraftingHolder.java @@ -106,7 +106,7 @@ public class VirtualCraftingHolder implements InventoryHolder { else if (recipe instanceof ShapelessRecipe) setCrafting((ShapelessRecipe) recipe); else { // For ComplexRecipes or other implementations just use the result and original matrix for choices. - result = ApiSpecific.getNmsProvider().getCraftingProvider().craft(storage.getOwner().getPlayer(), Bukkit.getWorlds().get(0), matrix).getResult(); + result = ApiSpecific.getNmsProvider().getCraftingProvider().craft(storage.getOwner().getPlayer(), Bukkit.getWorlds().get(0), matrix).result(); for (int i = 0; i < matrix.length; i++) { ItemStack item = matrix[i]; if (item != null) { @@ -419,20 +419,20 @@ public class VirtualCraftingHolder implements InventoryHolder { // PrepareItemCraftEvent itemCraftEvent = new PrepareItemCraftEvent(craftingInventoryImpl, inventoryView, false); // Bukkit.getPluginManager().callEvent(itemCraftEvent); - if (craftingResult.getResult() == null) return false; + if (craftingResult.result() == null) return false; //If we reach here there are enough materials so check for space in the Hopper and update inventory. //Check if output and input are the same inventory to avoid duplication. Inventory tempOutput = sameInv ? sameInventory : Utils.copyInventory(output); - HashMap map = tempOutput.addItem(craftingResult.getResult()); + HashMap map = tempOutput.addItem(craftingResult.result()); - boolean isEmpty = Arrays.stream(craftingResult.getMatrixResult()) + boolean isEmpty = Arrays.stream(craftingResult.matrixResult()) .anyMatch(itemStack -> (itemStack == null || itemStack.getType() == Material.AIR)); // Add any leftover items from the recipe e.g buckets. HashMap craftingMatrixLeftOvers = isEmpty ? Maps.newHashMap() - : tempOutput.addItem(craftingResult.getMatrixResult()); + : tempOutput.addItem(craftingResult.matrixResult()); //If result fits into output copy over the temporary inventories. if (map.isEmpty() && craftingMatrixLeftOvers.isEmpty()) { diff --git a/ChestsPlusPlus_Main/src/main/resources/lang/en_GB.properties b/ChestsPlusPlus_Main/src/main/resources/lang/en_GB.properties index abcecb0..c26e909 100644 --- a/ChestsPlusPlus_Main/src/main/resources/lang/en_GB.properties +++ b/ChestsPlusPlus_Main/src/main/resources/lang/en_GB.properties @@ -1,4 +1,4 @@ -# Chests++ Language File (Version 2.5.1-Release) +# Chests++ Language File (Version 2.5.2-BETA) # NOTE: This file gets replaced when the plugin launches! If you want to make modifications create a copy first! # To create a new language file simply create a copy of this file and rename it to your desired choice for example 'en_US.properties' # It should be located in the 'lang' folder