diff --git a/pom.xml b/pom.xml index aab7640..5bead8d 100644 --- a/pom.xml +++ b/pom.xml @@ -1,7 +1,7 @@ 4.0.0 nl.pim16aap2 ArmoredElytra - 1.6.0-SNAPSHOT + 1.7.0-SNAPSHOT diff --git a/src/main/java/nl/pim16aap2/armoredElytra/ArmoredElytra.java b/src/main/java/nl/pim16aap2/armoredElytra/ArmoredElytra.java index 31e82f6..fa53031 100644 --- a/src/main/java/nl/pim16aap2/armoredElytra/ArmoredElytra.java +++ b/src/main/java/nl/pim16aap2/armoredElytra/ArmoredElytra.java @@ -33,6 +33,8 @@ public class ArmoredElytra extends JavaPlugin implements Listener private String elytraReceivedMessage; private boolean checkForUpdates; private boolean upToDate; + private String elytraName; + private String elytraLore; @Override public void onEnable() @@ -47,6 +49,10 @@ public class ArmoredElytra extends JavaPlugin implements Listener "PROTECTION_PROJECTILE","PROTECTION_ENVIRONMENTAL","THORNS"}); config.addDefault("usageDeniedMessage", "You do not have the required permissions to wear %ARMOR_TIER% armored elytras!"); config.addDefault("elytraReceivedMessage", "A(n) %ARMOR_TIER% armored elytra has been bestowed upon you!"); + + config.addDefault("elytraName", "%ARMOR_TIER% Armored Elytra"); + config.addDefault("elytraLore", "Elytra with %ARMOR_TIER% level protection."); + config.addDefault("checkForUpdates", true); saveDefaultConfig(); @@ -59,11 +65,13 @@ public class ArmoredElytra extends JavaPlugin implements Listener allowedEnchants = list.toArray(new String[0]); usageDeniedMessage = config.getString("usageDeniedMessage"); elytraReceivedMessage = config.getString("elytraReceivedMessage"); + elytraName = config.getString("elytraName"); + elytraLore = config.getString("elytraLore"); checkForUpdates = config.getBoolean("checkForUpdates"); usageDeniedMessage = (Objects.equals(usageDeniedMessage, new String("NONE")) ? null : usageDeniedMessage); elytraReceivedMessage = (Objects.equals(elytraReceivedMessage, new String("NONE")) ? null : elytraReceivedMessage); - + elytraLore = (Objects.equals(elytraLore, new String("NONE")) ? null : elytraLore); // Check if the user allows checking for updates. if (checkForUpdates) @@ -122,25 +130,31 @@ public class ArmoredElytra extends JavaPlugin implements Listener messagePlayer(player, ChatColor.WHITE, s); } + // Convert int of armorTier to its string. public String armorTierToString(int armorTier) { String armorTierName = null; switch(armorTier) { case 0: - armorTierName = "unarmored"; + armorTierName = "Unarmored"; + break; case 1: - armorTierName = "leather"; + armorTierName = "Leather"; + break; case 2: - armorTierName = "gold"; + armorTierName = "Gold"; + break; case 3: - armorTierName = "chain"; + armorTierName = "Chain"; + break; case 4: - armorTierName = "iron"; + armorTierName = "Iron"; + break; case 5: - armorTierName = "diamond"; + armorTierName = "Diamond"; + break; } - return armorTierName; } @@ -149,8 +163,7 @@ public class ArmoredElytra extends JavaPlugin implements Listener { if (usageDeniedMessage != null) { - String armorTierName = armorTierToString(armorTier); - String message = usageDeniedMessage.replace("%ARMOR_TIER%", armorTierName); + String message = fillInArmorTierInString(usageDeniedMessage, armorTier); messagePlayer(player, ChatColor.RED, message); } } @@ -160,12 +173,19 @@ public class ArmoredElytra extends JavaPlugin implements Listener { if (elytraReceivedMessage != null) { - String armorTierName = armorTierToString(armorTier); - String message = elytraReceivedMessage.replace("%ARMOR_TIER%", armorTierName); + String message = fillInArmorTierInString(elytraReceivedMessage, armorTier); messagePlayer(player, ChatColor.GREEN, message); } } + // Replace %ARMOR_TIER% by the name of that armor tier in a string. + public String fillInArmorTierInString(String string, int armorTier) + { + String armorTierName = armorTierToString(armorTier); + String replaced = string.replace("%ARMOR_TIER%", armorTierName); + return replaced; + } + // Print a string to the log. public void myLogger(Level level, String s) { @@ -206,47 +226,37 @@ public class ArmoredElytra extends JavaPlugin implements Listener // Leather armor. if (tier.equalsIgnoreCase("leather")) { - if (player.hasPermission("armoredelytra.give.leather")) - { - armorTier = 1; + armorTier = 1; + if (player.hasPermission("armoredelytra.give.leather")) allowed = true; - } // Gold armor. } else if (tier.equalsIgnoreCase("gold")) { - if (player.hasPermission("armoredelytra.give.gold")) - { - armorTier = 2; + armorTier = 2; + if (player.hasPermission("armoredelytra.give.gold")) allowed = true; - } // Chain armor. } else if (tier.equalsIgnoreCase("chain")) { + armorTier = 3; if (player.hasPermission("armoredelytra.give.chain")) - { - armorTier = 3; allowed = true; - } // Iron armor. } else if (tier.equalsIgnoreCase("iron")) { - if (player.hasPermission("armoredelytra.give.iron")) - { - armorTier = 4; + armorTier = 4; + if (player.hasPermission("armoredelytra.give.iron")) allowed = true; - } // Diamond armor. } else if (tier.equalsIgnoreCase("diamond")) { - if (player.hasPermission("armoredelytra.give.diamond")) - { - armorTier = 5; + armorTier = 5; + if (player.hasPermission("armoredelytra.give.diamond")) allowed = true; - } } else { messagePlayer(player, "Not a supported armor tier! Try one of these: leather, gold, chain, iron, diamond."); @@ -337,11 +347,11 @@ public class ArmoredElytra extends JavaPlugin implements Listener if (version.equals("v1_11_R1")) { - nbtEditor = new NBTEditor_V1_11_R1(); + nbtEditor = new NBTEditor_V1_11_R1(elytraName, elytraLore, this); } else if (version.equals("v1_12_R1")) { - nbtEditor = new NBTEditor_V1_12_R1(); + nbtEditor = new NBTEditor_V1_12_R1(elytraName, elytraLore, this); } // Return true if compatible. return nbtEditor != null; diff --git a/src/main/java/nl/pim16aap2/armoredElytra/EventHandlers.java b/src/main/java/nl/pim16aap2/armoredElytra/EventHandlers.java index d015909..bfd3d30 100644 --- a/src/main/java/nl/pim16aap2/armoredElytra/EventHandlers.java +++ b/src/main/java/nl/pim16aap2/armoredElytra/EventHandlers.java @@ -89,20 +89,12 @@ public class EventHandlers implements Listener return false; } - // Check if the elytra being checked is an armored one. - public boolean isArmoredElytra(ItemStack elytra) + public boolean isArmoredElytra(ItemStack elytra) { - if (elytra.hasItemMeta() && elytra.getType() == Material.ELYTRA) - if (elytra.getItemMeta().hasLore()) - if (elytra.getItemMeta().getLore().toString().equals("[This is an armored Elytra.]")) - { - return true; - } - return false; + return (nbtEditor.getArmorTier(elytra) == 0 ? false : true); } - // Copy enchants of 2 items to one item. public ItemStack addEnchants(ItemStack itemOne, ItemStack itemTwo, Player player) { @@ -419,7 +411,6 @@ public class EventHandlers implements Listener } } - // Remove item from player's chestplate slot and puts it in their normal inventory. public void unenquipChestPlayer(Player p) { diff --git a/src/main/java/nl/pim16aap2/armoredElytra/nms/NBTEditor.java b/src/main/java/nl/pim16aap2/armoredElytra/nms/NBTEditor.java index d795105..417cdd4 100644 --- a/src/main/java/nl/pim16aap2/armoredElytra/nms/NBTEditor.java +++ b/src/main/java/nl/pim16aap2/armoredElytra/nms/NBTEditor.java @@ -5,6 +5,6 @@ import org.bukkit.inventory.ItemStack; public interface NBTEditor { public ItemStack addArmorNBTTags(ItemStack item, int armorTier); - + public int getArmorTier(ItemStack item); } diff --git a/src/main/java/nl/pim16aap2/armoredElytra/nms/NBTEditor_V1_11_R1.java b/src/main/java/nl/pim16aap2/armoredElytra/nms/NBTEditor_V1_11_R1.java index 8064755..6150382 100644 --- a/src/main/java/nl/pim16aap2/armoredElytra/nms/NBTEditor_V1_11_R1.java +++ b/src/main/java/nl/pim16aap2/armoredElytra/nms/NBTEditor_V1_11_R1.java @@ -11,10 +11,23 @@ import net.minecraft.server.v1_11_R1.NBTTagCompound; import net.minecraft.server.v1_11_R1.NBTTagInt; import net.minecraft.server.v1_11_R1.NBTTagList; import net.minecraft.server.v1_11_R1.NBTTagString; +import nl.pim16aap2.armoredElytra.ArmoredElytra; public class NBTEditor_V1_11_R1 implements NBTEditor { - + String elytraName; + String elytraLore; + ArmoredElytra plugin; + + // Get the names and lores for every tier of armor. + public NBTEditor_V1_11_R1(String elytraName, String elytraLore, ArmoredElytra plugin) + { + this.elytraName = elytraName; + this.elytraLore = elytraLore; + this.plugin = plugin; + } + + // Add armor to the supplied item, based on the armorTier. @Override public ItemStack addArmorNBTTags(ItemStack item, int armorTier) { @@ -56,9 +69,12 @@ public class NBTEditor_V1_11_R1 implements NBTEditor default: color = ChatColor.WHITE; } - itemmeta.setDisplayName(color+"Armored Elytra"); - itemmeta.setLore(Arrays.asList("This is an armored Elytra.")); + + itemmeta.setDisplayName(color+plugin.fillInArmorTierInString(elytraName, armorTier)); + if (elytraLore != null) + itemmeta.setLore(Arrays.asList(plugin.fillInArmorTierInString(elytraLore, armorTier))); item.setItemMeta(itemmeta); + net.minecraft.server.v1_11_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(item); NBTTagCompound compound = (nmsStack.hasTag()) ? nmsStack.getTag() : new NBTTagCompound(); NBTTagList modifiers = new NBTTagList(); @@ -70,7 +86,8 @@ public class NBTEditor_V1_11_R1 implements NBTEditor armor.set("UUIDLeast", new NBTTagInt(894654)); armor.set("UUIDMost", new NBTTagInt(2872)); armor.set("Slot", new NBTTagString("chest")); - modifiers.add(armor); + modifiers.add(armor); + NBTTagCompound armorTough = new NBTTagCompound(); armorTough.set("AttributeName", new NBTTagString("generic.armorToughness")); armorTough.set("Name", new NBTTagString("generic.armorToughness")); @@ -80,45 +97,52 @@ public class NBTEditor_V1_11_R1 implements NBTEditor armorTough.set("UUIDMost", new NBTTagInt(2872)); armorTough.set("Slot", new NBTTagString("chest")); modifiers.add(armorTough); + compound.set("AttributeModifiers", modifiers); item = CraftItemStack.asBukkitCopy(nmsStack); - return item; } - // Get the armor tier of the item. + // Get the armor tier of the supplied item. + @Override public int getArmorTier(ItemStack item) { - ItemStack itemTest = item.clone(); - itemTest = addArmorNBTTags(itemTest, 1); - if (itemTest.equals(item)) - { - return 1; - } + int armorTier = 0; + int armorValue = 0; - itemTest = addArmorNBTTags(itemTest, 2); - if (itemTest.equals(item)) - { - return 2; - } + // Get the NBT tags from the item. + NBTTagCompound compound = CraftItemStack.asNMSCopy(item).getTag(); + if (compound == null) + return 0; + String nbtTags = compound.toString(); - itemTest = addArmorNBTTags(itemTest, 3); - if (itemTest.equals(item)) + // Check if the item has the generic.armor attribute. + int pos = nbtTags.indexOf(",Slot:\"chest\",AttributeName:\"generic.armor\""); + if (pos > 0) { - return 3; - } + // If so, get the value of the generic.armor attribute. + pos--; + String stringAtPos = nbtTags.substring(pos, pos+1); + armorValue = Integer.parseInt(stringAtPos); + } else + // Otherwise, the item has no armor, so return 0; + return 0; - itemTest = addArmorNBTTags(itemTest, 4); - if (itemTest.equals(item)) + switch (armorValue) { - return 4; + case 3: + armorTier = 1; + break; + case 5: + armorTier = 2; + break; + case 6: + armorTier = 4; + break; + case 8: + armorTier = 5; + break; } - - itemTest = addArmorNBTTags(itemTest, 5); - if (itemTest.equals(item)) - { - return 5; - } - return 0; + return armorTier; } } diff --git a/src/main/java/nl/pim16aap2/armoredElytra/nms/NBTEditor_V1_12_R1.java b/src/main/java/nl/pim16aap2/armoredElytra/nms/NBTEditor_V1_12_R1.java index 630d09b..aa9b141 100644 --- a/src/main/java/nl/pim16aap2/armoredElytra/nms/NBTEditor_V1_12_R1.java +++ b/src/main/java/nl/pim16aap2/armoredElytra/nms/NBTEditor_V1_12_R1.java @@ -11,9 +11,23 @@ import net.minecraft.server.v1_12_R1.NBTTagCompound; import net.minecraft.server.v1_12_R1.NBTTagInt; import net.minecraft.server.v1_12_R1.NBTTagList; import net.minecraft.server.v1_12_R1.NBTTagString; +import nl.pim16aap2.armoredElytra.ArmoredElytra; public class NBTEditor_V1_12_R1 implements NBTEditor { + String elytraName; + String elytraLore; + ArmoredElytra plugin; + + // Get the names and lores for every tier of armor. + public NBTEditor_V1_12_R1(String elytraName, String elytraLore, ArmoredElytra plugin) + { + this.elytraName = elytraName; + this.elytraLore = elytraLore; + this.plugin = plugin; + } + + // Add armor to the supplied item, based on the armorTier. @Override public ItemStack addArmorNBTTags(ItemStack item, int armorTier) { @@ -55,9 +69,12 @@ public class NBTEditor_V1_12_R1 implements NBTEditor default: color = ChatColor.WHITE; } - itemmeta.setDisplayName(color+"Armored Elytra"); - itemmeta.setLore(Arrays.asList("This is an armored Elytra.")); + + itemmeta.setDisplayName(color+plugin.fillInArmorTierInString(elytraName, armorTier)); + if (elytraLore != null) + itemmeta.setLore(Arrays.asList(plugin.fillInArmorTierInString(elytraLore, armorTier))); item.setItemMeta(itemmeta); + net.minecraft.server.v1_12_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(item); NBTTagCompound compound = (nmsStack.hasTag()) ? nmsStack.getTag() : new NBTTagCompound(); NBTTagList modifiers = new NBTTagList(); @@ -69,7 +86,8 @@ public class NBTEditor_V1_12_R1 implements NBTEditor armor.set("UUIDLeast", new NBTTagInt(894654)); armor.set("UUIDMost", new NBTTagInt(2872)); armor.set("Slot", new NBTTagString("chest")); - modifiers.add(armor); + modifiers.add(armor); + NBTTagCompound armorTough = new NBTTagCompound(); armorTough.set("AttributeName", new NBTTagString("generic.armorToughness")); armorTough.set("Name", new NBTTagString("generic.armorToughness")); @@ -79,44 +97,52 @@ public class NBTEditor_V1_12_R1 implements NBTEditor armorTough.set("UUIDMost", new NBTTagInt(2872)); armorTough.set("Slot", new NBTTagString("chest")); modifiers.add(armorTough); + compound.set("AttributeModifiers", modifiers); item = CraftItemStack.asBukkitCopy(nmsStack); return item; } - // Get the armor tier of the item. + // Get the armor tier of the supplied item. + @Override public int getArmorTier(ItemStack item) { - ItemStack itemTest = item.clone(); - itemTest = addArmorNBTTags(itemTest, 1); - if (itemTest.equals(item)) - { - return 1; - } + int armorTier = 0; + int armorValue = 0; - itemTest = addArmorNBTTags(itemTest, 2); - if (itemTest.equals(item)) - { - return 2; - } + // Get the NBT tags from the item. + NBTTagCompound compound = CraftItemStack.asNMSCopy(item).getTag(); + if (compound == null) + return 0; + String nbtTags = compound.toString(); - itemTest = addArmorNBTTags(itemTest, 3); - if (itemTest.equals(item)) + // Check if the item has the generic.armor attribute. + int pos = nbtTags.indexOf(",Slot:\"chest\",AttributeName:\"generic.armor\""); + if (pos > 0) { - return 3; - } + // If so, get the value of the generic.armor attribute. + pos--; + String stringAtPos = nbtTags.substring(pos, pos+1); + armorValue = Integer.parseInt(stringAtPos); + } else + // Otherwise, the item has no armor, so return 0; + return 0; - itemTest = addArmorNBTTags(itemTest, 4); - if (itemTest.equals(item)) + switch (armorValue) { - return 4; + case 3: + armorTier = 1; + break; + case 5: + armorTier = 2; + break; + case 6: + armorTier = 4; + break; + case 8: + armorTier = 5; + break; } - - itemTest = addArmorNBTTags(itemTest, 5); - if (itemTest.equals(item)) - { - return 5; - } - return 0; + return armorTier; } -} +} \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 191d4ca..b1cb799 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -19,11 +19,19 @@ allowedEnchantments: - PROTECTION_ENVIRONMENTAL - THORNS -# Message players receive when they lack the required permissions to wear a certain armor tier. "NONE" = no message +# Message players receive when they lack the required permissions to wear a certain armor tier. "NONE" = no message. +# %ARMOR_TIER% is replaced by the name of the armor tier. usageDeniedMessage: "You do not have the required permissions to wear %ARMOR_TIER% armored elytras!" -# Message players receive when they are given an armored elytra using commands. "NONE" = no message +# Message players receive when they are given an armored elytra using commands. "NONE" = no message. +# %ARMOR_TIER% is replaced by the name of the armor tier. elytraReceivedMessage: "A(n) %ARMOR_TIER% armored elytra has been bestowed upon you!" +# The name of armored elytras. %ARMOR_TIER% is replaced by the name of the armor tier. +elytraName: '%ARMOR_TIER% Armored Elytra' + +# The lore of armored elytras. "NONE" = no lore. %ARMOR_TIER% is replaced by the name of the armor tier. +elytraLore: Elytra with %ARMOR_TIER% level protection. + # Allow this plugin to check for updates on startup. It will not download new versions! checkForUpdates: true \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 092d041..9d5b2a3 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: ArmoredElytra main: nl.pim16aap2.armoredElytra.ArmoredElytra -version: 1.6.0 +version: 1.7.0 author: Pim commands: ArmoredElytra: