Is this Method okay?

Discussion in 'Spigot Plugin Development' started by Zarlen, May 4, 2017.

  1. Code (Text):
    @EventHandler
    public void sendLobbyLores(final PlayerJoinEvent event) {
    Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() { public void run() {
    switch(ThreadLocalRandom.current().nextInt(4)) {
    case 0:
    // play sound
    break;
    case 1:
    // play another sound
    break;
    case 2:
    // play another sound
    break;
    case 3:
    // play another sound
    break;
    }
    }
    }, 20L, 100L);
    }
    Is this okay? Won't duplicate this more tasks? Or will the task get deleted when you leave and rejoin?
     
  2. No, this will definitely create more and more tasks. You should not be doing it like that. I would personally create a single repeating runnable, and add the players that joined into a list for example. The runnable would then loop through the players from the list instead of directly from joining, preventing you from creating many runnables.
     
  3. Yep, you're right. Thanks.
     
    • Like Like x 1