From f6c07e8db1d578dac2f07d97fce80a175674d238 Mon Sep 17 00:00:00 2001 From: James Peters Date: Thu, 31 Dec 2020 15:21:11 +0000 Subject: [PATCH] AutoCrafting now respects PrepareItemCraftEvent --- .../jamesdpeters/minecraft/chests/Api.java | 2 +- .../minecraft/chests/NMSProvider.java | 1 + .../minecraft/chests/NPCProvider.java | 7 + .../chests/v1_14_R1/NMSProviderImpl.java | 10 +- .../minecraft/chests/v1_14_R1/NPC.java | 24 ++ .../chests/v1_15_R1/NMSProviderImpl.java | 10 +- .../minecraft/chests/v1_15_R1/NPC.java | 24 ++ .../chests/v1_16_R1/NMSProviderImpl.java | 11 +- .../minecraft/chests/v1_16_R1/NPC.java | 24 ++ .../chests/v1_16_R2/NMSProviderImpl.java | 11 +- .../minecraft/chests/v1_16_R2/NPC.java | 24 ++ .../chests/v1_16_R3/NMSProviderImpl.java | 10 +- .../minecraft/chests/v1_16_R3/NPC.java | 24 ++ .../minecraft/chests/api/ApiSpecific.java | 2 +- .../chests/api/NMSProviderDefault.java | 10 +- .../interfaces/CraftingInventoryImpl.java | 230 ++++++++++++++++++ .../chests/interfaces/InventoryViewImpl.java | 49 ++++ .../interfaces/VirtualCraftingHolder.java | 34 +-- .../chests/listeners/InventoryListener.java | 15 +- 19 files changed, 476 insertions(+), 46 deletions(-) create mode 100644 ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/NPCProvider.java create mode 100644 ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/NPC.java create mode 100644 ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/NPC.java create mode 100644 ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/NPC.java create mode 100644 ChestsPlusPlus_1_16_R2/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R2/NPC.java create mode 100644 ChestsPlusPlus_1_16_R3/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R3/NPC.java create mode 100644 ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/interfaces/CraftingInventoryImpl.java create mode 100644 ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/interfaces/InventoryViewImpl.java 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 a2bf4db..f3c7c41 100644 --- a/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/Api.java +++ b/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/Api.java @@ -15,7 +15,7 @@ public class Api { return plugin; } - public static NMSProvider getNMSProvider() { + public static NMSProvider setupNMSProvider() { String packageName = NMSProvider.class.getPackage().getName(); String nmsVersion = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]; String nmsProvider = packageName + "." + nmsVersion + ".NMSProviderImpl"; 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 be15f57..b0d2db5 100644 --- a/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/NMSProvider.java +++ b/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/NMSProvider.java @@ -6,6 +6,7 @@ public interface NMSProvider { ChestOpener getChestOpener(); MaterialChecker getMaterialChecker(); CraftingProvider getCraftingProvider(); + NPCProvider getNPCProvider(); void setItemFrameVisible(ItemFrame itemFrame, boolean visible); } diff --git a/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/NPCProvider.java b/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/NPCProvider.java new file mode 100644 index 0000000..0bf9023 --- /dev/null +++ b/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/NPCProvider.java @@ -0,0 +1,7 @@ +package com.jamesdpeters.minecraft.chests; + +import org.bukkit.entity.HumanEntity; + +public interface NPCProvider { + HumanEntity createHumanEntity(); +} 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 eda8897..ded5a88 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 @@ -1,9 +1,6 @@ package com.jamesdpeters.minecraft.chests.v1_14_R1; -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 com.jamesdpeters.minecraft.chests.*; import org.bukkit.entity.ItemFrame; public class NMSProviderImpl implements NMSProvider { @@ -22,6 +19,11 @@ 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 new file mode 100644 index 0000000..243f03b --- /dev/null +++ b/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/NPC.java @@ -0,0 +1,24 @@ +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/NMSProviderImpl.java b/ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/NMSProviderImpl.java index 07f8095..7eb8d3f 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 @@ -1,9 +1,6 @@ package com.jamesdpeters.minecraft.chests.v1_15_R1; -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 com.jamesdpeters.minecraft.chests.*; import org.bukkit.entity.ItemFrame; public class NMSProviderImpl implements NMSProvider { @@ -22,6 +19,11 @@ 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 new file mode 100644 index 0000000..c3bc707 --- /dev/null +++ b/ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/NPC.java @@ -0,0 +1,24 @@ +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/NMSProviderImpl.java b/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/NMSProviderImpl.java index af1a139..22e1dc1 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 @@ -1,9 +1,7 @@ package com.jamesdpeters.minecraft.chests.v1_16_R1; -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 com.jamesdpeters.minecraft.chests.*; +import com.jamesdpeters.minecraft.chests.v1_14_R1.NPC; import org.bukkit.block.Lidded; import org.bukkit.entity.ItemFrame; @@ -35,6 +33,11 @@ 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 new file mode 100644 index 0000000..87634b0 --- /dev/null +++ b/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/NPC.java @@ -0,0 +1,24 @@ +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/NMSProviderImpl.java b/ChestsPlusPlus_1_16_R2/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R2/NMSProviderImpl.java index 9f1963d..ca05957 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 @@ -1,9 +1,7 @@ package com.jamesdpeters.minecraft.chests.v1_16_R2; -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 com.jamesdpeters.minecraft.chests.*; +import com.jamesdpeters.minecraft.chests.v1_15_R1.NPC; import org.bukkit.block.Lidded; import org.bukkit.entity.ItemFrame; @@ -35,6 +33,11 @@ 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 new file mode 100644 index 0000000..db65374 --- /dev/null +++ b/ChestsPlusPlus_1_16_R2/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R2/NPC.java @@ -0,0 +1,24 @@ +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/NMSProviderImpl.java b/ChestsPlusPlus_1_16_R3/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R3/NMSProviderImpl.java index aa74e77..e18afab 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 @@ -1,9 +1,6 @@ package com.jamesdpeters.minecraft.chests.v1_16_R3; -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 com.jamesdpeters.minecraft.chests.*; import org.bukkit.block.Lidded; import org.bukkit.entity.ItemFrame; @@ -35,6 +32,11 @@ 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 new file mode 100644 index 0000000..8418b5d --- /dev/null +++ b/ChestsPlusPlus_1_16_R3/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R3/NPC.java @@ -0,0 +1,24 @@ +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_Main/src/main/java/com/jamesdpeters/minecraft/chests/api/ApiSpecific.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/api/ApiSpecific.java index 208c4ac..0d8d3c3 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/api/ApiSpecific.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/api/ApiSpecific.java @@ -14,7 +14,7 @@ public class ApiSpecific { private static NMSProvider nmsProvider; public static void init() { - nmsProvider = Api.getNMSProvider(); + nmsProvider = Api.setupNMSProvider(); if (nmsProvider == null) nmsProvider = new NMSProviderDefault(); materialChecker = nmsProvider.getMaterialChecker(); chestOpener = nmsProvider.getChestOpener(); 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 0d43220..323ec8b 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,9 +1,6 @@ package com.jamesdpeters.minecraft.chests.api; -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 com.jamesdpeters.minecraft.chests.*; import com.jamesdpeters.minecraft.chests.v1_16_R1.NMSProviderImpl; import org.bukkit.entity.ItemFrame; @@ -34,6 +31,11 @@ public class NMSProviderDefault implements NMSProvider { return null; } + @Override + public NPCProvider getNPCProvider() { + return null; + } + @Override public void setItemFrameVisible(ItemFrame itemFrame, boolean visible) { //Not supported in Bukkit api 1.14. diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/interfaces/CraftingInventoryImpl.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/interfaces/CraftingInventoryImpl.java new file mode 100644 index 0000000..37f2fad --- /dev/null +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/interfaces/CraftingInventoryImpl.java @@ -0,0 +1,230 @@ +package com.jamesdpeters.minecraft.chests.interfaces; + +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.entity.HumanEntity; +import org.bukkit.event.inventory.InventoryType; +import org.bukkit.inventory.*; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.HashMap; +import java.util.List; +import java.util.ListIterator; + +public class CraftingInventoryImpl implements CraftingInventory { + + Inventory inventory; + ItemStack result; + ItemStack[] matrix; + Recipe recipe; + + public CraftingInventoryImpl(Inventory inventory, ItemStack result, List matrix, Recipe recipe) { + this.inventory = inventory; + this.result = result; + this.matrix = matrix.toArray(new ItemStack[0]); + this.recipe = recipe; + } + + @Nullable + @Override + public ItemStack getResult() { + return result; + } + + @NotNull + @Override + public ItemStack[] getMatrix() { + return matrix; + } + + @Override + public void setResult(@Nullable ItemStack newResult) { + result = newResult; + } + + @Override + public void setMatrix(@NotNull ItemStack[] contents) { + matrix = contents; + } + + @Nullable + @Override + public Recipe getRecipe() { + return recipe; + } + + + // INVENTORY METHODS + + @Override + public int getSize() { + return inventory.getSize(); + } + + @Override + public int getMaxStackSize() { + return inventory.getMaxStackSize(); + } + + @Override + public void setMaxStackSize(int size) { + inventory.setMaxStackSize(size); + } + + @Nullable + @Override + public ItemStack getItem(int index) { + return inventory.getItem(index); + } + + @Override + public void setItem(int index, @Nullable ItemStack item) { + inventory.setItem(index, item); + } + + @NotNull + @Override + public HashMap addItem(@NotNull ItemStack... items) + throws IllegalArgumentException { + return inventory.addItem(items); + } + + @NotNull + @Override + public HashMap removeItem(@NotNull ItemStack... items) + throws IllegalArgumentException { + return inventory.removeItem(items); + } + + @NotNull + @Override + public ItemStack[] getContents() { + return inventory.getContents(); + } + + @Override + public void setContents(@NotNull ItemStack[] items) throws IllegalArgumentException { + inventory.setContents(items); + } + + @NotNull + @Override + public ItemStack[] getStorageContents() { + return inventory.getStorageContents(); + } + + @Override + public void setStorageContents(@NotNull ItemStack[] items) throws IllegalArgumentException { + inventory.setStorageContents(items); + } + + @Override + public boolean contains(@NotNull Material material) throws IllegalArgumentException { + return inventory.contains(material); + } + + @Override + public boolean contains(@Nullable ItemStack item) { + return inventory.contains(item); + } + + @Override + public boolean contains(@NotNull Material material, int amount) throws IllegalArgumentException { + return inventory.contains(material, amount); + } + + @Override + public boolean contains(@Nullable ItemStack item, int amount) { + return inventory.contains(item, amount); + } + + @Override + public boolean containsAtLeast(@Nullable ItemStack item, int amount) { + return inventory.containsAtLeast(item, amount); + } + + @NotNull + @Override + public HashMap all(@NotNull Material material) + throws IllegalArgumentException { + return inventory.all(material); + } + + @NotNull + @Override + public HashMap all(@Nullable ItemStack item) { + return inventory.all(item); + } + + @Override + public int first(@NotNull Material material) throws IllegalArgumentException { + return inventory.first(material); + } + + @Override + public int first(@NotNull ItemStack item) { + return inventory.first(item); + } + + @Override + public int firstEmpty() { + return inventory.firstEmpty(); + } + + @Override + public void remove(@NotNull Material material) throws IllegalArgumentException { + inventory.remove(material); + } + + @Override + public void remove(@NotNull ItemStack item) { + inventory.remove(item); + } + + @Override + public void clear(int index) { + inventory.clear(index); + } + + @Override + public void clear() { + inventory.clear(); + } + + @NotNull + @Override + public List getViewers() { + return inventory.getViewers(); + } + + @NotNull + @Override + public InventoryType getType() { + return inventory.getType(); + } + + @Nullable + @Override + public InventoryHolder getHolder() { + return inventory.getHolder(); + } + + @NotNull + @Override + public ListIterator iterator() { + return inventory.iterator(); + } + + @NotNull + @Override + public ListIterator iterator(int index) { + return inventory.iterator(index); + } + + @Nullable + @Override + public Location getLocation() { + return inventory.getLocation(); + } +} diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/interfaces/InventoryViewImpl.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/interfaces/InventoryViewImpl.java new file mode 100644 index 0000000..54bda9f --- /dev/null +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/interfaces/InventoryViewImpl.java @@ -0,0 +1,49 @@ +package com.jamesdpeters.minecraft.chests.interfaces; + +import org.bukkit.entity.HumanEntity; +import org.bukkit.event.inventory.InventoryType; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryView; +import org.jetbrains.annotations.NotNull; + +public class InventoryViewImpl extends InventoryView { + + Inventory top, bottom; + HumanEntity humanEntity; + + public InventoryViewImpl(Inventory top, Inventory bottom, HumanEntity humanEntity){ + this.top = top; + this.bottom = bottom; + this.humanEntity = humanEntity; + } + + @NotNull + @Override + public Inventory getTopInventory() { + return top; + } + + @NotNull + @Override + public Inventory getBottomInventory() { + return bottom; + } + + @NotNull + @Override + public HumanEntity getPlayer() { + return humanEntity; + } + + @NotNull + @Override + public InventoryType getType() { + return InventoryType.WORKBENCH; + } + + @NotNull + @Override + public String getTitle() { + return "ChestsPlusPlusInventoryView"; + } +} 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 8e51076..e8d16b3 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 @@ -8,11 +8,6 @@ import com.jamesdpeters.minecraft.chests.serialize.Config; import com.jamesdpeters.minecraft.chests.serialize.LocationInfo; import com.jamesdpeters.minecraft.chests.storage.autocraft.AutoCraftingStorage; import com.jamesdpeters.minecraft.chests.storage.chestlink.ChestLinkStorage; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.stream.Collectors; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.Sound; @@ -22,16 +17,17 @@ import org.bukkit.block.Container; import org.bukkit.block.Hopper; import org.bukkit.entity.Player; import org.bukkit.event.inventory.InventoryType; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.InventoryHolder; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.Recipe; -import org.bukkit.inventory.RecipeChoice; -import org.bukkit.inventory.ShapedRecipe; -import org.bukkit.inventory.ShapelessRecipe; +import org.bukkit.event.inventory.PrepareItemCraftEvent; +import org.bukkit.inventory.*; import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitTask; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.stream.Collectors; + public class VirtualCraftingHolder implements InventoryHolder { private final Inventory inventory; @@ -261,7 +257,7 @@ public class VirtualCraftingHolder implements InventoryHolder { Utils.addIfNotNull(inventories, getInventory(block.getRelative(BlockFace.SOUTH))); Utils.addIfNotNull(inventories, getInventory(block.getRelative(BlockFace.WEST))); - boolean didCraft = craftItem(inventories, output); + boolean didCraft = craftItem(inventories, output, getInventory()); //Play sound if crafting occured. if (didCraft) { @@ -387,7 +383,7 @@ public class VirtualCraftingHolder implements InventoryHolder { return null; } - private boolean craftItem(List inputs, Inventory output) { + private boolean craftItem(List inputs, Inventory output, Inventory craftingInventory) { boolean sameInv = false; Inventory sameInventory = null; @@ -413,10 +409,18 @@ public class VirtualCraftingHolder implements InventoryHolder { // Use NMS to get the real result considering meta data etc. ItemStack realResult = Crafting.craft(recipe); + Recipe recipeActual = Crafting.getRecipe(recipe); + CraftingInventoryImpl craftingInventoryImpl = new CraftingInventoryImpl(craftingInventory, realResult, 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 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(realResult); + HashMap map = tempOutput.addItem(craftingInventoryImpl.result); //If result fits into output copy over the temporary inventories. if (map.isEmpty()) { diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/listeners/InventoryListener.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/listeners/InventoryListener.java index f8551d6..1ec4d33 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/listeners/InventoryListener.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/listeners/InventoryListener.java @@ -6,15 +6,12 @@ import com.jamesdpeters.minecraft.chests.interfaces.VirtualInventoryHolder; import com.jamesdpeters.minecraft.chests.misc.Utils; import com.jamesdpeters.minecraft.chests.sort.InventorySorter; import org.bukkit.Bukkit; +import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; -import org.bukkit.event.inventory.InventoryAction; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.inventory.InventoryCloseEvent; -import org.bukkit.event.inventory.InventoryDragEvent; -import org.bukkit.event.inventory.InventoryInteractEvent; +import org.bukkit.event.inventory.*; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; @@ -122,5 +119,13 @@ public class InventoryListener implements Listener { } } + @EventHandler + public void craftEvent(PrepareItemCraftEvent event){ + if (event.getRecipe() == null) return; + if (event.getRecipe().getResult().getType() == Material.DIAMOND_BLOCK){ + event.getInventory().setResult(null); + } + } + }