Skip to content

Commit

Permalink
Rewrote tpsbar and membar update logics
Browse files Browse the repository at this point in the history
  • Loading branch information
MrHua269 committed Dec 14, 2024
1 parent b428505 commit 43af4d5
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 140 deletions.
122 changes: 56 additions & 66 deletions patches/server/0008-Add-a-simple-tpsbar.patch
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -146,46 +146,37 @@ index 0000000000000000000000000000000000000000..026807e0851d67c6d57e81f573ac1bf8
+
+public class GlobalServerTpsBar {
+ protected static final NullPlugin NULL_PLUGIN = new NullPlugin();
+ protected static final Map<UUID,BossBar> uuid2Bossbars = new HashMap<>();
+ protected static volatile ScheduledTask tpsbarTask = null;
+ protected static final Map<UUID, BossBar> uuid2Bossbars = Maps.newConcurrentMap();
+ protected static final Map<UUID, ScheduledTask> 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<UUID,BossBar> 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){
Expand All @@ -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<UUID> toRemove = new ArrayList<>();
+
+ for (Map.Entry<UUID,BossBar> 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<UUID> toCleanUp = new ArrayList<>();
+
+ if (shouldRemove){
+ toRemove.add(uuid);
+ for (Map.Entry<UUID, ScheduledTask> 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<TickRegions.TickRegionData, TickRegions.TickRegionSectionData> 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<TickRegions.TickRegionData, TickRegions.TickRegionSectionData> 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){
Expand Down
127 changes: 56 additions & 71 deletions patches/server/0009-Add-a-simple-membar.patch
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,21 @@ 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;
+import net.kyori.adventure.bossbar.BossBar;
+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;
Expand All @@ -145,46 +142,36 @@ index 0000000000000000000000000000000000000000..47809d78392fe0cb30ad68ca1c331784
+
+public class GlobalServerMemoryBar {
+ protected static final NullPlugin NULL_PLUGIN = new NullPlugin();
+ protected static final Map<UUID, BossBar> uuid2Bossbars = new HashMap<>();
+ protected static volatile ScheduledTask membarTask = null;
+ protected static final Map<UUID, BossBar> uuid2Bossbars = Maps.newConcurrentMap();
+ protected static final Map<UUID, ScheduledTask> 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<UUID,BossBar> 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){
Expand All @@ -196,66 +183,64 @@ index 0000000000000000000000000000000000000000..47809d78392fe0cb30ad68ca1c331784
+ }
+
+ private static void update(){
+ updateBarValues();
+ cleanUpPlayers();
+ doUpdate();
+ cleanUp();
+ }
+
+ private static void cleanUpPlayers(){
+ final List<UUID> toRemove = new ArrayList<>();
+
+ for (Map.Entry<UUID,BossBar> bossBarEntry : uuid2Bossbars.entrySet()){
+ final UUID uuid = bossBarEntry.getKey();
+ boolean shouldRemove = true;
+ private static void cleanUp(){
+ final List<UUID> toCleanUp = new ArrayList<>();
+
+ final Player target = Bukkit.getPlayer(uuid);
+ if (target != null){
+ shouldRemove = !isPlayerVisible(target);
+ for (Map.Entry<UUID, ScheduledTask> 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<TickRegions.TickRegionData, TickRegions.TickRegionSectionData> 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){
Expand All @@ -276,7 +261,7 @@ index 0000000000000000000000000000000000000000..47809d78392fe0cb30ad68ca1c331784
+ final String content = "<%s><text></%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){
Expand Down
Loading

0 comments on commit 43af4d5

Please sign in to comment.