Getting players score from a scoreboard objective

Discussion in 'Plugin Development' started by DerpysLastMuffin, Feb 22, 2018.

Thread Status:
Not open for further replies.
  1. I am (eventually) trying to make a plugin the allows armor to give custom health without making the players actual health bar huge. Right now there are two scoreboard dummy objectives. "HP" and "maxHP". I am trying to make it so when you take damage it sets your health bar to what your HP score is. For example, if your maxHP is 100, and you take damage to bring you to 50 HP your health bar is at half.

    This is my code for getting the players HP score, it returns that their currentHP is 0.

    EDIT: (The players HP score is currently set to 70 right now. http://prntscr.com/iik5uk)

    Code:
       @EventHandler
        public void dmg(final EntityDamageEvent event)
        {
            Entity e = event.getEntity();
            if( e instanceof Player) {
                Player player = (Player)e;
    
                ScoreboardManager manager = Bukkit.getScoreboardManager();
                Scoreboard board = manager.getNewScoreboard();
           
                player.sendMessage("Oww ooff ouchy my bones!"); //Debugging tool
           
                Objective objective = board.registerNewObjective("HP", "dummy");
                objective.setDisplaySlot(DisplaySlot.SIDEBAR);
           
    
                Score HP = objective.getScore(player.getName());
           
           
           
                double maxHP = 100; //Temporary, will get a different score from ingame
                double currentHP = HP.getScore();
           
                currentHP = currentHP / maxHP; //Get your percentage of HP
                currentHP = currentHP * 20; //Apply it to mincrafts 20 HP system
    
                String strcurrentHP = String.valueOf(currentHP);
           
                player.sendMessage(strcurrentHP);
                //player.setHealth(currentHP);   Currently kills you because currentHP = 0
           
            }
       
       
        }
    I have also tried changing
    Objective objective = board.registerNewObjective("HP", "dummy");
    to
    Objective objective = board.getObjective("HP");
    To use the objective I have already create in game, but that doesnt return anything for strcurrentHP.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    EDIT: I have determined that the code breaks after running this line of text:
    Score HP = objective.getScore(player.getName());
     
    Last edited: Feb 23, 2018
Thread Status:
Not open for further replies.

Share This Page