From d66c6c5e1edd8e81019c3f0d21e73dc18f7d84e1 Mon Sep 17 00:00:00 2001 From: Steven Date: Sun, 27 Mar 2016 14:11:26 +0800 Subject: [PATCH] Updated for Spigot/Craftbukkit 1.9 (Backwards compatible with 1.8.x) Added GUI slot for setting the item in the Armor Stand's left hand (1.9 only) Added config.yml option for default item in left hand (inOffHand) Workaround for setting player skulls in 1.9 --- .../ArmorStandTools/ArmorStandGUI.java | 65 +++++--- .../ArmorStandTools/ArmorStandTool.java | 24 +-- .../plugins/ArmorStandTools/Config.java | 3 +- .../plugins/ArmorStandTools/Main.java | 115 +++++++++---- .../plugins/ArmorStandTools/MainListener.java | 96 ++++------- .../St3venAU/plugins/ArmorStandTools/NBT.java | 151 ++++++++++++------ .../plugins/ArmorStandTools/Utils.java | 47 ++++-- src/config.yml | 5 +- src/language.yml | 2 +- src/plugin.yml | 2 +- 10 files changed, 313 insertions(+), 197 deletions(-) diff --git a/src/com/gmail/St3venAU/plugins/ArmorStandTools/ArmorStandGUI.java b/src/com/gmail/St3venAU/plugins/ArmorStandTools/ArmorStandGUI.java index 3910056..5a64d5c 100644 --- a/src/com/gmail/St3venAU/plugins/ArmorStandTools/ArmorStandGUI.java +++ b/src/com/gmail/St3venAU/plugins/ArmorStandTools/ArmorStandGUI.java @@ -41,11 +41,14 @@ class ArmorStandGUI implements Listener { ItemMeta im = filler.getItemMeta(); im.setDisplayName(" "); filler.setItemMeta(im); - invSlots.add(0); - invSlots.add(9); invSlots.add(10); - invSlots.add(18); - invSlots.add(27); + if(Main.one_nine) { + invSlots.add(12); + } + invSlots.add(2); + invSlots.add(11); + invSlots.add(20); + invSlots.add(29); } this.plugin = plugin; plugin.getServer().getPluginManager().registerEvents(this, plugin); @@ -65,11 +68,16 @@ class ArmorStandGUI implements Listener { i.setItem(tool.getSlot(), updateLore(tool)); } } - i.setItem(0, as.getHelmet()); - i.setItem(9, as.getChestplate()); - i.setItem(10, as.getItemInHand()); - i.setItem(18, as.getLeggings()); - i.setItem(27, as.getBoots()); + if(Main.one_nine) { + i.setItem(10, as.getEquipment().getItemInMainHand()); + i.setItem(12, as.getEquipment().getItemInOffHand()); + } else { + i.setItem(10, as.getItemInHand()); + } + i.setItem(2, as.getHelmet()); + i.setItem(11, as.getChestplate()); + i.setItem(20, as.getLeggings()); + i.setItem(29, as.getBoots()); inUse.add(as.getEntityId()); p.openInventory(i); } @@ -148,32 +156,32 @@ class ArmorStandGUI implements Listener { switch (t) { case INVIS: as.setVisible(!as.isVisible()); - Utils.actionBarMsg(p, ChatColor.GREEN + Config.asVisible + ": " + (as.isVisible() ? Config.isTrue : Config.isFalse)); + Utils.actionBarMsg(p, Config.asVisible + ": " + (as.isVisible() ? Config.isTrue : Config.isFalse)); break; case CLONE: p.closeInventory(); plugin.pickUpArmorStand(plugin.clone(as), p, true); - Utils.actionBarMsg(p, ChatColor.GREEN + Config.carrying); + Utils.actionBarMsg(p, Config.carrying); break; case SAVE: plugin.generateCmdBlock(p.getLocation(), as); - Utils.actionBarMsg(p, ChatColor.GREEN + Config.cbCreated); + Utils.actionBarMsg(p, Config.cbCreated); break; case SIZE: as.setSmall(!as.isSmall()); - Utils.actionBarMsg(p, ChatColor.GREEN + Config.size + ": " + (as.isSmall() ? Config.small : Config.normal)); + Utils.actionBarMsg(p, Config.size + ": " + (as.isSmall() ? Config.small : Config.normal)); break; case BASE: as.setBasePlate(!as.hasBasePlate()); - Utils.actionBarMsg(p, ChatColor.GREEN + Config.basePlate + ": " + (as.hasBasePlate() ? Config.isOn : Config.isOff)); + Utils.actionBarMsg(p, Config.basePlate + ": " + (as.hasBasePlate() ? Config.isOn : Config.isOff)); break; case GRAV: as.setGravity(!as.hasGravity()); - Utils.actionBarMsg(p, ChatColor.GREEN + Config.gravity + ": " + (as.hasGravity() ? Config.isOn : Config.isOff)); + Utils.actionBarMsg(p, Config.gravity + ": " + (as.hasGravity() ? Config.isOn : Config.isOff)); break; case ARMS: as.setArms(!as.hasArms()); - Utils.actionBarMsg(p, ChatColor.GREEN + Config.arms + ": " + (as.hasArms() ? Config.isOn : Config.isOff)); + Utils.actionBarMsg(p, Config.arms + ": " + (as.hasArms() ? Config.isOn : Config.isOff)); break; case NAME: p.closeInventory(); @@ -184,10 +192,10 @@ class ArmorStandGUI implements Listener { plugin.setPlayerSkull(p, as); break; case INVUL: - Utils.actionBarMsg(p, ChatColor.GREEN + Config.invul + ": " + (NBT.toggleInvulnerability(as) ? Config.isOn : Config.isOff)); + Utils.actionBarMsg(p, Config.invul + ": " + (NBT.toggleInvulnerability(as) ? Config.isOn : Config.isOff)); break; case SLOTS: - Utils.actionBarMsg(p, ChatColor.GREEN + Config.equip + ": " + (NBT.toggleSlotsDisabled(as) ? Config.locked : Config.unLocked)); + Utils.actionBarMsg(p, Config.equip + ": " + (NBT.toggleSlotsDisabled(as) ? Config.locked : Config.unLocked)); break; case MOVE: p.closeInventory(); @@ -197,16 +205,16 @@ class ArmorStandGUI implements Listener { Utils.actionBarMsg(p, Config.asDropped); } else { plugin.pickUpArmorStand(as, p, false); - Utils.actionBarMsg(p, ChatColor.GREEN + Config.carrying); + Utils.actionBarMsg(p, Config.carrying); } break; case NODEL: // Developer tool - do not use if(as.getMaxHealth() == 50) { as.setMaxHealth(20); - Utils.actionBarMsg(p, ChatColor.GREEN + "Deletion Protection: Disabled"); + Utils.actionBarMsg(p, "Deletion Protection: Disabled"); } else { as.setMaxHealth(50); - Utils.actionBarMsg(p, ChatColor.GREEN + "Deletion Protection: Enabled"); + Utils.actionBarMsg(p, "Deletion Protection: Enabled"); } break; default: @@ -245,11 +253,16 @@ class ArmorStandGUI implements Listener { @Override public void run() { if(as == null || i == null) return; - as.setHelmet(i.getItem(0)); - as.setChestplate(i.getItem(9)); - as.setItemInHand(i.getItem(10)); - as.setLeggings(i.getItem(18)); - as.setBoots(i.getItem(27)); + if(Main.one_nine) { + as.getEquipment().setItemInMainHand(i.getItem(10)); + as.getEquipment().setItemInOffHand(i.getItem(12)); + } else { + as.setItemInHand(i.getItem(10)); + } + as.setHelmet(i.getItem(2)); + as.setChestplate(i.getItem(11)); + as.setLeggings(i.getItem(20)); + as.setBoots(i.getItem(29)); } }.runTaskLater(plugin, 1L); } diff --git a/src/com/gmail/St3venAU/plugins/ArmorStandTools/ArmorStandTool.java b/src/com/gmail/St3venAU/plugins/ArmorStandTools/ArmorStandTool.java index fb20132..8abdb80 100644 --- a/src/com/gmail/St3venAU/plugins/ArmorStandTools/ArmorStandTool.java +++ b/src/com/gmail/St3venAU/plugins/ArmorStandTools/ArmorStandTool.java @@ -33,18 +33,18 @@ public enum ArmorStandTool { SUMMON ("summon", Material.ARMOR_STAND, (short) 0, 0, false, true, "astools.use"), GUI ("gui", Material.NETHER_STAR, (short) 0, 1, false, true, "astools.use"), ROTAT ("rotat", Material.MAGMA_CREAM, (short) 0, 2, false, true, "astools.use"), - CLONE ("gui_clone", Material.GLOWSTONE_DUST, (short) 0, 16, true, true, "astools.clone"), - SAVE ("gui_save", Material.DIAMOND, (short) 0, 17, true, true, "astools.cmdblock"), - INVIS ("gui_invis", Material.GOLD_NUGGET, (short) 0, 14, true, true, "astools.use"), - SIZE ("gui_size", Material.EMERALD, (short) 0, 23, true, true, "astools.use"), - BASE ("gui_base", Material.BOOK, (short) 0, 22, true, true, "astools.use"), - GRAV ("gui_grav", Material.GHAST_TEAR, (short) 0, 24, true, true, "astools.use"), - ARMS ("gui_arms", Material.ARROW, (short) 0, 21, true, true, "astools.use"), - NAME ("gui_name", Material.NAME_TAG, (short) 0, 12, true, true, "astools.use"), - SLOTS ("gui_slots", Material.IRON_HOE, (short) 0, 26, true, true, "astools.use"), - PHEAD ("gui_pHead", Material.SKULL_ITEM, (short) 3, 13, true, true, "astools.use"), - INVUL ("gui_invul", Material.GOLDEN_CARROT, (short) 0, 25, true, true, "astools.use"), - MOVE ("gui_move", Material.FEATHER, (short) 0, 15, true, true, "astools.use"), + CLONE ("gui_clone", Material.GLOWSTONE_DUST, (short) 0, 15, true, true, "astools.clone"), + SAVE ("gui_save", Material.DIAMOND, (short) 0, 16, true, true, "astools.cmdblock"), + INVIS ("gui_invis", Material.GOLD_NUGGET, (short) 0, 7, true, true, "astools.use"), + SIZE ("gui_size", Material.EMERALD, (short) 0, 25, true, true, "astools.use"), + BASE ("gui_base", Material.BOOK, (short) 0, 24, true, true, "astools.use"), + GRAV ("gui_grav", Material.GHAST_TEAR, (short) 0, 32, true, true, "astools.use"), + ARMS ("gui_arms", Material.ARROW, (short) 0, 23, true, true, "astools.use"), + NAME ("gui_name", Material.NAME_TAG, (short) 0, 5, true, true, "astools.use"), + SLOTS ("gui_slots", Material.IRON_HOE, (short) 0, 34, true, true, "astools.use"), + PHEAD ("gui_pHead", Material.SKULL_ITEM, (short) 3, 6, true, true, "astools.use"), + INVUL ("gui_invul", Material.GOLDEN_CARROT, (short) 0, 33, true, true, "astools.use"), + MOVE ("gui_move", Material.FEATHER, (short) 0, 14, true, true, "astools.use"), NODEL ("gui_noDel", Material.WOOD_SPADE, (short) 0, 35, true, false, "astools.use"); // Developer tool, disabled by default private final ItemStack item; diff --git a/src/com/gmail/St3venAU/plugins/ArmorStandTools/Config.java b/src/com/gmail/St3venAU/plugins/ArmorStandTools/Config.java index ac1785f..233c662 100644 --- a/src/com/gmail/St3venAU/plugins/ArmorStandTools/Config.java +++ b/src/com/gmail/St3venAU/plugins/ArmorStandTools/Config.java @@ -19,7 +19,7 @@ class Config { public static WorldGuardPlugin worldGuardPlugin; - public static ItemStack helmet, chest, pants, boots, itemInHand; + public static ItemStack helmet, chest, pants, boots, itemInHand, itemInOffHand; public static boolean isVisible = true; public static boolean isSmall = false; public static boolean hasArms = true; @@ -100,6 +100,7 @@ class Config { pants = toItemStack(config.getString("pants")); boots = toItemStack(config.getString("boots")); itemInHand = toItemStack(config.getString("inHand")); + itemInOffHand = toItemStack(config.getString("inOffHand")); isVisible = config.getBoolean("isVisible"); isSmall = config.getBoolean("isSmall"); hasArms = config.getBoolean("hasArms"); diff --git a/src/com/gmail/St3venAU/plugins/ArmorStandTools/Main.java b/src/com/gmail/St3venAU/plugins/ArmorStandTools/Main.java index 48dec45..8b5c98b 100644 --- a/src/com/gmail/St3venAU/plugins/ArmorStandTools/Main.java +++ b/src/com/gmail/St3venAU/plugins/ArmorStandTools/Main.java @@ -27,10 +27,12 @@ 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 one_nine; @Override public void onEnable() { NMS_VERSION = getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3]; + one_nine = NMS_VERSION.startsWith("v1_9"); getServer().getPluginManager().registerEvents(new MainListener(this), this); CommandExecutor ce = new Commands(this); getCommand("astools").setExecutor(ce); @@ -77,15 +79,30 @@ public class Main extends JavaPlugin { void generateCmdBlock(Location l, ArmorStand as) { Location loc = as.getLocation(); int dSlots = NBT.getDisabledSlots(as); - int hand = as.getItemInHand() == null ? 0 : as.getItemInHand().getTypeId(); - int handDmg = as.getItemInHand() == null ? 0 : as.getItemInHand().getDurability(); - int boots = as.getBoots() == null ? 0 : as.getBoots().getTypeId(); + String hand, boots, legs, chest, helm, offHand = "0"; + int handDmg, offHandDmg = 0; + + if(one_nine) { + 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(); + } else { + 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(); + } + int bootsDmg = as.getBoots() == null ? 0 : as.getBoots().getDurability(); - int legs = as.getLeggings() == null ? 0 : as.getLeggings().getTypeId(); int legsDmg = as.getLeggings() == null ? 0 : as.getLeggings().getDurability(); - int chest = as.getChestplate() == null ? 0 : as.getChestplate().getTypeId(); int chestDmg = as.getChestplate() == null ? 0 : as.getChestplate().getDurability(); - int helm = as.getHelmet() == null ? 0 : as.getHelmet().getTypeId(); int helmDmg = as.getHelmet() == null ? 0 : as.getHelmet().getDurability(); EulerAngle he = as.getHeadPose(); EulerAngle ll = as.getLeftLegPose(); @@ -93,33 +110,65 @@ public class Main extends JavaPlugin { EulerAngle la = as.getLeftArmPose(); EulerAngle ra = as.getRightArmPose(); EulerAngle bo = as.getBodyPose(); - String 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],")) - + (hand == 0 && boots == 0 && legs == 0 && chest == 0 && helm == 0 ? "" : ( - "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]}}"; + String cmd; + if(one_nine) { + 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.getBoots() == null && as.getLeggings() == null && as.getChestplate() == null && as.getHelmet() == null ? "" : ( + "ArmorItems:[" + + "{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()) + "}],")) + + (as.getEquipment().getItemInMainHand() == null && as.getEquipment().getItemInOffHand() == null ? "" : ( + "HandItems:[" + + "{id:" + hand + ",Count:" + as.getEquipment().getItemInMainHand().getAmount() + ",Damage:" + handDmg + NBT.getItemStackTags(as.getEquipment().getItemInMainHand()) + "}," + + "{id:" + offHand + ",Count:" + as.getEquipment().getItemInOffHand().getAmount() + ",Damage:" + offHandDmg + NBT.getItemStackTags(as.getEquipment().getItemInOffHand()) + "}],")) + + "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]}}"; + } 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); diff --git a/src/com/gmail/St3venAU/plugins/ArmorStandTools/MainListener.java b/src/com/gmail/St3venAU/plugins/ArmorStandTools/MainListener.java index fea697e..75d4abd 100644 --- a/src/com/gmail/St3venAU/plugins/ArmorStandTools/MainListener.java +++ b/src/com/gmail/St3venAU/plugins/ArmorStandTools/MainListener.java @@ -1,11 +1,10 @@ package com.gmail.St3venAU.plugins.ArmorStandTools; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; -import org.bukkit.SkullType; import org.bukkit.block.Block; -import org.bukkit.block.Skull; import org.bukkit.entity.ArmorStand; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; @@ -64,7 +63,7 @@ public class MainListener implements Listener { p.sendMessage(ChatColor.RED + Config.wgNoPerm); } } - ArmorStandTool tool = ArmorStandTool.get(p.getItemInHand()); + ArmorStandTool tool = ArmorStandTool.get(p.getInventory().getItemInMainHand()); if(tool == null) return; ArmorStand as = (ArmorStand) event.getRightClicked(); if (!plugin.playerHasPermission(p, event.getRightClicked().getLocation().getBlock(), tool)) { @@ -190,7 +189,7 @@ public class MainListener implements Listener { return; } as.teleport(Utils.getLocationFacing(event.getTo())); - Utils.actionBarMsg(p, ChatColor.GREEN + Config.carrying); + Utils.actionBarMsg(p, Config.carrying); } } @@ -210,7 +209,7 @@ public class MainListener implements Listener { @Override public void run() { as.teleport(Utils.getLocationFacing(p.getLocation())); - Utils.actionBarMsg(p, ChatColor.GREEN + Config.carrying); + Utils.actionBarMsg(p, Config.carrying); } }.runTaskLater(plugin, 1L); } else { @@ -315,7 +314,7 @@ public class MainListener implements Listener { } Location l = Utils.getLocationFacing(p.getLocation()); plugin.pickUpArmorStand(spawnArmorStand(l), p, true); - Utils.actionBarMsg(p, ChatColor.GREEN + Config.carrying); + Utils.actionBarMsg(p, Config.carrying); } new BukkitRunnable() { @Override @@ -327,7 +326,7 @@ public class MainListener implements Listener { @EventHandler public void onEntityDamageByEntity(EntityDamageByEntityEvent event) { - if(event.getEntity() instanceof ArmorStand && event.getDamager() instanceof Player && ArmorStandTool.isTool(((Player) event.getDamager()).getItemInHand())) { + if(event.getEntity() instanceof ArmorStand && event.getDamager() instanceof Player && ArmorStandTool.isTool(((Player) event.getDamager()).getInventory().getItemInMainHand())) { event.setCancelled(true); if(noCooldown(event.getDamager())) { Utils.cycleInventory((Player) event.getDamager()); @@ -350,7 +349,12 @@ public class MainListener implements Listener { as.setChestplate(Config.chest); as.setLeggings(Config.pants); as.setBoots(Config.boots); - as.setItemInHand(Config.itemInHand); + if(Main.one_nine) { + as.getEquipment().setItemInMainHand(Config.itemInHand); + as.getEquipment().setItemInOffHand(Config.itemInOffHand); + } else { + as.setItemInHand(Config.itemInHand); + } as.setVisible(Config.isVisible); as.setSmall(Config.isSmall); as.setArms(Config.hasArms); @@ -379,7 +383,6 @@ public class MainListener implements Listener { if(event.getBlock().hasMetadata("armorStand")) { final Block b = event.getBlock(); final ArmorStand as = getArmorStand(b); - boolean delete = true; if (as != null) { String input = ""; for (String line : event.getLines()) { @@ -400,58 +403,27 @@ public class MainListener implements Listener { if(MC_USERNAME_PATTERN.matcher(input).matches()) { final String name = input; b.setMetadata("protected", new FixedMetadataValue(plugin, true)); - b.setType(Material.SKULL); - final Skull s = (Skull) b.getState(); - s.setSkullType(SkullType.PLAYER); - delete = false; event.getPlayer().sendMessage(ChatColor.GOLD + Config.pleaseWait); - new BukkitRunnable() { - @Override - public void run() { - final boolean ok = Utils.loadProfile(name); - new BukkitRunnable() { - @Override - public void run() { - if (ok) { - s.setOwner(name); - s.update(); - new BukkitRunnable() { - int n = 0; - ItemStack skull; - @Override - public void run() { - if(++n > 20) { - this.cancel(); - event.getPlayer().sendMessage(ChatColor.RED + Config.headFailed); - b.setType(Material.AIR); - b.setData((byte) 0); - b.removeMetadata("protected", plugin); - return; - } - skull = b.getDrops().iterator().next(); - if(skull.getType() == Material.SKULL_ITEM && skull.getData().getData() == (byte) 3) { - SkullMeta meta = (SkullMeta) skull.getItemMeta(); - if(meta.hasOwner() && meta.getOwner().equalsIgnoreCase(name)) { - as.setHelmet(skull); - event.getPlayer().sendMessage(ChatColor.GREEN + Config.appliedHead + ChatColor.GOLD + " " + name); - b.setType(Material.AIR); - b.setData((byte) 0); - b.removeMetadata("protected", plugin); - this.cancel(); - } - } - } - }.runTaskTimer(plugin, 10L, 10L); - } else { - event.getPlayer().sendMessage(ChatColor.RED + Config.noHead + ChatColor.GOLD + " " + name); - b.setType(Material.AIR); - b.setData((byte) 0); - b.removeMetadata("protected", plugin); - } - } - }.runTask(plugin); + String cmd = "give " + event.getPlayer().getName() + " minecraft:skull 1 3 {SkullOwner:\"" + name + "\"}"; + String current = b.getWorld().getGameRuleValue("sendCommandFeedback"); + b.getWorld().setGameRuleValue("sendCommandFeedback", "false"); + Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), cmd); + b.getWorld().setGameRuleValue("sendCommandFeedback", current); + boolean found = false; + for(int slot : event.getPlayer().getInventory().all(Material.SKULL_ITEM).keySet()) { + ItemStack skull = event.getPlayer().getInventory().getItem(slot); + SkullMeta sm = (SkullMeta) skull.getItemMeta(); + if(sm.hasOwner() && name.equalsIgnoreCase(sm.getOwner())) { + as.setHelmet(skull); + event.getPlayer().sendMessage(ChatColor.GREEN + Config.appliedHead + ChatColor.GOLD + " " + name); + event.getPlayer().getInventory().setItem(slot, null); + found = true; + break; } - }.runTaskAsynchronously(plugin); + } + if(!found) { + event.getPlayer().sendMessage(ChatColor.GOLD + Config.headFailed); + } } else { event.getPlayer().sendMessage(ChatColor.RED + input + " " + Config.invalidName); } @@ -461,10 +433,8 @@ public class MainListener implements Listener { b.removeMetadata("armorStand", plugin); b.removeMetadata("setName", plugin); b.removeMetadata("setSkull", plugin); - if(delete) { - b.setType(Material.AIR); - b.setData((byte) 0); - } + b.setType(Material.AIR); + b.setData((byte) 0); } } diff --git a/src/com/gmail/St3venAU/plugins/ArmorStandTools/NBT.java b/src/com/gmail/St3venAU/plugins/ArmorStandTools/NBT.java index fa58b03..bccb66e 100644 --- a/src/com/gmail/St3venAU/plugins/ArmorStandTools/NBT.java +++ b/src/com/gmail/St3venAU/plugins/ArmorStandTools/NBT.java @@ -6,66 +6,124 @@ import org.bukkit.inventory.ItemStack; 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 { static boolean toggleSlotsDisabled(ArmorStand as) { - Object nmsEntity = getNmsEntity(as); - if(nmsEntity == null) return false; - Object tag = getTag(nmsEntity); - if(tag == null) return false; - boolean slotsDisabled = getInt(tag, "DisabledSlots") == 0; - setInt(tag, "DisabledSlots", slotsDisabled ? 2039583 : 0); - saveTagA(nmsEntity, tag); + boolean slotsDisabled = getDisabledSlots(as) == 0; + setSlotsDisabled(as, slotsDisabled); return slotsDisabled; } - static boolean toggleInvulnerability(ArmorStand as) { - Object nmsEntity = getNmsEntity(as); - if(nmsEntity == null) return false; - Object tag = getTag(nmsEntity); - if(tag == null) return false; - boolean invulnerable = !getBoolean(tag, "Invulnerable"); - setBoolean(tag, "Invulnerable", invulnerable); - saveTagF(nmsEntity, tag); - return invulnerable; - } - - static void setSlotsDisabled(ArmorStand as, boolean slotsDisabled) { - Object nmsEntity = getNmsEntity(as); - if(nmsEntity == null) return; - Object tag = getTag(nmsEntity); - if(tag == null) return; - setInt(tag, "DisabledSlots", slotsDisabled ? 2039583 : 0); - saveTagA(nmsEntity, tag); - } - - static void setInvulnerable(ArmorStand as, boolean invulnerable) { - Object nmsEntity = getNmsEntity(as); - if(nmsEntity == null) return; - Object tag = getTag(nmsEntity); - if(tag == null) return; - setBoolean(tag, "Invulnerable", invulnerable); - saveTagF(nmsEntity, tag); - } - - static boolean isInvulnerable(ArmorStand as) { - Object nmsEntity = getNmsEntity(as); - if (nmsEntity == null) return false; - Object tag = getTag(nmsEntity); - return tag != null && getBoolean(tag, "Invulnerable"); - } - static int getDisabledSlots(ArmorStand as) { Object nmsEntity = getNmsEntity(as); if(nmsEntity == null) return 0; - Object tag = getTag(nmsEntity); - if (tag == null) return 0; - return getInt(tag, "DisabledSlots"); + if(Main.one_nine) { + Field f; + try { + f = nmsEntity.getClass().getDeclaredField("bz"); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + return 0; + } + f.setAccessible(true); + try { + return (Integer) f.get(nmsEntity); + } catch (IllegalAccessException e) { + e.printStackTrace(); + return 0; + } + } else { + Object tag = getTag(nmsEntity); + if (tag == null) return 0; + return getInt(tag, "DisabledSlots"); + } } + static void setSlotsDisabled(ArmorStand as, boolean slotsDisabled) { + Object nmsEntity = getNmsEntity(as); + if (nmsEntity == null) return; + if(Main.one_nine) { + Field f; + try { + f = nmsEntity.getClass().getDeclaredField("bz"); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + return; + } + f.setAccessible(true); + try { + f.set(nmsEntity, slotsDisabled ? 2039583 : 0); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } else { + Object tag = getTag(nmsEntity); + if (tag == null) return; + setInt(tag, "DisabledSlots", slotsDisabled ? 2039583 : 0); + saveTagA(nmsEntity, tag); + } + } + + static boolean toggleInvulnerability(ArmorStand as) { + boolean isInvulnerable = !isInvulnerable(as); + setInvulnerable(as, isInvulnerable); + return isInvulnerable; + } + + static boolean isInvulnerable(ArmorStand as) { + Object nmsEntity = getNmsEntity(as); + if (nmsEntity == null) return false; + if(Main.one_nine) { + 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; + } + } else { + Object tag = getTag(nmsEntity); + return tag != null && getBoolean(tag, "Invulnerable"); + } + } + + static void setInvulnerable(ArmorStand as, boolean invulnerable) { + Object nmsEntity = getNmsEntity(as); + if (nmsEntity == null) return; + if(Main.one_nine) { + 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(); + } + } else { + Object tag = getTag(nmsEntity); + if(tag == null) return; + setBoolean(tag, "Invulnerable", invulnerable); + saveTagF(nmsEntity, tag); + } + } + + private static Object getNmsEntity(org.bukkit.entity.Entity entity) { try { return entity.getClass().getMethod("getHandle").invoke(entity); @@ -76,6 +134,7 @@ class NBT { } private static Object getTag(Object nmsEntity) { + try { Method method = nmsEntity.getClass().getMethod("getNBTTag"); Object tag = method.invoke(nmsEntity); diff --git a/src/com/gmail/St3venAU/plugins/ArmorStandTools/Utils.java b/src/com/gmail/St3venAU/plugins/ArmorStandTools/Utils.java index d9a3c43..d97eba7 100644 --- a/src/com/gmail/St3venAU/plugins/ArmorStandTools/Utils.java +++ b/src/com/gmail/St3venAU/plugins/ArmorStandTools/Utils.java @@ -13,6 +13,7 @@ import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.util.Arrays; import java.util.Collection; +import java.util.Set; class Utils { @@ -93,7 +94,7 @@ class Utils { static void actionBarMsg(Player p, String msg) { try { - Object chat = getNMSClass("ChatSerializer").getMethod("a", String.class).invoke(null, "{text:\"" + msg + "\"}"); + Object chat = getNMSClass("ChatSerializer").getMethod("a", String.class).invoke(null, "{\"text\":\"" + msg + "\",\"color\":\"green\"}"); Object packet = getNMSClass("PacketPlayOutChat").getConstructor(getNMSClass("IChatBaseComponent"), byte.class).newInstance(chat, (byte) 2); sendPacket(p, packet); } catch (Exception e) { @@ -120,17 +121,6 @@ class Utils { return Class.forName("net.minecraft.server." + Main.NMS_VERSION + "." + nmsClassString); } - static boolean loadProfile(String name) { - try { - Object server = getNMSClass("MinecraftServer").getMethod("getServer").invoke(null); - Object cache = server.getClass().getMethod("getUserCache").invoke(server); - return cache.getClass().getMethod("getProfile", String.class).invoke(cache, name) != null; - } catch (Exception e) { - e.printStackTrace(); - return false; - } - } - private static void sendPacket(Player p, Object packet) { try { Object player = p.getClass().getMethod("getHandle").invoke(p); @@ -169,4 +159,37 @@ class Utils { return is; } + static String getNmsName(Material m) { + try { + Class block = getNMSClass("Block"); + Class item = getNMSClass("Item"); + Class registryBlocks = getNMSClass("RegistryBlocks"); + Class registryMaterials = getNMSClass("RegistryMaterials"); + Class regKey = getNMSClass("MinecraftKey"); + Object registry = block.getDeclaredField("REGISTRY").get(null); + Set set = (Set) registry.getClass().getMethod("keySet").invoke(registry); + for(Object key : set) { + Object b = registryBlocks.getMethod("get", Object.class).invoke(registry, key); + Integer id = (Integer) block.getMethod("getId", block).invoke(null, b); + if(id == m.getId()) { + return (String) regKey.getMethod("a").invoke(key); + } + } + + registry = item.getDeclaredField("REGISTRY").get(null); + set = (Set) registry.getClass().getMethod("keySet").invoke(registry); + for(Object key : set) { + Object i = registryMaterials.getMethod("get", Object.class).invoke(registry, key); + Integer id = (Integer) item.getMethod("getId", item).invoke(null, i); + if(id == m.getId()) { + return (String) regKey.getMethod("a").invoke(key); + } + } + } catch (Exception e) { + e.printStackTrace(); + return null; + } + return null; + } + } diff --git a/src/config.yml b/src/config.yml index 94c546f..9b3b505 100644 --- a/src/config.yml +++ b/src/config.yml @@ -4,7 +4,7 @@ # # Main Config # -# File generated by: v2.06 +# File generated by: v2.1 # (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) # @@ -13,7 +13,7 @@ # - Name armor stands # - Toggle: Gravity, Visibility, Arms, Base, Size, Invulnerability, Equipment Lock # - Manipulate the position of the Head, Body, Arms and Legs -# - Full control over armor stand's inventory (armor & item in hand) +# - Full control over armor stand's inventory (armor & items in hands) # - Clone armor stand tool # - Create command block tool: Creates a command block with the command that will summon this # armor stand in its current state @@ -44,6 +44,7 @@ chest: IRON_CHESTPLATE 0 pants: IRON_LEGGINGS 0 boots: IRON_BOOTS 0 inHand: AIR 0 +inOffHand: AIR 0 isVisible: true isSmall: false hasArms: true diff --git a/src/language.yml b/src/language.yml index 4d22178..03fd23b 100644 --- a/src/language.yml +++ b/src/language.yml @@ -4,7 +4,7 @@ # # Language Config # -# File generated by: v2.06 +# File generated by: v2.1 # (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 8331631..cab5c5d 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,6 +1,6 @@ main: com.gmail.St3venAU.plugins.ArmorStandTools.Main name: ArmorStandTools -version: 2.06 +version: 2.1 author: St3venAU description: Armor stand manipulation tools softdepend: [WorldGuard, PlotSquared]