Scoreboard Countdown doesn't work

Discussion in 'Plugin Development' started by Code4Life, Jan 17, 2016.

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

    Code4Life

    Hello guys,

    I just started making some plugins for Bukkit. But when I tries to make a Countdown in a Scoreboard I got a bug in my code. Here is my code will explain more down below.

    Code:
    public class JoinBoard implements Listener{
    
      
      
        public static Main plugin;
      
        public JoinBoard(Main instance){
            plugin = instance;
        }
      
        public static int count = 121;
      
        public static int timer;
      
        public static Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
        public static Objective ob = board.registerNewObjective("PlayerScoreboard", "dummy");
      
      
        public static void updateScoreboard(final Player p){
          
      
        ob.setDisplaySlot(DisplaySlot.SIDEBAR);
        ob.setDisplayName(ChatColor.GREEN + "[" + ChatColor.RED + "Pizza Place" + ChatColor.GREEN + "]");
      
      
      
        timer = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
      
        @Override
        public void run() {
        count --;
      
        for(String entry : board.getEntries()){
            board.resetScores(entry);
        }
      
        Score score4 = ob.getScore(" ");
        score4.setScore(4);
      
        Score score = ob.getScore(ChatColor.GRAY + "Players: " + Bukkit.getServer().getOnlinePlayers().size() + "/6");
        score.setScore(3);
      
        Score score1 = ob.getScore("  ");
        score1.setScore(2);
      
        Score score2 = ob.getScore(ChatColor.GRAY + "Time: " + count + "s");
        score2.setScore(1);
      
      
        if(count == 30){
            Bukkit.broadcastMessage(count+ " seconds remaining!");
        }else if(count <= 10){
            p.playSound(p.getLocation(), Sound.SUCCESSFUL_HIT, 10, 1);
            Bukkit.broadcastMessage(count+ " seconds remaining!");
        }else if(count == 0) {
            p.playSound(p.getLocation(), Sound.SUCCESSFUL_HIT, 10, 1);
            Bukkit.getServer().getScheduler().cancelTask(timer);
      
        if(Bukkit.getServer().getOnlinePlayers().size() == 6){
            Bukkit.broadcastMessage("Fck Yeah!");
        }else{
          
          
        }
        }
        }
        }, 0L, 20L);
              
        }
      
      
        @EventHandler
        public void onJoin(PlayerJoinEvent e){
        Player p = e.getPlayer();
          
        if(Bukkit.getServer().getScheduler().isCurrentlyRunning(timer)){
          
        }else{
            p.setScoreboard(board);
            updateScoreboard(p);
        }
        }
    
    }
    
    So the countdown works but when I relog on my server the countdown is running twice. I tried everything but I couldn't fix it. Hopefully you guys can help my. And yes I know my code is shit but i'm just trying.
     
  2. Offline

    Zombie_Striker

    @Code4Life
    First, make "plugin" private and remove the static.

    This is static abuse. Please stop making everything static and create getters and setters.
    MAIN PROBLEM: you do not fully understand Static/ are misusing static. Remove the static modifiers and it will fix your problem.
     
  3. Offline

    Code4Life

    @Zombie_Striker Thanks for your reply but it still doesn't work I replaced everything to private but when I relog it still runs twice.
     
  4. Offline

    Flaps

Thread Status:
Not open for further replies.

Share This Page