Solved How to update existing item meta?

Discussion in 'Spigot Plugin Development' started by Snowmite, Jun 28, 2018.

Thread Status:
Not open for further replies.
  1. I am trying to change an item's name every 5 seconds, it doesn't update though. I have tried player.updateInventory(), still doesn't work.

    Code:
    Code (Text):
                        ItemStack test = new ItemStack(Material.STICK);
                        ItemMeta meta = test.getItemMeta();
                        meta.setDisplayName("hi");
                        test.setItemMeta(meta);
                        player.getInventory().addItem(test);
                        new BukkitRunnable(){
                            @Override
                            public void run(){
                                meta.setDisplayName("hi" + new Random().nextInt(999999999));
                                test.setItemMeta(meta);
                            }
                        }.runTaskTimer(Core.getInstance(), 0L, 5L);
     
  2. Code (Java):

                      final  ItemStack test = new ItemStack(Material.STICK);

                        ItemMeta meta = test.getItemMeta();

                        meta.setDisplayName("hi");
                        test.setItemMeta(meta);

                        player.getInventory().addItem(test);

                        new BukkitRunnable(){
                            @Override
                            public void run(){
                                if (test==null) {cancel(); return;}
                                if (!test.hasItemMeta()){cancel(); return;}
                                if(meta == null) {cancel(); return;}

                                meta.setDisplayName("hi" + new Random().nextInt(999999999));
                                test.setItemMeta(meta);
                                player.updateInventory();

                            }
                        }.runTaskTimer(Core.getInstance(), 0L, 5L);
     
    Try out this code.

    Also, are there any errors? Have you tried debugging the item's name, etc?
     
  3. Nope doesn't work. There's no errors, I tried debugging it also.
     
  4. I don't know if adding the item and then updating the item works, try getting the item from the players inventory after you add it and then run the code on that.
     
    • Agree Agree x 1
  5. How would I get the item from the players inv?
     
  6. Well what are you trying to do in the long run? Whats your goal?
     
  7. Just use
    Code (Text):
    Inventory#getItem(Integer slot);
    if I recall correctly.
    None the less it seems like the updateInventory function as mentioned above is deprecated. I have read on other threads that if you add/remove (and probably also modify) items in the players inventory, it should automatically update. Though I haven't tried this in a while.
     
  8. The OP isn't adding it with an integer meaning that they will have to use a loop, if they are making something like a hub plugin they could just have one slot for the item and not have to worry about it, hence my question. And I believe in versions above 1.8 your comment is true; otherwise you have to manually update. (this is a guess, I don't use 1.8 :D)


    EDIT: @MattUGV Does your code work the first time and then never again?
     
  9. Im trying to have a players stats display on an item's description lore, and they are not updating. When he gets a kill, it doesn't update.
     
  10. Well, updating the name isn't the lore and using a loop/scheduler to update the lore for a kill wouldn't be required either? :D
     
    • Winner Winner x 1
    • Informative Informative x 1
  11. Get the slot of the item within the players inventory and replace that item with the updated one.
     
  12. How would I get the slot? I do not think there is an option for that.
     
  13. either use player#getInventory#setItem or loop through the inventory and find the item you have added.
     
  14. Loop through the player's inventory and find the item (filter the type, lore or name etc). By the time you find it you will be able to edit it however you wish (supposing the amount will always be 1).


    Code (Java):

    Player player = ...
    for (int i = 0; i < 36/* I assume this is the max number of available slots */; i++) {

    Inventory inv = player.getInventory();

    ItemStack item = inv.getItem(i); // getting the item from slot i (0 - 35)

    if (item == null) continue; // if null continue, it's probably an empty slot
    if (item.getType != Material.STICK) continue; // if it isn't your item, continue
    if (!item.hasItemMeta) continue; // if it hasn't an itemmeta continue

    ItemMeta meta = item.getItemMeta(); // this is now safe to use

    if (!meta.hasDisplayName()) continue; // doesn't have a displayname? continue
    if (!meta.getDisplayName.equals("custom Name...")continue; // here you filter the name so as to know whether this is your "unique" item.
    if (!meta.hasLore()) continue; // optional
    if (!meta.getLore().contains("something that doesn't change maby?") continue; // Not sure if you wanna use this.

    int slot = i;
    // you have now found your item.
    // you can now change the meta and do whatever.
    // assuming the item won't be changing slots (wont be movable) you won't have to loop through the inventory every time.

    }
     
     
    • Winner Winner x 1
  15. Tried this, doesn't work:
    Code (Text):
                        final  ItemStack test = new ItemStack(Material.STICK);

                        ItemMeta meta = test.getItemMeta();

                        meta.setDisplayName("hi");
                        test.setItemMeta(meta);

                        player.getInventory().addItem(test);

                        new BukkitRunnable(){
                            @Override
                            public void run(){
                                Player player = (Player) sender;
                                for (int i = 0; i < 36/* I assume this is the max number of available slots */; i++) {

                                    Inventory inv = player.getInventory();

                                    ItemStack item = inv.getItem(i); // getting the item from slot i (0 - 35)

                                    if (item == null) continue; // if null continue, it's probably an empty slot
                                    if (item.getType() != Material.STICK) continue; // if it isn't your item, continue
                                    if (!item.hasItemMeta()) continue; // if it hasn't an itemmeta continue

                                    ItemMeta meta = item.getItemMeta(); // this is now safe to use

                                    if (!meta.hasDisplayName()) continue; // doesn't have a displayname? continue
                                    if (!meta.getDisplayName().contains("hi"))continue; // here you filter the name so as to know whether this is your "unique" item.
                                    //if (!meta.hasLore()) continue; // optional

                                    int slot = i;
                                    ItemMeta meta1 = player.getInventory().getItem(i).getItemMeta();
                                    meta.setDisplayName("hi" + new Random().nextInt(999999999));
                                    player.getInventory().getItem(i).setItemMeta(meta1);

                                }

                            }
                        }.runTaskTimer(Core.getInstance(), 0L, 20L);
     
  16. First things first, avoid copy & pasting code. This will not lead you anywhere. The code was given so as to make you understand what must be done in order to accomplish what you want.
    The reason it doesnt work is because you haven't made any changes in the meta1 ItemMeta. You changed the meta ItemMeta and set the meta1 ItemMeta to the item.
    Code (Java):

                                    ItemMeta meta1 = player.getInventory().getItem(i).getItemMeta(); // meta1
                                    meta.setDisplayName("hi" + new Random().nextInt(999999999)); // meta (you change the meta ItemMeta)
                                    player.getInventory().getItem(i).setItemMeta(meta1); // you set the meta1 (unchanged)
     
     
    • Agree Agree x 1
  17. Wow, that typo made it work. Thank you so much :)
     
    • Funny Funny x 1
Thread Status:
Not open for further replies.