Scoreboard not showing?

Discussion in 'Spigot Plugin Development' started by DaanSander, May 23, 2015.

  1. Hello I am trying to create scoreboards for my minigame but when i try to show the scoreboard to all the players in the minigame it wont show it to them I checked if there where any players in the arena but the problem is not in that can someone help me?

    Code (Text):
    public void startScoreBoardTask() {
            ScoreboardManager scoreboardManager = Bukkit.getScoreboardManager();
            final Scoreboard scoreboard = scoreboardManager.getNewScoreboard();
            final Objective objective = scoreboard.registerNewObjective("o", "dummy");
            objective.setDisplayName("FarmMadness");
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
            scoreBoardtask = Bukkit.getScheduler().runTaskTimerAsynchronously(Farm.getPlugin(), new Runnable() {
                @Override
                public void run() {
                    for(Player player : getPlayers()) {
                        Score p = objective.getScore(player.getName());
                        player.setScoreboard(scoreboard);
                    }
                }
            }, 0L, 20L);
        }
    Sorry for bad english
     
  2. There are no errors
     
  3. You can post all class?
     
  4. Code (Text):
    package me.daansander.farm.arena;

    import me.daansander.farm.Farm;
    import me.daansander.farm.scoreboard.ScoreBoard;
    import me.daansander.farm.util.ActionBar;
    import me.daansander.farm.util.UnkownWaveException;
    import me.daansander.farm.wave.Wave;
    import net.md_5.bungee.api.chat.ClickEvent;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.scheduler.BukkitTask;
    import org.bukkit.scoreboard.*;

    import java.awt.font.ImageGraphicAttribute;
    import java.util.ArrayList;

    /**
    * Created by Daan on 20-5-2015.
    */
    public class Arena {

        private WaveType type;
        private static GameState gameState = GameState.LOBBY;
        private long timer;
        private BukkitTask lobbyTimer;
        private BukkitTask gameTimer;
        private BukkitTask waveTimer;
        private BukkitTask actionBarTask;
        private BukkitTask scoreBoardtask;
        private final String creator;
        private final int minPlayers;
        private Location lobbyLocation;
        private Location EggWave;
        private Location SheepWave;
        private Location CowMilkWave;
        private Location PigFeefWave;
        private Location MoverGrass;
        private Location WheatWave;
        private static ArrayList<Player> players = new ArrayList<Player>();
        private static ArrayList<Arena> arenas = new ArrayList<Arena>();
        private final int maxPlayers;
        private final String arenaName;
        private final int timeToStartDefault = 10;
        private final int timeToEndDefault = 50 * 60;
        private final int timeForWave = 10;
        private int timeToStart;
        private int waveToLoop;
        private int wave;
        private int timeToEnd;
        private Arena arena;

        //TODO constructor fixen

        public Arena(String creator, int minPlayers, int maxPlayers, String arenaName, Location lobbyLocation, Location eggWaveLoc, Location sheepWaveLoc, Location CowLocation,
                     Location PigWaveLoc, Location MoverGrassLoc, Location wheatWave) {
            this.creator = creator;
            this.minPlayers = minPlayers;
            this.maxPlayers = maxPlayers;
            this.arenaName = arenaName;
            this.lobbyLocation = lobbyLocation;
            this.EggWave = eggWaveLoc;
            this.SheepWave = sheepWaveLoc;
            this.CowMilkWave = CowLocation;
            this.PigFeefWave = PigWaveLoc;
            this.MoverGrass = MoverGrassLoc;
            this.arena = this;
            this.WheatWave = wheatWave;
            this.gameState = GameState.LOBBY;

            Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Created arena " + arenaName);
            arenas.add(this);
        }

        public static GameState getCurrentState() {
            return gameState;
        }

        public WaveType getType() {
            return type;
        }

        public void setType(WaveType type) {
            this.type = type;
        }

        public int getMaxPlayers() {
            return maxPlayers;
        }

        public int getMinPlayers() {
            return minPlayers;
        }

        public String getCreator() {
            return creator;
        }

        public BukkitTask getGameTimer() {
            return gameTimer;
        }

        public void setGameTimer(BukkitTask gameTimer) {
            this.gameTimer = gameTimer;
        }

        public BukkitTask getLobbyTimer() {
            return lobbyTimer;
        }

        public void setLobbyTimer(BukkitTask lobbyTimer) {
            this.lobbyTimer = lobbyTimer;
        }

        public long getTimer() {
            return timer;
        }

        public void setTimer(long timer) {
            this.timer = timer;
        }

        public GameState getGameState() {
            return gameState;
        }

        public void setGameState(GameState gameState) {
            this.gameState = gameState;
        }

        public static ArrayList<Player> getPlayers() {
            return players;
        }

        public void setPlayers(ArrayList<Player> players) {
            this.players = players;
        }

        public static ArrayList<Arena> getArenas() {
            return arenas;
        }

        public void setArenas(ArrayList<Arena> arenas) {
            this.arenas = arenas;
        }

        public String getArenaName() {
            return arenaName;
        }

        public void endLobbyTimer() {
            lobbyTimer.cancel();
        }

        public int getTimeToStartDefault() {
            return timeToStartDefault;
        }

        public int getTimeToEnd() {
            return timeToEnd;
        }

        public void setTimeToEnd(int timeToEnd) {
            this.timeToEnd = timeToEnd;
        }

        public int getTimeToStart() {
            return timeToStart;
        }

        public void setTimeToStart(int timeToStart) {
            this.timeToStart = timeToStart;
        }

        public int getTimeToEndDefault() {
            return timeToEndDefault;
        }

        public void addPlayer(Player player) {
            players.add(player);
            me.daansander.farm.player.Player player1 = new me.daansander.farm.player.Player(arena, player);
        }

        public BukkitTask getWaveTimer() {
            return waveTimer;
        }

        public void setWaveTimer(BukkitTask waveTimer) {
            this.waveTimer = waveTimer;
        }

        public Location getLobbyLocation() {
            return lobbyLocation;
        }

        public void setLobbyLocation(Location lobbyLocation) {
            this.lobbyLocation = lobbyLocation;
        }

        public void setWave(int wave) {
            this.wave = wave;
        }

        public int getWaveToLoop() {
            return waveToLoop;
        }

        public void setWaveToLoop(int waveToLoop) {
            this.waveToLoop = waveToLoop;
        }

        public int getTimeForWave() {
            return timeForWave;
        }

        public Location getEggWave() {
            return EggWave;
        }

        public void setEggWave(Location eggWave) {
            EggWave = eggWave;
        }

        public Location getMoverGrass() {
            return MoverGrass;
        }

        public void setMoverGrass(Location moverGrass) {
            MoverGrass = moverGrass;
        }

        public Location getPigFeefWave() {
            return PigFeefWave;
        }

        public void setPigFeefWave(Location pigFeefWave) {
            PigFeefWave = pigFeefWave;
        }

        public Location getCowMilkWave() {
            return CowMilkWave;
        }

        public void setCowMilkWave(Location cowMilkWave) {
            CowMilkWave = cowMilkWave;
        }

        public Location getSheepWave() {
            return SheepWave;
        }

        public void setSheepWave(Location sheepWave) {
            SheepWave = sheepWave;
        }

        public Arena getArena() {
            return arena;
        }

        public void setArena(Arena arena) {
            this.arena = arena;
        }

        @Override
        public String toString() {
            return "Arena{" +
                    "type=" + type +
                    ", gameState=" + gameState +
                    ", timer=" + timer +
                    ", lobbyTimer=" + lobbyTimer +
                    ", gameTimer=" + gameTimer +
                    ", waveTimer=" + waveTimer +
                    ", creator='" + creator + '\'' +
                    ", minPlayers=" + minPlayers +
                    ", lobbyLocation=" + lobbyLocation +
                    ", EggWave=" + EggWave +
                    ", SheepWave=" + SheepWave +
                    ", CowMilkWave=" + CowMilkWave +
                    ", PigFeefWave=" + PigFeefWave +
                    ", MoverGrass=" + MoverGrass +
                    ", maxPlayers=" + maxPlayers +
                    ", arenaName='" + arenaName + '\'' +
                    ", timeToStartDefault=" + timeToStartDefault +
                    ", timeToEndDefault=" + timeToEndDefault +
                    ", timeForWave=" + timeForWave +
                    ", timeToStart=" + timeToStart +
                    ", waveToLoop=" + waveToLoop +
                    ", wave=" + wave +
                    ", timeToEnd=" + timeToEnd +
                    ", arena=" + arena +
                    '}';
        }

        public void startScoreBoardTask() {
            ScoreboardManager scoreboardManager = Bukkit.getScoreboardManager();
            final Scoreboard scoreboard = scoreboardManager.getNewScoreboard();
            final Objective objective = scoreboard.registerNewObjective("o", "dummy");
            objective.setDisplayName("FarmMadness");
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
            scoreBoardtask = Bukkit.getScheduler().runTaskTimerAsynchronously(Farm.getPlugin(), new Runnable() {
                @Override
                public void run() {
                    for(Player player : getPlayers()) {
                        Score p = objective.getScore(player.getName());
                        player.setScoreboard(scoreboard);
                    }
                }
            }, 0L, 20L);
        }
        public void endScoreBoardTask() {
            scoreBoardtask.cancel();
        }

        public Player getPlayer(String name) {
            for(Player player : players) {
                if (player.getName().equals(name)) {
                    return player;
                }
            }
            return null;
        }

        public WaveType getWave() {
            return type;
        }

        public static Arena getArena(String name) {
            for(Arena arena : arenas) {
                if(arena.getArenaName().equals(name)) {
                    return arena;
                }
            }
            return null;
        }
        public static Arena getArena(Player player) {
            for(Arena arena : arenas) {
                if(getPlayers().contains(player)) {
                    return arena;
                }
            }
            return null;
        }
        public void showActionBar() {
            actionBarTask = Bukkit.getScheduler().runTaskTimerAsynchronously(Farm.getPlugin(), new Runnable() {
                @Override
                public void run() {
                    for(Player player : getPlayers()) {
                        ActionBar.sendAnnouncement(player, ChatColor.GREEN + "" + ChatColor.BOLD + "FarmMadness " + ChatColor.RESET + "" + ChatColor.AQUA + "" + ChatColor.BOLD + "> "
                        + ChatColor.RESET + "" + ChatColor.GREEN + timeToStart);
                    }
                }
            },0l , 20l);
        }

        public void endActionbarTask() {
            actionBarTask.cancel();
        }

        public void startGameTimer() {
            timeToEnd = timeToEndDefault;
            gameTimer = Bukkit.getScheduler().runTaskTimer(Farm.getPlugin(), new BukkitRunnable() {
                @Override
                public void run() {
                    timeToEnd--;

                    if(getPlayers().size() <= 1) {
                        timeToEnd = 0;

                        teleportAllToLobby();

                        gameState = GameState.LOBBY;

                        startLobbyTimer();
                        endGameTimer();
                    }

                    if (timeToEnd < 0) {
                        timeToEnd = 0;

                        teleportAllToLobby();

                        gameState = GameState.LOBBY;

                        startLobbyTimer();
                        endGameTimer();
                    }
                }
            }, 20l, 20l);
        }

        @SuppressWarnings("deprecation")
        private void startWaveTimer() {
            type = WaveType.EGG;
            final Wave wave1 = new Wave(arena, type);
            try {
                wave1.setUp();
            } catch (UnkownWaveException e) {
                e.printStackTrace();
            }
            teleportAllToWave();
            waveToLoop = timeForWave;
            this.waveTimer = Bukkit.getScheduler().runTaskTimer(Farm.getPlugin(), new BukkitRunnable() {
                @Override
                public void run() {
                    if(!(waveToLoop <= 0)) {
                        waveToLoop--;
                        Bukkit.broadcastMessage("" + wave);
                    } else {
                        if(wave == 0) {
                            wave++;
                            type = WaveType.PIGFOOD;
                            Wave wave = new Wave(arena, type);
                            try {
                                wave.setUp();
                            } catch (UnkownWaveException e) {
                                e.printStackTrace();
                            }
                            teleportAllToWave();
                            waveToLoop = timeForWave;
                        } else if(wave == 1) {
                            wave++;
                            type = WaveType.SHEEPWALK;
                            Wave wave = new Wave(arena, type);
                            try {
                                wave.setUp();
                            } catch (UnkownWaveException e) {
                                e.printStackTrace();
                            }
                            teleportAllToWave();
                            waveToLoop = timeForWave;
                        } else if(wave == 2) {
                            wave++;
                            type = WaveType.WHEAT;
                            Wave wave = new Wave(arena, type);
                            try {
                                wave.setUp();
                            } catch (UnkownWaveException e) {
                                e.printStackTrace();
                            }
                            Bukkit.broadcastMessage(type.toString());
                            teleportAllToWave();
                            waveToLoop = timeForWave;
                        } else if(wave == 3) {
                            wave++;
                            type = WaveType.MOVERGRASS;
                            Wave wave = new Wave(arena, type);
                            try {
                                wave.setUp();
                            } catch (UnkownWaveException e) {
                                e.printStackTrace();
                            }
                            Bukkit.broadcastMessage(ChatColor.RED + "WAVETELE");
                            teleportAllToWave();
                            waveToLoop = timeForWave;
                        } else if(wave == 4) {
                            wave++;
                            type = WaveType.COWMILK;
                            Wave wave = new Wave(arena, type);
                            try {
                                wave.setUp();
                            } catch (UnkownWaveException e) {
                                e.printStackTrace();
                            }
                            teleportAllToWave();
                            waveToLoop = timeForWave;
                        } else {
                            Bukkit.broadcastMessage("3");
                            endGameTimer();
                        }
                    }
                }
            },20,20l);
        }

        public void endWaveTimer() {
            waveTimer.cancel();
        }

        private void endGameTimer() {
            gameTimer.cancel();
            waveTimer.cancel();
            System.out.println(players.size());
            removeAllPlayers();
            endScoreBoardTask();
            players.removeAll(players);
            gameState = GameState.LOBBY;
            startLobbyTimer();
            teleportAllToLobby();
        }

        @SuppressWarnings("deprecation")
        public void startLobbyTimer() {
            showActionBar();
            timeToStart = timeToStartDefault;
            lobbyTimer = Bukkit.getScheduler().runTaskTimer(Farm.getPlugin(), new BukkitRunnable() {
                @Override
                public void run() {
                    if(getPlayers().size() >= minPlayers)
                        timeToStart--;

                    if (timeToStart < 0) {
                        timeToStart = 0;

                        teleportToArena();

                        gameState = GameState.INGAME;
                        System.out.println(gameState.toString());

                        startGameTimer();
                        startWaveTimer();
                        startScoreBoardTask();
                        endLobbyTimer();
                        endActionbarTask();
                    }
                }
            }, 20l, 20l);
        }

        public void teleportToArena() {
            for(Player player : getPlayers()) {
                teleportToWave(type, player);
            }
        }

        public void teleportToLobby(Player player) {
            player.teleport(lobbyLocation);
        }
        public void teleportAllToLobby() {
            for(Player player : getPlayers()) {
                teleportToLobby(player);
            }
        }

        public void removePlayer(Player player) {
            players.remove(player);
        }
        public void removeAllPlayers() {
            for(Player player : getPlayers()) {
                removePlayer(player);
            }
        }

        public void teleportToWave(WaveType type, Player player) {
            if(type == WaveType.EGG) {
                player.teleport(EggWave);
            } else if(type == WaveType.WHEAT) {
                player.teleport(WheatWave);
            } else if(type == WaveType.PIGFOOD) {
                player.teleport(PigFeefWave);
            } else if (type == WaveType.SHEEPWALK) {
                player.teleport(SheepWave);
            } else if (type == WaveType.COWMILK) {
                player.teleport(CowMilkWave);
            } else if(type == WaveType.MOVERGRASS) {
                player.teleport(MoverGrass);
            }
        }

        public void teleportAllToWave() {
            for(int i = 0; i < players.size(); i ++) {
                Player player = players.get(i);
                teleportToWave(type, player);
            }
        }

        public ArrayList<Player> getAPlayers() {
            return players;
        }

        public static boolean isIngame(Player player) {
            if(getArena(player) == null) {
                return false;
            }

            for(Arena arena1 : arenas) {
                if(arena1.getAPlayers().contains(player)) return true;
            }
            return false;
        }
    }
     
     
  5. Use onPlayerJoin for the public void run, so start the scoreboard and show. Example:
    Code (Text):

    @EventHandler
    public void alEntrar(PlayerJoinEvent event){
    final Player p = event.getPlayer();
    this.startScoreBoardTask();
    if(!Bukkit.getScheduler().isCurrentlyRunning(sched)){
    sched = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    @Override
    public void run() {
    startScoreBoardTask();
    }
    }, 20, 20);
    }
    }
     
  6. @ToPoEdiTs it still does not show the ScoreBoard and there are no erros

    EDIT:
    Updated code:
    Code (Text):
    public void startScoreBoardTask(Player pl) {
            Bukkit.broadcastMessage("Player: " + pl.getName());
            final Player player = pl;
            scr = scoreboard;
            obj = objective;
            obj.setDisplayName("FarmMadness");
            obj.setDisplaySlot(DisplaySlot.SIDEBAR);
            Score p = obj.getScore(player.getName());
            scoreBoardtask = Bukkit.getScheduler().runTaskTimerAsynchronously(Farm.getPlugin(), new Runnable() {
                @Override
                public void run() {
                    player.setScoreboard(scr);
                }
            }, 0L, 20L);
        }
     
    #7 DaanSander, May 23, 2015
    Last edited: May 24, 2015