Из-за BukkitRunnable не прогружается мир при входе на сервер

Baroness's Dev

Разработчик
Пользователь
Продолжение этой темы.
Создан класс с листенером, там же ивент onJoin, который исправно работает. В ивенте заложен следующий таск:
Java:
        new BukkitRunnable() {
            @Override
            public void run() {
                int clock = -1;
                while (true) {
                    clock++;
                    if (clock == 1) {
                        p.sendMessage(ChatColor.AQUA + "Вы играете " + ChatColor.GOLD + clock + ChatColor.AQUA + " час.");
                    }
                    if (clock == 2 || clock == 3 || clock == 4) {
                        p.sendMessage(ChatColor.AQUA + "Вы играете уже " + ChatColor.GOLD + clock + ChatColor.AQUA + " часа.");
                    }
                    if (clock >= 5 && clock <= 10 ) {
                        p.sendMessage(ChatColor.AQUA + "Вы играете уже " + ChatColor.GOLD + clock + ChatColor.AQUA + " часов.");
                    }
                    if (clock != 0) {
                        p.sendMessage(ChatColor.DARK_GREEN + "Не забывайте отдыхать.");
                    }
                    try {
                        Thread.sleep(3600000);
                    } catch (InterruptedException interruptedException) {
                        interruptedException.printStackTrace();
                    }
                }
            }
        }.runTask(Baroness.plugin);
Почему-то эта конструкция настолько нагружает сервер, что он зависает, а после вообще перестаёт отвечать. Я сделал какую-то ошибку? Может, есть другой способ реализовать это?
Авто объединение сообщений:

Отчёт при зависании:
Java:
[09:59:44] [Spigot Watchdog Thread/ERROR]: The server has stopped responding!
[09:59:44] [Spigot Watchdog Thread/ERROR]: Spigot version: git-CatServer-1.12.2-3440193 (MC: 1.12.2)
[09:59:44] [Spigot Watchdog Thread/ERROR]: ------------------------------
[09:59:44] [Spigot Watchdog Thread/ERROR]: Server thread dump (Look for plugins here before reporting to Spigot!):
[09:59:44] [Spigot Watchdog Thread/ERROR]: ------------------------------
[09:59:44] [Spigot Watchdog Thread/ERROR]: Current Thread: Server thread
[09:59:44] [Spigot Watchdog Thread/ERROR]:      PID: 21 | Suspended: false | Native: false | State: TIMED_WAITING
[09:59:44] [Spigot Watchdog Thread/ERROR]:      Stack:
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.lang.Thread.sleep(Native Method)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              com.baroness.handler$1.run(handler.java:52)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftTask.run(CraftTask.java:76)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:421)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              net.minecraft.server.MinecraftServer.func_71190_q(MinecraftServer.java:837)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              net.minecraft.server.dedicated.DedicatedServer.func_71190_q(DedicatedServer.java:472)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              net.minecraft.server.MinecraftServer.func_71217_p(MinecraftServer.java:782)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              net.minecraft.server.MinecraftServer.run(MinecraftServer.java:636)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.lang.Thread.run(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]: ------------------------------
[09:59:44] [Spigot Watchdog Thread/ERROR]: Entire Thread Dump:
[09:59:44] [Spigot Watchdog Thread/ERROR]: ------------------------------
[09:59:44] [Spigot Watchdog Thread/ERROR]: Current Thread: luckperms-scheduler-worker-12
[09:59:44] [Spigot Watchdog Thread/ERROR]:      PID: 133 | Suspended: false | Native: false | State: TIMED_WAITING
[09:59:44] [Spigot Watchdog Thread/ERROR]:      Stack:
[09:59:44] [Spigot Watchdog Thread/ERROR]:              sun.misc.Unsafe.park(Native Method)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.util.concurrent.locks.LockSupport.parkNanos(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.util.concurrent.SynchronousQueue$TransferStack.awaitFulfill(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.util.concurrent.SynchronousQueue$TransferStack.transfer(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.util.concurrent.SynchronousQueue.poll(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:      Stack:
[09:59:44] [Spigot Watchdog Thread/ERROR]:              sun.misc.Unsafe.park(Native Method)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.util.concurrent.locks.LockSupport.parkNanos(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.util.concurrent.ThreadPoolExecutor.getTask(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.lang.Thread.run(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]: ------------------------------
[09:59:44] [Spigot Watchdog Thread/ERROR]: Current Thread: etp1420097425-70
[09:59:44] [Spigot Watchdog Thread/ERROR]:      PID: 70 | Suspended: false | Native: true | State: RUNNABLE
[09:59:44] [Spigot Watchdog Thread/ERROR]:      Thread is waiting on monitor(s):
[09:59:44] [Spigot Watchdog Thread/ERROR]:              Locked on:sun.nio.ch.SelectorImpl.lockAndDoSelect(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              Locked on:sun.nio.ch.SelectorImpl.lockAndDoSelect(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              Locked on:sun.nio.ch.SelectorImpl.lockAndDoSelect(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:      Stack:
[09:59:44] [Spigot Watchdog Thread/ERROR]:              sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              sun.nio.ch.WindowsSelectorImpl$SubSelector.poll(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              sun.nio.ch.WindowsSelectorImpl$SubSelector.access$400(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              sun.nio.ch.WindowsSelectorImpl.doSelect(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              sun.nio.ch.SelectorImpl.lockAndDoSelect(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              sun.nio.ch.SelectorImpl.select(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              org.fusesource.jansi.internal.WindowsSupport.readConsoleInput(WindowsSupport.java:97)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              jline.WindowsTerminal.readConsoleInput(WindowsTerminal.java:214)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              jline.WindowsTerminal.access$000(WindowsTerminal.java:54)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              jline.WindowsTerminal$1.read(WindowsTerminal.java:156)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              jline.internal.NonBlockingInputStream.read(NonBlockingInputStream.java:166)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              jline.internal.NonBlockingInputStream.read(NonBlockingInputStream.java:135)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              jline.internal.NonBlockingInputStream.read(NonBlockingInputStream.java:243)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              jline.internal.InputStreamReader.read(InputStreamReader.java:257)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              jline.internal.InputStreamReader.read(InputStreamReader.java:194)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              jline.console.ConsoleReader.readCharacter(ConsoleReader.java:2136)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              jline.console.ConsoleReader.readCharacter(ConsoleReader.java:2126)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              jline.console.ConsoleReader.readBinding(ConsoleReader.java:2211)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              jline.console.ConsoleReader.readLine(ConsoleReader.java:2460)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              jline.console.ConsoleReader.readLine(ConsoleReader.java:2372)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              net.minecraft.server.dedicated.DedicatedServer$2.run(DedicatedServer.java:120)
[09:59:44] [Spigot Watchdog Thread/ERROR]: ------------------------------
[09:59:44] [Spigot Watchdog Thread/ERROR]: Current Thread: Thread-4
[09:59:44] [Spigot Watchdog Thread/ERROR]:      PID: 25 | Suspended: false | Native: false | State: RUNNABLE
[09:59:44] [Spigot Watchdog Thread/ERROR]:      Thread is waiting on monitor(s):
[09:59:44] [Spigot Watchdog Thread/ERROR]:              Locked on:java.io.BufferedOutputStream.flush(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              Locked on:java.io.PrintStream.flush(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              Locked on:java.io.PrintStream.flush(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              Locked on:java.io.PrintStream.flush(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              Locked on:sun.nio.cs.StreamEncoder.flush(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:      Stack:
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.io.FileOutputStream.writeBytes(Native Method)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.io.FileOutputStream.write(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.io.BufferedOutputStream.flushBuffer(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.io.BufferedOutputStream.flush(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.io.PrintStream.flush(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.io.PrintStream.flush(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.io.FilterOutputStream.flush(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.io.PrintStream.flush(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.io.FilterOutputStream.flush(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              sun.nio.cs.StreamEncoder.implFlush(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              sun.nio.cs.StreamEncoder.flush(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.io_OutputStreamWriter.flush(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              jline.console.ConsoleReader.flush(ConsoleReader.java:1032)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              org.bukkit.craftbukkit.v1_12_R1.util.TerminalConsoleWriterThread.run(TerminalConsoleWriterThread.java:48)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.lang.Thread.run(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]: ------------------------------
[09:59:44] [Spigot Watchdog Thread/ERROR]: Current Thread: DestroyJavaVM
[09:59:44] [Spigot Watchdog Thread/ERROR]:      PID: 23 | Suspended: false | Native: false | State: RUNNABLE
[09:59:44] [Spigot Watchdog Thread/ERROR]:      Stack:
[09:59:44] [Spigot Watchdog Thread/ERROR]: ------------------------------
[09:59:44] [Spigot Watchdog Thread/ERROR]: Current Thread: Server thread
[09:59:44] [Spigot Watchdog Thread/ERROR]:      PID: 21 | Suspended: false | Native: false | State: TIMED_WAITING
[09:59:44] [Spigot Watchdog Thread/ERROR]:      Stack:
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.lang.Thread.sleep(Native Method)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              com.baroness.handler$1.run(handler.java:52)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftTask.run(CraftTask.java:76)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:421)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              net.minecraft.server.MinecraftServer.func_71190_q(MinecraftServer.java:837)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              net.minecraft.server.dedicated.DedicatedServer.func_71190_q(DedicatedServer.java:472)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              net.minecraft.server.MinecraftServer.func_71217_p(MinecraftServer.java:782)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              net.minecraft.server.MinecraftServer.run(MinecraftServer.java:636)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.lang.Thread.run(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]: ------------------------------
[09:59:44] [Spigot Watchdog Thread/ERROR]: Current Thread: Server Infinisleeper
[09:59:44] [Spigot Watchdog Thread/ERROR]:      PID: 22 | Suspended: false | Native: false | State: TIMED_WAITING
[09:59:44] [Spigot Watchdog Thread/ERROR]:      Stack:
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.lang.Thread.sleep(Native Method)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              net.minecraft.server.dedicated.DedicatedServer$1.run(DedicatedServer.java:92)
[09:59:44] [Spigot Watchdog Thread/ERROR]: ------------------------------
[09:59:44] [Spigot Watchdog Thread/ERROR]: Current Thread: NonBlockingInputStreamThread
[09:59:44] [Spigot Watchdog Thread/ERROR]:      PID: 19 | Suspended: false | Native: false | State: WAITING
[09:59:44] [Spigot Watchdog Thread/ERROR]:      Stack:
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.lang.Object.wait(Native Method)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              jline.internal.NonBlockingInputStream.run(NonBlockingInputStream.java:275)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.lang.Thread.run(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]: ------------------------------
[09:59:44] [Spigot Watchdog Thread/ERROR]: Current Thread: Snooper Timer
[09:59:44] [Spigot Watchdog Thread/ERROR]:      PID: 17 | Suspended: false | Native: false | State: TIMED_WAITING
[09:59:44] [Spigot Watchdog Thread/ERROR]:      Stack:
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.lang.Object.wait(Native Method)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.util.TimerThread.mainLoop(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.util.TimerThread.run(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]: ------------------------------
[09:59:44] [Spigot Watchdog Thread/ERROR]: Current Thread: Attach Listener
[09:59:44] [Spigot Watchdog Thread/ERROR]:      PID: 5 | Suspended: false | Native: false | State: RUNNABLE
[09:59:44] [Spigot Watchdog Thread/ERROR]:      Stack:
[09:59:44] [Spigot Watchdog Thread/ERROR]: ------------------------------
[09:59:44] [Spigot Watchdog Thread/ERROR]: Current Thread: Signal Dispatcher
[09:59:44] [Spigot Watchdog Thread/ERROR]:      PID: 4 | Suspended: false | Native: false | State: RUNNABLE
[09:59:44] [Spigot Watchdog Thread/ERROR]:      Stack:
[09:59:44] [Spigot Watchdog Thread/ERROR]: ------------------------------
[09:59:44] [Spigot Watchdog Thread/ERROR]: Current Thread: Finalizer
[09:59:44] [Spigot Watchdog Thread/ERROR]:      PID: 3 | Suspended: false | Native: false | State: WAITING
[09:59:44] [Spigot Watchdog Thread/ERROR]:      Stack:
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.lang.Object.wait(Native Method)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.lang.ref.ReferenceQueue.remove(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.lang.ref.ReferenceQueue.remove(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.lang.ref.Finalizer$FinalizerThread.run(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]: ------------------------------
[09:59:44] [Spigot Watchdog Thread/ERROR]: Current Thread: Reference Handler
[09:59:44] [Spigot Watchdog Thread/ERROR]:      PID: 2 | Suspended: false | Native: false | State: WAITING
[09:59:44] [Spigot Watchdog Thread/ERROR]:      Stack:
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.lang.Object.wait(Native Method)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.lang.Object.wait(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.lang.ref.Reference.tryHandlePending(Unknown Source)
[09:59:44] [Spigot Watchdog Thread/ERROR]:              java.lang.ref.Reference$ReferenceHandler.run(Unknown Source)
 

BuseSo

Разработчик
Пользователь
Ну ясно почему зависает сервер. На 1.12 сервер и плагины работают в 1 потоке, поэтому нельзя использовать данный метод. Самый нормальный вариант .runTaskTimer();.
И тебя вообще не смущает, что даже когда игрок выйдет с сервера, таймер перестанет работать?
 

BuseSo

Разработчик
Пользователь
Таймер должен перезапускаться при каждом входе игрока.
Он не будет перезапускаться, всё время будет создавать новый и новый таймер.

Можете подсказать, как работает таймер?
Что именно?
Ты создаёшь таймер, он выполняет весь код в методе run(), потом через интервал времени опять повторяет, и опять, и до тех пор, пока его не остановят (например, методом cancel() или перезагрузкой сервера).
 

Baroness's Dev

Разработчик
Пользователь
Java:
public class handler implements Listener {

    @EventHandler
    public void join(PlayerJoinEvent e) throws InterruptedException {
        Player p = e.getPlayer();
        final int[] clock = {-1};
        new BukkitRunnable() {
            @Override
            public void run() {
              
                while (true) {
                    clock[0]++;
                    if (clock[0] == 1) {
                        p.sendMessage(ChatColor.AQUA + "Вы играете " + ChatColor.GOLD + clock[0] + ChatColor.AQUA + " час.");
                    }
                    if (clock[0] == 2 || clock[0] == 3 || clock[0] == 4) {
                        p.sendMessage(ChatColor.AQUA + "Вы играете уже " + ChatColor.GOLD + clock[0] + ChatColor.AQUA + " часа.");
                    }
                    if (clock[0] >= 5 && clock[0] <= 10 ) {
                        p.sendMessage(ChatColor.AQUA + "Вы играете уже " + ChatColor.GOLD + clock[0] + ChatColor.AQUA + " часов.");
                    }
                    if (clock[0] != 0) {
                        p.sendMessage(ChatColor.DARK_GREEN + "Не забывайте отдыхать.");
                    }
                }
            }
        }.runTaskTimer(Baroness.plugin, 0, 3600000);
Так правильно?
Авто объединение сообщений:

period хоть и 3600000, но оно моментально начинает считать почему то и не прекращается
 
Последнее редактирование:

BuseSo

Разработчик
Пользователь
Более-менее, но я бы поступил разумнее:
Можно хранить всех игроков и их время игры в HashMap<Player, Integer>,
А таймер сделать один, как я показывал в другой теме (*тык*), и просто запускать его в onEnable.
 

Baroness's Dev

Разработчик
Пользователь
У меня была ошибка: while true крайне плохая идея в таймере
Убрав его, я решил проблему, спасибо
Гляну на хэшмап
 

Комнаты в чате

Верх Низ