From b63c388523c6874bff9ac78fb774cb7f2ab1e0c2 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 1 May 2021 11:50:27 +0200 Subject: [PATCH] Add RamUsage --- .../bausystem/features/world/RamUsage.java | 39 +++++++++++++++++-- 1 file changed, 35 insertions(+), 4 deletions(-) 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 35f8c81a..ccbc9396 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/world/RamUsage.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/world/RamUsage.java @@ -19,16 +19,47 @@ package de.steamwar.bausystem.features.world; -import lombok.experimental.UtilityClass; +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.linkage.LinkageType; +import de.steamwar.bausystem.linkage.Linked; import org.bukkit.Bukkit; +import org.bukkit.boss.BarColor; +import org.bukkit.boss.BarStyle; +import org.bukkit.boss.BossBar; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; import java.lang.management.ManagementFactory; import java.util.logging.Level; -@UtilityClass -public class RamUsage { +@Linked(LinkageType.LISTENER) +public class RamUsage implements Listener { - public double getUsage() { + private BossBar bossBar; + + public RamUsage() { + bossBar = Bukkit.createBossBar("§eRAM", BarColor.YELLOW, BarStyle.SEGMENTED_10); + Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), () -> { + long allocatedMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); + long presumableFreeMemory = Runtime.getRuntime().maxMemory() - allocatedMemory; + double percentageFree = presumableFreeMemory / (double) Runtime.getRuntime().maxMemory(); + bossBar.setProgress(1 - percentageFree); + }, 1, 1); + } + + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) { + bossBar.addPlayer(event.getPlayer()); + } + + @EventHandler + public void onPlayerQuit(PlayerQuitEvent event) { + bossBar.removePlayer(event.getPlayer()); + } + + public static double getUsage() { try { long memorySize = ((com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean()).getTotalPhysicalMemorySize(); long freeMemory = ((com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean()).getFreePhysicalMemorySize();