From 567f88c916dbe6efddd3b48611fabae8310565d3 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 16 Jun 2023 19:40:09 +0200 Subject: [PATCH] Update RamUsage Signed-off-by: yoyosource --- .../features/world/AFKStopperListener.java | 38 +++++++++---------- .../bausystem/features/world/RamUsage.java | 28 ++++++++++++++ 2 files changed, 45 insertions(+), 21 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/world/AFKStopperListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/world/AFKStopperListener.java index 799ccba8..ec539714 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/world/AFKStopperListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/world/AFKStopperListener.java @@ -36,31 +36,27 @@ public class AFKStopperListener implements Listener { private int minutesAfk = 0; public AFKStopperListener() { - Bukkit.getScheduler().runTaskTimerAsynchronously(BauSystem.getInstance(), () -> { - double load = RamUsage.getLoad(); - double usage = RamUsage.getUsage(); + Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), () -> { // System.out.println("CPU: " + load + " RAM: " + usage); - if (load < 50.0 && usage < 0.6) { + if (RamUsage.getLoad() < 50.0 && RamUsage.getUsage() < 0.6) { minutesAfk = 0; return; } - Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> { - switch (minutesAfk) { - case 5: - if (Bukkit.getOnlinePlayers().isEmpty()) { - Bukkit.shutdown(); - return; - } - for (Player p : Bukkit.getOnlinePlayers()) { - p.kickPlayer(BauSystem.MESSAGE.parse("AFK_KICK_MESSAGE", p)); - } - break; - case 4: - BauSystem.MESSAGE.broadcast("AFK_WARNING_MESSAGE"); - default: - minutesAfk++; - } - }, 1); + switch (minutesAfk) { + case 5: + if (Bukkit.getOnlinePlayers().isEmpty()) { + Bukkit.shutdown(); + return; + } + for (Player p : Bukkit.getOnlinePlayers()) { + p.kickPlayer(BauSystem.MESSAGE.parse("AFK_KICK_MESSAGE", p)); + } + break; + case 4: + BauSystem.MESSAGE.broadcast("AFK_WARNING_MESSAGE"); + default: + minutesAfk++; + } }, 1200, 1200); //every minute } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/world/RamUsage.java b/BauSystem_Main/src/de/steamwar/bausystem/features/world/RamUsage.java index 9f5a1fcd..711a574c 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/world/RamUsage.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/world/RamUsage.java @@ -28,7 +28,31 @@ public class RamUsage { private File meminfo = new File("/proc/meminfo"); + private double usage = 0D; + private double load = 0D; + + static { + Thread thread = new Thread(() -> { + while (true) { + usage = _getUsage(); + load = _getLoad(); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + }); + thread.setDaemon(true); + thread.setName("RamUsage"); + thread.start(); + } + public static double getUsage() { + return usage; + } + + private static double _getUsage() { try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(meminfo)))) { String memTotal = bufferedReader.readLine().replaceAll(" +", " "); bufferedReader.readLine(); @@ -43,6 +67,10 @@ public class RamUsage { } public static double getLoad() { + return load; + } + + private static double _getLoad() { try { Process process = new ProcessBuilder("bash", "-c", "cat <(grep 'cpu ' /proc/stat) <(sleep 1 && grep 'cpu ' /proc/stat) | awk -v RS=\"\" '{printf \"%.2f\\n\", ($13-$2+$15-$4)*100/($13-$2+$15-$4+$16-$5)}'").start(); process.waitFor();