Solved Setting personal scoreboards not working

Discussion in 'Plugin Development' started by cheeseypotato, Nov 1, 2019.

  1. Offline

    cheeseypotato

    Soup, I'm making a 'paintball' plugin and trying to display kills and deaths on the sidebar which i have been able to achieve however it only displays one persons stats 'to everyone and not everyone's individual stats. No errors are occurring and I'm wondering if anyone would know why this is happening. Thanks

    Main / Where everything involved in the problem is going on

    Code:
    package me.billycombo.Main;
    
    import java.util.HashMap;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
    
    import me.billycombo.Main.events.Snowballs;
    import me.billycombo.commands.BlueSpawnSet;
    import me.billycombo.commands.RedSpawnSet;
    import me.billycombo.commands.joinTeam;
    public class Main extends JavaPlugin implements Listener {
       
       
       
        private Snowballs event = new Snowballs(this);
        private joinTeam event2 = new joinTeam();
       
       
    @Override
    public void onEnable(){
           
        Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
            @Override
            public void run() {
                for (Player p:Bukkit.getOnlinePlayers()){
                   
                    p.setScoreboard(UsedScoreboard.get(p.getName()));
                }
            }
      },0L,20L);
       
       
          
        }
    
    
    
    public static HashMap<String, Scoreboard> UsedScoreboard = new HashMap<String, Scoreboard>();
    public static HashMap<String, Score> killsscore = new HashMap<String, Score>();
    public static HashMap<String, Score> deathsscore = new HashMap<String, Score>();
    
    public static void create(Player p, String BoardName, String BoardDisplayName){
    ScoreboardManager manager = Bukkit.getScoreboardManager();
    Scoreboard board = manager.getMainScoreboard();
    Objective objective = board.registerNewObjective(BoardName, "dummy", "-Stats-");
    objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    objective.setDisplayName(BoardDisplayName);
    Score kills = objective.getScore(ChatColor.GREEN + "Kills:");
    Score deaths = objective.getScore(ChatColor.GREEN + "Deaths:");
    kills.setScore(0);
    deaths.setScore(0);
    
    killsscore.put(p.getName(), kills);
    deathsscore.put(p.getName(), deaths);
    UsedScoreboard.put(p.getName(), board);
    
    set(p);
    }
    
    public static void kill(Player p, Player p2){
        String pname = p.getName().toLowerCase();
        String pname2 = p2.getName().toLowerCase();
    
        Score kills = killsscore.get(pname);
        Score deaths = deathsscore.get(pname2);
    
        kills.setScore(killsscore.get(pname).getScore() + 1); //In this case everytime you Update the Scoreboard hist Money in the Scoreboard will be set to 100. (Replace the 100 with a Method of you to get the Players Money.
        deaths.setScore(deathsscore.get(pname2).getScore() + 1);
    }
    
    public static void clearKills(Player p){
        String pname = p.getName().toLowerCase();
    
        Score kills = killsscore.get(pname);
    
        kills.setScore(0);
    
    }
    
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent ev){
        Player p = ev.getPlayer();
        String pname = p.getName().toLowerCase();
       
        if (!UsedScoreboard.containsKey(p.getName()))
            create(p, pname, ChatColor.BLUE + "-Stats-");
       
        set(p);
    
    }
    
    public static void set(Player p){
       
    String pname = p.getName().toLowerCase();
    p.setScoreboard(UsedScoreboard.get(pname));
    }
    
    
    
    }
     
  2. Offline

    DerDonut

    You have to create multiple scoreboards, one for each player so that their scores aren't the same. When you call the create method you edit the main scoreboard which everyone sees. Use getNewScoreboard

    EDIT: Also remove the scheduler in the onEnable, once a scoreboard is set (onJoin for example) it updates automatically
     
    cheeseypotato likes this.
  3. Offline

    cheeseypotato

  4. Offline

    iosman000

    yeah that would help
     

Share This Page