Solved Timer increasing by 2 because 2 players in server

Discussion in 'Plugin Development' started by voltywolty, Oct 26, 2021.

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

    voltywolty

    I have a timer that is displayed on a scoreboard, but it increasing by x amount depending on how many players there are in the server. For example, timer increases by 1 due to one player, but 2 by two players, etc... I want it to increase by 1 no matter the amount of players. Here is my code
    Code (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.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);
    
                Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                    public void run() {
                        if (monstersReleasedFully) {
                            if (doomTimer != -1) {
                                doomTimer--;
    
                                if (doomTimer <= 0) {
                                    doomTimer = 0;
    
                                    Random randomDoomEvent = new Random();
                                    int randomDoom = randomDoomEvent.nextInt(25);
    
                                    if (randomDoom < 10) {
                                        DvZ.this.doomEvent = new GolemEvent(DvZ.this);
                                    }
                                    else if (randomDoom < 20) {
                                        DvZ.this.doomEvent = new DireWolvesEvent(DvZ.this);
                                    }
    
                                    doomEvent.run();
                                    Bukkit.getScheduler().runTaskLater(plugin, () -> players.sendTitle(ChatColor.RED + doomEvent.getName(), "", 20, 60, 30), 20L);
    
                                    doomTimer = 1200;
                                }
                            }
                        }
                        updateScoreboards();
                    }
                }, 20L, 20L);
    
                players.setScoreboard(scoreboard);
            }
        }
     
    Last edited: Oct 26, 2021
  2. Offline

    Dai_Kunai

    What's the question?
     
  3. Offline

    voltywolty

    My mistake, it’s meant to increase by one no matter the player size, not increase by the amount of players.
     
  4. Offline

    Dai_Kunai

    No no my mistake; I can't read...

    So your scheduler is inside the for loop, which means that it's creating a separate scheduler for every player, which means that timer is increasing by one for each player. The scheduler needs to be outside of the for loop, with the scoreboard updating possibly happening inside the scheduler, so you have scheduler doing stuff like adding 1 to time and adding that to scoreboard and then a simple for loop like:
    Code:
    for (Player p : Bukkit.getOnlinePlayers()) {
        p.setScoreboard(scoreboard);
    }
     
    Last edited: Oct 26, 2021
  5. Offline

    voltywolty

    I've done this, but it still increments by 2, can you tell me if this is right or wrong?
    Code (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.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);
            }
    
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                    totalTime++;
    
                    if (monstersReleasedFully) {
                        if (doomTimer != -1) {
                            doomTimer--;
    
                            if (doomTimer <= 0) {
                                doomTimer = 0;
    
                                Random randomDoomEvent = new Random();
                                int randomDoom = randomDoomEvent.nextInt(25);
    
                                if (randomDoom < 10) {
                                    DvZ.this.doomEvent = new GolemEvent(DvZ.this);
                                }
                                else if (randomDoom < 20) {
                                    DvZ.this.doomEvent = new DireWolvesEvent(DvZ.this);
                                }
    
                                doomEvent.run();
    
                                for (Player players : Bukkit.getOnlinePlayers())
                                    Bukkit.getScheduler().runTaskLater(plugin, () -> players.sendTitle(ChatColor.RED + doomEvent.getName(), "", 20, 60, 30), 20L);
    
                                doomTimer = 1200;
                            }
                        }
                    }
                    updateScoreboards();
                }
            }, 20L, 20L);
        }


    Nevermind, I had a different for statement for the initialize scoreboard that duplicated the scheduler for all players.
     
    Last edited: Oct 26, 2021
Thread Status:
Not open for further replies.

Share This Page