From 602acb0088e84682a89b2c3e17296e37a956bae8 Mon Sep 17 00:00:00 2001 From: Pim van der Loos Date: Tue, 24 Nov 2020 12:26:07 +0100 Subject: [PATCH] Allow taking AEs out of smithing table --- .../armoredElytra/handlers/AnvilHandler.java | 16 +++---- .../handlers/ArmoredElytraHandler.java | 26 +++++++++++- .../handlers/SmithingTableHandler.java | 42 +++++++++++++++++++ 3 files changed, 73 insertions(+), 11 deletions(-) diff --git a/src/main/java/nl/pim16aap2/armoredElytra/handlers/AnvilHandler.java b/src/main/java/nl/pim16aap2/armoredElytra/handlers/AnvilHandler.java index c91a076..f8e93ca 100644 --- a/src/main/java/nl/pim16aap2/armoredElytra/handlers/AnvilHandler.java +++ b/src/main/java/nl/pim16aap2/armoredElytra/handlers/AnvilHandler.java @@ -177,6 +177,9 @@ public class AnvilHandler extends ArmoredElytraHandler implements Listener @EventHandler public void onInventoryClick(InventoryClickEvent e) { + if (e.getRawSlot() != 2) + return; + if (!(e.getWhoClicked() instanceof Player)) return; @@ -204,7 +207,7 @@ public class AnvilHandler extends ArmoredElytraHandler implements Listener int slot = e.getRawSlot(); - if (slot == 2 && anvilInventory.getItem(0) != null && anvilInventory.getItem(1) != null && + if (anvilInventory.getItem(0) != null && anvilInventory.getItem(1) != null && anvilInventory.getItem(2) != null && anvilInventory.getItem(2).getType() == Material.ELYTRA) { ArmorTier armortier = ArmoredElytra.getInstance().getNbtEditor().getArmorTier(anvilInventory.getItem(2)); @@ -221,15 +224,8 @@ public class AnvilHandler extends ArmoredElytraHandler implements Listener plugin.getConfigLoader().unbreakable()); // Give the result to the player and clear the anvil's inventory. - if (e.isShiftClick()) - { - // If the player's inventory is full, don't do anything. - if (player.getInventory().firstEmpty() == -1) - return; - player.getInventory().addItem(result); - } - else - player.setItemOnCursor(result); + if (!giveItemToPlayer(player, result, e.isShiftClick())) + return; // Clean the anvil's inventory after transferring the items. cleanAnvilInventory(anvilInventory); diff --git a/src/main/java/nl/pim16aap2/armoredElytra/handlers/ArmoredElytraHandler.java b/src/main/java/nl/pim16aap2/armoredElytra/handlers/ArmoredElytraHandler.java index e3e6b0c..dbffb3e 100644 --- a/src/main/java/nl/pim16aap2/armoredElytra/handlers/ArmoredElytraHandler.java +++ b/src/main/java/nl/pim16aap2/armoredElytra/handlers/ArmoredElytraHandler.java @@ -3,6 +3,7 @@ package nl.pim16aap2.armoredElytra.handlers; import nl.pim16aap2.armoredElytra.ArmoredElytra; import nl.pim16aap2.armoredElytra.util.XMaterial; import org.bukkit.Material; +import org.bukkit.entity.Player; import org.bukkit.inventory.AnvilInventory; import org.bukkit.inventory.ItemStack; @@ -55,6 +56,29 @@ abstract class ArmoredElytraHandler int maxDurability = Material.ELYTRA.getMaxDurability(); int newDurability = (int) (curDur - (maxDurability * mult)); - return (short) (newDurability <= 0 ? 0 : newDurability); + return (short) (Math.max(newDurability, 0)); + } + + /** + * Attempts to move an item to a player's inventory. + * + * @param player The player to give the item to. + * @param item The item to give. + * @param direct Whether or not to put it in the player's inventory. When set to false it will be put in their + * cursor instead. + * @return True if the item could be given to the player, otherwise false (e.g. when their inventory is full). + */ + protected boolean giveItemToPlayer(final Player player, final ItemStack item, final boolean direct) + { + if (direct) + { + // If the player's inventory is full, don't do anything. + if (player.getInventory().firstEmpty() == -1) + return false; + player.getInventory().addItem(item); + } + else + player.setItemOnCursor(item); + return true; } } diff --git a/src/main/java/nl/pim16aap2/armoredElytra/handlers/SmithingTableHandler.java b/src/main/java/nl/pim16aap2/armoredElytra/handlers/SmithingTableHandler.java index 8c330b1..36d7bb1 100644 --- a/src/main/java/nl/pim16aap2/armoredElytra/handlers/SmithingTableHandler.java +++ b/src/main/java/nl/pim16aap2/armoredElytra/handlers/SmithingTableHandler.java @@ -8,10 +8,14 @@ import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryType; import org.bukkit.event.inventory.PrepareSmithingEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.SmithingInventory; +import java.util.logging.Level; + public class SmithingTableHandler extends ArmoredElytraHandler implements Listener { public SmithingTableHandler(final ArmoredElytra plugin, final boolean creationEnabled) @@ -46,4 +50,42 @@ public class SmithingTableHandler extends ArmoredElytraHandler implements Listen event.setResult(result); } } + + // Let the player take items out of the smithing table. + @EventHandler(ignoreCancelled = true) + public void onInventoryClick(InventoryClickEvent e) + { + if (e.getRawSlot() != 2 || !(e.getWhoClicked() instanceof Player)) + return; + + // Check if the event was a player who interacted with a smithing table. + Player player = (Player) e.getWhoClicked(); + if (e.getView().getType() != InventoryType.SMITHING) + return; + + SmithingInventory smithingInventory; + // Try to cast inventory being used in the event to a smithing inventory. + // This will throw a ClassCastException when a CraftInventoryCustom is used. + try + { + smithingInventory = (SmithingInventory) e.getInventory(); + } + catch (ClassCastException exception) + { + // Print warning to console and exit onInventoryClick event (no support for + // custom inventories as they are usually used for GUI's). + plugin.debugMsg(Level.WARNING, "Could not cast inventory to SmithingInventory for player " + + player.getName() + "! Armored Elytras cannot be crafted!"); + exception.printStackTrace(); + return; + } + + final ItemStack result = smithingInventory.getItem(2); + if (result == null || result.getType() != Material.ELYTRA || + ArmoredElytra.getInstance().getNbtEditor().getArmorTier(result) == ArmorTier.NONE) + return; + + giveItemToPlayer(player, result, e.isShiftClick()); + smithingInventory.clear(); + } }