Solved How to update int in Scoreboard?

Discussion in 'Spigot Plugin Development' started by Mysak, Jun 18, 2021.

  1. Hello,
    how can I update int in Scoreboard?
    I use this code:
    Code (Text):

    public static int player_score = 0;

    public static void createBoard(Player player) {
        ScoreboardManager manager = Bukkit.getScoreboardManager();
        Scoreboard board = manager.getNewScoreboard();
        Objective obj = board.registerNewObjective("Scoreboard", "dummy", ChatColor.translateAlternateColorCodes('&', "&6&lSCOREBOARD"));
        obj.setDisplaySlot(DisplaySlot.SIDEBAR);
        Score score = obj.getScore(ChatColor.BLUE + "-------------------");
        score.setScore(3);
        Score score2 = obj.getScore(ChatColor.GREEN + "Your Score: " + ChatColor.DARK_GREEN + player_score);
        score2.setScore(2);
        player.setScoreboard(board);
    }

    public static void scoreBoardChange() {
        new BukkitRunnable() {

            @Override
            public void run() {

            player_score++;

            }
        }.runTaskLater(plugin, 20);
    }
     
    I always need to increase "player_score" by one to count the score. But it doesn't work.
     
    #1 Mysak, Jun 18, 2021
    Last edited: Jun 18, 2021
  2. You need to send the scoreboard in the scheduler as well. Right now in your code you only send the scoreboard once, initially. Then you update the int, but you don't update the scoreboard. So do the createBoard(theplayer) in your scheduler.
     
  3. Ahh, I see. Thank you. If I want to make score for each player separately, how can I do that?

    Code (Text):

    private static HashMap<Player, Integer> player_score = new HashMap<>();

    public static void scoreBoardChange(Player player) {
        new BukkitRunnable() {

            @Override
            public void run() {

                //player_score.put or what????
                createBoard(player);

            }
        }.runTaskLater(plugin, 20);
    }
     
     
  4. What do you mean? Do you want to show different score for different players? If yes, you are already doing that here: https://prnt.sc/15sy5if
     
  5. For example, every time a player does something, he receives a score + 1. And I need for each player separately. So do I have to use HashMap for that?
     
  6. Because now I'm showing them the same score for everyone.
     
  7. Yes. player_score is just a normal variable, which means it'll be the same regardless of players. I'm not sure why you think the score would be different for each player though, since you seem to be increasing the score every second no matter what.
     
  8. But I don't think this variable should be different for each player. I'm asking how to create a Map that will store each player's score and increase by 1 after each action.
     
  9. Based on some action, I call boardUpdate.
     
  10. If you don't think the variable will be different for each player, why do you need to store a different score for different players?
     
  11. I need to save the score for example for Player1: 10 and for Player2: 2 and then show it to them in the Scoreboard.
     
  12. Then yes, create a HashMap with the player as the key and their score as the value. You'll probably want to add a Player argument to your scoreBoardChange function, where you change the score in the hashmap and call createBoard for the player. Make sure you use the score from the HashMap in the createBoard function.
     
  13. Code (Text):

    public static HashMap<Player, Integer> player_score = new HashMap<>();


    public static void createBoard(Player player) {
        ScoreboardManager manager = Bukkit.getScoreboardManager();
        Scoreboard board = manager.getNewScoreboard();
        Objective obj = board.registerNewObjective("Scoreboard", "dummy", ChatColor.translateAlternateColorCodes('&', "&c&lSCOREBOARD"));
        obj.setDisplaySlot(DisplaySlot.SIDEBAR);
        Score score = obj.getScore(ChatColor.GRAY + "");
        score.setScore(3);
        Score score2 = obj.getScore(ChatColor.DARK_GREEN + "Your score: " + ChatColor.GREEN + player_score.get(player));
        score2.setScore(2);
        player.setScoreboard(board);
    }


    public static void boardUpdate(Player player) {
        new BukkitRunnable() {

            @Override
            public void run() {

                player_score.put(player, + 1);
                createBoard(player);

            }
        }.runTaskLater(plugin, 20);
    }
     
    Like this?
     
  14. It still doesn't work for me.. Where am I wrong?
     
  15. You are using +1 with nothing. Go like this:
    player_score.put(player, player_score.get(player) + 1);
    By using that get method, you are getting the value assigned to the player. Oh and don't forget to check if that exists, I think it might drop NPE.
    Put this before that line:
    if (player_score.get(player) == null) return;
     
    #15 ZBLL, Jun 18, 2021
    Last edited: Jun 19, 2021
  16. SOLVED!
     
    • Like Like x 1
  17. Congrats! If I, or/and someone else helped you, please leave a positive rating under my/their/both messages. Also it will be good if you lock the thread.