Set item uses ?

Discussion in 'Spigot Plugin Development' started by Drawethree, May 28, 2017.

  1. Hey guys, what's the best way to set item's uses to specified amount, and after that, it will break / remove the item ?
    For example if the stick will have 3 uses, after 3 clicks, it will remove itemstack from player's inv.
     
  2. Save the uses and the player to one .yml or seperate player files, your choice.
     
  3. That is a way of doing it but there is a better way such as saving a players UUID and the count as an Integer to a hashmap.
    Code (Text):
    Map<UUID, Integer> counter = new HashMap<UUID, Integer>();
    counter.put(player.getUniqueId(), counter.get(player.getUniqueId()) + 1);
    Then check if the count in the counter hashmap has reached the int 3.
    Code (Text):
    if (counter.get(player.getUniqueId()) == 3)
    {
      counter.remove(player.getUniqueId());
      //Do code
    }
     
  4. I also need to works it after reload :)
     
  5. set the Metadata Of the item , subtract , add it with events . the metadata always on the item , reload or stop wont remove it

    Edit:
    Sorry I just realise that item dont have metadata , you might use cast the item to nms item and set tag

    I have E.x
    Code (Text):
    net.minecraft.server.v1_9_R2.ItemStack nmsItem = CraftItemStack.asNMSCopy(item);
                            if (!nmsItem.hasTag()) {
                                nmsItem.setTag(new NBTTagCompound());
                                nmsItem.getTag().setBoolean("rpgitem", true);
                                item = CraftItemStack.asBukkitCopy(nmsItem);
                            }
     
     
    #5 Nevermore, May 28, 2017
    Last edited: May 28, 2017
  6. ItemStack doesnt have metadatas
     
  7. Check my edit

    Another solution if you dont like nms

    Use Invisible string in item's lore to store variables

    Code (Text):
    public static String hideS(String s) {
            String hidden = "";
            for (char c : s.toCharArray())
                hidden += ChatColor.COLOR_CHAR + "" + c;
            return hidden;
        }

        public static String unhideS(String s) {
            String r = s.replaceAll("§", "");
            return r;
        }
     
  8. I didn't spoonfeed for a reason :p
     
  9. @Drawethree why not just use the ItemStack amount?

    Don't use replaceAll for literal replacement
     
  10. Which is