diff --git a/src/main/java/com/shanebeestudios/vf/api/FurnaceListener.java b/src/main/java/com/shanebeestudios/vf/api/FurnaceListener.java index abb6fd4..0ee3c59 100644 --- a/src/main/java/com/shanebeestudios/vf/api/FurnaceListener.java +++ b/src/main/java/com/shanebeestudios/vf/api/FurnaceListener.java @@ -3,7 +3,6 @@ import com.shanebeestudios.vf.api.chunk.VirtualChunk; import com.shanebeestudios.vf.api.event.machine.FurnaceExtractEvent; import com.shanebeestudios.vf.api.machine.Furnace; -import com.shanebeestudios.vf.api.machine.Machine; import com.shanebeestudios.vf.api.recipe.Fuel; import com.shanebeestudios.vf.api.tile.Tile; import org.bukkit.Chunk; @@ -14,7 +13,6 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.world.ChunkLoadEvent; import org.bukkit.event.world.ChunkUnloadEvent; @@ -22,17 +20,12 @@ import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.ItemStack; -import java.util.HashMap; -import java.util.Map; - class FurnaceListener implements Listener { private final FurnaceManager furnaceManager; private final RecipeManager recipeManager; private final TileManager tileManager; - private final Map openInventories = new HashMap<>(); - FurnaceListener(VirtualFurnaceAPI virtualFurnaceAPI) { this.furnaceManager = virtualFurnaceAPI.getFurnaceManager(); this.recipeManager = virtualFurnaceAPI.getRecipeManager(); @@ -51,7 +44,6 @@ private void onClickFurnace(PlayerInteractEvent event) { if (furnace != null) { event.setCancelled(true); furnace.openInventory(player); - openInventories.put(player, furnace); return; } } @@ -66,24 +58,16 @@ private void onClickFurnace(PlayerInteractEvent event) { event.setCancelled(true); if (event.getHand() != EquipmentSlot.OFF_HAND) { tile.activate(player); - openInventories.put(player, tile.getMachine()); } } } } - @EventHandler - private void onCloseInventory(InventoryCloseEvent event) { - HumanEntity player = event.getPlayer(); - openInventories.remove(player); - - } @EventHandler private void onInventoryClick(InventoryClickEvent event) { HumanEntity clicker = event.getWhoClicked(); - if (openInventories.containsKey(clicker) && clicker instanceof Player) { - Furnace furnace = (Furnace) openInventories.get(clicker); // TODO This may change in the future + if (event.getInventory().getHolder() instanceof Furnace furnace && clicker instanceof Player player) { int slot = event.getRawSlot(); // Give XP to player when they extract from the furnace if (slot == 2) { @@ -91,7 +75,7 @@ private void onInventoryClick(InventoryClickEvent event) { if (output != null) { int exp = (int) furnace.extractExperience(); // Call the furnace extract event - FurnaceExtractEvent extractEvent = new FurnaceExtractEvent(furnace, ((Player) clicker), output, exp); + FurnaceExtractEvent extractEvent = new FurnaceExtractEvent(furnace, player, output, exp); extractEvent.callEvent(); ((Player) clicker).giveExp(extractEvent.getExperience()); diff --git a/src/main/java/com/shanebeestudios/vf/api/FurnaceManager.java b/src/main/java/com/shanebeestudios/vf/api/FurnaceManager.java index 960befe..c557ba4 100644 --- a/src/main/java/com/shanebeestudios/vf/api/FurnaceManager.java +++ b/src/main/java/com/shanebeestudios/vf/api/FurnaceManager.java @@ -81,7 +81,7 @@ public Furnace createFurnace(@NotNull String name) { * Create a new furnace *

This will create a new furnace, add it to the tick list, and save to file

* - * @param name Name of new furnace (This shows up in the inventory view) + * @param name Name of new furnace (This shows up in the inventory view) * @param furnaceProperties Properties to apply to this furnace * @return Instance of this new furnace */ @@ -106,9 +106,9 @@ public Furnace createFurnace(@NotNull String name, @NotNull Consumer fu * Create a new furnace *

This will create a new furnace, add it to the tick list, and save to file

* - * @param name Name of new furnace (This shows up in the inventory view) + * @param name Name of new furnace (This shows up in the inventory view) * @param furnaceProperties Properties to apply to this furnace - * @param function Function to run before furnace is created + * @param function Function to run before furnace is created * @return Instance of this new furnace */ public Furnace createFurnace(@NotNull String name, @NotNull FurnaceProperties furnaceProperties, @Nullable Consumer function) { @@ -137,10 +137,10 @@ public ItemStack createItemWithFurnace(@NotNull String name, @NotNull Material m /** * Create a {@link Furnace} that is attached to an {@link ItemStack} * - * @param name Name of furnace (this will show up in the furnace UI) + * @param name Name of furnace (this will show up in the furnace UI) * @param furnaceProperties Properties associated with this furnace item - * @param material Material of the new ItemStack - * @param glowing Whether the item should glow (enchanted) + * @param material Material of the new ItemStack + * @param glowing Whether the item should glow (enchanted) * @return New ItemStack with a furnace attached */ public ItemStack createItemWithFurnace(@NotNull String name, @NotNull FurnaceProperties furnaceProperties, @NotNull Material material, boolean glowing) { @@ -164,11 +164,11 @@ public ItemStack createItemWithFurnace(@NotNull String name, @NotNull Material m /** * Create a {@link Furnace} that is attached to an {@link ItemStack} * - * @param name Name of furnace (this will show up in the furnace UI) + * @param name Name of furnace (this will show up in the furnace UI) * @param furnaceProperties Properties associated with this furnace item - * @param material Material of the new ItemStack - * @param glowing Whether the item should glow (enchanted) - * @param function Function to run before furnace is created + * @param material Material of the new ItemStack + * @param glowing Whether the item should glow (enchanted) + * @param function Function to run before furnace is created * @return New ItemStack with a furnace attached */ public ItemStack createItemWithFurnace(@NotNull String name, @NotNull FurnaceProperties furnaceProperties, @NotNull Material material, boolean glowing, @Nullable Consumer function) { @@ -190,10 +190,10 @@ public ItemStack createItemWithFurnace(@NotNull String name, @NotNull ItemStack /** * Create a {@link Furnace} that is attached to an {@link ItemStack} * - * @param name Name of furnace (this will show up in the furnace UI) + * @param name Name of furnace (this will show up in the furnace UI) * @param furnaceProperties Properties associated with this furnace item - * @param itemStack ItemStack to be copied and have a furnace attached - * @param glowing Whether the item should glow (enchanted) + * @param itemStack ItemStack to be copied and have a furnace attached + * @param glowing Whether the item should glow (enchanted) * @return Clone of the input ItemStack with a furnace attached */ public ItemStack createItemWithFurnace(@NotNull String name, @NotNull FurnaceProperties furnaceProperties, @NotNull ItemStack itemStack, boolean glowing) { @@ -217,11 +217,11 @@ public ItemStack createItemWithFurnace(@NotNull String name, @NotNull ItemStack /** * Create a {@link Furnace} that is attached to an {@link ItemStack} * - * @param name Name of furnace (this will show up in the furnace UI) + * @param name Name of furnace (this will show up in the furnace UI) * @param furnaceProperties Properties associated with this furnace item - * @param itemStack ItemStack to be copied and have a furnace attached - * @param glowing Whether the item should glow (enchanted) - * @param function Function to run before furnace is created + * @param itemStack ItemStack to be copied and have a furnace attached + * @param glowing Whether the item should glow (enchanted) + * @param function Function to run before furnace is created * @return Clone of the input ItemStack with a furnace attached */ public ItemStack createItemWithFurnace(@NotNull String name, @NotNull FurnaceProperties furnaceProperties, @NotNull ItemStack itemStack, boolean glowing, @Nullable Consumer function) { @@ -278,8 +278,7 @@ void loadFurnaces() { ConfigurationSection section = this.furnaceConfig.getConfigurationSection("furnaces"); if (section != null) { for (String string : section.getKeys(true)) { - if (section.get(string) instanceof Furnace) { - Furnace furnace = ((Furnace) section.get(string)); + if (section.get(string) instanceof Furnace furnace) { this.furnaceMap.put(UUID.fromString(string), (Furnace) section.get(string)); } } @@ -330,6 +329,7 @@ public void saveAll() { /** * Save current furnace YAML from RAM to file */ + @SuppressWarnings("CallToPrintStackTrace") public void saveConfig() { try { furnaceConfig.save(furnaceFile); diff --git a/src/main/java/com/shanebeestudios/vf/api/event/Event.java b/src/main/java/com/shanebeestudios/vf/api/event/Event.java deleted file mode 100644 index 225dd73..0000000 --- a/src/main/java/com/shanebeestudios/vf/api/event/Event.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.shanebeestudios.vf.api.event; - -import org.bukkit.Bukkit; - -/** - * Abstract event class for easy event calls - */ -public abstract class Event extends org.bukkit.event.Event { - - /** - * Call the event - *

This method just simplifies the need to use Bukkit's - * PluginManager to call events

- */ - public void callEvent() { - Bukkit.getPluginManager().callEvent(this); - } - -} diff --git a/src/main/java/com/shanebeestudios/vf/api/event/machine/FurnaceExtractEvent.java b/src/main/java/com/shanebeestudios/vf/api/event/machine/FurnaceExtractEvent.java index 2bf04f1..c14324f 100644 --- a/src/main/java/com/shanebeestudios/vf/api/event/machine/FurnaceExtractEvent.java +++ b/src/main/java/com/shanebeestudios/vf/api/event/machine/FurnaceExtractEvent.java @@ -1,8 +1,8 @@ package com.shanebeestudios.vf.api.event.machine; -import com.shanebeestudios.vf.api.event.Event; import com.shanebeestudios.vf.api.machine.Furnace; import org.bukkit.entity.Player; +import org.bukkit.event.Event; import org.bukkit.event.HandlerList; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; diff --git a/src/main/java/com/shanebeestudios/vf/api/machine/Furnace.java b/src/main/java/com/shanebeestudios/vf/api/machine/Furnace.java index b4fc1ff..c4a7604 100644 --- a/src/main/java/com/shanebeestudios/vf/api/machine/Furnace.java +++ b/src/main/java/com/shanebeestudios/vf/api/machine/Furnace.java @@ -63,6 +63,7 @@ public Furnace(String name) { * @param name Name of the object which will show up in the UI * @param furnaceProperties Property for this furnace */ + @SuppressWarnings("deprecation") // Paper deprecation for AdventureAPI public Furnace(String name, FurnaceProperties furnaceProperties) { super(UUID.randomUUID(), name); this.furnaceProperties = furnaceProperties; @@ -74,12 +75,13 @@ public Furnace(String name, FurnaceProperties furnaceProperties) { this.fuel = null; this.input = null; this.output = null; - this.inventory = Bukkit.createInventory(null, InventoryType.FURNACE, Util.getColString(name)); + this.inventory = Bukkit.createInventory(this, InventoryType.FURNACE, Util.getColString(name)); this.experience = 0.0f; this.updateInventory(); } // Used for deserializer + @SuppressWarnings("deprecation") // Paper deprecation for AdventureAPI private Furnace(String name, UUID uuid, int cookTime, int fuelTime, float xp, ItemStack fuel, ItemStack input, ItemStack output, FurnaceProperties furnaceProperties) { super(uuid, name); this.recipeManager = VirtualFurnaceAPI.getInstance().getRecipeManager(); @@ -103,7 +105,7 @@ private Furnace(String name, UUID uuid, int cookTime, int fuelTime, float xp, It this.fuelTimeTotal = 0; } this.experience = xp; - this.inventory = Bukkit.createInventory(null, InventoryType.FURNACE, Util.getColString(name)); + this.inventory = Bukkit.createInventory(this, InventoryType.FURNACE, Util.getColString(name)); this.updateInventory(); } @@ -262,7 +264,7 @@ private void processBurn() { Fuel fuel = this.recipeManager.getFuelByMaterial(this.fuel.getType()); if (fuel == null) return; FurnaceFuelBurnEvent event = new FurnaceFuelBurnEvent(this, this.fuel, fuel, fuel.getBurnTime()); - Bukkit.getPluginManager().callEvent(event); + event.callEvent(); if (event.isCancelled()) { return; } @@ -305,7 +307,7 @@ private void processCook() { this.experience += result.getExperience(); FurnaceCookEvent event = new FurnaceCookEvent(this, this.input, out); - Bukkit.getPluginManager().callEvent(event); + event.callEvent(); if (event.isCancelled()) { return; } diff --git a/src/main/java/com/shanebeestudios/vf/api/machine/Machine.java b/src/main/java/com/shanebeestudios/vf/api/machine/Machine.java index 6a3a6fd..72c222b 100644 --- a/src/main/java/com/shanebeestudios/vf/api/machine/Machine.java +++ b/src/main/java/com/shanebeestudios/vf/api/machine/Machine.java @@ -1,7 +1,7 @@ package com.shanebeestudios.vf.api.machine; -import com.shanebeestudios.vf.api.util.Util; import org.bukkit.entity.Player; +import org.bukkit.inventory.InventoryHolder; import java.util.Objects; import java.util.UUID; @@ -10,7 +10,7 @@ * Abstract machine class *

Other machines will extend from this class

*/ -public abstract class Machine { +public abstract class Machine implements InventoryHolder { private final String name; private final UUID uniqueID;