Allow taking AEs out of smithing table

This commit is contained in:
Pim van der Loos 2020-11-24 12:26:07 +01:00
parent 0e4be02056
commit 602acb0088
No known key found for this signature in database
GPG Key ID: C16F020ADAE6D5A8
3 changed files with 73 additions and 11 deletions

View File

@ -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)
if (!giveItemToPlayer(player, result, e.isShiftClick()))
return;
player.getInventory().addItem(result);
}
else
player.setItemOnCursor(result);
// Clean the anvil's inventory after transferring the items.
cleanAnvilInventory(anvilInventory);

View File

@ -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;
}
}

View File

@ -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();
}
}