Scheduler Countdown: Handling a player leaving

Discussion in 'Plugin Development' started by AaronL98, Feb 13, 2014.

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

    AaronL98

    Hi. I am currently coding a minigame, and have a scheduler start as a countdown when there is the sufficient amount of players on the server. However i've noticed when a player leaves when the countdown is active, it just pauses, and spams an error in the console every 20L. I am using the xp level as a countdown.

    Code:java
    1.  
    2. ArrayList<String> allP = new ArrayList<String>();
    3. public int num = 30;
    4. public void run() {
    5. if (num != -1) {
    6. if (num != 0) {
    7. for (Player p : Bukkit.getOnlinePlayers()) {
    8. allP.add(p.getName());
    9. }
    10.  
    11. for (String pl : allP) {
    12. Player player = Bukkit.getPlayerExact(pl);
    13. player.setLevel(num); // Console points to this line in the error, probably because the player is offline.
    14. player.playSound(player.getLocation(),
    15. Sound.NOTE_PLING, 1, 1);
    16. }
    17.  
    18. num--;
    19. } else {
    20. if (num == 0) { // Do start stuff
     
  2. Offline

    xTigerRebornx

    AaronL98 Check if player != null before trying to use it
     
  3. Offline

    jthort

    AaronL98 I don't think you need a scheduler (but of course it's still possible), you can just listen to the join event and add a counter with an if statement checking if it reached a certain point

    Edit: same with the leave event, just subtract from the counter
     
  4. Offline

    bennie3211

    check if player is not null (player != null) statement before doing stuff with the player(s). Second, why do you add the players again (every 20 ticks (20L)) to the AllP arraylist? Just add/remove them on join/leave to the arraylist. Third, if this is a class that implements BukkitRunnable you can call the this.cancel(); to stop the scheduler.
     
Thread Status:
Not open for further replies.

Share This Page