diff --git a/.travis.yml b/.travis.yml index de98295..42ec3b6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,7 @@ before_install: - export JAVA_HOME=$HOME/openjdk16 - $TRAVIS_BUILD_DIR/BuildTools/install-jdk.sh -F 16 --target $JAVA_HOME - export PATH=$JAVA_HOME/bin:$PATH -- bash build-1.17+.sh +#- bash build-1.17+.sh - cd ../ install: diff --git a/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/Api.java b/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/Api.java index f3c7c41..4f580dd 100644 --- a/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/Api.java +++ b/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/Api.java @@ -3,6 +3,8 @@ package com.jamesdpeters.minecraft.chests; import org.bukkit.Bukkit; import org.bukkit.plugin.Plugin; +import java.lang.reflect.InvocationTargetException; + public class Api { private static Plugin plugin; @@ -21,13 +23,11 @@ public class Api { String nmsProvider = packageName + "." + nmsVersion + ".NMSProviderImpl"; plugin.getLogger().info("Found API version: " + nmsVersion); try { - return (NMSProvider) Class.forName(nmsProvider).newInstance(); - } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) { - plugin.getLogger().warning("A valid server implementation wasn't found for: " + nmsVersion); - plugin.getLogger().warning("You may be running an outdated version of the plugin or it needs to be updated to the latest version!"); - - // Currently depends on NMS for any Crafting abilities - throw new RuntimeException("Currently no NMS version support for "+nmsVersion); + return (NMSProvider) Class.forName(nmsProvider).getDeclaredConstructor().newInstance(); + } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) { + // Plugin now doesn't depend on NMS after 1.17. + // So NMSProviderDefault is used for all versions 1.17+ + return null; } } } diff --git a/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/CraftingProvider.java b/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/CraftingProvider.java index 281bead..78d472f 100644 --- a/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/CraftingProvider.java +++ b/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/CraftingProvider.java @@ -5,11 +5,9 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.Recipe; -import java.util.List; - public interface CraftingProvider { - CraftingResult craft(Player player, World world, List items); + CraftingResult craft(Player player, World world, ItemStack[] items); - Recipe getRecipe(Player player, World world, List items); + Recipe getRecipe(Player player, World world, ItemStack[] items); } 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 99aa6e0..79f518e 100644 --- a/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/CraftingResult.java +++ b/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/CraftingResult.java @@ -2,17 +2,22 @@ package com.jamesdpeters.minecraft.chests; import org.bukkit.inventory.ItemStack; -import java.util.List; - public class CraftingResult { ItemStack result; - List matrixResult; + ItemStack[] matrixResult; - public List getMatrixResult() { + public CraftingResult(ItemStack result, ItemStack[] matrixResult) { + this.result = result; + this.matrixResult = matrixResult; + } + + public CraftingResult() {} + + public ItemStack[] getMatrixResult() { return matrixResult; } - public void setMatrixResult(List matrixResult) { + public void setMatrixResult(ItemStack[] matrixResult) { this.matrixResult = matrixResult; } diff --git a/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/NMSProvider.java b/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/NMSProvider.java index b0d2db5..be15f57 100644 --- a/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/NMSProvider.java +++ b/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/NMSProvider.java @@ -6,7 +6,6 @@ public interface NMSProvider { ChestOpener getChestOpener(); MaterialChecker getMaterialChecker(); CraftingProvider getCraftingProvider(); - NPCProvider getNPCProvider(); void setItemFrameVisible(ItemFrame itemFrame, boolean visible); } diff --git a/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/Crafting.java b/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/Crafting.java index 038ee8c..3730329 100644 --- a/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/Crafting.java +++ b/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/Crafting.java @@ -3,75 +3,67 @@ package com.jamesdpeters.minecraft.chests.v1_14_R1; import com.jamesdpeters.minecraft.chests.CraftingProvider; import com.jamesdpeters.minecraft.chests.CraftingResult; import net.minecraft.server.v1_14_R1.Container; +import net.minecraft.server.v1_14_R1.ContainerWorkbench; import net.minecraft.server.v1_14_R1.EntityHuman; import net.minecraft.server.v1_14_R1.IRecipe; +import net.minecraft.server.v1_14_R1.InventoryCraftResult; import net.minecraft.server.v1_14_R1.InventoryCrafting; import net.minecraft.server.v1_14_R1.Item; import net.minecraft.server.v1_14_R1.RecipeCrafting; +import net.minecraft.server.v1_14_R1.RecipeRepair; import net.minecraft.server.v1_14_R1.Recipes; import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.craftbukkit.v1_14_R1.CraftServer; import org.bukkit.craftbukkit.v1_14_R1.CraftWorld; +import org.bukkit.craftbukkit.v1_14_R1.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_14_R1.event.CraftEventFactory; +import org.bukkit.craftbukkit.v1_14_R1.inventory.CraftInventoryCrafting; +import org.bukkit.craftbukkit.v1_14_R1.inventory.CraftInventoryView; import org.bukkit.craftbukkit.v1_14_R1.inventory.CraftItemStack; import org.bukkit.entity.Player; import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.Recipe; -import java.util.List; import java.util.Optional; -import java.util.stream.Collectors; public class Crafting implements CraftingProvider { @Override - public CraftingResult craft(Player player, World world, List items) { - Container container = new Container(null, -1) { - @Override - public InventoryView getBukkitView() { - return null; - } - - @Override - public boolean canUse(EntityHuman entityHuman) { - return false; - } - }; - - InventoryCrafting crafting = new InventoryCrafting(container, 3, 3); - - for (int i = 0; i < items.size(); i++) { - crafting.setItem(i, CraftItemStack.asNMSCopy(items.get(i))); - } - - CraftServer server = (CraftServer) Bukkit.getServer(); + public CraftingResult craft(Player player, World world, ItemStack[] items) { CraftWorld craftWorld = (CraftWorld) world; - Optional optional = server.getServer().getCraftingManager().craft(Recipes.CRAFTING, crafting, craftWorld.getHandle()); + CraftPlayer craftPlayer = (CraftPlayer) player; + ContainerWorkbench workbench = new ContainerWorkbench(-1, craftPlayer.getHandle().inventory); + + CraftInventoryView view = workbench.getBukkitView(); + CraftInventoryCrafting craftInventoryCrafting = (CraftInventoryCrafting) view.getTopInventory(); + InventoryCrafting inventoryCrafting = (InventoryCrafting) craftInventoryCrafting.getMatrixInventory(); + InventoryCraftResult resultInventory = (InventoryCraftResult) craftInventoryCrafting.getResultInventory(); + + Optional recipe = getNMSRecipe(items, inventoryCrafting, craftWorld); net.minecraft.server.v1_14_R1.ItemStack itemstack = net.minecraft.server.v1_14_R1.ItemStack.a; - if (optional.isPresent()) { - RecipeCrafting recipeCrafting = optional.get(); - itemstack = recipeCrafting.a(crafting); + if (recipe.isPresent()) { + RecipeCrafting recipeCrafting = recipe.get(); + if (resultInventory.a(craftWorld.getHandle(), craftPlayer.getHandle(), recipeCrafting)) { + itemstack = recipeCrafting.a(inventoryCrafting); + } } - CraftingResult result = new CraftingResult(); - result.setResult(CraftItemStack.asBukkitCopy(itemstack)); + net.minecraft.server.v1_14_R1.ItemStack result = CraftEventFactory.callPreCraftEvent(inventoryCrafting, resultInventory, itemstack, view, recipe.orElse(null) instanceof RecipeRepair); - List matrixResult = crafting.getContents().stream() - .map(itemStack -> { - Item remainingItem = itemStack.getItem().n(); - return remainingItem != null ? CraftItemStack.asBukkitCopy(new net.minecraft.server.v1_14_R1.ItemStack(remainingItem)) : null; - }).collect(Collectors.toList()); + for(int i = 0; i < items.length; ++i) { + Item remaining = inventoryCrafting.getContents().get(i).getItem().n(); + items[i] = remaining != null ? CraftItemStack.asBukkitCopy(new net.minecraft.server.v1_14_R1.ItemStack(remaining)) : null; + } - result.setMatrixResult(matrixResult); - - return result; + return new CraftingResult(CraftItemStack.asBukkitCopy(result), items); } @Override - public Recipe getRecipe(Player player, World world, List items) { + public Recipe getRecipe(Player player, World world, ItemStack[] items) { Container container = new Container(null, -1) { @Override public InventoryView getBukkitView() { @@ -86,9 +78,9 @@ public class Crafting implements CraftingProvider { InventoryCrafting crafting = new InventoryCrafting(container, 3, 3); - for (int i = 0; i < items.size(); i++) { + for (int i = 0; i < items.length; i++) { if(i >= 9) break; // ItemList cant contain more than 9 items. - crafting.setItem(i, CraftItemStack.asNMSCopy(items.get(i))); + crafting.setItem(i, CraftItemStack.asNMSCopy(items[i])); } CraftServer server = (CraftServer) Bukkit.getServer(); @@ -96,6 +88,14 @@ public class Crafting implements CraftingProvider { Optional optional = server.getServer().getCraftingManager().craft(Recipes.CRAFTING, crafting, craftWorld.getHandle()); return optional.map(IRecipe::toBukkitRecipe).orElse(null); + } + private Optional getNMSRecipe(ItemStack[] craftingMatrix, InventoryCrafting inventoryCrafting, CraftWorld world) { + for(int i = 0; i < craftingMatrix.length; ++i) { + inventoryCrafting.setItem(i, CraftItemStack.asNMSCopy(craftingMatrix[i])); + } + + CraftServer server = (CraftServer) Bukkit.getServer(); + return server.getServer().getCraftingManager().craft(Recipes.CRAFTING, inventoryCrafting, world.getHandle()); } } diff --git a/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/NMSProviderImpl.java b/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/NMSProviderImpl.java index ded5a88..1df517e 100644 --- a/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/NMSProviderImpl.java +++ b/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/NMSProviderImpl.java @@ -19,11 +19,6 @@ public class NMSProviderImpl implements NMSProvider { return new Crafting(); } - @Override - public NPCProvider getNPCProvider() { - return new NPC(); - } - @Override public void setItemFrameVisible(ItemFrame itemFrame, boolean visible) { //Not support in 1.14 diff --git a/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/NPC.java b/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/NPC.java deleted file mode 100644 index 243f03b..0000000 --- a/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/NPC.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.jamesdpeters.minecraft.chests.v1_14_R1; - -import com.jamesdpeters.minecraft.chests.NPCProvider; -import com.mojang.authlib.GameProfile; -import net.minecraft.server.v1_14_R1.EntityPlayer; -import net.minecraft.server.v1_14_R1.MinecraftServer; -import net.minecraft.server.v1_14_R1.PlayerInteractManager; -import net.minecraft.server.v1_14_R1.WorldServer; -import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_14_R1.CraftServer; -import org.bukkit.craftbukkit.v1_14_R1.CraftWorld; -import org.bukkit.entity.HumanEntity; - -import java.util.UUID; - -public class NPC implements NPCProvider { - public HumanEntity createHumanEntity() { - MinecraftServer nmsServer = ((CraftServer) Bukkit.getServer()).getServer(); - WorldServer nmsWorld = ((CraftWorld) Bukkit.getWorlds().get(0)).getHandle(); - GameProfile gameProfile = new GameProfile(UUID.randomUUID(), "Chests++NPC"); - EntityPlayer npc = new EntityPlayer(nmsServer, nmsWorld, gameProfile, new PlayerInteractManager(nmsWorld)); - return npc.getBukkitEntity(); - } -} \ No newline at end of file diff --git a/ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/Crafting.java b/ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/Crafting.java index 81861e1..ecd2caa 100644 --- a/ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/Crafting.java +++ b/ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/Crafting.java @@ -3,75 +3,67 @@ package com.jamesdpeters.minecraft.chests.v1_15_R1; import com.jamesdpeters.minecraft.chests.CraftingProvider; import com.jamesdpeters.minecraft.chests.CraftingResult; import net.minecraft.server.v1_15_R1.Container; +import net.minecraft.server.v1_15_R1.ContainerWorkbench; import net.minecraft.server.v1_15_R1.EntityHuman; import net.minecraft.server.v1_15_R1.IRecipe; +import net.minecraft.server.v1_15_R1.InventoryCraftResult; import net.minecraft.server.v1_15_R1.InventoryCrafting; import net.minecraft.server.v1_15_R1.Item; import net.minecraft.server.v1_15_R1.RecipeCrafting; +import net.minecraft.server.v1_15_R1.RecipeRepair; import net.minecraft.server.v1_15_R1.Recipes; import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.craftbukkit.v1_15_R1.CraftServer; import org.bukkit.craftbukkit.v1_15_R1.CraftWorld; +import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_15_R1.event.CraftEventFactory; +import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftInventoryCrafting; +import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftInventoryView; import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack; import org.bukkit.entity.Player; import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.Recipe; -import java.util.List; import java.util.Optional; -import java.util.stream.Collectors; public class Crafting implements CraftingProvider { @Override - public CraftingResult craft(Player player, World world, List items) { - Container container = new Container(null, -1) { - @Override - public InventoryView getBukkitView() { - return null; - } - - @Override - public boolean canUse(EntityHuman entityHuman) { - return false; - } - }; - - InventoryCrafting crafting = new InventoryCrafting(container, 3, 3); - - for (int i = 0; i < items.size(); i++) { - crafting.setItem(i, CraftItemStack.asNMSCopy(items.get(i))); - } - - CraftServer server = (CraftServer) Bukkit.getServer(); + public CraftingResult craft(Player player, World world, ItemStack[] items) { CraftWorld craftWorld = (CraftWorld) world; - Optional optional = server.getServer().getCraftingManager().craft(Recipes.CRAFTING, crafting, craftWorld.getHandle()); + CraftPlayer craftPlayer = (CraftPlayer) player; + ContainerWorkbench workbench = new ContainerWorkbench(-1, craftPlayer.getHandle().inventory); + + CraftInventoryView view = workbench.getBukkitView(); + CraftInventoryCrafting craftInventoryCrafting = (CraftInventoryCrafting) view.getTopInventory(); + InventoryCrafting inventoryCrafting = (InventoryCrafting) craftInventoryCrafting.getMatrixInventory(); + InventoryCraftResult resultInventory = (InventoryCraftResult) craftInventoryCrafting.getResultInventory(); + + Optional recipe = getNMSRecipe(items, inventoryCrafting, craftWorld); net.minecraft.server.v1_15_R1.ItemStack itemstack = net.minecraft.server.v1_15_R1.ItemStack.a; - if (optional.isPresent()) { - RecipeCrafting recipeCrafting = optional.get(); - itemstack = recipeCrafting.a(crafting); + if (recipe.isPresent()) { + RecipeCrafting recipeCrafting = recipe.get(); + if (resultInventory.a(craftWorld.getHandle(), craftPlayer.getHandle(), recipeCrafting)) { + itemstack = recipeCrafting.a(inventoryCrafting); + } } - CraftingResult result = new CraftingResult(); - result.setResult(CraftItemStack.asBukkitCopy(itemstack)); + net.minecraft.server.v1_15_R1.ItemStack result = CraftEventFactory.callPreCraftEvent(inventoryCrafting, resultInventory, itemstack, view, recipe.orElse(null) instanceof RecipeRepair); - List matrixResult = crafting.getContents().stream() - .map(itemStack -> { - Item remainingItem = itemStack.getItem().p(); - return remainingItem != null ? CraftItemStack.asBukkitCopy(new net.minecraft.server.v1_15_R1.ItemStack(remainingItem)) : null; - }).collect(Collectors.toList()); + for(int i = 0; i < items.length; ++i) { + Item remaining = inventoryCrafting.getContents().get(i).getItem().p(); + items[i] = remaining != null ? CraftItemStack.asBukkitCopy(new net.minecraft.server.v1_15_R1.ItemStack(remaining)) : null; + } - result.setMatrixResult(matrixResult); - - return result; + return new CraftingResult(CraftItemStack.asBukkitCopy(result), items); } @Override - public Recipe getRecipe(Player player, World world, List items) { + public Recipe getRecipe(Player player, World world, ItemStack[] items) { Container container = new Container(null, -1) { @Override public InventoryView getBukkitView() { @@ -86,9 +78,9 @@ public class Crafting implements CraftingProvider { InventoryCrafting crafting = new InventoryCrafting(container, 3, 3); - for (int i = 0; i < items.size(); i++) { + for (int i = 0; i < items.length; i++) { if(i >= 9) break; // ItemList cant contain more than 9 items. - crafting.setItem(i, CraftItemStack.asNMSCopy(items.get(i))); + crafting.setItem(i, CraftItemStack.asNMSCopy(items[i])); } CraftServer server = (CraftServer) Bukkit.getServer(); @@ -96,6 +88,14 @@ public class Crafting implements CraftingProvider { Optional optional = server.getServer().getCraftingManager().craft(Recipes.CRAFTING, crafting, craftWorld.getHandle()); return optional.map(IRecipe::toBukkitRecipe).orElse(null); + } + private Optional getNMSRecipe(ItemStack[] craftingMatrix, InventoryCrafting inventoryCrafting, CraftWorld world) { + for(int i = 0; i < craftingMatrix.length; ++i) { + inventoryCrafting.setItem(i, CraftItemStack.asNMSCopy(craftingMatrix[i])); + } + + CraftServer server = (CraftServer) Bukkit.getServer(); + return server.getServer().getCraftingManager().craft(Recipes.CRAFTING, inventoryCrafting, world.getHandle()); } } diff --git a/ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/NMSProviderImpl.java b/ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/NMSProviderImpl.java index 7eb8d3f..97cc1df 100644 --- a/ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/NMSProviderImpl.java +++ b/ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/NMSProviderImpl.java @@ -19,11 +19,6 @@ public class NMSProviderImpl implements NMSProvider { return new Crafting(); } - @Override - public NPCProvider getNPCProvider() { - return new NPC(); - } - @Override public void setItemFrameVisible(ItemFrame itemFrame, boolean visible) { //Not supported in 1.15 diff --git a/ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/NPC.java b/ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/NPC.java deleted file mode 100644 index c3bc707..0000000 --- a/ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/NPC.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.jamesdpeters.minecraft.chests.v1_15_R1; - -import com.jamesdpeters.minecraft.chests.NPCProvider; -import com.mojang.authlib.GameProfile; -import net.minecraft.server.v1_15_R1.EntityPlayer; -import net.minecraft.server.v1_15_R1.MinecraftServer; -import net.minecraft.server.v1_15_R1.PlayerInteractManager; -import net.minecraft.server.v1_15_R1.WorldServer; -import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_15_R1.CraftServer; -import org.bukkit.craftbukkit.v1_15_R1.CraftWorld; -import org.bukkit.entity.HumanEntity; - -import java.util.UUID; - -public class NPC implements NPCProvider { - public HumanEntity createHumanEntity() { - MinecraftServer nmsServer = ((CraftServer) Bukkit.getServer()).getServer(); - WorldServer nmsWorld = ((CraftWorld) Bukkit.getWorlds().get(0)).getHandle(); - GameProfile gameProfile = new GameProfile(UUID.randomUUID(), "Chests++NPC"); - EntityPlayer npc = new EntityPlayer(nmsServer, nmsWorld, gameProfile, new PlayerInteractManager(nmsWorld)); - return npc.getBukkitEntity(); - } -} diff --git a/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/Crafting.java b/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/Crafting.java index 72f4584..80254b7 100644 --- a/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/Crafting.java +++ b/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/Crafting.java @@ -3,75 +3,67 @@ package com.jamesdpeters.minecraft.chests.v1_16_R1; import com.jamesdpeters.minecraft.chests.CraftingProvider; import com.jamesdpeters.minecraft.chests.CraftingResult; import net.minecraft.server.v1_16_R1.Container; +import net.minecraft.server.v1_16_R1.ContainerWorkbench; import net.minecraft.server.v1_16_R1.EntityHuman; import net.minecraft.server.v1_16_R1.IRecipe; +import net.minecraft.server.v1_16_R1.InventoryCraftResult; import net.minecraft.server.v1_16_R1.InventoryCrafting; import net.minecraft.server.v1_16_R1.Item; import net.minecraft.server.v1_16_R1.RecipeCrafting; +import net.minecraft.server.v1_16_R1.RecipeRepair; import net.minecraft.server.v1_16_R1.Recipes; import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.craftbukkit.v1_16_R1.CraftServer; import org.bukkit.craftbukkit.v1_16_R1.CraftWorld; +import org.bukkit.craftbukkit.v1_16_R1.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_16_R1.event.CraftEventFactory; +import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftInventoryCrafting; +import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftInventoryView; import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftItemStack; import org.bukkit.entity.Player; import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.Recipe; -import java.util.List; import java.util.Optional; -import java.util.stream.Collectors; public class Crafting implements CraftingProvider { @Override - public CraftingResult craft(Player player, World world, List items) { - Container container = new Container(null, -1) { - @Override - public InventoryView getBukkitView() { - return null; - } - - @Override - public boolean canUse(EntityHuman entityHuman) { - return false; - } - }; - - InventoryCrafting crafting = new InventoryCrafting(container, 3, 3); - - for (int i = 0; i < items.size(); i++) { - crafting.setItem(i, CraftItemStack.asNMSCopy(items.get(i))); - } - - CraftServer server = (CraftServer) Bukkit.getServer(); + public CraftingResult craft(Player player, World world, ItemStack[] items) { CraftWorld craftWorld = (CraftWorld) world; - Optional optional = server.getServer().getCraftingManager().craft(Recipes.CRAFTING, crafting, craftWorld.getHandle()); + CraftPlayer craftPlayer = (CraftPlayer) player; + ContainerWorkbench workbench = new ContainerWorkbench(-1, craftPlayer.getHandle().inventory); + + CraftInventoryView view = workbench.getBukkitView(); + CraftInventoryCrafting craftInventoryCrafting = (CraftInventoryCrafting) view.getTopInventory(); + InventoryCrafting inventoryCrafting = (InventoryCrafting) craftInventoryCrafting.getMatrixInventory(); + InventoryCraftResult resultInventory = (InventoryCraftResult) craftInventoryCrafting.getResultInventory(); + + Optional recipe = getNMSRecipe(items, inventoryCrafting, craftWorld); net.minecraft.server.v1_16_R1.ItemStack itemstack = net.minecraft.server.v1_16_R1.ItemStack.b; - if (optional.isPresent()) { - RecipeCrafting recipeCrafting = optional.get(); - itemstack = recipeCrafting.a(crafting); + if (recipe.isPresent()) { + RecipeCrafting recipeCrafting = recipe.get(); + if (resultInventory.a(craftWorld.getHandle(), craftPlayer.getHandle(), recipeCrafting)) { + itemstack = recipeCrafting.a(inventoryCrafting); + } } - CraftingResult result = new CraftingResult(); - result.setResult(CraftItemStack.asBukkitCopy(itemstack)); + net.minecraft.server.v1_16_R1.ItemStack result = CraftEventFactory.callPreCraftEvent(inventoryCrafting, resultInventory, itemstack, view, recipe.orElse(null) instanceof RecipeRepair); - List matrixResult = crafting.getContents().stream() - .map(itemStack -> { - Item remainingItem = itemStack.getItem().getCraftingRemainingItem(); - return remainingItem != null ? CraftItemStack.asBukkitCopy(remainingItem.r()) : null; - }).collect(Collectors.toList()); + for(int i = 0; i < items.length; ++i) { + Item remaining = inventoryCrafting.getContents().get(i).getItem().getCraftingRemainingItem(); + items[i] = remaining != null ? CraftItemStack.asBukkitCopy(remaining.r()) : null; + } - result.setMatrixResult(matrixResult); - - return result; + return new CraftingResult(CraftItemStack.asBukkitCopy(result), items); } @Override - public Recipe getRecipe(Player player, World world, List items) { + public Recipe getRecipe(Player player, World world, ItemStack[] items) { Container container = new Container(null, -1) { @Override public InventoryView getBukkitView() { @@ -86,9 +78,9 @@ public class Crafting implements CraftingProvider { InventoryCrafting crafting = new InventoryCrafting(container, 3, 3); - for (int i = 0; i < items.size(); i++) { + for (int i = 0; i < items.length; i++) { if(i >= 9) break; // ItemList cant contain more than 9 items. - crafting.setItem(i, CraftItemStack.asNMSCopy(items.get(i))); + crafting.setItem(i, CraftItemStack.asNMSCopy(items[i])); } CraftServer server = (CraftServer) Bukkit.getServer(); @@ -96,6 +88,14 @@ public class Crafting implements CraftingProvider { Optional optional = server.getServer().getCraftingManager().craft(Recipes.CRAFTING, crafting, craftWorld.getHandle()); return optional.map(IRecipe::toBukkitRecipe).orElse(null); + } + private Optional getNMSRecipe(ItemStack[] craftingMatrix, InventoryCrafting inventoryCrafting, CraftWorld world) { + for(int i = 0; i < craftingMatrix.length; ++i) { + inventoryCrafting.setItem(i, CraftItemStack.asNMSCopy(craftingMatrix[i])); + } + + CraftServer server = (CraftServer) Bukkit.getServer(); + return server.getServer().getCraftingManager().craft(Recipes.CRAFTING, inventoryCrafting, world.getHandle()); } } diff --git a/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/NMSProviderImpl.java b/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/NMSProviderImpl.java index e4fa9f4..1fb211b 100644 --- a/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/NMSProviderImpl.java +++ b/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/NMSProviderImpl.java @@ -32,11 +32,6 @@ public class NMSProviderImpl implements NMSProvider { return new Crafting(); } - @Override - public NPCProvider getNPCProvider() { - return new NPC(); - } - @Override public void setItemFrameVisible(ItemFrame itemFrame, boolean visible) { itemFrame.setVisible(visible); diff --git a/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/NPC.java b/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/NPC.java deleted file mode 100644 index 87634b0..0000000 --- a/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/NPC.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.jamesdpeters.minecraft.chests.v1_16_R1; - -import com.jamesdpeters.minecraft.chests.NPCProvider; -import com.mojang.authlib.GameProfile; -import net.minecraft.server.v1_16_R1.EntityPlayer; -import net.minecraft.server.v1_16_R1.MinecraftServer; -import net.minecraft.server.v1_16_R1.PlayerInteractManager; -import net.minecraft.server.v1_16_R1.WorldServer; -import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_16_R1.CraftServer; -import org.bukkit.craftbukkit.v1_16_R1.CraftWorld; -import org.bukkit.entity.HumanEntity; - -import java.util.UUID; - -public class NPC implements NPCProvider { - public HumanEntity createHumanEntity() { - MinecraftServer nmsServer = ((CraftServer) Bukkit.getServer()).getServer(); - WorldServer nmsWorld = ((CraftWorld) Bukkit.getWorlds().get(0)).getHandle(); - GameProfile gameProfile = new GameProfile(UUID.randomUUID(), "Chests++NPC"); - EntityPlayer npc = new EntityPlayer(nmsServer, nmsWorld, gameProfile, new PlayerInteractManager(nmsWorld)); - return npc.getBukkitEntity(); - } -} diff --git a/ChestsPlusPlus_1_16_R2/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R2/Crafting.java b/ChestsPlusPlus_1_16_R2/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R2/Crafting.java index 28cc25b..30fc9e2 100644 --- a/ChestsPlusPlus_1_16_R2/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R2/Crafting.java +++ b/ChestsPlusPlus_1_16_R2/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R2/Crafting.java @@ -3,75 +3,67 @@ package com.jamesdpeters.minecraft.chests.v1_16_R2; import com.jamesdpeters.minecraft.chests.CraftingProvider; import com.jamesdpeters.minecraft.chests.CraftingResult; import net.minecraft.server.v1_16_R2.Container; +import net.minecraft.server.v1_16_R2.ContainerWorkbench; import net.minecraft.server.v1_16_R2.EntityHuman; import net.minecraft.server.v1_16_R2.IRecipe; +import net.minecraft.server.v1_16_R2.InventoryCraftResult; import net.minecraft.server.v1_16_R2.InventoryCrafting; import net.minecraft.server.v1_16_R2.Item; import net.minecraft.server.v1_16_R2.RecipeCrafting; +import net.minecraft.server.v1_16_R2.RecipeRepair; import net.minecraft.server.v1_16_R2.Recipes; import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.craftbukkit.v1_16_R2.CraftServer; import org.bukkit.craftbukkit.v1_16_R2.CraftWorld; +import org.bukkit.craftbukkit.v1_16_R2.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_16_R2.event.CraftEventFactory; +import org.bukkit.craftbukkit.v1_16_R2.inventory.CraftInventoryCrafting; +import org.bukkit.craftbukkit.v1_16_R2.inventory.CraftInventoryView; import org.bukkit.craftbukkit.v1_16_R2.inventory.CraftItemStack; import org.bukkit.entity.Player; import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.Recipe; -import java.util.List; import java.util.Optional; -import java.util.stream.Collectors; public class Crafting implements CraftingProvider { @Override - public CraftingResult craft(Player player, World world, List items) { - Container container = new Container(null, -1) { - @Override - public InventoryView getBukkitView() { - return null; - } - - @Override - public boolean canUse(EntityHuman entityHuman) { - return false; - } - }; - - InventoryCrafting crafting = new InventoryCrafting(container, 3, 3); - - for (int i = 0; i < items.size(); i++) { - crafting.setItem(i, CraftItemStack.asNMSCopy(items.get(i))); - } - - CraftServer server = (CraftServer) Bukkit.getServer(); + public CraftingResult craft(Player player, World world, ItemStack[] items) { CraftWorld craftWorld = (CraftWorld) world; - Optional optional = server.getServer().getCraftingManager().craft(Recipes.CRAFTING, crafting, craftWorld.getHandle()); + CraftPlayer craftPlayer = (CraftPlayer) player; + ContainerWorkbench workbench = new ContainerWorkbench(-1, craftPlayer.getHandle().inventory); + + CraftInventoryView view = workbench.getBukkitView(); + CraftInventoryCrafting craftInventoryCrafting = (CraftInventoryCrafting) view.getTopInventory(); + InventoryCrafting inventoryCrafting = (InventoryCrafting) craftInventoryCrafting.getMatrixInventory(); + InventoryCraftResult resultInventory = (InventoryCraftResult) craftInventoryCrafting.getResultInventory(); + + Optional recipe = getNMSRecipe(items, inventoryCrafting, craftWorld); net.minecraft.server.v1_16_R2.ItemStack itemstack = net.minecraft.server.v1_16_R2.ItemStack.b; - if (optional.isPresent()) { - RecipeCrafting recipeCrafting = optional.get(); - itemstack = recipeCrafting.a(crafting); + if (recipe.isPresent()) { + RecipeCrafting recipeCrafting = recipe.get(); + if (resultInventory.a(craftWorld.getHandle(), craftPlayer.getHandle(), recipeCrafting)) { + itemstack = recipeCrafting.a(inventoryCrafting); + } } - CraftingResult result = new CraftingResult(); - result.setResult(CraftItemStack.asBukkitCopy(itemstack)); + net.minecraft.server.v1_16_R2.ItemStack result = CraftEventFactory.callPreCraftEvent(inventoryCrafting, resultInventory, itemstack, view, recipe.orElse(null) instanceof RecipeRepair); - List matrixResult = crafting.getContents().stream() - .map(itemStack -> { - Item remainingItem = itemStack.getItem().getCraftingRemainingItem(); - return remainingItem != null ? CraftItemStack.asBukkitCopy(remainingItem.createItemStack()) : null; - }).collect(Collectors.toList()); + for(int i = 0; i < items.length; ++i) { + Item remaining = inventoryCrafting.getContents().get(i).getItem().getCraftingRemainingItem(); + items[i] = remaining != null ? CraftItemStack.asBukkitCopy(remaining.createItemStack()) : null; + } - result.setMatrixResult(matrixResult); - - return result; + return new CraftingResult(CraftItemStack.asBukkitCopy(result), items); } @Override - public Recipe getRecipe(Player player, World world, List items) { + public Recipe getRecipe(Player player, World world, ItemStack[] items) { Container container = new Container(null, -1) { @Override public InventoryView getBukkitView() { @@ -86,9 +78,9 @@ public class Crafting implements CraftingProvider { InventoryCrafting crafting = new InventoryCrafting(container, 3, 3); - for (int i = 0; i < items.size(); i++) { + for (int i = 0; i < items.length; i++) { if(i >= 9) break; // ItemList cant contain more than 9 items. - crafting.setItem(i, CraftItemStack.asNMSCopy(items.get(i))); + crafting.setItem(i, CraftItemStack.asNMSCopy(items[i])); } CraftServer server = (CraftServer) Bukkit.getServer(); @@ -96,6 +88,14 @@ public class Crafting implements CraftingProvider { Optional optional = server.getServer().getCraftingManager().craft(Recipes.CRAFTING, crafting, craftWorld.getHandle()); return optional.map(IRecipe::toBukkitRecipe).orElse(null); + } + private Optional getNMSRecipe(ItemStack[] craftingMatrix, InventoryCrafting inventoryCrafting, CraftWorld world) { + for(int i = 0; i < craftingMatrix.length; ++i) { + inventoryCrafting.setItem(i, CraftItemStack.asNMSCopy(craftingMatrix[i])); + } + + CraftServer server = (CraftServer) Bukkit.getServer(); + return server.getServer().getCraftingManager().craft(Recipes.CRAFTING, inventoryCrafting, world.getHandle()); } } diff --git a/ChestsPlusPlus_1_16_R2/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R2/NMSProviderImpl.java b/ChestsPlusPlus_1_16_R2/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R2/NMSProviderImpl.java index 41fa21c..ed9ea93 100644 --- a/ChestsPlusPlus_1_16_R2/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R2/NMSProviderImpl.java +++ b/ChestsPlusPlus_1_16_R2/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R2/NMSProviderImpl.java @@ -32,11 +32,6 @@ public class NMSProviderImpl implements NMSProvider { return new Crafting(); } - @Override - public NPCProvider getNPCProvider() { - return new NPC(); - } - @Override public void setItemFrameVisible(ItemFrame itemFrame, boolean visible) { itemFrame.setVisible(visible); diff --git a/ChestsPlusPlus_1_16_R2/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R2/NPC.java b/ChestsPlusPlus_1_16_R2/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R2/NPC.java deleted file mode 100644 index db65374..0000000 --- a/ChestsPlusPlus_1_16_R2/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R2/NPC.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.jamesdpeters.minecraft.chests.v1_16_R2; - -import com.jamesdpeters.minecraft.chests.NPCProvider; -import com.mojang.authlib.GameProfile; -import net.minecraft.server.v1_16_R2.EntityPlayer; -import net.minecraft.server.v1_16_R2.MinecraftServer; -import net.minecraft.server.v1_16_R2.PlayerInteractManager; -import net.minecraft.server.v1_16_R2.WorldServer; -import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_16_R2.CraftServer; -import org.bukkit.craftbukkit.v1_16_R2.CraftWorld; -import org.bukkit.entity.HumanEntity; - -import java.util.UUID; - -public class NPC implements NPCProvider { - public HumanEntity createHumanEntity() { - MinecraftServer nmsServer = ((CraftServer) Bukkit.getServer()).getServer(); - WorldServer nmsWorld = ((CraftWorld) Bukkit.getWorlds().get(0)).getHandle(); - GameProfile gameProfile = new GameProfile(UUID.randomUUID(), "Chests++NPC"); - EntityPlayer npc = new EntityPlayer(nmsServer, nmsWorld, gameProfile, new PlayerInteractManager(nmsWorld)); - return npc.getBukkitEntity(); - } -} diff --git a/ChestsPlusPlus_1_16_R3/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R3/Crafting.java b/ChestsPlusPlus_1_16_R3/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R3/Crafting.java index 047ee74..a140d1a 100644 --- a/ChestsPlusPlus_1_16_R3/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R3/Crafting.java +++ b/ChestsPlusPlus_1_16_R3/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R3/Crafting.java @@ -3,75 +3,67 @@ package com.jamesdpeters.minecraft.chests.v1_16_R3; import com.jamesdpeters.minecraft.chests.CraftingProvider; import com.jamesdpeters.minecraft.chests.CraftingResult; import net.minecraft.server.v1_16_R3.Container; +import net.minecraft.server.v1_16_R3.ContainerWorkbench; import net.minecraft.server.v1_16_R3.EntityHuman; import net.minecraft.server.v1_16_R3.IRecipe; +import net.minecraft.server.v1_16_R3.InventoryCraftResult; import net.minecraft.server.v1_16_R3.InventoryCrafting; import net.minecraft.server.v1_16_R3.Item; import net.minecraft.server.v1_16_R3.RecipeCrafting; +import net.minecraft.server.v1_16_R3.RecipeRepair; import net.minecraft.server.v1_16_R3.Recipes; import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.craftbukkit.v1_16_R3.CraftServer; import org.bukkit.craftbukkit.v1_16_R3.CraftWorld; +import org.bukkit.craftbukkit.v1_16_R3.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_16_R3.event.CraftEventFactory; +import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftInventoryCrafting; +import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftInventoryView; import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack; import org.bukkit.entity.Player; import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.Recipe; -import java.util.List; import java.util.Optional; -import java.util.stream.Collectors; public class Crafting implements CraftingProvider { @Override - public CraftingResult craft(Player player, World world, List items) { - Container container = new Container(null, -1) { - @Override - public InventoryView getBukkitView() { - return null; - } - - @Override - public boolean canUse(EntityHuman entityHuman) { - return false; - } - }; - - InventoryCrafting crafting = new InventoryCrafting(container, 3, 3); - - for (int i = 0; i < items.size(); i++) { - crafting.setItem(i, CraftItemStack.asNMSCopy(items.get(i))); - } - - CraftServer server = (CraftServer) Bukkit.getServer(); + public CraftingResult craft(Player player, World world, ItemStack[] items) { CraftWorld craftWorld = (CraftWorld) world; - Optional optional = server.getServer().getCraftingManager().craft(Recipes.CRAFTING, crafting, craftWorld.getHandle()); + CraftPlayer craftPlayer = (CraftPlayer) player; + ContainerWorkbench workbench = new ContainerWorkbench(-1, craftPlayer.getHandle().inventory); + + CraftInventoryView view = workbench.getBukkitView(); + CraftInventoryCrafting craftInventoryCrafting = (CraftInventoryCrafting) view.getTopInventory(); + InventoryCrafting inventoryCrafting = (InventoryCrafting) craftInventoryCrafting.getMatrixInventory(); + InventoryCraftResult resultInventory = (InventoryCraftResult) craftInventoryCrafting.getResultInventory(); + + Optional recipe = getNMSRecipe(items, inventoryCrafting, craftWorld); net.minecraft.server.v1_16_R3.ItemStack itemstack = net.minecraft.server.v1_16_R3.ItemStack.b; - if (optional.isPresent()) { - RecipeCrafting recipeCrafting = optional.get(); - itemstack = recipeCrafting.a(crafting); + if (recipe.isPresent()) { + RecipeCrafting recipeCrafting = recipe.get(); + if (resultInventory.a(craftWorld.getHandle(), craftPlayer.getHandle(), recipeCrafting)) { + itemstack = recipeCrafting.a(inventoryCrafting); + } } - CraftingResult result = new CraftingResult(); - result.setResult(CraftItemStack.asBukkitCopy(itemstack)); + net.minecraft.server.v1_16_R3.ItemStack result = CraftEventFactory.callPreCraftEvent(inventoryCrafting, resultInventory, itemstack, view, recipe.orElse(null) instanceof RecipeRepair); - List matrixResult = crafting.getContents().stream() - .map(itemStack -> { - Item remainingItem = itemStack.getItem().getCraftingRemainingItem(); - return remainingItem != null ? CraftItemStack.asBukkitCopy(remainingItem.createItemStack()) : null; - }).collect(Collectors.toList()); + for(int i = 0; i < items.length; ++i) { + Item remaining = inventoryCrafting.getContents().get(i).getItem().getCraftingRemainingItem(); + items[i] = remaining != null ? CraftItemStack.asBukkitCopy(remaining.createItemStack()) : null; + } - result.setMatrixResult(matrixResult); - - return result; + return new CraftingResult(CraftItemStack.asBukkitCopy(result), items); } @Override - public Recipe getRecipe(Player player, World world, List items) { + public Recipe getRecipe(Player player, World world, ItemStack[] items) { Container container = new Container(null, -1) { @Override public InventoryView getBukkitView() { @@ -86,9 +78,9 @@ public class Crafting implements CraftingProvider { InventoryCrafting crafting = new InventoryCrafting(container, 3, 3); - for (int i = 0; i < items.size(); i++) { + for (int i = 0; i < items.length; i++) { if(i >= 9) break; // ItemList cant contain more than 9 items. - crafting.setItem(i, CraftItemStack.asNMSCopy(items.get(i))); + crafting.setItem(i, CraftItemStack.asNMSCopy(items[i])); } CraftServer server = (CraftServer) Bukkit.getServer(); @@ -96,6 +88,14 @@ public class Crafting implements CraftingProvider { Optional optional = server.getServer().getCraftingManager().craft(Recipes.CRAFTING, crafting, craftWorld.getHandle()); return optional.map(IRecipe::toBukkitRecipe).orElse(null); + } + private Optional getNMSRecipe(ItemStack[] craftingMatrix, InventoryCrafting inventoryCrafting, CraftWorld world) { + for(int i = 0; i < craftingMatrix.length; ++i) { + inventoryCrafting.setItem(i, CraftItemStack.asNMSCopy(craftingMatrix[i])); + } + + CraftServer server = (CraftServer) Bukkit.getServer(); + return server.getServer().getCraftingManager().craft(Recipes.CRAFTING, inventoryCrafting, world.getHandle()); } } diff --git a/ChestsPlusPlus_1_16_R3/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R3/NMSProviderImpl.java b/ChestsPlusPlus_1_16_R3/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R3/NMSProviderImpl.java index e18afab..91aab53 100644 --- a/ChestsPlusPlus_1_16_R3/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R3/NMSProviderImpl.java +++ b/ChestsPlusPlus_1_16_R3/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R3/NMSProviderImpl.java @@ -32,11 +32,6 @@ public class NMSProviderImpl implements NMSProvider { return new Crafting(); } - @Override - public NPCProvider getNPCProvider() { - return new NPC(); - } - @Override public void setItemFrameVisible(ItemFrame itemFrame, boolean visible) { itemFrame.setVisible(visible); diff --git a/ChestsPlusPlus_1_16_R3/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R3/NPC.java b/ChestsPlusPlus_1_16_R3/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R3/NPC.java deleted file mode 100644 index 8418b5d..0000000 --- a/ChestsPlusPlus_1_16_R3/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R3/NPC.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.jamesdpeters.minecraft.chests.v1_16_R3; - -import com.jamesdpeters.minecraft.chests.NPCProvider; -import com.mojang.authlib.GameProfile; -import net.minecraft.server.v1_16_R3.EntityPlayer; -import net.minecraft.server.v1_16_R3.MinecraftServer; -import net.minecraft.server.v1_16_R3.PlayerInteractManager; -import net.minecraft.server.v1_16_R3.WorldServer; -import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_16_R3.CraftServer; -import org.bukkit.craftbukkit.v1_16_R3.CraftWorld; -import org.bukkit.entity.HumanEntity; - -import java.util.UUID; - -public class NPC implements NPCProvider { - public HumanEntity createHumanEntity() { - MinecraftServer nmsServer = ((CraftServer) Bukkit.getServer()).getServer(); - WorldServer nmsWorld = ((CraftWorld) Bukkit.getWorlds().get(0)).getHandle(); - GameProfile gameProfile = new GameProfile(UUID.randomUUID(), "Chests++NPC"); - EntityPlayer npc = new EntityPlayer(nmsServer, nmsWorld, gameProfile, new PlayerInteractManager(nmsWorld)); - return npc.getBukkitEntity(); - } -} diff --git a/ChestsPlusPlus_1_17_R1/pom.xml b/ChestsPlusPlus_1_17_R1/pom.xml deleted file mode 100644 index 70035f1..0000000 --- a/ChestsPlusPlus_1_17_R1/pom.xml +++ /dev/null @@ -1,105 +0,0 @@ - - - - - ChestsPlusPlus-Parent - com.jamesdpeters.minecraft.chests - 1.0-SNAPSHOT - ../pom.xml - - - - 1.17.1 - - - 4.0.0 - jar - ChestsPlusPlus_1_17_R1 - 1.0-SNAPSHOT - - - - spigotmc-repo - https://hub.spigotmc.org/nexus/content/repositories/snapshots/ - - - - - - com.jamesdpeters.minecraft.chests - ChestsPlusPlus-API - 1.0-SNAPSHOT - - - - org.spigotmc - spigot-api - ${minecraft.version}-R0.1-SNAPSHOT - provided - - - com.jamesdpeters.minecraft.chests - ChestsPlusPlus_1_16 - 1.0-SNAPSHOT - compile - - - - org.spigotmc - spigot - ${minecraft.version}-R0.1-SNAPSHOT - remapped-mojang - provided - - - - - - - net.md-5 - specialsource-maven-plugin - 1.2.2 - - - package - - remap - - remap-obf - - org.spigotmc:minecraft-server:${minecraft.version}-R0.1-SNAPSHOT:txt:maps-mojang - true - org.spigotmc:spigot:${minecraft.version}-R0.1-SNAPSHOT:jar:remapped-mojang - true - remapped-obf - - - - package - - remap - - remap-spigot - - ${project.build.directory}/${project.artifactId}-${project.version}-remapped-obf.jar - org.spigotmc:minecraft-server:${minecraft.version}-R0.1-SNAPSHOT:csrg:maps-spigot - org.spigotmc:spigot:${minecraft.version}-R0.1-SNAPSHOT:jar:remapped-obf - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - 16 - 16 - - - - - - - \ No newline at end of file diff --git a/ChestsPlusPlus_1_17_R1/src/main/java/com/jamesdpeters/minecraft/chests/v1_17_R1/Crafting.java b/ChestsPlusPlus_1_17_R1/src/main/java/com/jamesdpeters/minecraft/chests/v1_17_R1/Crafting.java deleted file mode 100644 index 4dd9fab..0000000 --- a/ChestsPlusPlus_1_17_R1/src/main/java/com/jamesdpeters/minecraft/chests/v1_17_R1/Crafting.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.jamesdpeters.minecraft.chests.v1_17_R1; - -import com.jamesdpeters.minecraft.chests.CraftingProvider; -import com.jamesdpeters.minecraft.chests.CraftingResult; -import net.minecraft.world.inventory.CraftingContainer; -import net.minecraft.world.inventory.CraftingMenu; -import net.minecraft.world.item.Item; -import net.minecraft.world.item.crafting.CraftingRecipe; -import net.minecraft.world.item.crafting.RecipeType; -import org.bukkit.Bukkit; -import org.bukkit.World; -import org.bukkit.craftbukkit.v1_17_R1.CraftServer; -import org.bukkit.craftbukkit.v1_17_R1.CraftWorld; -import org.bukkit.craftbukkit.v1_17_R1.entity.CraftPlayer; -import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.Recipe; - -import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; - -public class Crafting implements CraftingProvider { - - @Override - public CraftingResult craft(Player player, World world, List items) { - CraftPlayer craftPlayer = (CraftPlayer) player; - CraftingMenu menu = new CraftingMenu(-1, craftPlayer.getHandle().getInventory()); - CraftingContainer crafting = new CraftingContainer(menu.getBukkitView().getHandle(), 3, 3); - - for (int i = 0; i < items.size(); i++) { - crafting.setItem(i, CraftItemStack.asNMSCopy(items.get(i))); - } - - CraftServer server = (CraftServer) Bukkit.getServer(); - CraftWorld craftWorld = (CraftWorld) world; - Optional optional = server.getServer().getRecipeManager().getRecipeFor(RecipeType.CRAFTING, crafting, craftWorld.getHandle()); - - net.minecraft.world.item.ItemStack itemStack = net.minecraft.world.item.ItemStack.EMPTY; - - if (optional.isPresent()) { - CraftingRecipe recipeCrafting = optional.get(); - itemStack = recipeCrafting.assemble(crafting); - } - - CraftingResult result = new CraftingResult(); - result.setResult(CraftItemStack.asBukkitCopy(itemStack)); - - List matrixResult = crafting.getContents().stream() - .map(item -> { - Item remainingItem = item.getItem().getCraftingRemainingItem(); - return remainingItem != null ? CraftItemStack.asBukkitCopy(remainingItem.getDefaultInstance()) : null; - }).collect(Collectors.toList()); - - result.setMatrixResult(matrixResult); - - return result; - } - - @Override - public Recipe getRecipe(Player player, World world, List items) { - CraftPlayer craftPlayer = (CraftPlayer) player; - CraftingMenu menu = new CraftingMenu(-1, craftPlayer.getHandle().getInventory()); - CraftingContainer crafting = new CraftingContainer(menu.getBukkitView().getHandle(), 3, 3); - - for (int i = 0; i < items.size(); i++) { - if(i >= 9) break; // ItemList cant contain more than 9 items. - crafting.setItem(i, CraftItemStack.asNMSCopy(items.get(i))); - } - - CraftServer server = (CraftServer) Bukkit.getServer(); - CraftWorld craftWorld = (CraftWorld) world; - Optional optional = server.getServer().getRecipeManager().getRecipeFor(RecipeType.CRAFTING, crafting, craftWorld.getHandle()); - - return optional.map(net.minecraft.world.item.crafting.Recipe::toBukkitRecipe).orElse(null); - - } -} diff --git a/ChestsPlusPlus_1_17_R1/src/main/java/com/jamesdpeters/minecraft/chests/v1_17_R1/NPC.java b/ChestsPlusPlus_1_17_R1/src/main/java/com/jamesdpeters/minecraft/chests/v1_17_R1/NPC.java deleted file mode 100644 index b64b6b8..0000000 --- a/ChestsPlusPlus_1_17_R1/src/main/java/com/jamesdpeters/minecraft/chests/v1_17_R1/NPC.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.jamesdpeters.minecraft.chests.v1_17_R1; - -import com.jamesdpeters.minecraft.chests.NPCProvider; -import com.mojang.authlib.GameProfile; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.level.ServerLevel; -import net.minecraft.server.level.ServerPlayer; -import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_17_R1.CraftServer; -import org.bukkit.craftbukkit.v1_17_R1.CraftWorld; -import org.bukkit.entity.HumanEntity; - -import java.util.UUID; - -public class NPC implements NPCProvider { - public HumanEntity createHumanEntity() { - MinecraftServer nmsServer = ((CraftServer) Bukkit.getServer()).getServer(); - ServerLevel nmsWorld = ((CraftWorld) Bukkit.getWorlds().get(0)).getHandle(); - GameProfile gameProfile = new GameProfile(UUID.randomUUID(), "Chests++NPC"); - ServerPlayer npc = new ServerPlayer(nmsServer, nmsWorld, gameProfile); - return npc.getBukkitEntity(); - } -} diff --git a/ChestsPlusPlus_LatestMC/pom.xml b/ChestsPlusPlus_LatestMC/pom.xml new file mode 100644 index 0000000..779271d --- /dev/null +++ b/ChestsPlusPlus_LatestMC/pom.xml @@ -0,0 +1,64 @@ + + + + + ChestsPlusPlus-Parent + com.jamesdpeters.minecraft.chests + 1.0-SNAPSHOT + ../pom.xml + + + + 1.17.1 + + + 4.0.0 + jar + ChestsPlusPlus_LatestMC + 1.0-SNAPSHOT + + + + spigotmc-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + + + + com.jamesdpeters.minecraft.chests + ChestsPlusPlus-API + 1.0-SNAPSHOT + + + + org.spigotmc + spigot-api + ${minecraft.version}-R0.1-SNAPSHOT + provided + + + com.jamesdpeters.minecraft.chests + ChestsPlusPlus_1_16 + 1.0-SNAPSHOT + compile + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 16 + 16 + + + + + + + \ No newline at end of file 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 new file mode 100644 index 0000000..685747d --- /dev/null +++ b/ChestsPlusPlus_LatestMC/src/main/java/com/jamesdpeters/minecraft/chests/latest/Crafting.java @@ -0,0 +1,26 @@ +package com.jamesdpeters.minecraft.chests.latest; + +import com.jamesdpeters.minecraft.chests.CraftingProvider; +import com.jamesdpeters.minecraft.chests.CraftingResult; +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.Recipe; + +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; + } + + @Override + public Recipe getRecipe(Player player, World world, ItemStack[] items) { + return Bukkit.getCraftingRecipe(items, world); + } +} diff --git a/ChestsPlusPlus_1_17_R1/src/main/java/com/jamesdpeters/minecraft/chests/v1_17_R1/MaterialChecker_1_17_R1.java b/ChestsPlusPlus_LatestMC/src/main/java/com/jamesdpeters/minecraft/chests/latest/MaterialChecker_1_17_R1.java similarity index 94% rename from ChestsPlusPlus_1_17_R1/src/main/java/com/jamesdpeters/minecraft/chests/v1_17_R1/MaterialChecker_1_17_R1.java rename to ChestsPlusPlus_LatestMC/src/main/java/com/jamesdpeters/minecraft/chests/latest/MaterialChecker_1_17_R1.java index 8289745..5593022 100644 --- a/ChestsPlusPlus_1_17_R1/src/main/java/com/jamesdpeters/minecraft/chests/v1_17_R1/MaterialChecker_1_17_R1.java +++ b/ChestsPlusPlus_LatestMC/src/main/java/com/jamesdpeters/minecraft/chests/latest/MaterialChecker_1_17_R1.java @@ -1,4 +1,4 @@ -package com.jamesdpeters.minecraft.chests.v1_17_R1; +package com.jamesdpeters.minecraft.chests.latest; import com.jamesdpeters.minecraft.chests.MaterialChecker; import com.jamesdpeters.minecraft.chests.v1_16_R1.MaterialChecker_1_16; diff --git a/ChestsPlusPlus_1_17_R1/src/main/java/com/jamesdpeters/minecraft/chests/v1_17_R1/NMSProviderImpl.java b/ChestsPlusPlus_LatestMC/src/main/java/com/jamesdpeters/minecraft/chests/latest/NMSProviderImpl.java similarity index 55% rename from ChestsPlusPlus_1_17_R1/src/main/java/com/jamesdpeters/minecraft/chests/v1_17_R1/NMSProviderImpl.java rename to ChestsPlusPlus_LatestMC/src/main/java/com/jamesdpeters/minecraft/chests/latest/NMSProviderImpl.java index cd1c075..780f4bb 100644 --- a/ChestsPlusPlus_1_17_R1/src/main/java/com/jamesdpeters/minecraft/chests/v1_17_R1/NMSProviderImpl.java +++ b/ChestsPlusPlus_LatestMC/src/main/java/com/jamesdpeters/minecraft/chests/latest/NMSProviderImpl.java @@ -1,6 +1,9 @@ -package com.jamesdpeters.minecraft.chests.v1_17_R1; +package com.jamesdpeters.minecraft.chests.latest; -import com.jamesdpeters.minecraft.chests.*; +import com.jamesdpeters.minecraft.chests.ChestOpener; +import com.jamesdpeters.minecraft.chests.CraftingProvider; +import com.jamesdpeters.minecraft.chests.MaterialChecker; +import com.jamesdpeters.minecraft.chests.NMSProvider; import org.bukkit.block.Lidded; import org.bukkit.entity.ItemFrame; @@ -9,13 +12,11 @@ public class NMSProviderImpl implements NMSProvider { @Override public ChestOpener getChestOpener() { return (inventory, container, tileEntityOpener) -> { - if(hasLiddedAPI()){ - if(container instanceof Lidded){ - if(inventory.getViewers().size() > 0){ - ((Lidded) container).open(); - } else { - ((Lidded) container).close(); - } + if(container instanceof Lidded){ + if(inventory.getViewers().size() > 0){ + ((Lidded) container).open(); + } else { + ((Lidded) container).close(); } } return null; @@ -32,17 +33,9 @@ public class NMSProviderImpl implements NMSProvider { return new Crafting(); } - @Override - public NPCProvider getNPCProvider() { - return new NPC(); - } - @Override public void setItemFrameVisible(ItemFrame itemFrame, boolean visible) { itemFrame.setVisible(visible); } - private boolean hasLiddedAPI(){ - return true; - } } diff --git a/ChestsPlusPlus_Main/dependency-reduced-pom.xml b/ChestsPlusPlus_Main/dependency-reduced-pom.xml new file mode 100644 index 0000000..a060ed2 --- /dev/null +++ b/ChestsPlusPlus_Main/dependency-reduced-pom.xml @@ -0,0 +1,161 @@ + + + 4.0.0 + com.jamesdpeters.minecraft.chests + ChestsPlusPlus-Master + 2.5-Beta + + scm:git:git@github.com:JamesPeters98/ChestsPlusPlus.git + scm:git:git@github.com:JamesPeters98/ChestsPlusPlus.git + @{project.version} + https://github.com/JamesPeters98/ChestsPlusPlus + + + + + true + src/main/resources + + + + + maven-shade-plugin + 3.3.0-SNAPSHOT + + + package + + shade + + + ../Server/plugins/ChestsPlusPlus-${project.version}.jar + + + + + + + fr.minuskube.inv*:* + com.github.MinusKube*:* + net.wesjd:anvilgui* + org.bstats*:* + com.jamesdpeters.minecraft.chests + + + + + org.bstats + com.jamesdpeters.minecraft.chests + + + fr.minuskube.inv + com.jamesdpeters.minecraft.chests + + + + + + org.codehaus.mojo + templating-maven-plugin + 1.0.0 + + + filter-src + + filter-sources + filter-test-sources + + + + + + maven-release-plugin + 3.0.0-M1 + + + org.codehaus.mojo + exec-maven-plugin + 3.0.0 + + + compile + + java + + + + + com.jamesdpeters.minecraft.chests.lang.LangFileProperties + + + + + + + spigotmc-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + dmulloy2-repo + http://repo.dmulloy2.net/nexus/repository/public/ + + + CodeMC + https://repo.codemc.org/repository/maven-public + + + sytm-nexus + https://repo.sytm.de/repository/maven-hosted/ + + + jitpack.io + https://jitpack.io + + + codemc-snapshots + https://repo.codemc.io/repository/maven-snapshots/ + + + + + maven-snapshots + https://repository.apache.org/content/repositories/snapshots/ + + + + + org.spigotmc + spigot-api + 1.14.4-R0.1-SNAPSHOT + provided + + + org.apache.commons + commons-lang3 + 3.4 + compile + + + org.spigotmc + plugin-annotations + 1.2.3-SNAPSHOT + compile + + + org.jetbrains + annotations + 18.0.0 + compile + + + org.projectlombok + lombok + 1.18.20 + provided + + + + 16 + 16 + + diff --git a/ChestsPlusPlus_Main/pom.xml b/ChestsPlusPlus_Main/pom.xml index 8511d5a..669aa99 100644 --- a/ChestsPlusPlus_Main/pom.xml +++ b/ChestsPlusPlus_Main/pom.xml @@ -101,7 +101,7 @@ com.jamesdpeters.minecraft.chests - ChestsPlusPlus_1_17_R1 + ChestsPlusPlus_LatestMC 1.0-SNAPSHOT jar compile diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/api/NMSProviderDefault.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/api/NMSProviderDefault.java index 323ec8b..5607eff 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/api/NMSProviderDefault.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/api/NMSProviderDefault.java @@ -1,44 +1,39 @@ package com.jamesdpeters.minecraft.chests.api; import com.jamesdpeters.minecraft.chests.*; -import com.jamesdpeters.minecraft.chests.v1_16_R1.NMSProviderImpl; +import com.jamesdpeters.minecraft.chests.latest.NMSProviderImpl; import org.bukkit.entity.ItemFrame; public class NMSProviderDefault implements NMSProvider { - //Latest version at time of build is 1.16 - NMSProviderImpl provider1_16; + //Latest version at time of build is 1.17 + //All new versions should be able to run with the default provided after 1.17 + NMSProviderImpl defaultProvider; public NMSProviderDefault() { - provider1_16 = new NMSProviderImpl(); + defaultProvider = new NMSProviderImpl(); } @Override public ChestOpener getChestOpener() { //1.16 ChestOpener contains lidded API! - return provider1_16.getChestOpener(); + return defaultProvider.getChestOpener(); } @Override public MaterialChecker getMaterialChecker() { //Return the current latest MaterialChecker when an newer server implementation is found. - return provider1_16.getMaterialChecker(); + return defaultProvider.getMaterialChecker(); } @Override public CraftingProvider getCraftingProvider() { - // TODO - return null; - } - - @Override - public NPCProvider getNPCProvider() { - return null; + return defaultProvider.getCraftingProvider(); } @Override public void setItemFrameVisible(ItemFrame itemFrame, boolean visible) { //Not supported in Bukkit api 1.14. - provider1_16.setItemFrameVisible(itemFrame, visible); + defaultProvider.setItemFrameVisible(itemFrame, visible); } } diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/crafting/Crafting.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/crafting/Crafting.java index 119d002..efea76d 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/crafting/Crafting.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/crafting/Crafting.java @@ -32,11 +32,11 @@ public class Crafting { }); } - public static Recipe getRecipe(Player player, List craftingTable) { + public static Recipe getRecipe(Player player, ItemStack[] craftingTable) { return ApiSpecific.getNmsProvider().getCraftingProvider().getRecipe(player, Bukkit.getWorlds().get(0), craftingTable); } - public static CraftingResult craft(Player player, List recipe) { + public static CraftingResult craft(Player player, ItemStack[] recipe) { return ApiSpecific.getNmsProvider().getCraftingProvider().craft(player, Bukkit.getWorlds().get(0), recipe); } 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 2a8cdbe..9ab8250 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 @@ -19,7 +19,6 @@ import org.bukkit.block.Container; import org.bukkit.block.Hopper; import org.bukkit.entity.Player; import org.bukkit.event.inventory.InventoryType; -import org.bukkit.event.inventory.PrepareItemCraftEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; @@ -34,7 +33,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; -import java.util.Objects; import java.util.stream.Collectors; public class VirtualCraftingHolder implements InventoryHolder { @@ -105,14 +103,14 @@ public class VirtualCraftingHolder implements InventoryHolder { setHasCompleteRecipe(); } - public void setCrafting(Recipe recipe, List matrix) { + public void setCrafting(Recipe recipe, ItemStack[] matrix) { if (recipe instanceof ShapedRecipe) setCrafting((ShapedRecipe) recipe); 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(); - for (int i = 0; i < matrix.size(); i++) { - ItemStack item = matrix.get(i); + for (int i = 0; i < matrix.length; i++) { + ItemStack item = matrix[i]; if (item != null) { recipeChoices[i] = new ItemStack[]{item}; } else { @@ -136,8 +134,7 @@ public class VirtualCraftingHolder implements InventoryHolder { } public void updateCrafting() { - List crafting = new ArrayList<>(Arrays.asList(inventory.getContents())); - crafting.remove(0); + var crafting = Arrays.copyOfRange(inventory.getContents(),1, inventory.getContents().length); Recipe recipe = Crafting.getRecipe(storage.getOwner().getPlayer(), crafting); getStorage().setRecipe(recipe, crafting); // Only store the crafting matrix if the recipe is valid @@ -314,17 +311,16 @@ public class VirtualCraftingHolder implements InventoryHolder { * @param inventories * @return a valid recipe matrix selected from the inventories provided. */ - private List getRecipeMatrix(List inventories) { + private ItemStack[] getRecipeMatrix(List inventories) { // Loop through recipe choice array to find recipeChoice index. int recipeChoicesAmount = getRecipeChoiceAmount(); -// Bukkit.broadcastMessage("Recipe choices: "+recipeChoicesAmount); for (int recipeChoiceIndex = 0; recipeChoiceIndex < recipeChoicesAmount; recipeChoiceIndex++) { // Store each item selected for the recipe. // This is used to retrieve the actual result taking into account meta data, such as repairing items. - List tempRecipe = Utils.createAirList(9); + ItemStack[] tempRecipe = Utils.createAirList(9); int recipeIndex = 0; // Need a new copy of the inventories to test for each recipe choice. @@ -337,7 +333,7 @@ public class VirtualCraftingHolder implements InventoryHolder { // If there's no recipe choice at this index skip and set the matrix pos to null if (choices == null){ - tempRecipe.set(recipeIndex, null); + tempRecipe[recipeIndex] = null; recipeIndex++; } else { // Otherwise check for a valid item for this recipe choice. @@ -361,7 +357,7 @@ public class VirtualCraftingHolder implements InventoryHolder { tempInv.setItem(index, item); selectedItem.setAmount(1); - tempRecipe.set(recipeIndex, selectedItem); + tempRecipe[recipeIndex] = selectedItem; recipeIndex++; foundMatch = true; @@ -412,31 +408,34 @@ public class VirtualCraftingHolder implements InventoryHolder { } } - List recipe = getRecipeMatrix(tempInvs); + ItemStack[] recipe = getRecipeMatrix(tempInvs); // null recipe means no recipe was found from the inventories. if (recipe == null) return false; // Use NMS to get the real result considering meta data etc. CraftingResult craftingResult = Crafting.craft(storage.getOwner().getPlayer(), recipe); - Recipe recipeActual = Crafting.getRecipe(storage.getOwner().getPlayer(), recipe); - CraftingInventoryImpl craftingInventoryImpl = new CraftingInventoryImpl(craftingInventory, craftingResult.getResult(), recipe, recipeActual); - InventoryViewImpl inventoryView = new InventoryViewImpl(craftingInventory, output, ApiSpecific.getNmsProvider().getNPCProvider().createHumanEntity()); - PrepareItemCraftEvent itemCraftEvent = new PrepareItemCraftEvent(craftingInventoryImpl, inventoryView, false); - Bukkit.getPluginManager().callEvent(itemCraftEvent); + //TODO +// Recipe recipeActual = Crafting.getRecipe(storage.getOwner().getPlayer(), recipe); +// CraftingInventoryImpl craftingInventoryImpl = new CraftingInventoryImpl(craftingInventory, craftingResult.getResult(), recipe, recipeActual); +// InventoryViewImpl inventoryView = new InventoryViewImpl(craftingInventory, output, ApiSpecific.getNmsProvider().getNPCProvider().createHumanEntity()); +// PrepareItemCraftEvent itemCraftEvent = new PrepareItemCraftEvent(craftingInventoryImpl, inventoryView, false); +// Bukkit.getPluginManager().callEvent(itemCraftEvent); - if (craftingInventoryImpl.result == null) return false; + if (craftingResult.getResult() == 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(craftingInventoryImpl.result); + HashMap map = tempOutput.addItem(craftingResult.getResult()); + + boolean isEmpty = Arrays.stream(craftingResult.getMatrixResult()) + .anyMatch(itemStack -> (itemStack == null || itemStack.getType() == Material.AIR)); // Add any leftover items from the recipe e.g buckets. HashMap craftingMatrixLeftOvers = - craftingResult.getMatrixResult().isEmpty() - ? Maps.newHashMap() - : tempOutput.addItem(craftingResult.getMatrixResult().stream().filter(Objects::nonNull).toArray(ItemStack[]::new)); + isEmpty ? Maps.newHashMap() + : tempOutput.addItem(craftingResult.getMatrixResult()); //If result fits into output copy over the temporary inventories. if (map.isEmpty() && craftingMatrixLeftOvers.isEmpty()) { diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/misc/Utils.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/misc/Utils.java index c7986d7..c6a8d82 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/misc/Utils.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/misc/Utils.java @@ -224,11 +224,11 @@ public class Utils { return tempInvs; } - public static List createAirList(int size) { - List itemStacks = new ArrayList<>(); + public static ItemStack[] createAirList(int size) { + ItemStack[] itemStacks = new ItemStack[size]; ItemStack AIR = new ItemStack(Material.AIR); for (int i = 0; i < size; i++) { - itemStacks.add(AIR); + itemStacks[i] = AIR; } return itemStacks; } diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/serialize/RecipeSerializable.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/serialize/RecipeSerializable.java index 70829ae..85864d0 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/serialize/RecipeSerializable.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/serialize/RecipeSerializable.java @@ -24,9 +24,9 @@ public class RecipeSerializable implements ConfigurationSerializable { private NamespacedKey namespacedKey; // Store items used for ComplexRecipes - private List items; + private ItemStack[] items; - public RecipeSerializable(Recipe recipe, List items) { + public RecipeSerializable(Recipe recipe, ItemStack[] items) { this.recipe = recipe; this.items = items; if (recipe instanceof Keyed){ @@ -38,7 +38,7 @@ public class RecipeSerializable implements ConfigurationSerializable { Object obj = map.get("items"); if (obj != null) { //noinspection unchecked - items = (List) obj; + items = (ItemStack[]) obj; } //noinspection deprecation @@ -71,7 +71,7 @@ public class RecipeSerializable implements ConfigurationSerializable { return namespacedKey; } - public List getItems() { + public ItemStack[] getItems() { return items; } } diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/StorageType.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/StorageType.java index a23bae9..9251ba6 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/StorageType.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/StorageType.java @@ -11,6 +11,7 @@ import com.jamesdpeters.minecraft.chests.serialize.ConfigStorage; import com.jamesdpeters.minecraft.chests.serialize.LocationInfo; import com.jamesdpeters.minecraft.chests.serialize.PluginConfig; import com.jamesdpeters.minecraft.chests.storage.StorageUtils; +import org.bukkit.Bukkit; import org.bukkit.Chunk; import org.bukkit.GameMode; import org.bukkit.Location; @@ -27,6 +28,7 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; +import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.SignChangeEvent; import org.bukkit.event.player.PlayerInteractEvent; @@ -269,10 +271,14 @@ public abstract class StorageType implements Listener public void removeStorage(Player player, String group) { T storage = getStorage(player.getUniqueId(), group); if (storage != null) { - storage.getLocations().forEach(location -> { + var locations = new ArrayList<>(storage.getLocations()); + locations.forEach(location -> { if (location != null) { Block block = location.getLocation().getBlock(); - block.breakNaturally(); + BlockBreakEvent bbe = new BlockBreakEvent(block, player); + Bukkit.getPluginManager().callEvent(bbe); + if (!bbe.isCancelled()) + block.breakNaturally(); } }); storage.dropInventory(player.getLocation()); diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/autocraft/AutoCraftingStorage.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/autocraft/AutoCraftingStorage.java index 590ed68..390a303 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/autocraft/AutoCraftingStorage.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/autocraft/AutoCraftingStorage.java @@ -17,7 +17,6 @@ import org.bukkit.inventory.Recipe; import org.bukkit.inventory.ShapedRecipe; import org.bukkit.inventory.ShapelessRecipe; -import java.util.List; import java.util.Map; @SerializableAs("AutoCraftingStorage") @@ -61,7 +60,7 @@ public class AutoCraftingStorage extends AbstractStorage implements Configuratio return false; } - public void setRecipe(Recipe recipe, List items) { + public void setRecipe(Recipe recipe, ItemStack[] items) { if (recipe == null) { recipeSerializable = null; return; diff --git a/pom.xml b/pom.xml index 106332b..e58612a 100644 --- a/pom.xml +++ b/pom.xml @@ -17,7 +17,7 @@ ChestsPlusPlus-Database ChestsPlusPlusAPI - ChestsPlusPlus_1_17_R1 + ChestsPlusPlus_LatestMC ChestsPlusPlus_1_16_R3 ChestsPlusPlus_1_16_R2 ChestsPlusPlus_1_16