Solved Meta from Item in mainhand

Discussion in 'Plugin Development' started by CraftCreeper6, Aug 27, 2019.

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

    CraftCreeper6

    Hello, just a question that's probably got an easy solution, for the life of me I can't figure it out.

    Code:
    player.getInventory().getItemInMainHand().getItemMeta().getDisplayName()
    The code has no value. It simply does not return a value. I tried printing it with player.sendMessage, sout etc... No avail.

    Code:
    System.out.println(player.getInventory().getItemInMainHand().getItemMeta().getDisplayName());
    That code does not print a line in the console, at all. Absolutely no trace of it whatsoever, and no errors.

    I tried checking if the item had itemmeta and it did, and I also tried to use getLocalizedName() but that didn't work either.

    Probably a more reasonable solution is how do I get the exact name of the item. Which method as extension of getItemMeta() is the correct value?

    Any help is appreciated.
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    CraftCreeper6

    @timtower
    That's what my assumption was.

    As a test line of code (just make everything as dumbed down as possible):
    Code:
    player.sendMessage(this.prefix + ChatColor.BLUE + "" + ChatColor.BOLD + builder + ChatColor.RESET + "" + ChatColor.BLUE + "added to " + player.getInventory().getItemInMainHand().getItemMeta().getDisplayName());
    The builder string and added to get printed, but the display name does not.

    And when using System.out, nothing is displayed in the console (not even null)
     
  4. Offline

    timtower Administrator Administrator Moderator

    @CraftCreeper6 And if you set the displayname first?
    The displayname is always a custom name, you won't get the minecraft name from it.
     
  5. Offline

    CraftCreeper6

    @timtower
    Does work if I use an anvil, but that wouldn't explain why getLocalizedName doesn't work either.
     
  6. Offline

    timtower Administrator Administrator Moderator

    Same story after a look at the docs, needs to be set first.
     
  7. Offline

    CraftCreeper6

    @timtower
    Okay - that works.

    Now there's just the issue of what to set the display name to. Is there any way to get the name as displayed in Minecraft?
     
  8. Offline

    timtower Administrator Administrator Moderator

    I don't know, might be the case that you remove it if you set it to null again.
    But the name displayed in Minecraft itself is client side.
     
  9. Offline

    CraftCreeper6

    @timtower
    Could potentially get the enum value as a string and use replace with some word capitalization; for anyone looking at a solution to this.
     
  10. Offline

    timtower Administrator Administrator Moderator

    CraftCreeper6 likes this.
Thread Status:
Not open for further replies.

Share This Page