getHealth()

Discussion in 'Plugin Development' started by JHG0, Jul 31, 2013.

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

    JHG0

    Hey it's JHG0 again!
    Coding up a new plugin at the moment.
    It seems that I can't figure out the correct usage for getHealth()

    I have a player set up.
    Code:java
    1. Player player = (Player) sender; //...I took out a few lines of code here
    2.  
    3. Player tPlayer = player.getServer().getPlayer(args[1]);
    4. if (tPlayer == null){
    5. player.sendMessage(ChatColor.GOLD + "The player you have selected is not online!");
    6. } else {
    7. double health = tPlayer.getHealth();


    Any suggestions?
     
  2. Offline

    TheBoy3

    The code looks fine to me, are you using craftbukkit or the bukkit API for the external jar? Because you should use the bukkit API.

    If you use craftbukkit, you get a "getHealth() is ambiguous" like error.
     
  3. Offline

    JHG0

    TheBoy3
    The one from dl.bukkit.org
    Is that the wrong one?
     
  4. Offline

    JHG0

  5. Offline

    JHG0

    Just wondering, is there a way to automatically display the item name to the player?
    So I don't have to
    if(itemBlah == itemDiamondSword){
    player.sendMessage("Blah is a DiamondSword!");
    }
     
  6. Offline

    Zach_1919

    Just do something like this:
    Code:java
    1. String s = p.getItemInHand().getItemMeta().getDisplayName();
    2. p.sendMessage(You are holding + s);
     
  7. Offline

    daboross Retired Staff

    Zach_1919 That code is functional in theory, but you are not quoting the string.

    JHG0: Zach_1919's example would work perfectly if using
    Code:
    ("You are holding" + s);
    instead of
    Code:
    (You are holding + s);
     
  8. Offline

    JHG0

    daboross Zach_1919

    And how would the output look?
    Would it say, You are holding a Diamond Sword?

    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  9. Offline

    daboross Retired Staff

    JHG0 No need to bump within 1 hour :p. I had just been sleeping :p.

    Anyways, yes! That would display 'You are holding <x>'. Where <x> is the item's name.
     
  10. Offline

    KingFaris11

    daboross and everyone else saying to do this.
    Please correct me if I'm wrong but items that aren't renamed don't have a display name which is why the boolean hasDisplayName() exists. If every item had a display name, why would that boolean exist? I think it would be better to use item.getType().toString() although it won't say the display name how it is (as in, if you have a potion it'll say POTION not POISON_POTION). You can try and make your own list of item names but that'd take quite a while.

    Yes, I just looked into it now and I found this:
    http://jd.bukkit.org/rb/doxygen/db/...emMeta.html#a3bfb981f4ce43a627cf67e19f2cd3daf
    It says that you must check for hasDisplayName() first as getDisplayName() "gets the display name that is set". "Set" implies you have to set it before getting it, so basically it just returns the "nickname" of the item if it has one. So use item.getType().toString().

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  11. Offline

    daboross Retired Staff

    Ah, true! I had forgotten about that.
     
  12. Offline

    JHG0

    daboross KingFaris11
    I am so confused now. Can someone just explain what I would do.
    Would it be item.getType().hasDisplayName().getDisplayName()?
     
  13. Offline

    KingFaris11

    No.
    Code:
    String itemName = item.getType().toString();
    if (item.hasItemMeta()) {
        if (item.getItemMeta().hasDisplayName()) itemName = item.getItemMeta().getDisplayName();
    }
    
    What this does is checks to see whether the item has a custom name, if it doesn't then use the item type as its name.

    It'd end up looking like this:
    Code:
    ItemStack item = player.getInventory().getItemInHand();
    String itemName = item.getType().toString();
    if (item.hasItemMeta()) {
        if (item.getItemMeta().hasDisplayName()) itemName = item.getItemMeta().getDisplayName();
    }
    player.sendMessage("You are holding: " + itemName);
    
    Note: getType().toString() returns the name in capital letters with underscores instead of spaces. To fix this you can do getType().toString().replaceAll("_", " ")
    It also uses dodgy Bukkit enum names and not Vanilla Minecraft names so removing underscores would make it look even more dodgy. If you want it to have the same capitalisation as Vanilla Minecraft names does, use the following code:

    Code:
    String itemName = WordUtils.capitalizeFully(item.getType().toString().replaceAll("_", " "));
     
  14. Offline

    daboross Retired Staff

    Sorry about all that. Well, first you need to check `if (item.hasDisplayName())` and if it does you would use `item.getDisplayName()` if it doesn't you would use `item.getType()`.
    Let me write this in an example, I will edit this post.

    Edit: Hadn't seen your post KingFaris11. That would work but it would involve 2 unrequired copies of the ItemMeta. When getItemMeta() is called it makes a copy, so it is preferred that you store it in a variable if calling it multiple times.

    This is what I would do:
    Code:java
    1. ItemStack item = p.getItemInHand();
    2. ItemMeta meta = item.getItemMeta();
    3. String name;
    4. if (meta.hasDisplayName()) {
    5. name = meta.getDisplayName();
    6. } else {
    7. name = item.getType().toString();
    8. }
    9. p.sendMessage("You are holding " + name);
     
    KingFaris11 likes this.
  15. Offline

    KingFaris11

    You read my code and you still didn't add in the thing that you need. There's also hasItemMeta() and getItemMeta() so that means the ItemMeta could be null. Double check by using hasItemMeta() first. I don't want to confuse this guy so I think he should use my way.
     
  16. Offline

    daboross Retired Staff

    Ah sorry, I forgot that it couldn't have itemmeta. Sorry about that, yes. Your way will work KingFaris11
     
  17. Offline

    JHG0

    KingFaris11 likes this.
  18. Offline

    Taketheword

    My plugins are both on my other account (Bukkit Dev) and privated for my server and a couple other servers I develop for.
     
  19. Offline

    Jade Retired Staff

    Cleaned up insults towards another member.
     
    KingFaris11 likes this.
  20. Offline

    JHG0

    Jade
    Jade can you please delete all these insult posts

    Oh and also, can it take more than 2 days for a plugin to be accepted?
     
  21. Offline

    Taketheword

    Sorry :3

    I've found it takes around 12 hours for the plugin to get accepted and an addition 6 or so for the jar file. But it all varies considering that it goes in order of submission. So the more plugins submitted that day/time the longer it takes.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  22. Offline

    Jade Retired Staff

    It can, however it depends on how big the workload for our BukkitDev staff.
     
  23. Offline

    Taketheword

    Jade
    Out of curiosity Jade, how do you add your Bukkit Dev plugins to your normal Bukkit account?
     
  24. Offline

    JHG0

    daboross KingFaris11

    Now, how would I get the durability and the max durability of the item?


    Link the accounts together in your settings

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  25. Offline

    Taketheword

    JHG0
    Oh :p I havn't even touched the settings much in this account because this account isn't really permanent.
     
  26. Offline

    daboross Retired Staff

  27. Offline

    Jade Retired Staff

  28. Offline

    JHG0

    daboross
    How would I make all my strings and ints local. At the moment they are in an if statement and I can't use them if they are in there
     
Thread Status:
Not open for further replies.

Share This Page