Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
Ursprung
8a5c3b1647
Commit
567f88c916
@ -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
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren