From 8127efe8a2f6f5142ce3b034382345e668953efb Mon Sep 17 00:00:00 2001 From: Steven Date: Thu, 24 Nov 2016 02:09:08 +0800 Subject: [PATCH] Update to 1.11 --- .../ArmorStandTools/ArmorStandGUI.java | 23 +- .../ArmorStandTools/ArmorStandTool.java | 7 +- .../plugins/ArmorStandTools/Commands.java | 6 +- .../plugins/ArmorStandTools/Main.java | 122 +++++----- .../plugins/ArmorStandTools/MainListener.java | 21 +- .../St3venAU/plugins/ArmorStandTools/NBT.java | 210 ++++-------------- .../ArmorStandTools/PlotSquaredHook.java | 11 +- .../plugins/ArmorStandTools/Utils.java | 2 +- src/config.yml | 2 +- src/language.yml | 2 +- src/plugin.yml | 2 +- 11 files changed, 126 insertions(+), 282 deletions(-) diff --git a/src/com/gmail/St3venAU/plugins/ArmorStandTools/ArmorStandGUI.java b/src/com/gmail/St3venAU/plugins/ArmorStandTools/ArmorStandGUI.java index c0b5048..424292e 100644 --- a/src/com/gmail/St3venAU/plugins/ArmorStandTools/ArmorStandGUI.java +++ b/src/com/gmail/St3venAU/plugins/ArmorStandTools/ArmorStandGUI.java @@ -42,9 +42,7 @@ class ArmorStandGUI implements Listener { im.setDisplayName(" "); filler.setItemMeta(im); invSlots.add(10); - if(!Main.oneEight) { - invSlots.add(12); - } + invSlots.add(12); invSlots.add(2); invSlots.add(11); invSlots.add(20); @@ -68,12 +66,9 @@ class ArmorStandGUI implements Listener { i.setItem(tool.getSlot(), updateLore(tool)); } } - if(Main.oneEight) { - i.setItem(10, as.getItemInHand()); - } else { - i.setItem(10, as.getEquipment().getItemInMainHand()); - i.setItem(12, as.getEquipment().getItemInOffHand()); - } + + i.setItem(10, as.getEquipment().getItemInMainHand()); + i.setItem(12, as.getEquipment().getItemInOffHand()); i.setItem(2, as.getHelmet()); i.setItem(11, as.getChestplate()); i.setItem(20, as.getLeggings()); @@ -96,7 +91,7 @@ class ArmorStandGUI implements Listener { case ARMS: return Utils.setLore(item, ChatColor.AQUA + Config.arms + ": " + (as.hasArms() ? (ChatColor.GREEN + Config.isOn) : (ChatColor.RED + Config.isOff))); case INVUL: - return Utils.setLore(item, ChatColor.AQUA + Config.invul + ": " + (NBT.isInvulnerable(as) ? (ChatColor.GREEN + Config.isOn) : (ChatColor.RED + Config.isOff))); + return Utils.setLore(item, ChatColor.AQUA + Config.invul + ": " + (as.isInvulnerable() ? (ChatColor.GREEN + Config.isOn) : (ChatColor.RED + Config.isOff))); case SLOTS: return Utils.setLore(item, ChatColor.AQUA + Config.equip + ": " + (NBT.getDisabledSlots(as) == 2039583 ? (ChatColor.GREEN + Config.locked) : (ChatColor.RED + Config.unLocked))); case NODEL: @@ -253,12 +248,8 @@ class ArmorStandGUI implements Listener { @Override public void run() { if(as == null || i == null) return; - if(Main.oneEight) { - as.setItemInHand(i.getItem(10)); - } else { - as.getEquipment().setItemInMainHand(i.getItem(10)); - as.getEquipment().setItemInOffHand(i.getItem(12)); - } + as.getEquipment().setItemInMainHand(i.getItem(10)); + as.getEquipment().setItemInOffHand(i.getItem(12)); as.setHelmet(i.getItem(2)); as.setChestplate(i.getItem(11)); as.setLeggings(i.getItem(20)); diff --git a/src/com/gmail/St3venAU/plugins/ArmorStandTools/ArmorStandTool.java b/src/com/gmail/St3venAU/plugins/ArmorStandTools/ArmorStandTool.java index 635900f..5eff262 100644 --- a/src/com/gmail/St3venAU/plugins/ArmorStandTools/ArmorStandTool.java +++ b/src/com/gmail/St3venAU/plugins/ArmorStandTools/ArmorStandTool.java @@ -105,9 +105,6 @@ public enum ArmorStandTool { static void give(Player p) { PlayerInventory i = p.getInventory(); - for(int slot = 0; slot < 36; slot++) { - i.setItem(slot, null); - } for(ArmorStandTool t : values()) { if(t.enabled && !t.forGui) { i.setItem(t.slot, t.item); @@ -125,7 +122,7 @@ public enum ArmorStandTool { @SuppressWarnings("deprecation") static ArmorStandTool get(Player p) { - return get(Main.oneEight ? p.getItemInHand() : p.getInventory().getItemInMainHand()); + return get(p.getInventory().getItemInMainHand()); } static boolean isTool(ItemStack is) { @@ -134,6 +131,6 @@ public enum ArmorStandTool { @SuppressWarnings("deprecation") static boolean isHoldingTool(Player p) { - return isTool(Main.oneEight ? p.getItemInHand() : p.getInventory().getItemInMainHand()); + return isTool(p.getInventory().getItemInMainHand()); } } \ No newline at end of file diff --git a/src/com/gmail/St3venAU/plugins/ArmorStandTools/Commands.java b/src/com/gmail/St3venAU/plugins/ArmorStandTools/Commands.java index b832989..eeef37f 100644 --- a/src/com/gmail/St3venAU/plugins/ArmorStandTools/Commands.java +++ b/src/com/gmail/St3venAU/plugins/ArmorStandTools/Commands.java @@ -30,12 +30,10 @@ class Commands implements CommandExecutor { if(args.length == 0) { UUID uuid = p.getUniqueId(); if(plugin.savedInventories.containsKey(uuid)) { - p.getInventory().setContents(plugin.savedInventories.get(uuid)); - plugin.savedInventories.remove(uuid); - p.sendMessage(ChatColor.GREEN + Config.invReturned); + plugin.restoreInventory(p); return true; } else { - plugin.savedInventories.put(uuid, p.getInventory().getContents()); + plugin.saveInventoryAndClear(p); ArmorStandTool.give(p); p.sendMessage(ChatColor.GREEN + Config.giveMsg1); p.sendMessage(ChatColor.AQUA + Config.giveMsg2); diff --git a/src/com/gmail/St3venAU/plugins/ArmorStandTools/Main.java b/src/com/gmail/St3venAU/plugins/ArmorStandTools/Main.java index c9b6b95..68256b6 100644 --- a/src/com/gmail/St3venAU/plugins/ArmorStandTools/Main.java +++ b/src/com/gmail/St3venAU/plugins/ArmorStandTools/Main.java @@ -12,6 +12,7 @@ import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; import org.bukkit.metadata.FixedMetadataValue; import org.bukkit.metadata.MetadataValue; import org.bukkit.plugin.java.JavaPlugin; @@ -26,14 +27,10 @@ public class Main extends JavaPlugin { public final HashMap savedInventories = new HashMap(); private final EulerAngle zero = new EulerAngle(0D, 0D, 0D); static String NMS_VERSION; - static boolean oneEight, oneNineFour, oneTen; @Override public void onEnable() { NMS_VERSION = getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3]; - oneEight = NMS_VERSION.startsWith("v1_8"); - oneNineFour = NMS_VERSION.startsWith("v1_9_R2"); - oneTen = NMS_VERSION.startsWith("v1_10"); getServer().getPluginManager().registerEvents(new MainListener(this), this); CommandExecutor ce = new Commands(this); getCommand("astools").setExecutor(ce); @@ -50,8 +47,7 @@ public class Main extends JavaPlugin { for(UUID uuid : savedInventories.keySet()) { p = getServer().getPlayer(uuid); if(p != null && p.isOnline()) { - p.getInventory().setContents(savedInventories.get(uuid)); - p.sendMessage(ChatColor.GREEN + Config.invReturned); + restoreInventory(p); } } savedInventories.clear(); @@ -70,36 +66,66 @@ public class Main extends JavaPlugin { as.remove(); } + private void removeAllTools(Player p) { + PlayerInventory i = p.getInventory(); + for(ArmorStandTool t : ArmorStandTool.values()) { + i.remove(t.getItem()); + } + } + + public void saveInventoryAndClear(Player p) { + ItemStack[] inv = p.getInventory().getContents().clone(); + savedInventories.put(p.getUniqueId(), inv); + p.getInventory().clear(); + } + + void restoreInventory(Player p) { + removeAllTools(p); + UUID uuid = p.getUniqueId(); + ItemStack[] savedInv = savedInventories.get(uuid); + if(savedInv == null) return; + PlayerInventory plrInv = p.getInventory(); + ItemStack[] newItems = plrInv.getContents().clone(); + plrInv.setContents(savedInv); + savedInventories.remove(uuid); + for(ItemStack i : newItems) { + if(i == null) continue; + HashMap couldntFit = plrInv.addItem(i); + for (ItemStack is : couldntFit.values()) { + p.getWorld().dropItem(p.getLocation(), is); + } + } + p.sendMessage(ChatColor.GREEN + Config.invReturned); + } + void pickUpArmorStand(ArmorStand as, Player p, boolean newlySummoned) { carryingArmorStand.put(p.getUniqueId(), as); if(newlySummoned) return; as.setMetadata("startLoc", new FixedMetadataValue(this, as.getLocation())); } + private String getSummonEntityName() { + if(NMS_VERSION.startsWith("v1_9_R2") || NMS_VERSION.startsWith("v1_10")) { + return "ArmorStand"; + } else { + return "minecraft:armor_stand"; + } + } + @SuppressWarnings({"deprecation", "ConstantConditions"}) void generateCmdBlock(Location l, ArmorStand as) { Location loc = as.getLocation(); int dSlots = NBT.getDisabledSlots(as); - String hand, boots, legs, chest, helm, offHand = "0"; - int handDmg, offHandDmg = 0; - if(oneEight) { - hand = as.getItemInHand() == null ? "0" : String.valueOf(as.getItemInHand().getTypeId()); - boots = as.getBoots() == null ? "0" : String.valueOf(as.getBoots().getTypeId()); - legs = as.getLeggings() == null ? "0" : String.valueOf(as.getLeggings().getTypeId()); - chest = as.getChestplate() == null ? "0" : String.valueOf(as.getChestplate().getTypeId()); - helm = as.getHelmet() == null ? "0" : String.valueOf(as.getHelmet().getTypeId()); - handDmg = as.getItemInHand() == null ? 0 : as.getItemInHand().getDurability(); - - } else { - hand = as.getEquipment().getItemInMainHand() == null ? "air" : Utils.getNmsName(as.getEquipment().getItemInMainHand().getType()); - offHand = as.getEquipment().getItemInOffHand() == null ? "air" : Utils.getNmsName(as.getEquipment().getItemInOffHand().getType()); - boots = as.getBoots() == null ? "air" : Utils.getNmsName(as.getBoots().getType()); - legs = as.getLeggings() == null ? "air" : Utils.getNmsName(as.getLeggings().getType()); - chest = as.getChestplate() == null ? "air" : Utils.getNmsName(as.getChestplate().getType()); - helm = as.getHelmet() == null ? "air" : Utils.getNmsName(as.getHelmet().getType()); - handDmg = as.getEquipment().getItemInMainHand() == null ? 0 : as.getEquipment().getItemInMainHand().getDurability(); - offHandDmg = as.getEquipment().getItemInOffHand() == null ? 0 : as.getEquipment().getItemInOffHand().getDurability(); - } + String hand, boots, legs, chest, helm, offHand; + int handDmg, offHandDmg; + hand = as.getEquipment().getItemInMainHand() == null ? "air" : Utils.getNmsName(as.getEquipment().getItemInMainHand().getType()); + offHand = as.getEquipment().getItemInOffHand() == null ? "air" : Utils.getNmsName(as.getEquipment().getItemInOffHand().getType()); + boots = as.getBoots() == null ? "air" : Utils.getNmsName(as.getBoots().getType()); + legs = as.getLeggings() == null ? "air" : Utils.getNmsName(as.getLeggings().getType()); + chest = as.getChestplate() == null ? "air" : Utils.getNmsName(as.getChestplate().getType()); + helm = as.getHelmet() == null ? "air" : Utils.getNmsName(as.getHelmet().getType()); + handDmg = as.getEquipment().getItemInMainHand() == null ? 0 : as.getEquipment().getItemInMainHand().getDurability(); + offHandDmg = as.getEquipment().getItemInOffHand() == null ? 0 : as.getEquipment().getItemInOffHand().getDurability(); int bootsDmg = as.getBoots() == null ? 0 : as.getBoots().getDurability(); int legsDmg = as.getLeggings() == null ? 0 : as.getLeggings().getDurability(); @@ -111,16 +137,14 @@ public class Main extends JavaPlugin { EulerAngle la = as.getLeftArmPose(); EulerAngle ra = as.getRightArmPose(); EulerAngle bo = as.getBodyPose(); - String cmd; - if(!oneEight) { - cmd = "summon ArmorStand " + Utils.twoDec(loc.getX()) + " " + Utils.twoDec(loc.getY()) + " " + Utils.twoDec(loc.getZ()) + " {" + String cmd = "summon " + getSummonEntityName() + " " + Utils.twoDec(loc.getX()) + " " + Utils.twoDec(loc.getY()) + " " + Utils.twoDec(loc.getZ()) + " {" + (as.getMaxHealth() != 20 ? "Attributes:[{Name:\"generic.maxHealth\", Base:" + as.getMaxHealth() + "}]," : "") + (as.isVisible() ? "" : "Invisible:1,") + (as.hasBasePlate() ? "" : "NoBasePlate:1,") + (as.hasGravity() ? "" : "NoGravity:1,") + (as.hasArms() ? "ShowArms:1," : "") + (as.isSmall() ? "Small:1," : "") - + (NBT.isInvulnerable(as) ? "Invulnerable:1," : "") + + (as.isInvulnerable() ? "Invulnerable:1," : "") + (dSlots == 0 ? "" : ("DisabledSlots:" + dSlots + ",")) + (as.isCustomNameVisible() ? "CustomNameVisible:1," : "") + (as.getCustomName() == null ? "" : ("CustomName:\"" + as.getCustomName() + "\",")) @@ -142,34 +166,6 @@ public class Main extends JavaPlugin { + (rl.equals(zero) ? "" : ("RightLeg:[" + Utils.angle(rl.getX()) + "f," + Utils.angle(rl.getY()) + "f," + Utils.angle(rl.getZ()) + "f],")) + (la.equals(zero) ? "" : ("LeftArm:[" + Utils.angle(la.getX()) + "f," + Utils.angle(la.getY()) + "f," + Utils.angle(la.getZ()) + "f],")) + "RightArm:[" + Utils.angle(ra.getX()) + "f," + Utils.angle(ra.getY()) + "f," + Utils.angle(ra.getZ()) + "f]}}"; - } else { - cmd = "summon ArmorStand " + Utils.twoDec(loc.getX()) + " " + Utils.twoDec(loc.getY()) + " " + Utils.twoDec(loc.getZ()) + " {" - + (as.getMaxHealth() != 20 ? "Attributes:[{Name:\"generic.maxHealth\", Base:" + as.getMaxHealth() + "}]," : "") - + (as.isVisible() ? "" : "Invisible:1,") - + (as.hasBasePlate() ? "" : "NoBasePlate:1,") - + (as.hasGravity() ? "" : "NoGravity:1,") - + (as.hasArms() ? "ShowArms:1," : "") - + (as.isSmall() ? "Small:1," : "") - + (NBT.isInvulnerable(as) ? "Invulnerable:1," : "") - + (dSlots == 0 ? "" : ("DisabledSlots:" + dSlots + ",")) - + (as.isCustomNameVisible() ? "CustomNameVisible:1," : "") - + (as.getCustomName() == null ? "" : ("CustomName:\"" + as.getCustomName() + "\",")) - + (loc.getYaw() == 0F ? "" : ("Rotation:[" + Utils.twoDec(loc.getYaw()) + "f],")) - + (as.getItemInHand() == null && as.getBoots() == null && as.getLeggings() == null && as.getChestplate() == null && as.getHelmet() == null ? "" : ( - "Equipment:[" - + "{id:" + hand + ",Count:" + as.getItemInHand().getAmount() + ",Damage:" + handDmg + NBT.getItemStackTags(as.getItemInHand()) + "}," - + "{id:" + boots + ",Count:" + as.getBoots().getAmount() + ",Damage:" + bootsDmg + NBT.getItemStackTags(as.getBoots()) + "}," - + "{id:" + legs + ",Count:" + as.getLeggings().getAmount() + ",Damage:" + legsDmg + NBT.getItemStackTags(as.getLeggings()) + "}," - + "{id:" + chest + ",Count:" + as.getChestplate().getAmount() + ",Damage:" + chestDmg + NBT.getItemStackTags(as.getChestplate()) + "}," - + "{id:" + helm + ",Count:" + as.getHelmet().getAmount() + ",Damage:" + helmDmg + NBT.getItemStackTags(as.getHelmet()) + NBT.skullOwner(as.getHelmet()) + "}],")) - + "Pose:{" - + (bo.equals(zero) ? "" : ("Body:[" + Utils.angle(bo.getX()) + "f," + Utils.angle(bo.getY()) + "f," + Utils.angle(bo.getZ()) + "f],")) - + (he.equals(zero) ? "" : ("Head:[" + Utils.angle(he.getX()) + "f," + Utils.angle(he.getY()) + "f," + Utils.angle(he.getZ()) + "f],")) - + (ll.equals(zero) ? "" : ("LeftLeg:[" + Utils.angle(ll.getX()) + "f," + Utils.angle(ll.getY()) + "f," + Utils.angle(ll.getZ()) + "f],")) - + (rl.equals(zero) ? "" : ("RightLeg:[" + Utils.angle(rl.getX()) + "f," + Utils.angle(rl.getY()) + "f," + Utils.angle(rl.getZ()) + "f],")) - + (la.equals(zero) ? "" : ("LeftArm:[" + Utils.angle(la.getX()) + "f," + Utils.angle(la.getY()) + "f," + Utils.angle(la.getZ()) + "f],")) - + "RightArm:[" + Utils.angle(ra.getX()) + "f," + Utils.angle(ra.getY()) + "f," + Utils.angle(ra.getZ()) + "f]}}"; - } Block b = l.getBlock(); b.setType(Material.COMMAND); b.setData((byte) 0); @@ -185,12 +181,8 @@ public class Main extends JavaPlugin { clone.setChestplate(as.getChestplate()); clone.setLeggings(as.getLeggings()); clone.setBoots(as.getBoots()); - if(oneEight) { - clone.setItemInHand(as.getItemInHand()); - } else { - clone.getEquipment().setItemInMainHand(as.getEquipment().getItemInMainHand()); - clone.getEquipment().setItemInOffHand(as.getEquipment().getItemInOffHand()); - } + clone.getEquipment().setItemInMainHand(as.getEquipment().getItemInMainHand()); + clone.getEquipment().setItemInOffHand(as.getEquipment().getItemInOffHand()); clone.setHeadPose(as.getHeadPose()); clone.setBodyPose(as.getBodyPose()); clone.setLeftArmPose(as.getLeftArmPose()); @@ -205,7 +197,7 @@ public class Main extends JavaPlugin { clone.setSmall(as.isSmall()); clone.setMaxHealth(as.getMaxHealth()); NBT.setSlotsDisabled(clone, NBT.getDisabledSlots(as) == 2039583); - NBT.setInvulnerable(clone, NBT.isInvulnerable(as)); + clone.setInvulnerable(as.isInvulnerable()); return clone; } diff --git a/src/com/gmail/St3venAU/plugins/ArmorStandTools/MainListener.java b/src/com/gmail/St3venAU/plugins/ArmorStandTools/MainListener.java index 8b922c3..fdfc5fc 100644 --- a/src/com/gmail/St3venAU/plugins/ArmorStandTools/MainListener.java +++ b/src/com/gmail/St3venAU/plugins/ArmorStandTools/MainListener.java @@ -217,14 +217,12 @@ public class MainListener implements Listener { } else { plugin.carryingArmorStand.remove(uuid); if (plugin.savedInventories.containsKey(uuid)) { - p.getInventory().setContents(plugin.savedInventories.get(uuid)); - plugin.savedInventories.remove(uuid); + plugin.restoreInventory(p); } } } if(Config.deactivateOnWorldChange && !sameWorld && plugin.savedInventories.containsKey(p.getUniqueId())) { - p.getInventory().setContents(plugin.savedInventories.get(p.getUniqueId())); - plugin.savedInventories.remove(p.getUniqueId()); + plugin.restoreInventory(p); } } @@ -287,8 +285,7 @@ public class MainListener implements Listener { plugin.carryingArmorStand.remove(uuid); } if(plugin.savedInventories.containsKey(uuid)) { - event.getPlayer().getInventory().setContents(plugin.savedInventories.get(uuid)); - plugin.savedInventories.remove(uuid); + plugin.restoreInventory(event.getPlayer()); } } @@ -333,7 +330,7 @@ public class MainListener implements Listener { @EventHandler public void onEntityDamageByEntity(EntityDamageByEntityEvent event) { if(event.getEntity() instanceof ArmorStand) { - if(NBT.isInvulnerable((ArmorStand) event.getEntity())) { + if(event.getEntity().isInvulnerable()) { event.setCancelled(true); } if(event.getDamager() instanceof Player && ArmorStandTool.isHoldingTool((Player) event.getDamager())) { @@ -360,12 +357,8 @@ public class MainListener implements Listener { as.setChestplate(Config.chest); as.setLeggings(Config.pants); as.setBoots(Config.boots); - if(Main.oneEight) { - as.setItemInHand(Config.itemInHand); - } else { - as.getEquipment().setItemInMainHand(Config.itemInHand); - as.getEquipment().setItemInOffHand(Config.itemInOffHand); - } + as.getEquipment().setItemInMainHand(Config.itemInHand); + as.getEquipment().setItemInOffHand(Config.itemInOffHand); as.setVisible(Config.isVisible); as.setSmall(Config.isSmall); as.setArms(Config.hasArms); @@ -376,7 +369,7 @@ public class MainListener implements Listener { as.setCustomNameVisible(true); } NBT.setSlotsDisabled(as, Config.equipmentLock); - NBT.setInvulnerable(as, Config.invulnerable); + as.setInvulnerable(Config.invulnerable); return as; } diff --git a/src/com/gmail/St3venAU/plugins/ArmorStandTools/NBT.java b/src/com/gmail/St3venAU/plugins/ArmorStandTools/NBT.java index 249ee90..d375ff7 100644 --- a/src/com/gmail/St3venAU/plugins/ArmorStandTools/NBT.java +++ b/src/com/gmail/St3venAU/plugins/ArmorStandTools/NBT.java @@ -7,11 +7,25 @@ import org.bukkit.inventory.meta.LeatherArmorMeta; import org.bukkit.inventory.meta.SkullMeta; import java.lang.reflect.Field; -import java.lang.reflect.Method; import java.util.Map; class NBT { + private static String disabledSlotsFieldName; + private static String getDisabledSlotsFieldName() { + if(disabledSlotsFieldName != null) return disabledSlotsFieldName; + if(Main.NMS_VERSION.startsWith("v1_9_R2")) { + disabledSlotsFieldName = "bA"; + } else if(Main.NMS_VERSION.startsWith("v1_10")) { + disabledSlotsFieldName = "bB"; + } else if(Main.NMS_VERSION.startsWith("v1_11")) { + disabledSlotsFieldName = "bA"; + } else { + disabledSlotsFieldName = "bA"; // If ver greater than 1.11 then assume the same as 1.11 and hope that it didn't change :) + } + return disabledSlotsFieldName; + } + static boolean toggleSlotsDisabled(ArmorStand as) { boolean slotsDisabled = getDisabledSlots(as) == 0; setSlotsDisabled(as, slotsDisabled); @@ -21,115 +35,46 @@ class NBT { static int getDisabledSlots(ArmorStand as) { Object nmsEntity = getNmsEntity(as); if(nmsEntity == null) return 0; - if(Main.oneEight) { - Object tag = getTag(nmsEntity); - if (tag == null) return 0; - return getInt(tag, "DisabledSlots"); - } else { - Field f; - try { - f = nmsEntity.getClass().getDeclaredField(Main.oneNineFour ? "bA" : (Main.oneTen ? "bB" : "bz")); - } catch (NoSuchFieldException e) { - e.printStackTrace(); - return 0; - } - f.setAccessible(true); - try { - return (Integer) f.get(nmsEntity); - } catch (IllegalAccessException e) { - e.printStackTrace(); - return 0; - } + Field f; + try { + f = nmsEntity.getClass().getDeclaredField(getDisabledSlotsFieldName()); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + return 0; + } + f.setAccessible(true); + try { + return (Integer) f.get(nmsEntity); + } catch (IllegalAccessException e) { + e.printStackTrace(); + return 0; } } static void setSlotsDisabled(ArmorStand as, boolean slotsDisabled) { Object nmsEntity = getNmsEntity(as); if (nmsEntity == null) return; - if(Main.oneEight) { - Object tag = getTag(nmsEntity); - if (tag == null) return; - setInt(tag, "DisabledSlots", slotsDisabled ? 2039583 : 0); - saveTagA(nmsEntity, tag); - } else { - Field f; - try { - f = nmsEntity.getClass().getDeclaredField(Main.oneNineFour ? "bA" : (Main.oneTen ? "bB" : "bz")); - } catch (NoSuchFieldException e) { - e.printStackTrace(); - return; - } - f.setAccessible(true); - try { - f.set(nmsEntity, slotsDisabled ? 2039583 : 0); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } + Field f; + try { + f = nmsEntity.getClass().getDeclaredField(getDisabledSlotsFieldName()); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + return; + } + f.setAccessible(true); + try { + f.set(nmsEntity, slotsDisabled ? 2039583 : 0); + } catch (IllegalAccessException e) { + e.printStackTrace(); } } static boolean toggleInvulnerability(ArmorStand as) { - boolean isInvulnerable = !isInvulnerable(as); - setInvulnerable(as, isInvulnerable); + boolean isInvulnerable = !as.isInvulnerable(); + as.setInvulnerable(isInvulnerable); return isInvulnerable; } - static boolean isInvulnerable(ArmorStand as) { - if(Main.oneNineFour || Main.oneTen) { - return as.isInvulnerable(); - } - Object nmsEntity = getNmsEntity(as); - if (nmsEntity == null) return false; - if(Main.oneEight) { - Object tag = getTag(nmsEntity); - return tag != null && getBoolean(tag, "Invulnerable"); - } else { - Field f; - try { - f = Utils.getNMSClass("Entity").getDeclaredField("invulnerable"); - } catch (Exception e) { - e.printStackTrace(); - return false; - } - f.setAccessible(true); - try { - return (Boolean) f.get(nmsEntity); - } catch (IllegalAccessException e) { - e.printStackTrace(); - return false; - } - } - } - - static void setInvulnerable(ArmorStand as, boolean invulnerable) { - if(Main.oneNineFour || Main.oneTen) { - as.setInvulnerable(invulnerable); - } - Object nmsEntity = getNmsEntity(as); - if (nmsEntity == null) return; - if(Main.oneEight) { - Object tag = getTag(nmsEntity); - if(tag == null) return; - setBoolean(tag, "Invulnerable", invulnerable); - saveTagF(nmsEntity, tag); - } else { - Field f; - try { - f = Utils.getNMSClass("Entity").getDeclaredField("invulnerable"); - } catch (Exception e) { - e.printStackTrace(); - return; - } - f.setAccessible(true); - try { - f.set(nmsEntity, invulnerable); - } catch (Exception e) { - e.printStackTrace(); - } - } - } - - private static Object getNmsEntity(org.bukkit.entity.Entity entity) { try { return entity.getClass().getMethod("getHandle").invoke(entity); @@ -139,23 +84,6 @@ class NBT { } } - private static Object getTag(Object nmsEntity) { - - try { - Method method = nmsEntity.getClass().getMethod("getNBTTag"); - Object tag = method.invoke(nmsEntity); - if(tag == null) { - tag = Utils.getNMSClass("NBTTagCompound").newInstance(); - } - method = nmsEntity.getClass().getMethod("c", Utils.getNMSClass("NBTTagCompound")); - method.invoke(nmsEntity, tag); - return tag; - } catch (Exception e) { - e.printStackTrace(); - return null; - } - } - @SuppressWarnings("deprecation") static String getItemStackTags(ItemStack is) { if(is == null) { @@ -199,58 +127,4 @@ class NBT { return ""; } } - - @SuppressWarnings("SameParameterValue") - private static int getInt(Object tag, String name) { - try { - return (Integer) tag.getClass().getMethod("getInt", String.class).invoke(tag, name); - } catch (Exception e) { - e.printStackTrace(); - return 0; - } - } - - @SuppressWarnings("SameParameterValue") - private static boolean getBoolean(Object tag, String name) { - try { - return (Boolean) tag.getClass().getMethod("getBoolean", String.class).invoke(tag, name); - } catch (Exception e) { - e.printStackTrace(); - return false; - } - } - - @SuppressWarnings("SameParameterValue") - private static void setInt(Object tag, String name, int value) { - try { - tag.getClass().getMethod("setInt", String.class, int.class).invoke(tag, name, value); - } catch (Exception e) { - e.printStackTrace(); - } - } - - @SuppressWarnings("SameParameterValue") - private static void setBoolean(Object tag, String name, boolean value) { - try { - tag.getClass().getMethod("setBoolean", String.class, boolean.class).invoke(tag, name, value); - } catch (Exception e) { - e.printStackTrace(); - } - } - - private static void saveTagA(Object nmsEntity, Object tag) { - try { - nmsEntity.getClass().getMethod("a", Utils.getNMSClass("NBTTagCompound")).invoke(nmsEntity, tag); - } catch (Exception e) { - e.printStackTrace(); - } - } - - private static void saveTagF(Object nmsEntity, Object tag) { - try { - nmsEntity.getClass().getMethod("f", Utils.getNMSClass("NBTTagCompound")).invoke(nmsEntity, tag); - } catch (Exception e) { - e.printStackTrace(); - } - } } diff --git a/src/com/gmail/St3venAU/plugins/ArmorStandTools/PlotSquaredHook.java b/src/com/gmail/St3venAU/plugins/ArmorStandTools/PlotSquaredHook.java index 409d419..de9a874 100644 --- a/src/com/gmail/St3venAU/plugins/ArmorStandTools/PlotSquaredHook.java +++ b/src/com/gmail/St3venAU/plugins/ArmorStandTools/PlotSquaredHook.java @@ -1,14 +1,13 @@ package com.gmail.St3venAU.plugins.ArmorStandTools; -import java.util.UUID; - -import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.entity.Player; - import com.intellectualcrafters.plot.api.PlotAPI; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotPlayer; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.entity.Player; + +import java.util.UUID; @SuppressWarnings("deprecation") class PlotSquaredHook { diff --git a/src/com/gmail/St3venAU/plugins/ArmorStandTools/Utils.java b/src/com/gmail/St3venAU/plugins/ArmorStandTools/Utils.java index 2391df7..f337c61 100644 --- a/src/com/gmail/St3venAU/plugins/ArmorStandTools/Utils.java +++ b/src/com/gmail/St3venAU/plugins/ArmorStandTools/Utils.java @@ -114,7 +114,7 @@ class Utils { } } - static Class getNMSClass(String nmsClassString) throws ClassNotFoundException { + private static Class getNMSClass(String nmsClassString) throws ClassNotFoundException { if(nmsClassString.equals("ChatSerializer") && !Main.NMS_VERSION.equals("v1_8_R1")) { nmsClassString = "IChatBaseComponent$ChatSerializer"; } diff --git a/src/config.yml b/src/config.yml index 415f066..2378103 100644 --- a/src/config.yml +++ b/src/config.yml @@ -4,7 +4,7 @@ # # Main Config # -# File generated by: v2.1.4 +# File generated by: v2.2.0 # (If this is not the version you are running, consider deleting this # config to allow it to be re-created. There may be new config options) # diff --git a/src/language.yml b/src/language.yml index e0ca3c7..3601b32 100644 --- a/src/language.yml +++ b/src/language.yml @@ -4,7 +4,7 @@ # # Language Config # -# File generated by: v2.1.4 +# File generated by: v2.2.0 # (If this is not the version you are running, consider deleting this # config to allow it to be re-created. There may be new config options) # diff --git a/src/plugin.yml b/src/plugin.yml index 11390de..68d03c6 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,6 +1,6 @@ main: com.gmail.St3venAU.plugins.ArmorStandTools.Main name: ArmorStandTools -version: 2.1.4 +version: 2.2.0 author: St3venAU description: Armor stand manipulation tools softdepend: [WorldGuard, PlotSquared]