From 49ffc021f75f394814e71972101afcba17459274 Mon Sep 17 00:00:00 2001 From: Pim van der Loos Date: Sat, 16 Sep 2017 16:46:05 +0200 Subject: [PATCH] - Added leather, gold, chainmail and iron armor tiers. - Added config options to allow configuration of repair cost for every type of armor. - Fixed repair bug where repairing would fully fix the armored elytra with only 1 repair item, regardless of configuration options. - Fixed bug where making a new armored elytra would fully reset durability, even when both items have low durability. --- pom.xml | 2 +- .../armoredElytra/ArmoredElytra.java | 8 +- .../armoredElytra/EventHandlers.java | 111 ++++++++++++++---- .../armoredElytra/nms/NBTEditor.java | 4 +- .../pim16aap2/armoredElytra/nms/V1_11_R1.java | 84 ++++++++++++- .../pim16aap2/armoredElytra/nms/V1_12_R1.java | 81 ++++++++++++- src/main/resources/config.yml | 5 +- src/main/resources/plugin.yml | 2 +- 8 files changed, 263 insertions(+), 34 deletions(-) diff --git a/pom.xml b/pom.xml index 7393910..20b692e 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 nl.pim16aap2 ArmoredElytra - 1.2.0-SNAPSHOT + 1.3.0-SNAPSHOT spigot-repo diff --git a/src/main/java/nl/pim16aap2/armoredElytra/ArmoredElytra.java b/src/main/java/nl/pim16aap2/armoredElytra/ArmoredElytra.java index 54093a6..7ea90d3 100644 --- a/src/main/java/nl/pim16aap2/armoredElytra/ArmoredElytra.java +++ b/src/main/java/nl/pim16aap2/armoredElytra/ArmoredElytra.java @@ -15,6 +15,9 @@ public class ArmoredElytra extends JavaPlugin implements Listener { private NBTEditor nbtEditor; private boolean cursesAllowed; + private int LEATHER_TO_FULL; + private int GOLD_TO_FULL; + private int IRON_TO_FULL; private int DIAMONDS_TO_FULL; private String[] allowedEnchants; @@ -22,6 +25,9 @@ public class ArmoredElytra extends JavaPlugin implements Listener public void onEnable() { saveDefaultConfig(); + LEATHER_TO_FULL = this.getConfig().getInt("leatherRepair"); + GOLD_TO_FULL = this.getConfig().getInt("goldRepair"); + IRON_TO_FULL = this.getConfig().getInt("ironRepair"); DIAMONDS_TO_FULL = this.getConfig().getInt("diamondsRepair"); cursesAllowed = this.getConfig().getBoolean("allowCurses"); List list = this.getConfig().getStringList("allowedEnchantments"); @@ -35,7 +41,7 @@ public class ArmoredElytra extends JavaPlugin implements Listener if (compatibleMCVer()) { - Bukkit.getPluginManager().registerEvents(new EventHandlers(this, nbtEditor, cursesAllowed, DIAMONDS_TO_FULL, allowedEnchants), this); + Bukkit.getPluginManager().registerEvents(new EventHandlers(this, nbtEditor, cursesAllowed, LEATHER_TO_FULL, GOLD_TO_FULL, IRON_TO_FULL, DIAMONDS_TO_FULL, allowedEnchants), this); } else { Bukkit.getLogger().log(Level.WARNING, "Trying to load the plugin on an incompatible version of Minecraft!"); } diff --git a/src/main/java/nl/pim16aap2/armoredElytra/EventHandlers.java b/src/main/java/nl/pim16aap2/armoredElytra/EventHandlers.java index e94f473..3264329 100644 --- a/src/main/java/nl/pim16aap2/armoredElytra/EventHandlers.java +++ b/src/main/java/nl/pim16aap2/armoredElytra/EventHandlers.java @@ -4,7 +4,9 @@ import java.util.HashMap; import java.util.Map; import java.util.Random; +import org.bukkit.Bukkit; import org.bukkit.Material; +import org.bukkit.attribute.Attribute; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -25,7 +27,9 @@ import net.md_5.bungee.api.ChatColor; public class EventHandlers implements Listener { private int DIAMONDS_TO_FULL; - private int LEATHER_TO_FULL = 4; + private int LEATHER_TO_FULL; + private int GOLD_TO_FULL; + private int IRON_TO_FULL; private boolean cursesAllowed; private NBTEditor nbtEditor; private final ArmoredElytra plugin; @@ -33,14 +37,17 @@ public class EventHandlers implements Listener private String[] cursedEnchantments = {"MENDING", "VANISHING_CURSE", "BINDING_CURSE"}; - - public EventHandlers(ArmoredElytra plugin, NBTEditor nbtEditor, boolean allowCurses, int DIAMONDS_TO_FULL, String[] allowedEnchantments) + + public EventHandlers(ArmoredElytra plugin, NBTEditor nbtEditor, boolean allowCurses, int LEATHER_TO_FULL, int GOLD_TO_FULL, int IRON_TO_FULL, int DIAMONDS_TO_FULL, String[] allowedEnchantments) { this.plugin = plugin; this.nbtEditor = nbtEditor; this.cursesAllowed = allowCurses; this.DIAMONDS_TO_FULL = DIAMONDS_TO_FULL; this.allowedEnchantments = allowedEnchantments; + this.LEATHER_TO_FULL = LEATHER_TO_FULL; + this.GOLD_TO_FULL = GOLD_TO_FULL; + this.IRON_TO_FULL = IRON_TO_FULL; } @@ -180,12 +187,29 @@ public class EventHandlers implements Listener // Create the resulting item. ItemStack result = one.clone(); - int mult = two.getType() == Material.DIAMOND ? 100/DIAMONDS_TO_FULL : 100/LEATHER_TO_FULL; + // Get the multiplier for the repair items. + double mult = 0.01; + if (two.getType() == Material.LEATHER) + { + mult *= (100/LEATHER_TO_FULL); + + } else if (two.getType() == Material.GOLD_INGOT) + { + mult *= (100/GOLD_TO_FULL); + + } else if (two.getType() == Material.IRON_INGOT) + { + mult *= (100/IRON_TO_FULL); + + } else if (two.getType() == Material.DIAMOND) + { + mult *= (100/DIAMONDS_TO_FULL); + } + int maxDurability = one.getType().getMaxDurability(); int durability = one.getDurability(); - int newDurability = (durability - (int) (maxDurability*mult)); - result.setDurability((short) (newDurability >= 0 ? newDurability : 0) ); - + int newDurability = (int) (durability - (maxDurability*mult)); + result.setDurability((short) (newDurability <= 0 ? 0 : newDurability)); return result; } @@ -225,26 +249,62 @@ public class EventHandlers implements Listener @Override public void run() { + ItemStack result = null; // Check if there are items in both input slots. if (anvilInventory.getItem(0) != null && anvilInventory.getItem(1) != null) { // Check if the first input slot contains an elytra. if (anvilInventory.getItem(0).getType() == Material.ELYTRA) { - ItemStack result = null; + int armorTier = 0; + int currentArmorTier = 0; + if (isArmoredElytra(anvilInventory.getItem(0))) + { + currentArmorTier = nbtEditor.getArmorTier(anvilInventory.getItem(0)); + } + /* 0 = No Armor. + * 1 = Leather Armor. + * 2 = Gold Armor. + * 3 = Chain Armor. + * 4 = Iron Armor. + * 5 = Diamond Armor. + */ // Check if the second input slot contains a diamond chestplate. - if (anvilInventory.getItem(1).getType() == Material.DIAMOND_CHESTPLATE) + if (anvilInventory.getItem(1).getType() == Material.LEATHER_CHESTPLATE || + anvilInventory.getItem(1).getType() == Material.GOLD_CHESTPLATE || + anvilInventory.getItem(1).getType() == Material.CHAINMAIL_CHESTPLATE || + anvilInventory.getItem(1).getType() == Material.IRON_CHESTPLATE || + anvilInventory.getItem(1).getType() == Material.DIAMOND_CHESTPLATE) { // Combine the enchantments of the two items in the input slots. result = addEnchants(anvilInventory.getItem(0), anvilInventory.getItem(1), p); - if (anvilInventory.getItem(1).getType() == Material.DIAMOND_CHESTPLATE) + if (anvilInventory.getItem(1).getType() == Material.LEATHER_CHESTPLATE) { - result.setDurability((short)0); + armorTier = 1; + } else if (anvilInventory.getItem(1).getType() == Material.GOLD_CHESTPLATE) + { + armorTier = 2; + } else if (anvilInventory.getItem(1).getType() == Material.CHAINMAIL_CHESTPLATE) + { + armorTier = 3; + } else if (anvilInventory.getItem(1).getType() == Material.IRON_CHESTPLATE) + { + armorTier = 4; + } else if (anvilInventory.getItem(1).getType() == Material.DIAMOND_CHESTPLATE) + { + armorTier = 5; } + short durability = (short) (-anvilInventory.getItem(0).getType().getMaxDurability() - anvilInventory.getItem(0).getDurability() - anvilInventory.getItem(1).getDurability()); + durability = durability < 0 ? 0 : durability; + result.setDurability(durability); } - // If the player tries to repair an armored elytra with diamonds or a regular elytra with leather, repair 52% or 26%. - else if ((anvilInventory.getItem(1).getType() == Material.LEATHER && !isArmoredElytra(anvilInventory.getItem(0))) || - (anvilInventory.getItem(1).getType() == Material.DIAMOND && isArmoredElytra(anvilInventory.getItem(0)))) + // If the player tries to repair an armored elytra. Check if the armor tier and the repair item match. + // If the repair item is leather it can only repair + else if ((anvilInventory.getItem(1).getType() == Material.LEATHER && (!isArmoredElytra(anvilInventory.getItem(0))) || currentArmorTier == 1) || + (anvilInventory.getItem(1).getType() == Material.GOLD_INGOT && isArmoredElytra(anvilInventory.getItem(0)) && currentArmorTier == 2) || + (anvilInventory.getItem(1).getType() == Material.IRON_INGOT && isArmoredElytra(anvilInventory.getItem(0)) && currentArmorTier == 3) || + (anvilInventory.getItem(1).getType() == Material.IRON_INGOT && isArmoredElytra(anvilInventory.getItem(0)) && currentArmorTier == 4) || + (anvilInventory.getItem(1).getType() == Material.DIAMOND && isArmoredElytra(anvilInventory.getItem(0)) && currentArmorTier == 5)) { // Repair the item in the first input slot with items from the second input slot. result = repairItem(anvilInventory.getItem(0), anvilInventory.getItem(1)); @@ -260,10 +320,14 @@ public class EventHandlers implements Listener // Put the created item in the second slot of the anvil. if (result!=null) { - if (anvilInventory.getItem(1).getType() == Material.DIAMOND_CHESTPLATE) + if (anvilInventory.getItem(1).getType() == Material.LEATHER_CHESTPLATE || + anvilInventory.getItem(1).getType() == Material.GOLD_CHESTPLATE || + anvilInventory.getItem(1).getType() == Material.CHAINMAIL_CHESTPLATE || + anvilInventory.getItem(1).getType() == Material.IRON_CHESTPLATE || + anvilInventory.getItem(1).getType() == Material.DIAMOND_CHESTPLATE) { // Add the NBT Tags for the elytra, to give it diamond_chestplate tier of armor protection. - result = nbtEditor.addArmorNBTTags(result); + result = nbtEditor.addArmorNBTTags(result, armorTier); } anvilInventory.setItem(2, result); } @@ -315,7 +379,7 @@ public class EventHandlers implements Listener int durabilityDelta = (100/(enchantLevel+1)) < randomInt ? 0 : 1; if (durability>=maxDurability) { - enquipChestPlayer(p); + unenquipChestPlayer(p); } else newDurability = durability+durabilityDelta; } @@ -323,7 +387,7 @@ public class EventHandlers implements Listener if (newDurability >= maxDurability) { newDurability = maxDurability; - enquipChestPlayer(p); + unenquipChestPlayer(p); } p.getInventory().getChestplate().setDurability((short) (newDurability)); } @@ -334,7 +398,7 @@ public class EventHandlers implements Listener // Remove item from player's chestplate slot and puts it in their normal inventory. - public void enquipChestPlayer(Player p) + public void unenquipChestPlayer(Player p) { p.getInventory().addItem(p.getInventory().getChestplate()); p.getInventory().getChestplate().setAmount(0); @@ -365,8 +429,8 @@ public class EventHandlers implements Listener { if (p.getInventory().getChestplate().getDurability() >= p.getInventory().getChestplate().getType().getMaxDurability()) { - p.sendMessage(ChatColor.RED + "You cannot equip this elytra! Please repair it first by combining it with diamonds in an anvil."); - enquipChestPlayer(p); + p.sendMessage(ChatColor.RED + "You cannot equip this elytra! Please repair it in an anvil first."); + unenquipChestPlayer(p); } } } @@ -376,3 +440,8 @@ public class EventHandlers implements Listener } } } + + + + + diff --git a/src/main/java/nl/pim16aap2/armoredElytra/nms/NBTEditor.java b/src/main/java/nl/pim16aap2/armoredElytra/nms/NBTEditor.java index 473f90d..d795105 100644 --- a/src/main/java/nl/pim16aap2/armoredElytra/nms/NBTEditor.java +++ b/src/main/java/nl/pim16aap2/armoredElytra/nms/NBTEditor.java @@ -4,5 +4,7 @@ import org.bukkit.inventory.ItemStack; public interface NBTEditor { - public ItemStack addArmorNBTTags(ItemStack item); + public ItemStack addArmorNBTTags(ItemStack item, int armorTier); + + public int getArmorTier(ItemStack item); } diff --git a/src/main/java/nl/pim16aap2/armoredElytra/nms/V1_11_R1.java b/src/main/java/nl/pim16aap2/armoredElytra/nms/V1_11_R1.java index 741a9a0..52dbdf7 100644 --- a/src/main/java/nl/pim16aap2/armoredElytra/nms/V1_11_R1.java +++ b/src/main/java/nl/pim16aap2/armoredElytra/nms/V1_11_R1.java @@ -16,10 +16,49 @@ public class V1_11_R1 implements NBTEditor { @Override - public ItemStack addArmorNBTTags(ItemStack item) + public ItemStack addArmorNBTTags(ItemStack item, int armorTier) { ItemMeta itemmeta = item.getItemMeta(); - itemmeta.setDisplayName(ChatColor.AQUA+"Armored Elytra"); + ChatColor color = ChatColor.WHITE; + int armorProtection = 0; + int armorToughness = 0; + /* 0 = No Armor. + * 1 = Leather Armor. + * 2 = Gold Armor. + * 3 = Chain Armor. + * 4 = Iron Armor. + * 5 = Diamond Armor. + */ + // Give the name the correct color. + switch (armorTier) + { + case 1: +// color = ChatColor.valueOf("733D31"); + color = ChatColor.DARK_GREEN; + armorProtection = 3; + break; + case 2: +// color = ChatColor.valueOf("FFD700"); + color = ChatColor.YELLOW; + armorProtection = 5; + break; + case 3: + color = ChatColor.DARK_GRAY; + armorProtection = 5; + break; + case 4: + color = ChatColor.GRAY; + armorProtection = 6; + break; + case 5: + color = ChatColor.AQUA; + armorProtection = 8; + armorToughness = 2; + break; + default: + color = ChatColor.WHITE; + } + itemmeta.setDisplayName(color+"Armored Elytra"); itemmeta.setLore(Arrays.asList("This is an armored Elytra.")); item.setItemMeta(itemmeta); net.minecraft.server.v1_11_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(item); @@ -28,7 +67,7 @@ public class V1_11_R1 implements NBTEditor NBTTagCompound armor = new NBTTagCompound(); armor.set("AttributeName", new NBTTagString("generic.armor")); armor.set("Name", new NBTTagString("generic.armor")); - armor.set("Amount", new NBTTagInt(8)); + armor.set("Amount", new NBTTagInt(armorProtection)); armor.set("Operation", new NBTTagInt(0)); armor.set("UUIDLeast", new NBTTagInt(894654)); armor.set("UUIDMost", new NBTTagInt(2872)); @@ -37,7 +76,7 @@ public class V1_11_R1 implements NBTEditor NBTTagCompound armorTough = new NBTTagCompound(); armorTough.set("AttributeName", new NBTTagString("generic.armorToughness")); armorTough.set("Name", new NBTTagString("generic.armorToughness")); - armorTough.set("Amount", new NBTTagInt(2)); + armorTough.set("Amount", new NBTTagInt(armorToughness)); armorTough.set("Operation", new NBTTagInt(0)); armorTough.set("UUIDLeast", new NBTTagInt(894654)); armorTough.set("UUIDMost", new NBTTagInt(2872)); @@ -45,6 +84,43 @@ public class V1_11_R1 implements NBTEditor modifiers.add(armorTough); compound.set("AttributeModifiers", modifiers); item = CraftItemStack.asBukkitCopy(nmsStack); + return item; } + + // Get the armor tier of the item. + public int getArmorTier(ItemStack item) + { + ItemStack itemTest = item.clone(); + itemTest = addArmorNBTTags(itemTest, 1); + if (itemTest.equals(item)) + { + return 1; + } + + itemTest = addArmorNBTTags(itemTest, 2); + if (itemTest.equals(item)) + { + return 2; + } + + itemTest = addArmorNBTTags(itemTest, 3); + if (itemTest.equals(item)) + { + return 3; + } + + itemTest = addArmorNBTTags(itemTest, 4); + if (itemTest.equals(item)) + { + return 4; + } + + itemTest = addArmorNBTTags(itemTest, 5); + if (itemTest.equals(item)) + { + return 5; + } + return 0; + } } diff --git a/src/main/java/nl/pim16aap2/armoredElytra/nms/V1_12_R1.java b/src/main/java/nl/pim16aap2/armoredElytra/nms/V1_12_R1.java index d660bd4..ad76d2c 100644 --- a/src/main/java/nl/pim16aap2/armoredElytra/nms/V1_12_R1.java +++ b/src/main/java/nl/pim16aap2/armoredElytra/nms/V1_12_R1.java @@ -15,10 +15,47 @@ import net.minecraft.server.v1_12_R1.NBTTagString; public class V1_12_R1 implements NBTEditor { @Override - public ItemStack addArmorNBTTags(ItemStack item) + public ItemStack addArmorNBTTags(ItemStack item, int armorTier) { ItemMeta itemmeta = item.getItemMeta(); - itemmeta.setDisplayName(ChatColor.AQUA+"Armored Elytra"); + ChatColor color = ChatColor.WHITE; + int armorProtection = 0; + int armorToughness = 0; + /* 0 = No Armor. + * 1 = Leather Armor. + * 2 = Gold Armor. + * 3 = Chain Armor. + * 4 = Iron Armor. + * 5 = Diamond Armor. + */ + // Give the name the correct color. + switch (armorTier) + { + case 1: + color = ChatColor.valueOf("733D31"); + armorProtection = 3; + break; + case 2: + color = ChatColor.valueOf("FFD700"); + armorProtection = 5; + break; + case 3: + color = ChatColor.DARK_GRAY; + armorProtection = 5; + break; + case 4: + color = ChatColor.GRAY; + armorProtection = 6; + break; + case 5: + color = ChatColor.AQUA; + armorProtection = 8; + armorToughness = 2; + break; + default: + color = ChatColor.WHITE; + } + itemmeta.setDisplayName(color+"Armored Elytra"); itemmeta.setLore(Arrays.asList("This is an armored Elytra.")); item.setItemMeta(itemmeta); net.minecraft.server.v1_12_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(item); @@ -27,7 +64,7 @@ public class V1_12_R1 implements NBTEditor NBTTagCompound armor = new NBTTagCompound(); armor.set("AttributeName", new NBTTagString("generic.armor")); armor.set("Name", new NBTTagString("generic.armor")); - armor.set("Amount", new NBTTagInt(8)); + armor.set("Amount", new NBTTagInt(armorProtection)); armor.set("Operation", new NBTTagInt(0)); armor.set("UUIDLeast", new NBTTagInt(894654)); armor.set("UUIDMost", new NBTTagInt(2872)); @@ -36,7 +73,7 @@ public class V1_12_R1 implements NBTEditor NBTTagCompound armorTough = new NBTTagCompound(); armorTough.set("AttributeName", new NBTTagString("generic.armorToughness")); armorTough.set("Name", new NBTTagString("generic.armorToughness")); - armorTough.set("Amount", new NBTTagInt(2)); + armorTough.set("Amount", new NBTTagInt(armorToughness)); armorTough.set("Operation", new NBTTagInt(0)); armorTough.set("UUIDLeast", new NBTTagInt(894654)); armorTough.set("UUIDMost", new NBTTagInt(2872)); @@ -46,4 +83,40 @@ public class V1_12_R1 implements NBTEditor item = CraftItemStack.asBukkitCopy(nmsStack); return item; } + + // Get the armor tier of the item. + public int getArmorTier(ItemStack item) + { + ItemStack itemTest = item.clone(); + itemTest = addArmorNBTTags(itemTest, 1); + if (itemTest.equals(item)) + { + return 1; + } + + itemTest = addArmorNBTTags(itemTest, 2); + if (itemTest.equals(item)) + { + return 2; + } + + itemTest = addArmorNBTTags(itemTest, 3); + if (itemTest.equals(item)) + { + return 3; + } + + itemTest = addArmorNBTTags(itemTest, 4); + if (itemTest.equals(item)) + { + return 4; + } + + itemTest = addArmorNBTTags(itemTest, 5); + if (itemTest.equals(item)) + { + return 5; + } + return 0; + } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index ee6ef48..0979049 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,4 +1,7 @@ -# Amount of diamonds it takes to fully repair +# Amount of items it takes to fully repair an armored elytra +leatherRepair: 6 +goldRepair: 5 +ironRepair: 4 diamondsRepair: 3 # Will curses (vanishing, binding) be transferred when creating armored elytras? diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 8302448..7d192ca 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,4 +1,4 @@ name: ArmoredElytra main: nl.pim16aap2.armoredElytra.ArmoredElytra -version: 1.2.0 +version: 1.3.0 author: Pim \ No newline at end of file