Solved Bukkit Runnable not working properly

Discussion in 'Spigot Plugin Development' started by Henrik, Jun 4, 2017.

  1. I wanted to create a scoreboard with a timer on it. As you can see in the code below, the time the runnable is using is the int outside of the methods. When I send the scoreboard the first time when I run the plugin, everything works perfectly, but the second time it is stuck on the first second, and doesn't update at all. Do I have to cancel the runnable when I don't want the scoreboard to show to make it work the next time I send it, and if so how do I cancel it? I have tried different things with taskIds, but no luck there... What am I doing wrong?

    Code (Text):
    int lobbytime = 29;

        private Plugin plugin;

        public Scoreboards(Plugin plugin) {
            this.plugin = plugin;
        }

        public void lobbyBoard() {
            Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
            Objective obj = board.registerNewObjective("noflicker", "dummy");
            obj.setDisplaySlot(DisplaySlot.SIDEBAR);
            obj.setDisplayName("§e§lDEATH TAG");
       
            Score map = obj.getScore("§fPlayers");
            map.setScore(11);
       
            Score mapname1 = obj.getScore("§f§a" + plugin.getServer().getOnlinePlayers().size());
            mapname1.setScore(10);
       
            Score spacer3 = obj.getScore("§d");
            spacer3.setScore(9);
       
            Score runners = obj.getScore("§fRank");
            runners.setScore(8);
       
            for(Player player : Bukkit.getOnlinePlayers()) {
                String uuid = player.getUniqueId().toString();
                Score runnerscount = obj.getScore("§r§a" + plugin.getConfig().getString("Players." + uuid + ".Rank"));
                runnerscount.setScore(7);
            }
       
            Score spacer2 = obj.getScore("§e");
            spacer2.setScore(6);
       
            Score chasers = obj.getScore("§fCoins");
            chasers.setScore(5);
       
            for(Player player : Bukkit.getOnlinePlayers()) {
                String uuid = player.getUniqueId().toString();
                Score chaserscount = obj.getScore("§a" + plugin.getConfig().getInt("Players." + uuid + ".Coins"));
                chaserscount.setScore(4);
            }
       
            Score space = obj.getScore("§f");
            space.setScore(3);
       
            Score timeleft = obj.getScore("§fStarting In");
            timeleft.setScore(2);
       
            Team timer = board.registerNewTeam("timer");
            timer.addEntry(ChatColor.RED.toString());
            timer.setPrefix(ChatColor.GREEN + "");
            timer.setSuffix(ChatColor.GREEN + "§a30 Seconds");
       
            obj.getScore(ChatColor.RED.toString()).setScore(1);
       
            new BukkitRunnable() {
                @Override
                public void run() {
               
                    int seconds = (int) lobbytime % 60;
               
                    lobbytime --;
               
                    board.getTeam("timer").setSuffix(String.valueOf(ChatColor.GREEN) + "§a" + seconds + " §aSeconds");
                }
            }.runTaskTimer(plugin, 20, 20);
       
            for(Player player : Bukkit.getOnlinePlayers()) {
                player.setScoreboard(board);
            }
        }



    EDIT: I got it to work by doing a cancel of the runnable when it was done.
     
    #1 Henrik, Jun 4, 2017
    Last edited: Jun 4, 2017
  2. inside the runnable do cancel();