- Fixed NPE when you don't handle in the chestplate correctly.

- Fixed being able to repair AE's with leather even if it's not a leather AE.
- Fixed not being able to repair regular, unarmored elytras with leather.
This commit is contained in:
Pim van der Loos 2018-01-18 13:33:54 +01:00
parent cb18e97165
commit 32fe48d0e1
3 changed files with 64 additions and 27 deletions

View File

@ -5,6 +5,7 @@ import java.util.Map;
import java.util.Random; import java.util.Random;
import java.util.logging.Level; import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
@ -89,10 +90,6 @@ public class EventHandlers implements Listener
Map<Enchantment, Integer> newEnchantments = itemTwo.getEnchantments(); Map<Enchantment, Integer> newEnchantments = itemTwo.getEnchantments();
for (Map.Entry<Enchantment, Integer> entry : result.getEnchantments().entrySet())
if (isAllowedEnchantment(entry.getKey()) == false && (cursesAllowed && isCursedEnchantment(entry.getKey())) == false)
result.removeEnchantment(entry.getKey());
// Enchants from enchanted books have to be accessed in a different way. // Enchants from enchanted books have to be accessed in a different way.
if (itemTwo.getType() == Material.ENCHANTED_BOOK && (nbtEditor.getArmorTier(itemOne) != ArmorTier.NONE)) if (itemTwo.getType() == Material.ENCHANTED_BOOK && (nbtEditor.getArmorTier(itemOne) != ArmorTier.NONE))
{ {
@ -182,6 +179,15 @@ public class EventHandlers implements Listener
return true; return true;
} }
public ItemStack fixEnchants(ItemStack item)
{
ItemStack result = item.clone();
for (Map.Entry<Enchantment, Integer> entry : result.getEnchantments().entrySet())
if (isAllowedEnchantment(entry.getKey()) == false && (cursesAllowed && isCursedEnchantment(entry.getKey())) == false)
result.removeEnchantment(entry.getKey());
return result;
}
// Handle the anvil related parts. // Handle the anvil related parts.
@EventHandler @EventHandler
public void onInventoryClick(InventoryClickEvent e) public void onInventoryClick(InventoryClickEvent e)
@ -214,9 +220,25 @@ public class EventHandlers implements Listener
// If there is an elytra in the final slot (it is unenchantable by default, so we can reasonably expect it to be an enchanted elytra) // If there is an elytra in the final slot (it is unenchantable by default, so we can reasonably expect it to be an enchanted elytra)
// and the player selects it, let the player transfer it to their inventory. // and the player selects it, let the player transfer it to their inventory.
// Verify the end result first, to prevent glitches. If the end result is invalid, remove the item and update the player's inventory. // Verify the end result first, to prevent glitches. If the end result is invalid, remove the item and update the player's inventory.
if (anvilInventory.getItem(2).getType() == Material.ELYTRA && anvilInventory.getItem(0) != null && anvilInventory.getItem(1) != null && verifyEnchants(anvilInventory.getItem(2).getEnchantments())) if (anvilInventory.getItem(2).getType() == Material.ELYTRA &&
anvilInventory.getItem(0) != null &&
anvilInventory.getItem(1) != null &&
verifyEnchants(anvilInventory.getItem(2).getEnchantments()))
{ {
if (e.isShiftClick()) // If the elytra is armored with any tier other than leather and the other item is leather, remove the elytra.
if ((nbtEditor.getArmorTier(anvilInventory.getItem(0)) != ArmorTier.LEATHER ||
nbtEditor.getArmorTier(anvilInventory.getItem(1)) != ArmorTier.LEATHER) &&
(anvilInventory.getItem(0).getType() == Material.LEATHER ||
anvilInventory.getItem(1).getType() == Material.LEATHER) &&
(nbtEditor.getArmorTier(anvilInventory.getItem(0)) != ArmorTier.NONE ||
nbtEditor.getArmorTier(anvilInventory.getItem(1)) != ArmorTier.NONE))
{
Bukkit.broadcastMessage("Nope");
anvilInventory.getItem(2).setAmount(0);
p.updateInventory();
return;
}
else if (e.isShiftClick())
p.getInventory().addItem(anvilInventory.getItem(2)); p.getInventory().addItem(anvilInventory.getItem(2));
else else
p.setItemOnCursor(anvilInventory.getItem(2)); p.setItemOnCursor(anvilInventory.getItem(2));
@ -238,10 +260,10 @@ public class EventHandlers implements Listener
ItemStack itemA = anvilInventory.getItem(0); ItemStack itemA = anvilInventory.getItem(0);
ItemStack itemB = anvilInventory.getItem(1); ItemStack itemB = anvilInventory.getItem(1);
ItemStack result = null; ItemStack result = null;
if (itemB != null) if (itemA != null && itemB != null)
{ {
// If itemB is the elytra, switch itemA and itemB. // If itemB is the elytra, while itemA isn't, switch itemA and itemB.
if (itemB.getType() == Material.ELYTRA) if (itemB.getType() == Material.ELYTRA && itemA.getType() != Material.ELYTRA)
{ {
result = itemA; result = itemA;
itemA = itemB; itemA = itemB;
@ -258,8 +280,12 @@ public class EventHandlers implements Listener
{ {
ArmorTier armorTier = ArmorTier.NONE; ArmorTier armorTier = ArmorTier.NONE;
ArmorTier currentArmorTier = nbtEditor.getArmorTier(itemA); ArmorTier currentArmorTier = nbtEditor.getArmorTier(itemA);
if (currentArmorTier == ArmorTier.NONE && itemB.getType() == Material.LEATHER)
return;
// Check if the second input slot contains a diamond chestplate. // Check if the second input slot contains a diamond chestplate.
if (itemB.getType() == Material.LEATHER_CHESTPLATE || else if (itemB.getType() == Material.LEATHER_CHESTPLATE ||
itemB.getType() == Material.GOLD_CHESTPLATE || itemB.getType() == Material.GOLD_CHESTPLATE ||
itemB.getType() == Material.CHAINMAIL_CHESTPLATE || itemB.getType() == Material.CHAINMAIL_CHESTPLATE ||
itemB.getType() == Material.IRON_CHESTPLATE || itemB.getType() == Material.IRON_CHESTPLATE ||
@ -298,6 +324,18 @@ public class EventHandlers implements Listener
else if (itemB.getType() == Material.ENCHANTED_BOOK) else if (itemB.getType() == Material.ENCHANTED_BOOK)
result = addEnchants(itemA, itemB, p); result = addEnchants(itemA, itemB, p);
// If itemA and itemB are both armored elytras of the same armor tier, repair + share enchantments
else if (itemB.getType() == Material.ELYTRA)
{
if (nbtEditor.getArmorTier(itemB) != ArmorTier.NONE && nbtEditor.getArmorTier(itemA) == nbtEditor.getArmorTier(itemB))
{
result = addEnchants(itemA, itemB, p);
short durability = (short) (- itemA.getType().getMaxDurability() - itemA.getDurability() - itemB.getDurability());
durability = durability < 0 ? 0 : durability;
result.setDurability(durability);
}
}
// Otherwise, remove the item in the result slot (slot2). // Otherwise, remove the item in the result slot (slot2).
else if (anvilInventory.getItem(2) != null) else if (anvilInventory.getItem(2) != null)
anvilInventory.getItem(2).setAmount(0); anvilInventory.getItem(2).setAmount(0);
@ -310,7 +348,7 @@ public class EventHandlers implements Listener
itemB.getType() == Material.CHAINMAIL_CHESTPLATE || itemB.getType() == Material.CHAINMAIL_CHESTPLATE ||
itemB.getType() == Material.IRON_CHESTPLATE || itemB.getType() == Material.IRON_CHESTPLATE ||
itemB.getType() == Material.DIAMOND_CHESTPLATE) itemB.getType() == Material.DIAMOND_CHESTPLATE)
// Add the NBT Tags for the elytra, to give it diamond_chestplate tier of armor protection. // Add the NBT Tags for the elytra, to give it armorTier tier of armor protection.
result = nbtEditor.addArmorNBTTags(result, armorTier, plugin.getConfigLoader().getBool("unbreakable")); result = nbtEditor.addArmorNBTTags(result, armorTier, plugin.getConfigLoader().getBool("unbreakable"));
else if ((nbtEditor.getArmorTier(itemA) != ArmorTier.NONE) && else if ((nbtEditor.getArmorTier(itemA) != ArmorTier.NONE) &&
(nbtEditor.getArmorTier(result) != ArmorTier.NONE)) (nbtEditor.getArmorTier(result) != ArmorTier.NONE))
@ -318,9 +356,11 @@ public class EventHandlers implements Listener
armorTier = nbtEditor.getArmorTier(itemA); armorTier = nbtEditor.getArmorTier(itemA);
result = nbtEditor.addArmorNBTTags(result, armorTier, plugin.getConfigLoader().getBool("unbreakable")); result = nbtEditor.addArmorNBTTags(result, armorTier, plugin.getConfigLoader().getBool("unbreakable"));
} }
// result.setItemMeta(itemMeta) result = fixEnchants(result);
anvilInventory.setItem(2, result); anvilInventory.setItem(2, result);
} }
else if (anvilInventory.getItem(2) != null)
anvilInventory.getItem(2).setAmount(0);
} }
} }
// Check if either itemA or itemB is unoccupied. // Check if either itemA or itemB is unoccupied.

View File

@ -3,6 +3,7 @@ package nl.pim16aap2.armoredElytra.nms;
import java.util.Arrays; import java.util.Arrays;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_11_R1.inventory.CraftItemStack; import org.bukkit.craftbukkit.v1_11_R1.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
@ -78,6 +79,9 @@ public class NBTEditor_V1_11_R1 implements NBTEditor
@Override @Override
public ArmorTier getArmorTier(ItemStack item) public ArmorTier getArmorTier(ItemStack item)
{ {
if (item.getType() != Material.ELYTRA)
return ArmorTier.NONE;
// Get the NBT tags from the item. // Get the NBT tags from the item.
NBTTagCompound compound = CraftItemStack.asNMSCopy(item).getTag(); NBTTagCompound compound = CraftItemStack.asNMSCopy(item).getTag();
if (compound == null) if (compound == null)

View File

@ -18,7 +18,6 @@ public enum ArmorTier
private ChatColor color; private ChatColor color;
private String name; private String name;
// Create a new chip with the given face and suit
private ArmorTier (int armor, int toughness, ChatColor color, String name) private ArmorTier (int armor, int toughness, ChatColor color, String name)
{ {
this.armor = armor; this.armor = armor;
@ -38,10 +37,4 @@ public enum ArmorTier
// return the name of a tier. // return the name of a tier.
public static String getArmorName (ArmorTier item) { return item.name; } public static String getArmorName (ArmorTier item) { return item.name; }
public static ArmorTier tierFromMat(Material type)
{
// TODO Auto-generated method stub
return null;
}
} }