Rename items while in hand

Discussion in 'Plugin Help/Development/Requests' started by JUSTCAMH, Apr 18, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    JUSTCAMH

    So this is probably an extremely noobish question, but I am unsure how I would rename items that are in the player's hand. I am trying to make the display name of an item animated and I have almost finished it. The only problem is that when you hold the item in hand, it moves in your hand, constantly, the sort of thing that happens when you scroll along your hotbar. I have tried many different ways but I have been unable to find a way of changing the name or lore without the item shaking in your hand.

    EDIT:
    I have found out that what I am trying to do requires 1.7.10 and I am currently in version 1.8 and the plugin needs to support 1.8. So instead of what it says above does anybody know how to make custom messages where the text telling you the music playing when you put a disk in a jukebox. I know this is possible and as a matter of a fact that's what I've seen 1.8 servers do for player compasses
     
    Last edited: Apr 23, 2015
  2. Offline

    crolemol

    @JUSTCAMH
    don't make a new itemstack every time you animate it, just change the itemmeta of the itemstack. so =>
    Code:
    ItemStack item = player.getItemInHand();
    ItemMeta itemmeta = item.getItemMeta();
    itemmeta.setDisplayName(YOURNAMEHERE);
    item.setItemMeta(itemmeta);
     
  3. Offline

    JUSTCAMH

    @crolemol Thats precisly what I did. It still does the change item animation. Any other ideas?
     
  4. Offline

    nverdier

  5. Offline

    JUSTCAMH

    @nverdier Well I don't really have any code to post. I have tried everything I could think of. But hey! If it makes a difference here it is:
    Code:
    p.getItemInHand().getItemMeta().setDisplayName(ChatColor.GRAY
                                                    + "Tuning: " + (percent + 1)
                                                    + "%");
    This first one did absolutely nothing. And for the record, I tried all of these options without the (percent + 1) and they still don't work. For whatever reason this did absolutely nothing. No errors, no result nothing. These were also all tested outside of a timer and still the same result.

    I also tried remaking the ItemStack and that didn't work either.

    I also tried this code, still the same result, the item moving up and down with the switching names.

    For the record, what I am trying to achieve is a timer which every tick increases the percent by one. Everything works fine, all I need is a method of setting the name without the item shaking. Just using the method I have now works, it just has the item shaking. For all of the methods I tried, none returned any errors.
     
  6. Offline

    nverdier

    @JUSTCAMH You have to set the ItemStack's ItemMeta after you alter it.
     
  7. Offline

    JUSTCAMH

  8. Offline

    crolemol

    @JUSTCAMH
    like in my code, in the last line i set the itemmeta so it will be applied
     
  9. Offline

    JUSTCAMH

    @crolemol Ok, well I copied and pasted that code and it still does the same thing. Have you tested it and are you sure it works, cause it works, but the item still shakes.
     
  10. Offline

    crolemol

    @JUSTCAMH
    post full code (whole class) and not tested but should work
     
  11. Offline

    JUSTCAMH

    @crolemol
    Whole class:
    Code:
    public class PlayerTrackerUpdate {
        public HashMap<Player, String> ps = new HashMap<Player, String>();
        SurvivalGamesBeyond sg;
    
        public PlayerTrackerUpdate(SurvivalGamesBeyond sgb) {
            sg = sgb;
        }
    
        Runnable run = new Runnable() {
    
            @Override
            public void run() {
                for (World w : Bukkit.getWorlds()) {
                    for (Player p : w.getPlayers()) {
                        for (Game g : sg.activeGames) {
                            if (g.getPlayers().contains(p)) {
                                if (p.getItemInHand().getType() == Material.COMPASS
                                        && p.getItemInHand().getItemMeta()
                                                .getDisplayName()
                                                .contains("Tuning")) {
                                    File f = new File(sg.getDataFolder()
                                            + File.separator + "Players"
                                            + File.separator
                                            + p.getUniqueId().toString()
                                            + File.separator
                                            + p.getUniqueId().toString() + ".yml");
                                    FileConfiguration con = YamlConfiguration
                                            .loadConfiguration(f);
                                    int speed = con.getInt("CompassTuningSpeed");
                                    int percent = Integer.parseInt(p
                                            .getItemInHand().getItemMeta()
                                            .getDisplayName()
                                            .replaceAll(ChatColor.GRAY + "", "")
                                            .replaceAll("Tuning: ", "")
                                            .replaceAll("%", ""));
                                    if (ps.containsKey(p)) {
                                        ps.put(p, ps.get(p) + 1);
                                        int tick = Integer.parseInt(ps.get(p));
                                        if (tick >= 6 - speed) {
                                            ps.remove(p);
                                            ps.put(p, "0");
                                            ItemMeta im = p.getItemInHand().getItemMeta();
                                            im.setDisplayName(ChatColor.GRAY
                                                    + "Tuning: " + (percent + 1)
                                                    + "%");
                                            p.getItemInHand().setItemMeta(im);
                                            p.sendMessage("Hi");
                                        }
                                    } else {
                                        ps.put(p, "0");
                                    }
                                } else {
    
                                }
                            }
                        }
                    }
                }
            }
        };
    
        public void beginTick() {
            sg.getServer().getScheduler()
                    .runTaskTimerAsynchronously(sg, run, 0L, 1L);
        }
    }
    It's a mess. I know. I'm fairly new to Java and bukkit in general so if there is anything else you see that is wrong, poorly coded, lacking or could be done better, please tell me.

    Explanation of code:
    every tick, if you are holding the compass, it will pretty much add one to the percent. It works, the only problem is the moving item. The 'beginTick' method is used in my onEnable(). There is alot of use of files etc and I won't explain them all unless I need to. So far, it will go above 100% and as a matter of fact it will keep going forever as long as it is in your hand. I thought I wouldn't do any more work on the class until I got it fixed. The "speed" int is a work in progress and will eventually do stuff but don't worry about that

    Also, I don't think its a problem with the class, I tried the:
    Code:
    ItemMeta im = p.getItemInHand().getItemMeta();
                      im.setDisplayName(ChatColor.GRAY
                              + "Tuning: " + (percent + 1)
                              + "%");
                      p.getItemInHand().setItemMeta(im);
    in my onCommand and still it doesn't work :(

    <Edit by mrCookieSlime: Merged posts. Please don't double post. There is an Edit Button right next to the Date.>
     
    Last edited by a moderator: Apr 18, 2015
  12. Offline

    nverdier

    @JUSTCAMH You have to set the ItemMeta of the item in the players hand after you change the display name. ItemStack#setItemMeta(ItemMeta im).
     
  13. Offline

    JUSTCAMH

    @nverdier Huh?

    Code:
    p.getItemInHand().setItemMeta(im);
    is after
    Code:
    im.setDisplayName(ChatColor.GRAY
                + "Tuning: " + (percent + 1)
                + "%");
     
  14. Offline

    nverdier

    Nevermind... Didn't see the second post and for some reason missed the first time you did it.
     
  15. Offline

    JUSTCAMH

    @nverdier Do you have any other solutions?
     
  16. Offline

    crolemol

    @JUSTCAMH
    hmm i thought this worked :/ i don't know what's wrong but i am sure it is/was possible because hypixel used the target compas with a distance in the name and it didn't pop
     
  17. Offline

    bwfcwalshy Retired Staff

  18. Offline

    I Al Istannen

    @JUSTCAMH I did something a long time ago (Messy code, its a shame xD) using PACKETS (there might be a BETTER way). It was quite easy with ProtocolLib to fake the change of the item, and as far as i remember it didn't pop up. You may look into ProtocolLib (and PacketWrapper), i was a noob with bukkit back then and i managed it, so you should get it working too ;).
     
    Last edited: Apr 18, 2015
  19. Offline

    Zombie_Striker

    Inset this after you have done anything to an inventory.
    p.updateInventory(); or p.getInventory.update();
     
  20. Offline

    JUSTCAMH

  21. Offline

    Shmobi

    @JUSTCAMH try setting another slot selected and reselect the old one. kinda ugly workarround but should work
     
  22. Offline

    MaTaMoR_

    This must work, cuz im using it :
    Code:
    public void setName(Player player, String name) {
       ItemStack stack = player.getItemInHand();
    
       if(stack == null || stack.getType.equals(Material.AIR)) {
          return;
       }
    
       ItemMeta im = stack.getItemMeta();
                     im.setDisplayName(name);
    
       stack.setItemMeta(im);
       player.setItemInHand(stack);
    }
    
     
  23. Offline

    I Al Istannen

    @MaTaMoR_ @JUSTCAMH
    I quickly tried it. It doesn't work for me in 1.8, but it does in 1.7.10. I have just tested my old thing, which also does't work. Quite logically, its the same as replacing the itemInHand, but without doing so actually. Seems to be handled differently.
     
  24. Offline

    Konato_K

    @JUSTCAMH Let me see if I understand you...

    The item does change name
    The item makes an animation like when it's given for the player

    If this is the case, I belive this is a client side feature and can't be avoided (unless someone has a solution).
     
  25. Offline

    Agentleader1

    Problem solved (with no errors possible):

    Code:
    Player player = your player;
    if(player.getInventory().getItemInHand() != null){
        ItemStack item = player.getInventory().getItemInHand();
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName(String name goes here);
        item.setItemMeta(meta);
        player.updateInventory();
    }
    //Edit: If you want it to say the newest name after it's been changed sightly above your hotbar, that's adjustable in video settings. I believe you can only configure it in Optifine:
    Esc > Options > Video Settings > Details > Held Item Tooltips
     
  26. Offline

    JUSTCAMH

    @Agentleader1 Ok so apparently I haven't made myself clear. I double checked what you posted and still not the outcome I was hoping. I am able to set the name of the item. That's easy. What I want is that when I set the name of the item, it will NOT show the animation for the transition between items. For example, when you switch your held item, the previous item will go down and the new item will come back up. Because I'm going to be every tick setting the name of the item, when I first finished it and ran it you couldn't see the item because it was constantly bobbing up and down


    @Konato_K You quite possibly are right. Still looking into the packets idea but it is increasingly looking less likely that I will get the outcome I desire. So I came up with another solution. Once again I have no idea how to do it (I would imagine with packets) but what my idea was was to keep the item's name the same and not edit the item at all, but instead make custom messages that appear above your hotbar. And by this I mean in the spot that tells you what music is being played. I would just update that message instead of the item name meaning that the animation wont run. So to make it clear, I could edit the message that appears when you play music. It's been done many times on servers and looks pretty neat.


    EDIT:
    Just saw I Al Istannen's post, sorry I couldn't quote him because this is an edit. Thanks for doing that I didn't even think about versions being the issue. I am on 1.8 spigot hack server so that must be why. All the servers I know that have updating item names are in 1.7.10 come to think about it. If someone please help with what I asked in the above paragraph, it would be highly appreciated.
     
    Last edited: Apr 23, 2015
  27. Offline

    Shmobi

    @JUSTCAMH change the display name of the item meta, reset the itemmeta of the itemstack, set the item in the players hand to null and then set it back to the itemstack you change the display name of. should work just fine. tell me if it doesn't
     
  28. Offline

    Agentleader1

    @JUSTCAMH So you want the item to disappear for like a few ticks, then reshow up as the new item?
     
  29. Offline

    Konato_K

    @JUSTCAMH You can only send messages above the hotbar (called action bar I think) in 1.8
     
  30. Offline

    JUSTCAMH

    @Shmobi Tried it, still same thing when in 1.8.


    @Konato_K anyway of differentiating people from 1.8 and from 1.7? So I could detect which version they are in and do what is compatible?
     
Thread Status:
Not open for further replies.

Share This Page