Solved Always running loop after server starts

Discussion in 'Spigot Plugin Development' started by JavaNaza, Jun 23, 2018.

  1. Hello everybody, I m trying to make always running loop after the plugin gets enabled. This is what I have
    but it crashes my server:

    Code (Java):
            Bukkit.getScheduler().scheduleSyncDelayedTask(Advertiser.plugin, new BukkitRunnable() {
                public void run() {
                    while (true) {
                        for (Player player : Bukkit.getOnlinePlayers()) {
                            for (Message message : messages) {
                                 if (!messagesOnCooldown.contains(message)) {
                                    for (HashMap.Entry<Integer, String> entry : message.getMessages().entrySet()) {

                }, 20 * 20);
    All methods are working but the problem is with loops.
    Thank you for your help and have a great day !
  2. Well, you are running a while-loop on the main thread of the server, what did you expect to happen? Can I ask what you are trying to achieve? If you just want a message to be sent every x minutes, you should use a repeating task build into spigot.
  3. Okay, so currently how the Minecraft server works, a single tick should be around one-twentieth of a second. When you do slightly more intensive workloads in a single tick (think setting off thousands of TNT blocks), the CPU and GPU just physically can't process all that data in that time limit, so the tick is extended. This extended tick is seen as server-side lag. So, by having a constant loop that has no exit, you're effectively trying to do an infinite number of things in a finite number of milliseconds, which is impossible. So that one tick continues forever, and the server sees itself as unresponsive and crashes. Therefore, you have to seperate your messages across multiple ticks to allow the server to complete each tick. Try just sending every player, say, 1 message every tick. you'd do this with a bukkit runnable timer, which is this format:
    Code (Java):
    Bukkit.getScheduler().runTaskTimer(this, () -> {
    // your code
  4. Thank you !! It worked.
    • Informative Informative x 1