From 43af4d5432a009183d7d769a21e8dfaf014b02dd Mon Sep 17 00:00:00 2001 From: MrHua269 Date: Sat, 14 Dec 2024 17:04:11 +0800 Subject: [PATCH] Rewrote tpsbar and membar update logics --- patches/server/0008-Add-a-simple-tpsbar.patch | 122 ++++++++--------- patches/server/0009-Add-a-simple-membar.patch | 127 ++++++++---------- ...PI-and-display-of-chunkhot-in-tpsbar.patch | 6 +- 3 files changed, 115 insertions(+), 140 deletions(-) diff --git a/patches/server/0008-Add-a-simple-tpsbar.patch b/patches/server/0008-Add-a-simple-tpsbar.patch index 03625ab..d8057e3 100644 --- a/patches/server/0008-Add-a-simple-tpsbar.patch +++ b/patches/server/0008-Add-a-simple-tpsbar.patch @@ -117,15 +117,17 @@ index 0000000000000000000000000000000000000000..aafb2f5052c7c8e5971a47308253badb +} diff --git a/src/main/java/me/earthme/luminol/functions/GlobalServerTpsBar.java b/src/main/java/me/earthme/luminol/functions/GlobalServerTpsBar.java new file mode 100644 -index 0000000000000000000000000000000000000000..026807e0851d67c6d57e81f573ac1bf8fedc6109 +index 0000000000000000000000000000000000000000..ff503ab69dce8f034500e64ce31556d721395732 --- /dev/null +++ b/src/main/java/me/earthme/luminol/functions/GlobalServerTpsBar.java -@@ -0,0 +1,224 @@ +@@ -0,0 +1,214 @@ +package me.earthme.luminol.functions; + ++import com.google.common.collect.Maps; +import com.mojang.logging.LogUtils; +import io.papermc.paper.threadedregions.ThreadedRegionizer; +import io.papermc.paper.threadedregions.TickData; ++import io.papermc.paper.threadedregions.TickRegionScheduler; +import io.papermc.paper.threadedregions.TickRegions; +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; +import me.earthme.luminol.config.modules.misc.TpsBarConfig; @@ -134,8 +136,6 @@ index 0000000000000000000000000000000000000000..026807e0851d67c6d57e81f573ac1bf8 +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; -+import net.minecraft.server.level.ServerLevel; -+import net.minecraft.server.level.ServerPlayer; +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.entity.CraftPlayer; +import org.bukkit.entity.Player; @@ -146,46 +146,37 @@ index 0000000000000000000000000000000000000000..026807e0851d67c6d57e81f573ac1bf8 + +public class GlobalServerTpsBar { + protected static final NullPlugin NULL_PLUGIN = new NullPlugin(); -+ protected static final Map uuid2Bossbars = new HashMap<>(); -+ protected static volatile ScheduledTask tpsbarTask = null; ++ protected static final Map uuid2Bossbars = Maps.newConcurrentMap(); ++ protected static final Map scheduledTasks = new HashMap<>(); ++ ++ protected static volatile ScheduledTask scannerTask = null; + private static final Logger logger = LogUtils.getLogger(); + + public static void init(){ + cancelBarUpdateTask(); + -+ Bukkit.getGlobalRegionScheduler().runAtFixedRate(NULL_PLUGIN,c -> { -+ tpsbarTask = c; ++ scannerTask = Bukkit.getGlobalRegionScheduler().runAtFixedRate(NULL_PLUGIN, unused -> { + try { + update(); ++ cleanUp(); + }catch (Exception e){ + logger.error(e.getLocalizedMessage()); + } -+ },1,TpsBarConfig.updateInterval); -+ } -+ -+ public static void removeAllBars(){ -+ for (Map.Entry barEntry : uuid2Bossbars.entrySet()){ -+ final UUID playerUUID = barEntry.getKey(); -+ final BossBar tpsBar = barEntry.getValue(); -+ -+ final Player targetPlayer = Bukkit.getPlayer(playerUUID);{ -+ if (targetPlayer != null){ -+ targetPlayer.hideBossBar(tpsBar); -+ } -+ } -+ } -+ -+ uuid2Bossbars.clear(); ++ }, 1, TpsBarConfig.updateInterval); + } + + public static void cancelBarUpdateTask(){ -+ removeAllBars(); -+ -+ if (tpsbarTask == null || tpsbarTask.isCancelled()){ ++ if (scannerTask == null || scannerTask.isCancelled()){ + return; + } + -+ tpsbarTask.cancel(); ++ scannerTask.cancel(); ++ ++ for (ScheduledTask task : scheduledTasks.values()) { ++ if (!task.isCancelled()) { ++ task.cancel(); ++ } ++ } + } + + public static boolean isPlayerVisible(Player player){ @@ -197,63 +188,62 @@ index 0000000000000000000000000000000000000000..026807e0851d67c6d57e81f573ac1bf8 + } + + private static void update(){ -+ updateBarValues(); -+ cleanUpPlayers(); ++ for (Player player : Bukkit.getOnlinePlayers()) { ++ scheduledTasks.computeIfAbsent(player.getUniqueId(), unused -> createBossBarForPlayer(player)); ++ } + } + -+ private static void cleanUpPlayers(){ -+ final List toRemove = new ArrayList<>(); -+ -+ for (Map.Entry bossBarEntry : uuid2Bossbars.entrySet()){ -+ final UUID uuid = bossBarEntry.getKey(); -+ boolean shouldRemove = true; -+ -+ final Player target = Bukkit.getPlayer(uuid); -+ if (target != null){ -+ shouldRemove = !isPlayerVisible(target); -+ } ++ private static void cleanUp() { ++ final List toCleanUp = new ArrayList<>(); + -+ if (shouldRemove){ -+ toRemove.add(uuid); ++ for (Map.Entry toCheck : scheduledTasks.entrySet()) { ++ if (toCheck.getValue().isCancelled()) { ++ toCleanUp.add(toCheck.getKey()); + } + } + -+ for (UUID uuid : toRemove){ -+ final BossBar removed = uuid2Bossbars.remove(uuid); -+ if (removed != null){ -+ final Player targetPlayer = Bukkit.getPlayer(uuid); -+ if (targetPlayer != null){ -+ targetPlayer.hideBossBar(removed); -+ } -+ } ++ for (UUID uuid : toCleanUp) { ++ scheduledTasks.remove(uuid); + } + } + -+ private static void updateBarValues(){ -+ for (Player apiPlayer : Bukkit.getOnlinePlayers()){ -+ final ServerPlayer nmsPlayer = ((CraftPlayer) apiPlayer).getHandle(); -+ final ThreadedRegionizer.ThreadedRegion region = ((ServerLevel) nmsPlayer.level()).regioniser.getRegionAtUnsynchronised(nmsPlayer.moonrise$getSectionX(),nmsPlayer.moonrise$getSectionZ()); ++ public static ScheduledTask createBossBarForPlayer(@NotNull Player apiPlayer) { ++ final UUID playerUUID = apiPlayer.getUniqueId(); ++ ++ return apiPlayer.getScheduler().runAtFixedRate(NULL_PLUGIN, (n) -> { ++ if (!isPlayerVisible(apiPlayer)) { ++ final BossBar removed = uuid2Bossbars.remove(playerUUID); + -+ if (region == null){ -+ continue; ++ if (removed != null) { ++ apiPlayer.hideBossBar(removed); ++ } ++ return; + } + ++ final ThreadedRegionizer.ThreadedRegion region = TickRegionScheduler.getCurrentRegion(); + final TickData.TickReportData reportData = region.getData().getRegionSchedulingHandle().getTickReport5s(System.nanoTime()); + -+ BossBar targetBossbar = uuid2Bossbars.get(nmsPlayer.getUUID()); + -+ if (targetBossbar == null && isPlayerVisible(apiPlayer)){ -+ targetBossbar = BossBar.bossBar(Component.text(""),0.0F, BossBar.Color.valueOf(TpsBarConfig.tpsColors.get(3)), BossBar.Overlay.NOTCHED_20); -+ uuid2Bossbars.put(nmsPlayer.getUUID(),targetBossbar); -+ apiPlayer.showBossBar(targetBossbar); -+ } ++ BossBar targetBossbar = uuid2Bossbars.computeIfAbsent( ++ playerUUID, ++ unused -> BossBar.bossBar(Component.text(""),0.0F, BossBar.Color.valueOf(TpsBarConfig.tpsColors.get(3)), BossBar.Overlay.NOTCHED_20) ++ ); ++ ++ apiPlayer.showBossBar(targetBossbar); + -+ if (reportData != null && targetBossbar != null){ ++ if (reportData != null){ + final TickData.SegmentData tpsData = reportData.tpsData().segmentAll(); + final double mspt = reportData.timePerTickData().segmentAll().average() / 1.0E6; -+ updateTpsBar(tpsData.average(),mspt,targetBossbar,apiPlayer); ++ ++ updateTpsBar(tpsData.average(), mspt, targetBossbar, apiPlayer); + } -+ } ++ }, () -> { ++ final BossBar removed = uuid2Bossbars.remove(playerUUID); // Auto clean up it ++ ++ if (removed != null) { ++ apiPlayer.hideBossBar(removed); ++ } ++ }, 0, TpsBarConfig.updateInterval); + } + + private static void updateTpsBar(double tps, double mspt, @NotNull BossBar bar, @NotNull Player player){ diff --git a/patches/server/0009-Add-a-simple-membar.patch b/patches/server/0009-Add-a-simple-membar.patch index 3e4fcf1..b3fd9b3 100644 --- a/patches/server/0009-Add-a-simple-membar.patch +++ b/patches/server/0009-Add-a-simple-membar.patch @@ -115,15 +115,14 @@ index 0000000000000000000000000000000000000000..b632c4a636974535bf001f010de1dcb6 +} diff --git a/src/main/java/me/earthme/luminol/functions/GlobalServerMemoryBar.java b/src/main/java/me/earthme/luminol/functions/GlobalServerMemoryBar.java new file mode 100644 -index 0000000000000000000000000000000000000000..47809d78392fe0cb30ad68ca1c331784f1843269 +index 0000000000000000000000000000000000000000..4827562a5b4deca0153995b8e1680d820123a712 --- /dev/null +++ b/src/main/java/me/earthme/luminol/functions/GlobalServerMemoryBar.java -@@ -0,0 +1,186 @@ +@@ -0,0 +1,171 @@ +package me.earthme.luminol.functions; + ++import com.google.common.collect.Maps; +import com.mojang.logging.LogUtils; -+import io.papermc.paper.threadedregions.ThreadedRegionizer; -+import io.papermc.paper.threadedregions.TickRegions; +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; +import me.earthme.luminol.config.modules.misc.MembarConfig; +import me.earthme.luminol.utils.NullPlugin; @@ -131,8 +130,6 @@ index 0000000000000000000000000000000000000000..47809d78392fe0cb30ad68ca1c331784 +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; -+import net.minecraft.server.level.ServerLevel; -+import net.minecraft.server.level.ServerPlayer; +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.entity.CraftPlayer; +import org.bukkit.entity.Player; @@ -145,46 +142,36 @@ index 0000000000000000000000000000000000000000..47809d78392fe0cb30ad68ca1c331784 + +public class GlobalServerMemoryBar { + protected static final NullPlugin NULL_PLUGIN = new NullPlugin(); -+ protected static final Map uuid2Bossbars = new HashMap<>(); -+ protected static volatile ScheduledTask membarTask = null; ++ protected static final Map uuid2Bossbars = Maps.newConcurrentMap(); ++ protected static final Map scheduledTasks = new HashMap<>(); ++ protected static volatile ScheduledTask scannerTask = null; + private static final Logger logger = LogUtils.getLogger(); + + public static void init(){ + cancelBarUpdateTask(); + -+ Bukkit.getGlobalRegionScheduler().runAtFixedRate(NULL_PLUGIN, c -> { -+ membarTask = c; ++ scannerTask = Bukkit.getGlobalRegionScheduler().runAtFixedRate(NULL_PLUGIN, unused -> { + try { + update(); + }catch (Exception e){ + logger.error(e.getLocalizedMessage()); + } -+ },1, MembarConfig.updateInterval); ++ }, 1, MembarConfig.updateInterval); + } + -+ public static void removeAllBars(){ -+ for (Map.Entry barEntry : uuid2Bossbars.entrySet()){ -+ final UUID playerUUID = barEntry.getKey(); -+ final BossBar memBar = barEntry.getValue(); -+ -+ final Player targetPlayer = Bukkit.getPlayer(playerUUID);{ -+ if (targetPlayer != null){ -+ targetPlayer.hideBossBar(memBar); -+ } -+ } -+ } -+ -+ uuid2Bossbars.clear(); -+ } + + public static void cancelBarUpdateTask(){ -+ removeAllBars(); -+ -+ if (membarTask == null || membarTask.isCancelled()){ ++ if (scannerTask == null || scannerTask.isCancelled()){ + return; + } + -+ membarTask.cancel(); ++ scannerTask.cancel(); ++ ++ for (ScheduledTask task : scheduledTasks.values()) { ++ if (!task.isCancelled()) { ++ task.cancel(); ++ } ++ } + } + + public static boolean isPlayerVisible(Player player){ @@ -196,66 +183,64 @@ index 0000000000000000000000000000000000000000..47809d78392fe0cb30ad68ca1c331784 + } + + private static void update(){ -+ updateBarValues(); -+ cleanUpPlayers(); ++ doUpdate(); ++ cleanUp(); + } + -+ private static void cleanUpPlayers(){ -+ final List toRemove = new ArrayList<>(); -+ -+ for (Map.Entry bossBarEntry : uuid2Bossbars.entrySet()){ -+ final UUID uuid = bossBarEntry.getKey(); -+ boolean shouldRemove = true; ++ private static void cleanUp(){ ++ final List toCleanUp = new ArrayList<>(); + -+ final Player target = Bukkit.getPlayer(uuid); -+ if (target != null){ -+ shouldRemove = !isPlayerVisible(target); ++ for (Map.Entry toCheck : scheduledTasks.entrySet()) { ++ if (toCheck.getValue().isCancelled()) { ++ toCleanUp.add(toCheck.getKey()); + } ++ } + -+ if (shouldRemove){ -+ toRemove.add(uuid); -+ } ++ for (UUID uuid : toCleanUp) { ++ scheduledTasks.remove(uuid); + } ++ } + -+ for (UUID uuid : toRemove){ -+ final BossBar removed = uuid2Bossbars.remove(uuid); -+ if (removed != null){ -+ final Player targetPlayer = Bukkit.getPlayer(uuid); -+ if (targetPlayer != null){ -+ targetPlayer.hideBossBar(removed); -+ } -+ } ++ private static void doUpdate(){ ++ for (Player player : Bukkit.getOnlinePlayers()) { ++ scheduledTasks.computeIfAbsent(player.getUniqueId(), unused -> createBossBarForPlayer(player)); + } + } + -+ private static void updateBarValues(){ -+ MemoryUsage heap = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); ++ private static ScheduledTask createBossBarForPlayer(Player apiPlayer) { ++ return apiPlayer.getScheduler().runAtFixedRate(NULL_PLUGIN, (unused) -> { ++ final UUID playerUUID = apiPlayer.getUniqueId(); + -+ long used = heap.getUsed(); -+ long xmx = heap.getMax(); ++ if (!isPlayerVisible(apiPlayer)) { ++ final BossBar removed = uuid2Bossbars.remove(playerUUID); + ++ if (removed != null) { ++ apiPlayer.hideBossBar(removed); ++ } + ++ return; ++ } + -+ for (Player apiPlayer : Bukkit.getOnlinePlayers()){ -+ final ServerPlayer nmsPlayer = ((CraftPlayer) apiPlayer).getHandle(); -+ final ThreadedRegionizer.ThreadedRegion region = ((ServerLevel) nmsPlayer.level()).regioniser.getRegionAtUnsynchronised(nmsPlayer.moonrise$getSectionX(),nmsPlayer.moonrise$getSectionZ()); ++ MemoryUsage heap = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); + -+ if (region == null){ -+ continue; -+ } ++ long used = heap.getUsed(); ++ long xmx = heap.getMax(); + -+ BossBar targetBossbar = uuid2Bossbars.get(nmsPlayer.getUUID()); ++ BossBar targetBossbar = uuid2Bossbars.computeIfAbsent( ++ playerUUID, ++ (unused1) -> BossBar.bossBar(Component.text(""),0.0F, BossBar.Color.valueOf(MembarConfig.memColors.get(3)), BossBar.Overlay.NOTCHED_20) ++ ); + -+ if (targetBossbar == null && isPlayerVisible(apiPlayer)){ -+ targetBossbar = BossBar.bossBar(Component.text(""),0.0F, BossBar.Color.valueOf(MembarConfig.memColors.get(3)), BossBar.Overlay.NOTCHED_20); -+ uuid2Bossbars.put(nmsPlayer.getUUID(),targetBossbar); -+ apiPlayer.showBossBar(targetBossbar); -+ } ++ apiPlayer.showBossBar(targetBossbar); + -+ if (targetBossbar != null){ -+ updateMembar(targetBossbar,used,xmx); ++ updateMembar(targetBossbar, used, xmx); ++ }, () -> { ++ final BossBar removed = uuid2Bossbars.remove(apiPlayer.getUniqueId()); ++ ++ if (removed != null) { ++ apiPlayer.hideBossBar(removed); + } -+ } ++ }, 0, MembarConfig.updateInterval); + } + + private static void updateMembar(@NotNull BossBar bar, long used, long xmx){ @@ -276,7 +261,7 @@ index 0000000000000000000000000000000000000000..47809d78392fe0cb30ad68ca1c331784 + final String content = "<%s>"; + final String replaced = String.format(content,colorString,colorString); + -+ return MiniMessage.miniMessage().deserialize(replaced,Placeholder.parsed("text", String.format("%.2f", (double)max / (1024 * 1024)))); ++ return MiniMessage.miniMessage().deserialize(replaced,Placeholder.parsed("text", String.format("%.2f", max / (1024 * 1024)))); + } + + private static @NotNull Component getMemoryComponent(long used,long max){ diff --git a/patches/server/0045-KioCG-Chunk-API-and-display-of-chunkhot-in-tpsbar.patch b/patches/server/0045-KioCG-Chunk-API-and-display-of-chunkhot-in-tpsbar.patch index 9859dd8..a603852 100644 --- a/patches/server/0045-KioCG-Chunk-API-and-display-of-chunkhot-in-tpsbar.patch +++ b/patches/server/0045-KioCG-Chunk-API-and-display-of-chunkhot-in-tpsbar.patch @@ -132,10 +132,10 @@ index aafb2f5052c7c8e5971a47308253badb3027093c..9fe7ac7ba83bbcc9a2a851a5cace4764 public static int updateInterval = 15; diff --git a/src/main/java/me/earthme/luminol/functions/GlobalServerTpsBar.java b/src/main/java/me/earthme/luminol/functions/GlobalServerTpsBar.java -index 026807e0851d67c6d57e81f573ac1bf8fedc6109..c45f6bd60d1cf7a915aa6ceea07c0929507b86e2 100644 +index ff503ab69dce8f034500e64ce31556d721395732..fab8c7f1f527e8a9986cdbeab473a4c389a87587 100644 --- a/src/main/java/me/earthme/luminol/functions/GlobalServerTpsBar.java +++ b/src/main/java/me/earthme/luminol/functions/GlobalServerTpsBar.java -@@ -138,7 +138,8 @@ public class GlobalServerTpsBar { +@@ -128,7 +128,8 @@ public class GlobalServerTpsBar { TpsBarConfig.tpsBarFormat, Placeholder.component("tps",getTpsComponent(tps)), Placeholder.component("mspt",getMsptComponent(mspt)), @@ -145,7 +145,7 @@ index 026807e0851d67c6d57e81f573ac1bf8fedc6109..c45f6bd60d1cf7a915aa6ceea07c0929 )); bar.color(barColorFromTps(tps)); bar.progress((float) Math.min((float)1,Math.max(mspt / 50,0))); -@@ -180,6 +181,32 @@ public class GlobalServerTpsBar { +@@ -170,6 +171,32 @@ public class GlobalServerTpsBar { return MiniMessage.miniMessage().deserialize(replaced,Placeholder.parsed("text", String.format("%.2f", mspt))); }