Make username one big healthbar

Discussion in 'Plugin Development' started by plisov, Aug 13, 2017.

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

    plisov

    Hi, I'm trying to make a player's name a health bar. I have the healthbar working however I also want the players name to be apart of the healthbar, if that makes sense. Heres a visual

    Lets say the player is at full health. Their name would look like

    |||PLAYERNAME|||

    Now lets say the player is at half health. Their name would look like

    |||PLAYERNAME|||

    I have the ||| working as a bar but I don't know how to make the whole name the bar.

    Code:
        @EventHandler
        public void onPlayerInteract(EntityDamageByEntityEvent event) {
    
            if (event.getEntity() instanceof Player) {
    
                Player player = (Player) event.getEntity();
    
                FileConfiguration config = null;
                File file = new File("plugins" + File.separator + "VentureLand" + File.separator + "user-data"
                        + File.separator + player.getName() + ".yml");
    
                config = YamlConfiguration.loadConfiguration(file);
    
                StringBuilder mratio = new StringBuilder("");
    
                double currentHealth = player.getHealth();
                @SuppressWarnings("deprecation")
                double maxHealth = player.getMaxHealth();
    
                double formula = (currentHealth / maxHealth) * 5;
    
                // Have for loop
    
                for (int i = 0; i < formula; i++) {
                    mratio.append(ChatColor.RED);
                    mratio.append("|");
                }
    
                // Need for loop
    
                for (; formula < 5; formula++) {
                    mratio.append(ChatColor.DARK_RED);
                    mratio.append("|");
                }
    
                String bar = mratio.toString();
               
                NametagEdit.getApi().setSuffix(player, bar);
               
            }
        }
     
  2. Offline

    PenguinOwl

    I would suggest making an array of each character in your message and using a for loop to append each letter and insert the color code.
     
Thread Status:
Not open for further replies.

Share This Page