Solved Health indicator

Discussion in 'Plugin Development' started by Xp10d3, May 22, 2021.

  1. Offline


    Hello. I'm wondering how you can have a "health indicator" using an action bar similar to Hypixel The Bridge.
  2. Offline


    Anything other servers do you can code unless it's modded, this can be done. I would have a repeating task constantly sending a message to each player's action bar with their health, you need to build a string and use
    to indicate the hearts with chat color so it would probably look something like this
    public static String stringBuilder(Player p) {
        String str = ChatColor.BLUE+p.getName()+" ";
        int health = (int) Math.ceil(p.getHealth()/2);
        for (int i=0; i < health; i++)
            str += ChatColor.RED+"♥";
        for (int i=(10-health); i > 0; i--)
            str += ChatColor.YELLOW+"♥";
        return str;
  3. Offline


    Java recommends using a StringBuilder instance (or StringBuffer if you want thread-safe?) instead of continuously concatenating strings within loops.
    1. // I'd suggest using this over concatenating with ChatColor enums. But you do you, shouldn't matter much.
    2. // If you want to use this, you could add this method in some sort of Utils classes for easier calling.
    3. public static String color(final String s) {
    4. return ChatColor.translateAlternateColorCodes('&', s);
    5. }
    7. public static String buildHealthString(final Player p) {
    8. final StringBuilder sb = new StringBuilder("&9").append(p.getName()).append(" ");
    9. final int hearts = (int) Math.ceil(p.getHealth() / 2);
    11. for(int i = 0; i < hearts; i++) {
    12. sb.append("&c").append("♥"); // Represents remaining hearts, colored red.
    13. }
    14. for(int i = 10 - hearts; i > 0; i--) {
    15. sb.append("&4").append("♥"); // Represents missing hearts, colored dark red.
    16. }
    18. // Same for absorption hearts.
    19. return sb.toString();
    20. }

    But if you really want to use action bars, you need a PacketPlayOutChat. It's not present in Bukkit api, so you need to add a Craftbukkit jar as a dependency. I don't know if there's an official method to do so, all I could find is a sending titles and subtitles.
    1. // You can get the json data (IChatBaseComponent) from ChatSerializer.a(String) method.
    2. // The byte is the position, 0 for chatbox, 1 for system message, 2 for action bar.
    3. PacketPlayOutChat(JSON data, byte); // 1.8
    4. PacketPlayOutChat(JSON data, ChatMessageType, UUID); // Later versions.
    6. //Here's an example:
    7. final PacketPlayOutChat packet = PacketPlayOutChat(ChatSerializer.a("{\"text\":\"" + color("&chello!") + "\"}"), (byte) 2);
    8. // One way to send packet. Other is by writing into their channels but that's protocollib's thing and idk much.
    9. ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
    davidclue and KarimAKL like this.
  4. Offline


    Thanks; sorry for the late reply I've been quite busy. I'll try and look at it soon. Going on vacation so I won't be on my PC for a while lol. Will update once I've added.
  5. Offline


    Sorry for the super late reply, but it works haha. Marking as solved.

Share This Page