Help

Discussion in 'Spigot Plugin Development' started by nickelectron, May 1, 2017.

  1. So I am having a really weird issue whilst trying to set a players armor contents.
    Here is my method to set the contents for a variable player.
    Code (Text):

    public void addMagenta(Player p) {
        PlayerInventory inventory = p.getInventory();
        inventory.setHelmet(new ItemBuilder(Material.LEATHER_HELMET).addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 1).setLeatherArmorColor(DyeColor.MAGENTA.getColor()).toItemStack().clone());
        inventory.setChestplate(new ItemBuilder(Material.LEATHER_CHESTPLATE).addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 1).setLeatherArmorColor(DyeColor.MAGENTA.getColor()).toItemStack().clone());
        inventory.setLeggings(new ItemBuilder(Material.LEATHER_LEGGINGS).addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 1).setLeatherArmorColor(DyeColor.MAGENTA.getColor()).toItemStack().clone());
        inventory.setBoots(new ItemBuilder(Material.LEATHER_BOOTS).addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 1).setLeatherArmorColor(DyeColor.MAGENTA.getColor()).toItemStack().clone());
        p.sendMessage(ChatColor.BLACK + "Set");
    }
     
    The addMagenta method is called on here:
    Code (Text):

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        Player player = event.getPlayer();
        player.getInventory().setArmorContents(new ItemStack[] { null, null, null, null });
        player.getInventory().clear();
        player.getInventory().setItem(1, plugin.getSelectorItem());
        player.getInventory().setItem(3, plugin.getActivateSpeedBoost());
        player.getInventory().setItem(5, plugin.getToggleShow());
        player.getInventory().setItem(7, plugin.getEnderButt());
        plugin.getArmor().addMagenta(event.getPlayer());
        for (UUID hider : hiddenEnabled) {
            plugin.getServer().getPlayer(hider).hidePlayer(player);
        }
    }
     
    The ItemBuilder:
    Code (Text):

    public class ItemBuilder {

        private ItemStack is;

        /**
         * Create a new ItemBuilder from scratch.
         *
         * @param m The material to create the ItemBuilder with.
         */
        public ItemBuilder(Material m) {
            this(m, 1);
        }

        /**
         * Create a new ItemBuilder over an existing itemstack.
         *
         * @param is The itemstack to create the ItemBuilder over.
         */
        public ItemBuilder(ItemStack is) {
            this.is = is;
        }

        /**
         * Create a new ItemBuilder from scratch.
         *
         * @param m      The material of the item.
         * @param amount The amount of the item.
         */
        public ItemBuilder(Material m, int amount) {
            is = new ItemStack(m, amount);
        }

        /**
         * Create a new ItemBuilder from scratch.
         *
         * @param m          The material of the item.
         * @param amount     The amount of the item.
         * @param durability The durability of the item.
         */
        public ItemBuilder(Material m, int amount, byte durability) {
            is = new ItemStack(m, amount, durability);
        }

        /**
         * Clone the ItemBuilder into a new one.
         *
         * @return The cloned instance.
         */
        public ItemBuilder clone() {
            return new ItemBuilder(is);
        }

        /**
         * Change the durability of the item.
         *
         * @param dur The durability to set it to.
         */
        public ItemBuilder setDurability(short dur) {
            is.setDurability(dur);
            return this;
        }

        /**
         * Set the displayname of the item.
         *
         * @param name The name to change it to.
         */
        public ItemBuilder setName(String name) {
            ItemMeta im = is.getItemMeta();
            im.setDisplayName(name);
            is.setItemMeta(im);
            return this;
        }

        /**
         * Add an unsafe enchantment.
         *
         * @param ench  The enchantment to add.
         * @param level The level to put the enchant on.
         */
        public ItemBuilder addUnsafeEnchantment(Enchantment ench, int level) {
            is.addUnsafeEnchantment(ench, level);
            return this;
        }

        /**
         * Remove a certain enchant from the item.
         *
         * @param ench The enchantment to remove
         */
        public ItemBuilder removeEnchantment(Enchantment ench) {
            is.removeEnchantment(ench);
            return this;
        }

        /**
         * Set the skull owner for the item. Works on skulls only.
         *
         * @param owner The name of the skull's owner.
         */
        public ItemBuilder setSkullOwner(String owner) {
            try {
                SkullMeta im = (SkullMeta) is.getItemMeta();
                im.setOwner(owner);
                is.setItemMeta(im);
            } catch (ClassCastException expected) {
            }
            return this;
        }

        /**
         * Add an enchant to the item.
         *
         * @param ench  The enchant to add
         * @param level The level
         */
        public ItemBuilder addEnchant(Enchantment ench, int level) {
            ItemMeta im = is.getItemMeta();
            im.addEnchant(ench, level, true);
            is.setItemMeta(im);
            return this;
        }

        /**
         * Add multiple enchants at once.
         *
         * @param enchantments The enchants to add.
         */
        public ItemBuilder addEnchantments(Map<Enchantment, Integer> enchantments) {
            is.addEnchantments(enchantments);
            return this;
        }

        /**
         * Sets infinity durability on the item by setting the durability to Short.MAX_VALUE.
         */
        public ItemBuilder setInfinityDurability() {
            is.setDurability(Short.MAX_VALUE);
            return this;
        }

        /**
         * Re-sets the lore.
         *
         * @param lore The lore to set it to.
         */
        public ItemBuilder setLore(String... lore) {
            ItemMeta im = is.getItemMeta();
            im.setLore(Arrays.asList(lore));
            is.setItemMeta(im);
            return this;
        }

        /**
         * Re-sets the lore.
         *
         * @param lore The lore to set it to.
         */
        public ItemBuilder setLore(List<String> lore) {
            ItemMeta im = is.getItemMeta();
            im.setLore(lore);
            is.setItemMeta(im);
            return this;
        }

        /**
         * Remove a lore line.
         *
         * @param line The lore to remove.
         */
        public ItemBuilder removeLoreLine(String line) {
            ItemMeta im = is.getItemMeta();
            List<String> lore = new ArrayList<>(im.getLore());
            if (!lore.contains(line)) return this;
            lore.remove(line);
            im.setLore(lore);
            is.setItemMeta(im);
            return this;
        }

        /**
         * Remove a lore line.
         *
         * @param index The index of the lore line to remove.
         */
        public ItemBuilder removeLoreLine(int index) {
            ItemMeta im = is.getItemMeta();
            List<String> lore = new ArrayList<>(im.getLore());
            if (index < 0 || index > lore.size()) return this;
            lore.remove(index);
            im.setLore(lore);
            is.setItemMeta(im);
            return this;
        }

        /**
         * Add a lore line.
         *
         * @param line The lore line to add.
         */
        public ItemBuilder addLoreLine(String line) {
            ItemMeta im = is.getItemMeta();
            List<String> lore = new ArrayList<>();
            if (im.hasLore()) lore = new ArrayList<>(im.getLore());
            lore.add(line);
            im.setLore(lore);
            is.setItemMeta(im);
            return this;
        }

        /**
         * Add a lore line.
         *
         * @param line The lore line to add.
         * @param pos  The index of where to put it.
         */
        public ItemBuilder addLoreLine(String line, int pos) {
            ItemMeta im = is.getItemMeta();
            List<String> lore = new ArrayList<>(im.getLore());
            lore.set(pos, line);
            im.setLore(lore);
            is.setItemMeta(im);
            return this;
        }

        /**
         * Sets the dye color on an item.
         * <b>* Notice that this doesn't check for item type, sets the literal data of the dyecolor as durability.</b>
         *
         * @param color The color to put.
         */
        @SuppressWarnings("deprecation")
        public ItemBuilder setDyeColor(DyeColor color) {
            this.is.setDurability(color.getData());
            return this;
        }

        /**
         * Sets the dye color of a wool item. Works only on wool.
         *
         * @param color The DyeColor to set the wool item to.
         * @see [email protected](DyeColor)
         * @deprecated As of version 1.2 changed to setDyeColor.
         */
        @Deprecated
        public ItemBuilder setWoolColor(DyeColor color) {
            if (!is.getType().equals(Material.WOOL)) return this;
            this.is.setDurability(color.getData());
            return this;
        }

        /**
         * Sets the armor color of a leather armor piece. Works only on leather armor pieces.
         *
         * @param color The color to set it to.
         */
        public ItemBuilder setLeatherArmorColor(Color color) {
            LeatherArmorMeta im = (LeatherArmorMeta) is.getItemMeta();
            im.setColor(color);
            is.setItemMeta(im);
            return this;
        }

        /**
         * Retrieves the itemstack from the ItemBuilder.
         *
         * @return The itemstack created/modified by the ItemBuilder instance.
         */
        public ItemStack toItemStack() {
            return is;
        }

    }
     


    The kicker is that there are no error messages in my console.
     
  2. Have you registered the event in the main class (onEnable())?
     
  3. Yea, everything except calling that specific method works.
     
  4. Why make a function to make the armor? Just Make a new itemstack then add the enchant how you would to an itemstack normally
     
  5. What's the issue? It doesn't set the armor to the player?
     
  6. Yep
     
  7. The ItemBuilder is a lot nicer...
     
  8. It should be as simple as Player#getInventory().setArmorContents(ItemStack[])
     
  9. I mean I can try that buts what's the difference between that and individually setting the armor piece?
     
  10. Shouldn't be any difference. Calling update() on the player or inventory might fix it.