Scoreboard Problem

Discussion in 'Plugin Development' started by EvilestVirus7, Feb 14, 2020.

  1. Offline

    EvilestVirus7

    So my goal is to create a simple scoreboard for each player that join the server. I've put the `setScoreboard();` on the `PlayerJoinEvent` but when I join the server, the scoreboard is not there.

    So this is what I've do.

    Main class:

    Code:
        package can.evilestvirus.board;
      
        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 java.util.HashMap;
      
        public class Board extends JavaPlugin implements Listener {
      
            public HashMap<Player, ScoreboardManager> sb = new HashMap<>();
      
            public static Board instance;
      
            public static Board getInstance(){
                return instance;
            }
      
            @Override
            public void onEnable() {
                instance = this;
                new ScoreboardRunnable().runTaskTimer(this, 0L, 20L);
                getServer().getPluginManager().registerEvents(this, this);
                super.onEnable();
            }
      
            @EventHandler
            public void onJoin(PlayerJoinEvent event){
                Player player = event.getPlayer();
                ScoreboardManager board = new ScoreboardManager(player);
                board.refresh();
                board.setScoreboard();
            }
        }
    My ScoreboardManager:

    Code:
        package can.evilestvirus.board;
       
        import org.bukkit.Bukkit;
        import org.bukkit.entity.Player;
        import org.bukkit.scoreboard.DisplaySlot;
        import org.bukkit.scoreboard.Objective;
        import org.bukkit.scoreboard.Scoreboard;
       
        import java.util.Random;
       
        public class ScoreboardManager implements org.bukkit.scoreboard.ScoreboardManager {
       
            public Scoreboard scoreboard;
            public Objective objective;
            public String name = "scoreboard";
       
            public Player player;
       
            public ScoreboardManager(Player player){
                this.player = player;
                this.scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
                this.name = "sb." + new Random().nextInt(99999);
                this.objective = scoreboard.registerNewObjective(name, "dummy");
                objective.setDisplayName("ScoreBoard");
                objective.setDisplaySlot(DisplaySlot.SIDEBAR);
       
                if(Board.getInstance().sb.containsKey(player)) return;
       
                Board.getInstance().sb.put(player, this);
            }
       
            @Override
            public Scoreboard getMainScoreboard() {
       
                return scoreboard;
            }
       
            @Override
            public Scoreboard getNewScoreboard() {
                return null;
            }
       
            public void refresh() {
       
            }
       
            public void sendLine(){
                objective.getScore("§4➤ §6§lJoueur: " + player.getDisplayName()).setScore(0);
       
            }
       
            public void setScoreboard(){
                player.setScoreboard(scoreboard);
            }
        }
    This is my Scoreboard refresh class:

    Code:
        package can.evilestvirus.board;
       
        import org.bukkit.entity.Player;
        import org.bukkit.scheduler.BukkitRunnable;
       
        import java.util.Map.Entry;
       
        public class ScoreboardRunnable extends BukkitRunnable {
       
            @Override
            public void run() {
                for(Entry<Player, ScoreboardManager> scoreboard: Board.getInstance().sb.entrySet()){
                    ScoreboardManager board = scoreboard.getValue();
                    board.refresh();
                }
            }
        }
     

Share This Page