Solved Countdown stops increasing by one when a player rejoins

Discussion in 'Plugin Development' started by voltywolty, Nov 19, 2021.

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

    voltywolty

    Hey everyone, I currently have a timer that increments by one overtime. However, when a player leaves and rejoins, the timer starts increasing by 2, then 3, then 4, every time a player leaves and rejoins. How can I fix this? Here is the code:

    Timer (open)

    Code:
    final BukkitTask timer = new BukkitRunnable() {
                public void run() {
                    if (!gameEnded) {
                        totalTime++;
                    }
                    else {
                        cancel();
                    }
                }
            }.runTaskTimer(this, 0L, 20L);

    Scoreboard Information (open)

    Code:
    public void initializeScoreboard() {
            for (Player players : Bukkit.getOnlinePlayers()) {
                this.manager = Bukkit.getScoreboardManager();
                this.scoreboard = manager.getMainScoreboard();
                this.objective = this.scoreboard.getObjective("Dwarves");
    
                if (this.objective == null) {
                    this.objective = this.scoreboard.registerNewObjective("Dwarves", "dummy", ChatColor.AQUA + "Dwarves");
                    this.objective.setDisplaySlot(DisplaySlot.SIDEBAR);
                }
    
                this.vaultScore = this.objective.getScore(ChatColor.GOLD + "Vault");
                this.vaultScore.setScore(totalVaultAmount);
                this.remainingDwarvesScore = this.objective.getScore(ChatColor.GREEN + "Remaining");
                this.remainingDwarvesScore.setScore(dwarves.size());
                this.timeScore = this.objective.getScore(ChatColor.LIGHT_PURPLE + "Time");
                this.timeScore.setScore(totalTime);
    
                players.setScoreboard(scoreboard);
            }
    
            final BukkitTask timer = new BukkitRunnable() {
                public void run() {
                    if (!gameEnded) {
                        totalTime++;
                    }
                    else {
                        cancel();
                    }
                }
            }.runTaskTimer(this, 0L, 20L);
    
            final BukkitTask doomTimerCountdown = new BukkitRunnable() {
                public void run() {
                    if (monstersReleasedFully) {
                        if (doomTimer != -1) {
                            doomTimer--;
    
                            if (doomTimer <= 0) {
                                doomTimer = 0;
    
                                doomEventStart();
                                cancel();
                            }
                        }
                    }
                }
            }.runTaskTimer(this, 0L, 20L);
    
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                    updateScoreboards();
                }
            }, 20L, 20L);
        }
     
  2. Offline

    Strahan

    You didn't post the whole class, so I can't say for certain but I assume you are calling the initialize scoreboard method when people join? If so, that's your problem. You're gonna be firing off multiple runnables that modify the timers. I.E. player 1 joins, it sets up the boards and fires off those three tasks (btw, why are you mixing task formats? that's weird). Then player 2 joins, it will fire the same three tasks now you have two instances of the 3 tasks running. Player 2 quits and rejoins, now you have three instances of the tasks all running simultaneously which results in a 3 count decrement every second.

    Stop creating runnables in something that gets called like that. Make a master runnable.
     
  3. Offline

    voltywolty

    Okay, I’ll see what I can do.
     
Thread Status:
Not open for further replies.

Share This Page