From b7059d4544713c1413c02a7e584c6b5226ce9b1a Mon Sep 17 00:00:00 2001 From: Pim van der Loos Date: Fri, 22 Sep 2017 20:47:00 +0200 Subject: [PATCH] - Added permission nodes for wearing and spawning in armor! - armoredelytra.give. - armoredelytra.wear. - Fixed bug where you could not craft leather/gold armored elytras in version 1.12.x of MineCraft. --- pom.xml | 2 +- .../armoredElytra/ArmoredElytra.java | 121 ++++++++++++++++-- .../armoredElytra/EventHandlers.java | 88 ++++++++----- ...{V1_11_R1.java => NBTEditor_V1_11_R1.java} | 4 +- ...{V1_12_R1.java => NBTEditor_V1_12_R1.java} | 6 +- src/main/resources/plugin.yml | 31 ++++- 6 files changed, 204 insertions(+), 48 deletions(-) rename src/main/java/nl/pim16aap2/armoredElytra/nms/{V1_11_R1.java => NBTEditor_V1_11_R1.java} (96%) rename src/main/java/nl/pim16aap2/armoredElytra/nms/{V1_12_R1.java => NBTEditor_V1_12_R1.java} (96%) diff --git a/pom.xml b/pom.xml index 20b692e..d8c5f00 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 nl.pim16aap2 ArmoredElytra - 1.3.0-SNAPSHOT + 1.4.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 7ea90d3..ee21715 100644 --- a/src/main/java/nl/pim16aap2/armoredElytra/ArmoredElytra.java +++ b/src/main/java/nl/pim16aap2/armoredElytra/ArmoredElytra.java @@ -4,12 +4,17 @@ import java.util.List; import java.util.logging.Level; import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; import org.bukkit.event.Listener; +import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; import nl.pim16aap2.armoredElytra.nms.NBTEditor; -import nl.pim16aap2.armoredElytra.nms.V1_11_R1; -import nl.pim16aap2.armoredElytra.nms.V1_12_R1; +import nl.pim16aap2.armoredElytra.nms.NBTEditor_V1_11_R1; +import nl.pim16aap2.armoredElytra.nms.NBTEditor_V1_12_R1; public class ArmoredElytra extends JavaPlugin implements Listener { @@ -23,13 +28,13 @@ public class ArmoredElytra extends JavaPlugin implements Listener @Override 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"); + LEATHER_TO_FULL = this.getConfig().getInt("leatherRepair", 6); + GOLD_TO_FULL = this.getConfig().getInt("goldRepair", 5); + IRON_TO_FULL = this.getConfig().getInt("ironRepair", 4); + DIAMONDS_TO_FULL = this.getConfig().getInt("diamondsRepair", 3); + cursesAllowed = this.getConfig().getBoolean("allowCurses", true); List list = this.getConfig().getStringList("allowedEnchantments"); allowedEnchants = list.toArray(new String[0]); @@ -47,6 +52,102 @@ public class ArmoredElytra extends JavaPlugin implements Listener } } + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) + { + if (sender instanceof Player) + { + Player player = (Player) sender; + if (cmd.getName().equalsIgnoreCase("ArmoredElytra")) + { + if (args.length == 1) + { + ItemStack newElytra = null; + + String tier = args[0]; + // Leather armor. + if (tier.equalsIgnoreCase("leather")) + { + if (player.hasPermission("armoredelytra.give.leather")) + { + player.sendMessage("Giving you an armored elytra of the leather armor tier!"); + newElytra = nbtEditor.addArmorNBTTags(new ItemStack(Material.ELYTRA, 1), 1); + } else + { + player.sendMessage("You do not have the required permission node for this armor tier!"); + } + + // Gold armor. + } else if (tier.equalsIgnoreCase("gold")) + { + if (player.hasPermission("armoredelytra.give.gold")) + { + player.sendMessage("Giving you an armored elytra of the gold armor tier!"); + newElytra = nbtEditor.addArmorNBTTags(new ItemStack(Material.ELYTRA, 1), 2); + } else + { + player.sendMessage("You do not have the required permission node for this armor tier!"); + } + + // Chain armor. + } else if (tier.equalsIgnoreCase("chain")) + { + if (player.hasPermission("armoredelytra.give.chain")) + { + player.sendMessage("Giving you an armored elytra of the chain armor tier!"); + newElytra = nbtEditor.addArmorNBTTags(new ItemStack(Material.ELYTRA, 1), 3); + } else + { + player.sendMessage("You do not have the required permission node for this armor tier!"); + } + + // Iron armor. + } else if (tier.equalsIgnoreCase("iron")) + { + if (player.hasPermission("armoredelytra.give.iron")) + { + player.sendMessage("Giving you an armored elytra of the iron armor tier!"); + newElytra = nbtEditor.addArmorNBTTags(new ItemStack(Material.ELYTRA, 1), 4); + } else + { + player.sendMessage("You do not have the required permission node for this armor tier!"); + } + + // Diamond armor. + } else if (tier.equalsIgnoreCase("diamond")) + { + if (player.hasPermission("armoredelytra.give.diamond")) + { + player.sendMessage("Giving you an armored elytra of the diamond armor tier!"); + newElytra = nbtEditor.addArmorNBTTags(new ItemStack(Material.ELYTRA, 1), 5); + } else + { + player.sendMessage("You do not have the required permission node for this armor tier!"); + } + + } else + { + player.sendMessage("Not a supported armor tier! Try one of these: leather, gold, chain, iron, diamond."); + } + giveArmoredElytraToPlayer(player, newElytra); + return true; + } + } + } + return false; + } + + + // Give the provided player the provided item. + public void giveArmoredElytraToPlayer(Player player, ItemStack item) + { + if (item != null) + { + player.getInventory().addItem(item); + } + } + + // Check + initialize for the correct version of Minecraft. public boolean compatibleMCVer() { @@ -62,11 +163,11 @@ public class ArmoredElytra extends JavaPlugin implements Listener if (version.equals("v1_11_R1")) { - nbtEditor = new V1_11_R1(); + nbtEditor = new NBTEditor_V1_11_R1(); } else if (version.equals("v1_12_R1")) { - nbtEditor = new V1_12_R1(); + nbtEditor = new NBTEditor_V1_12_R1(); } // 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 3264329..95ef84b 100644 --- a/src/main/java/nl/pim16aap2/armoredElytra/EventHandlers.java +++ b/src/main/java/nl/pim16aap2/armoredElytra/EventHandlers.java @@ -6,7 +6,6 @@ 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; @@ -15,6 +14,7 @@ import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityDamageEvent.DamageCause; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryType; +import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.AnvilInventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.EnchantmentStorageMeta; @@ -405,43 +405,73 @@ public class EventHandlers implements Listener } + // Check if the player tries to equip armor by richt clicking it. + @SuppressWarnings("deprecation") + @EventHandler + public void onRightClick(PlayerInteractEvent event) + { + Player player = event.getPlayer(); + + ItemStack item = player.getItemInHand(); + + if (item != null) + { + if (item.getType() == Material.ELYTRA && isArmoredElytra(item)) + { + int armorTier = nbtEditor.getArmorTier(item); + if ((armorTier == 1 && !player.hasPermission("armoredelytra.wear.leather")) || + (armorTier == 2 && !player.hasPermission("armoredelytra.wear.gold")) || + (armorTier == 3 && !player.hasPermission("armoredelytra.wear.chain")) || + (armorTier == 4 && !player.hasPermission("armoredelytra.wear.iron")) || + (armorTier == 5 && !player.hasPermission("armoredelytra.wear.diamond"))) + { + player.sendMessage(ChatColor.RED + "You do not have the required permission to wear this armor tier!."); + event.setCancelled(true); + } + } + } + } + + // Check if the player is trying to equip a broken elytra (and prevent that). @EventHandler public void playerEquipsArmor(InventoryClickEvent e) { if (e.getWhoClicked() instanceof Player) { - Player p = (Player) e.getWhoClicked(); - int slot = e.getRawSlot(); - // Chestplate slot. - if (slot == 6) + Player player = (Player) e.getWhoClicked(); + new BukkitRunnable() { - new BukkitRunnable() - { - @Override - public void run() - { - // If the player equips a new chestplate. - if (p.getInventory().getChestplate() != null) + @Override + public void run() + { + ItemStack chestplate = player.getInventory().getChestplate(); + // If the player equips a new chestplate. + if (player.getInventory().getChestplate() != null) + { + // If that chestplate is an (armored) elytra. + if (chestplate.getType() == Material.ELYTRA && isArmoredElytra(chestplate)) { - // If that chestplate is an (armored) elytra. - if (p.getInventory().getChestplate().getType() == Material.ELYTRA && isArmoredElytra(p.getInventory().getChestplate())) + int armorTier = nbtEditor.getArmorTier(chestplate); + if ((chestplate.getDurability() >= chestplate.getType().getMaxDurability())) { - if (p.getInventory().getChestplate().getDurability() >= p.getInventory().getChestplate().getType().getMaxDurability()) - { - p.sendMessage(ChatColor.RED + "You cannot equip this elytra! Please repair it in an anvil first."); - unenquipChestPlayer(p); - } + player.sendMessage(ChatColor.RED + "You cannot equip this elytra! Please repair it in an anvil first."); + unenquipChestPlayer(player); + } else if ((armorTier == 1 && !player.hasPermission("armoredelytra.wear.leather")) || + (armorTier == 2 && !player.hasPermission("armoredelytra.wear.gold")) || + (armorTier == 3 && !player.hasPermission("armoredelytra.wear.chain")) || + (armorTier == 4 && !player.hasPermission("armoredelytra.wear.iron")) || + (armorTier == 5 && !player.hasPermission("armoredelytra.wear.diamond"))) + { + player.sendMessage(ChatColor.RED + "You do not have the required permission to wear this armor tier!."); + unenquipChestPlayer(player); } + player.updateInventory(); + e.setCancelled(true); } - } - }.runTaskLater(this.plugin, 1); - } - } + } + } + }.runTaskLater(this.plugin, 1); + } } -} - - - - - +} \ No newline at end of file diff --git a/src/main/java/nl/pim16aap2/armoredElytra/nms/V1_11_R1.java b/src/main/java/nl/pim16aap2/armoredElytra/nms/NBTEditor_V1_11_R1.java similarity index 96% rename from src/main/java/nl/pim16aap2/armoredElytra/nms/V1_11_R1.java rename to src/main/java/nl/pim16aap2/armoredElytra/nms/NBTEditor_V1_11_R1.java index 52dbdf7..8064755 100644 --- a/src/main/java/nl/pim16aap2/armoredElytra/nms/V1_11_R1.java +++ b/src/main/java/nl/pim16aap2/armoredElytra/nms/NBTEditor_V1_11_R1.java @@ -12,7 +12,7 @@ import net.minecraft.server.v1_11_R1.NBTTagInt; import net.minecraft.server.v1_11_R1.NBTTagList; import net.minecraft.server.v1_11_R1.NBTTagString; -public class V1_11_R1 implements NBTEditor +public class NBTEditor_V1_11_R1 implements NBTEditor { @Override @@ -33,12 +33,10 @@ public class V1_11_R1 implements NBTEditor 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; diff --git a/src/main/java/nl/pim16aap2/armoredElytra/nms/V1_12_R1.java b/src/main/java/nl/pim16aap2/armoredElytra/nms/NBTEditor_V1_12_R1.java similarity index 96% rename from src/main/java/nl/pim16aap2/armoredElytra/nms/V1_12_R1.java rename to src/main/java/nl/pim16aap2/armoredElytra/nms/NBTEditor_V1_12_R1.java index ad76d2c..630d09b 100644 --- a/src/main/java/nl/pim16aap2/armoredElytra/nms/V1_12_R1.java +++ b/src/main/java/nl/pim16aap2/armoredElytra/nms/NBTEditor_V1_12_R1.java @@ -12,7 +12,7 @@ import net.minecraft.server.v1_12_R1.NBTTagInt; import net.minecraft.server.v1_12_R1.NBTTagList; import net.minecraft.server.v1_12_R1.NBTTagString; -public class V1_12_R1 implements NBTEditor +public class NBTEditor_V1_12_R1 implements NBTEditor { @Override public ItemStack addArmorNBTTags(ItemStack item, int armorTier) @@ -32,11 +32,11 @@ public class V1_12_R1 implements NBTEditor 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: diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 7d192ca..997e56b 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,4 +1,31 @@ name: ArmoredElytra main: nl.pim16aap2.armoredElytra.ArmoredElytra -version: 1.3.0 -author: Pim \ No newline at end of file +version: 1.4.0 +author: Pim +commands: + ArmoredElytra: + description: Give an armored elytra of the specified tier. + usage: /ArmoredElytra + permission: armoredelytra.give + permission-message: You do not have the armoredelytra.give permission node. +permissions: + armoredelytra.wear.leather: + description: Allow the player to wear leather tier armored elytras. + armoredelytra.wear.gold: + description: Allow the player to wear gold tier armored elytras. + armoredelytra.wear.chain: + description: Allow the player to wear chain tier armored elytras. + armoredelytra.wear.iron: + description: Allow the player to wear iron tier armored elytras. + armoredelytra.wear.diamond: + description: Allow the player to wear diamond tier armored elytras. + armoredelytra.give.leather: + description: Allow the player to spawn in leather tier armored elytras. + armoredelytra.give.gold: + description: Allow the player to spawn in gold tier armored elytras. + armoredelytra.give.chain: + description: Allow the player to spawn in chain tier armored elytras. + armoredelytra.give.iron: + description: Allow the player to spawn in iron tier armored elytras. + armoredelytra.give.diamond: + description: Allow the player to spawn in diamond tier armored elytras. \ No newline at end of file