Setting a scoreboard for players in a minigame.

Discussion in 'Plugin Development' started by iceypotatoguy, Apr 1, 2021.

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

    iceypotatoguy

    I want the scoreboard to display kills for each player, but for some reason, my code just displays the same score for everyone. I have made a test command with subcommands that manually changes my kill count along with updating the scoreboard.
    Code:
    public class Test implements CommandExecutor {
        private WallsScoreboard wallsSc;
      
        public Test(MainClass main, WallsTool wallsTool) {
            this.myplugin = main;
            this.wallsTool = wallsTool;
            wallsSc = new WallsScoreboard("test", "Test", "dummy", DisplaySlot.SIDEBAR);
            wallsSc.clearSB();
            wallsSc.setKills(new HashMap<Player, Integer>());
        }
      
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (args.length > 0) {
                Player player = (Player) sender;
                if (args[0].equalsIgnoreCase("setsb")) {
                    wallsSc.clearSB();
                    wallsSc.getKills().put(player, 0);
                    wallsSc.putKills(player);
                    wallsSc.updatePlayersSB(player);
                }
                else if (args[0].equalsIgnoreCase("rmsb")) {
                    wallsSc.getKills().remove(player);
                    player.setScoreboard(wallsSc.getManager().getMainScoreboard());
                }
                else if (args[0].equalsIgnoreCase("addkill")) {
                    wallsSc.getKills().replace(player, wallsSc.getKills().get(player)+1);
                }
                else if (args[0].equalsIgnoreCase("updatesb")) {
                    wallsSc.clearSB();
                    wallsSc.putKills(player);
                    wallsSc.updatePlayersSB(player);
                }
                else {
                    sender.sendMessage("Invalid subcommand");
                }
                return true;
            }
            else {
                sender.sendMessage("Invalid subcommand.");
            }
            return false;
        }
    }
    
    Code:
    public class WallsScoreboard {
    
        private HashMap<Player, Integer> kills;
    
        public void putKills(Player player) {
            Score space = objective.getScore(" ");
            space.setScore(0);
            Score kills = objective.getScore(ChatColor.GREEN + "Kills: " + ChatColor.RESET +""+ this.kills.get(player));
            kills.setScore(-1);
        }
      
        //Displays the scoreboard to the player.
        public void updatePlayersSB(Player player) {
            player.setScoreboard(board);
        }
    

    The issue is that when I update the scoreboard, it displays the kill count of the person who executed the command, to all players that have this scoreboard displayed. I need my scoreboard to display the kill count for each player.
     
    Last edited: Apr 1, 2021
  2. Offline

    Newdel

    Seems like you have 1 scoreboard for every player. You have to create a new Scoreboard for every player.
    For updating use Player#getScoreboard() and update this one.


    Also, use guard clauses insted of nested ifs and don't store player objects in a map. Use the UUID instead.
     
  3. Offline

    iceypotatoguy

    I was planning to do this, but then I would be missing out on teams and an HP bar over each player's head. Is there a solution for this? Oh, and I am using UUID, I just use Bukkit.getPlayer when I need to pass in something.
     
Thread Status:
Not open for further replies.

Share This Page