Updating scoreboard objectives

Discussion in 'Plugin Development' started by Peavalu, Jan 11, 2015.

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

    Peavalu

    Hello people,
    I'm updating a scoreboard every time a player dies or gets a kill, here's the method.

    Code:
        @SuppressWarnings("deprecation")
        public void updateScoreboard(Player player){
           
            Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
            Objective objective = board.registerNewObjective("aaa", "bbb");
           
            objective.setDisplayName("§2§lPVP-Stats");
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
           
    
           
            Score zero = objective.getScore(Bukkit.getOfflinePlayer("§4§lKills        " + "§2§l" + sKitsmain.inst.kills.get(player)));
            Score one = objective.getScore(Bukkit.getOfflinePlayer("§4§lDeaths     " + "§2§l" + sKitsmain.inst.deaths.get(player)));
           
           
           
            zero.setScore(1);
            one.setScore(0);
           
            player.setScoreboard(board);
           
           
           
           
           
        }
    Then I call this method every time I want to. But the problem is that i'm creating a NEW scoreboard every time a player dies or gets a kill and this creates delay. So how would I make it so it would only update the objectives. I've tried looking around but haven't found a sufficent answer.

    Best regards,
    Peavalu
     
  2. Offline

    BlazingBroGamer

    @Peavalu
    Erm... put the board and objective outside the method. You are doing Bukkit()#getScoreboardManager#getNewScoreboard, thats why it is making a new scoreboard.
     
    Ganga likes this.
  3. Offline

    Peavalu

    @BlazingBroGamer
    I did as you said, and now the lag still occurs, and it also creates a new objective every time this event fires. So i'd get multiple lines of "deaths" and "kills". Here's my whole class. What should I do?

    Code:
    public class ScoreboardClass implements Listener {
      
        int sched;
       
          Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
      
          Objective objective = board.registerNewObjective("aaa", "bbb");
      
        @EventHandler
        public void onJoin(PlayerJoinEvent event){
        final    Player player = event.getPlayer();
      
            //updating the scoreboard after all the values have been loaded
            this.updateScoreboard(player);
          
    
          
        }  
      
      
      
        //storing player deaths
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent event) {
            Player player = event.getEntity();
            //getting the older deaths and adding 1 to them each time this method runs
             int deathCount = sKitsmain.inst.deaths.get(player);
             sKitsmain.inst.deaths.put(player, deathCount + 1);
             updateScoreboard(player);
        }
    
      
        //storing player kills
        @EventHandler
        public void killPlayer(EntityDeathEvent event){
          
            Entity deadEntity = event.getEntity();
          
            Entity killer = event.getEntity().getKiller();
          
            //this if block adds 1 to a player if he kills anything
            if(killer instanceof Player && deadEntity instanceof Player){
              
                Player player = (Player)killer;
              
               int killCount =    sKitsmain.inst.kills.get(player);
               sKitsmain.inst.kills.put(player, killCount + 1);
               updateScoreboard(player);
    
              
            }
          
        }
          
      
      
        //updateing the scoreboard, runs when a player kills someone or dies
        @SuppressWarnings("deprecation")
        public void updateScoreboard(Player player){
          
          
          
            objective.setDisplayName("§2§lPVP-Stats");
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
          
    
          
            Score zero = objective.getScore(Bukkit.getOfflinePlayer("§4§lKills        " + "§2§l" + sKitsmain.inst.kills.get(player)));
            Score one = objective.getScore(Bukkit.getOfflinePlayer("§4§lDeaths     " + "§2§l" + sKitsmain.inst.deaths.get(player)));
          
          
          
            zero.setScore(1);
            one.setScore(0);
          
            player.setScoreboard(board);
          
          
          
          
          
        }
      
    
    
    }
     
  4. Offline

    BlazingBroGamer

    @Peavalu
    Do board#resetScores(Object Name). Also, do objective#getScore(String), instead of offline player.
     
  5. Offline

    Peavalu

    @BlazingBroGamer
    Could you show me an example by fixing up my code above?

    Best regards,
    Peavalu
     
Thread Status:
Not open for further replies.

Share This Page