397 lines
16 KiB
Diff
397 lines
16 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: MrHua269 <novau233@163.com>
|
|
Date: Wed, 7 Feb 2024 04:39:56 +0000
|
|
Subject: [PATCH] Add a simple tpsbar
|
|
|
|
|
|
diff --git a/src/main/java/me/earthme/luminol/commands/TpsBarCommand.java b/src/main/java/me/earthme/luminol/commands/TpsBarCommand.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..01677f210fae53a108bfe194189cb4c233e9fa47
|
|
--- /dev/null
|
|
+++ b/src/main/java/me/earthme/luminol/commands/TpsBarCommand.java
|
|
@@ -0,0 +1,50 @@
|
|
+package me.earthme.luminol.commands;
|
|
+
|
|
+import me.earthme.luminol.config.modules.misc.TpsBarConfig;
|
|
+import me.earthme.luminol.functions.GlobalServerTpsBar;
|
|
+import net.kyori.adventure.text.Component;
|
|
+import net.kyori.adventure.text.format.TextColor;
|
|
+import net.kyori.adventure.util.RGBLike;
|
|
+import org.bukkit.ChatColor;
|
|
+import org.bukkit.Color;
|
|
+import org.bukkit.command.Command;
|
|
+import org.bukkit.command.CommandSender;
|
|
+import org.bukkit.entity.Player;
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+
|
|
+public class TpsBarCommand extends Command {
|
|
+ public TpsBarCommand(@NotNull String name) {
|
|
+ super(name);
|
|
+ this.setPermission("luminol.commands.tpsbar");
|
|
+ this.setDescription("Show the tps and mspt through a bossbar");
|
|
+ this.setUsage("/tpsbar");
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
|
|
+ if (!testPermission(sender)){
|
|
+ return true;
|
|
+ }
|
|
+
|
|
+ if (!TpsBarConfig.tpsbarEnabled){
|
|
+ sender.sendMessage(Component.text("Tpsbar was already disabled!").color(TextColor.color(255,0,0)));
|
|
+ return true;
|
|
+ }
|
|
+
|
|
+ if (!(sender instanceof Player player)){
|
|
+ sender.sendMessage(Component.text("Only player can use this command!").color(TextColor.color(255,0,0)));
|
|
+ return true;
|
|
+ }
|
|
+
|
|
+ if (GlobalServerTpsBar.isPlayerVisible(player)) {
|
|
+ player.sendMessage(Component.text("Disabled tps bar").color(TextColor.color(0,255,0)));
|
|
+ GlobalServerTpsBar.setVisibilityForPlayer(player,false);
|
|
+ return true;
|
|
+ }
|
|
+
|
|
+ player.sendMessage(Component.text("Enabled tps bar").color(TextColor.color(0,255,0)));
|
|
+ GlobalServerTpsBar.setVisibilityForPlayer(player,true);
|
|
+
|
|
+ return true;
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/me/earthme/luminol/config/modules/misc/TpsBarConfig.java b/src/main/java/me/earthme/luminol/config/modules/misc/TpsBarConfig.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..aafb2f5052c7c8e5971a47308253badb3027093c
|
|
--- /dev/null
|
|
+++ b/src/main/java/me/earthme/luminol/config/modules/misc/TpsBarConfig.java
|
|
@@ -0,0 +1,49 @@
|
|
+package me.earthme.luminol.config.modules.misc;
|
|
+
|
|
+import com.electronwill.nightconfig.core.file.CommentedFileConfig;
|
|
+import me.earthme.luminol.commands.TpsBarCommand;
|
|
+import me.earthme.luminol.config.*;
|
|
+import me.earthme.luminol.functions.GlobalServerTpsBar;
|
|
+import org.bukkit.Bukkit;
|
|
+
|
|
+import java.util.List;
|
|
+
|
|
+public class TpsBarConfig implements IConfigModule {
|
|
+ @ConfigInfo(baseName = "enabled")
|
|
+ public static boolean tpsbarEnabled = false;
|
|
+ @ConfigInfo(baseName = "format")
|
|
+ public static String tpsBarFormat = "<gray>TPS<yellow>:</yellow> <tps> MSPT<yellow>:</yellow> <mspt> Ping<yellow>:</yellow> <ping>ms";
|
|
+ @ConfigInfo(baseName = "tps_color_list")
|
|
+ public static List<String> tpsColors = List.of("GREEN","YELLOW","RED","PURPLE");
|
|
+ @ConfigInfo(baseName = "ping_color_list")
|
|
+ public static List<String> pingColors = List.of("GREEN","YELLOW","RED","PURPLE");
|
|
+ @ConfigInfo(baseName = "update_interval_ticks")
|
|
+ public static int updateInterval = 15;
|
|
+
|
|
+ @DoNotLoad
|
|
+ private static boolean inited = false;
|
|
+
|
|
+ @Override
|
|
+ public EnumConfigCategory getCategory() {
|
|
+ return EnumConfigCategory.MISC;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public String getBaseName() {
|
|
+ return "tpsbar";
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public void onLoaded(CommentedFileConfig configInstance){
|
|
+ if (tpsbarEnabled){
|
|
+ GlobalServerTpsBar.init();
|
|
+ }else{
|
|
+ GlobalServerTpsBar.cancelBarUpdateTask();
|
|
+ }
|
|
+
|
|
+ if (!inited){
|
|
+ Bukkit.getCommandMap().register("tpsbar","luminol",new TpsBarCommand("tpsbar"));
|
|
+ inited = true;
|
|
+ }
|
|
+ }
|
|
+}
|
|
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..3f1478dde900d671c4bfa43df80f629519cd602e
|
|
--- /dev/null
|
|
+++ b/src/main/java/me/earthme/luminol/functions/GlobalServerTpsBar.java
|
|
@@ -0,0 +1,225 @@
|
|
+package me.earthme.luminol.functions;
|
|
+
|
|
+import io.papermc.paper.threadedregions.ThreadedRegionizer;
|
|
+import io.papermc.paper.threadedregions.TickData;
|
|
+import io.papermc.paper.threadedregions.TickRegions;
|
|
+import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
|
|
+import me.earthme.luminol.config.modules.misc.TpsBarConfig;
|
|
+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.apache.logging.log4j.LogManager;
|
|
+import org.apache.logging.log4j.Logger;
|
|
+import org.bukkit.Bukkit;
|
|
+import org.bukkit.craftbukkit.entity.CraftPlayer;
|
|
+import org.bukkit.craftbukkit.scheduler.MinecraftInternalPlugin;
|
|
+import org.bukkit.entity.Player;
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+
|
|
+import java.util.*;
|
|
+import java.util.concurrent.TimeUnit;
|
|
+
|
|
+public class GlobalServerTpsBar {
|
|
+ protected static final MinecraftInternalPlugin NULL_PLUGIN = new MinecraftInternalPlugin();
|
|
+ protected static final Map<UUID,BossBar> uuid2Bossbars = new HashMap<>();
|
|
+ protected static volatile ScheduledTask tpsbarTask = null;
|
|
+ private static final Logger logger = LogManager.getLogger();
|
|
+
|
|
+ public static void init(){
|
|
+ cancelBarUpdateTask();
|
|
+
|
|
+ Bukkit.getGlobalRegionScheduler().runAtFixedRate(NULL_PLUGIN,c -> {
|
|
+ tpsbarTask = c;
|
|
+ try {
|
|
+ update();
|
|
+ }catch (Exception e){
|
|
+ logger.error(e);
|
|
+ }
|
|
+ },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();
|
|
+ }
|
|
+
|
|
+ public static void cancelBarUpdateTask(){
|
|
+ removeAllBars();
|
|
+
|
|
+ if (tpsbarTask == null || tpsbarTask.isCancelled()){
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ tpsbarTask.cancel();
|
|
+ }
|
|
+
|
|
+ public static boolean isPlayerVisible(Player player){
|
|
+ return ((CraftPlayer) player).getHandle().isTpsBarVisible;
|
|
+ }
|
|
+
|
|
+ public static void setVisibilityForPlayer(Player target,boolean canSee){
|
|
+ ((CraftPlayer) target).getHandle().isTpsBarVisible = canSee;
|
|
+ }
|
|
+
|
|
+ private static void update(){
|
|
+ updateBarValues();
|
|
+ cleanUpPlayers();
|
|
+ }
|
|
+
|
|
+ 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);
|
|
+ }
|
|
+
|
|
+ if (shouldRemove){
|
|
+ toRemove.add(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 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.sectionX,nmsPlayer.sectionZ);
|
|
+
|
|
+ if (region == null){
|
|
+ continue;
|
|
+ }
|
|
+
|
|
+ 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);
|
|
+ }
|
|
+
|
|
+ if (reportData != null && targetBossbar != null){
|
|
+ final TickData.SegmentData tpsData = reportData.tpsData().segmentAll();
|
|
+ final double mspt = reportData.timePerTickData().segmentAll().average() / 1.0E6;
|
|
+ updateTpsBar(tpsData.average(),mspt,targetBossbar,apiPlayer);
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ private static void updateTpsBar(double tps, double mspt, @NotNull BossBar bar, @NotNull Player player){
|
|
+ bar.name(MiniMessage.miniMessage().deserialize(
|
|
+ TpsBarConfig.tpsBarFormat,
|
|
+ Placeholder.component("tps",getTpsComponent(tps)),
|
|
+ Placeholder.component("mspt",getMsptComponent(mspt)),
|
|
+ Placeholder.component("ping",getPingComponent(player.getPing()))
|
|
+ ));
|
|
+ bar.color(barColorFromTps(tps));
|
|
+ bar.progress((float) Math.min((float)1,Math.max(mspt / 50,0)));
|
|
+ }
|
|
+
|
|
+ private static @NotNull Component getPingComponent(int ping){
|
|
+ final BossBar.Color colorBukkit = barColorFromPing(ping);
|
|
+ final String colorString = colorBukkit.name();
|
|
+
|
|
+ final String content = "<%s><text></%s>";
|
|
+ final String replaced = String.format(content,colorString,colorString);
|
|
+
|
|
+ return MiniMessage.miniMessage().deserialize(replaced,Placeholder.parsed("text", String.valueOf(ping)));
|
|
+ }
|
|
+
|
|
+ private static BossBar.Color barColorFromPing(int ping){
|
|
+ if (ping == -1){
|
|
+ return BossBar.Color.valueOf(TpsBarConfig.pingColors.get(3));
|
|
+ }
|
|
+
|
|
+ if (ping <= 80){
|
|
+ return BossBar.Color.valueOf(TpsBarConfig.pingColors.get(0));
|
|
+ }
|
|
+
|
|
+ if (ping <= 160){
|
|
+ return BossBar.Color.valueOf(TpsBarConfig.pingColors.get(1));
|
|
+ }
|
|
+
|
|
+ return BossBar.Color.valueOf(TpsBarConfig.pingColors.get(2));
|
|
+ }
|
|
+
|
|
+ private static @NotNull Component getMsptComponent(double mspt){
|
|
+ final BossBar.Color colorBukkit = barColorFromMspt(mspt);
|
|
+ final String colorString = colorBukkit.name();
|
|
+
|
|
+ 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", mspt)));
|
|
+ }
|
|
+
|
|
+ private static BossBar.Color barColorFromMspt(double mspt){
|
|
+ if (mspt == -1){
|
|
+ return BossBar.Color.valueOf(TpsBarConfig.tpsColors.get(3));
|
|
+ }
|
|
+
|
|
+ if (mspt <= 25){
|
|
+ return BossBar.Color.valueOf(TpsBarConfig.tpsColors.get(0));
|
|
+ }
|
|
+
|
|
+ if (mspt <= 50){
|
|
+ return BossBar.Color.valueOf(TpsBarConfig.tpsColors.get(1));
|
|
+ }
|
|
+
|
|
+ return BossBar.Color.valueOf(TpsBarConfig.tpsColors.get(2));
|
|
+ }
|
|
+
|
|
+ private static @NotNull Component getTpsComponent(double tps){
|
|
+ final BossBar.Color colorBukkit = barColorFromTps(tps);
|
|
+ final String colorString = colorBukkit.name();
|
|
+
|
|
+ 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", tps)));
|
|
+ }
|
|
+
|
|
+ private static BossBar.Color barColorFromTps(double tps){
|
|
+ if (tps == -1){
|
|
+ return BossBar.Color.valueOf(TpsBarConfig.tpsColors.get(3));
|
|
+ }
|
|
+
|
|
+ if (tps >= 18){
|
|
+ return BossBar.Color.valueOf(TpsBarConfig.tpsColors.get(0));
|
|
+ }
|
|
+
|
|
+ if (tps >= 15){
|
|
+ return BossBar.Color.valueOf(TpsBarConfig.tpsColors.get(1));
|
|
+ }
|
|
+
|
|
+ return BossBar.Color.valueOf(TpsBarConfig.tpsColors.get(2));
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
|
|
index e456435e2f0606e840c7f4c0ff522e49da6e40e1..0c9b1289f445b7951a8c92c6d9effb0a7fb8ba01 100644
|
|
--- a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
|
|
+++ b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
|
|
@@ -850,6 +850,7 @@ public class DedicatedServer extends MinecraftServer implements ServerInterface
|
|
|
|
@Override
|
|
public void stopServer() {
|
|
+ me.earthme.luminol.functions.GlobalServerTpsBar.cancelBarUpdateTask(); //Luminol - Tpsbar
|
|
super.stopServer();
|
|
//Util.shutdownExecutors(); // Paper - moved into super
|
|
SkullBlockEntity.clear();
|
|
diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java
|
|
index 8ee61c2415455f8d4ff1ae687753117a40eb687b..a8f2eddba3ac3b7c551d39e260e096067e8bdc0f 100644
|
|
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
|
|
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
|
|
@@ -302,6 +302,7 @@ public class ServerPlayer extends Player {
|
|
// Paper start - replace player chunk loader
|
|
private final java.util.concurrent.atomic.AtomicReference<io.papermc.paper.chunk.system.RegionizedPlayerChunkLoader.ViewDistances> viewDistances = new java.util.concurrent.atomic.AtomicReference<>(new io.papermc.paper.chunk.system.RegionizedPlayerChunkLoader.ViewDistances(-1, -1, -1));
|
|
public io.papermc.paper.chunk.system.RegionizedPlayerChunkLoader.PlayerChunkLoaderData chunkLoader;
|
|
+ public volatile boolean isTpsBarVisible = false; //Luminol - Tps bar
|
|
|
|
public io.papermc.paper.chunk.system.RegionizedPlayerChunkLoader.ViewDistances getViewDistances() {
|
|
return this.viewDistances.get();
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
|
|
index 639b8678ad7711582f2e912772a98233c5313f63..28583ad8117167400faf3ca0dc4bc9f3f44dbedb 100644
|
|
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
|
|
@@ -2370,6 +2370,9 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
|
|
handle.expToDrop = data.getInt("expToDrop");
|
|
handle.keepLevel = data.getBoolean("keepLevel");
|
|
}
|
|
+ //Luminol start - Tpsbar
|
|
+ getHandle().isTpsBarVisible = data.getBoolean("tpsbarVisible");
|
|
+ //Luminol end
|
|
}
|
|
}
|
|
|
|
@@ -2391,6 +2394,9 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
|
|
data.putLong("lastPlayed", System.currentTimeMillis());
|
|
data.putString("lastKnownName", handle.getScoreboardName());
|
|
|
|
+ //Luminol start - Tpsbar
|
|
+ data.putBoolean("tpsbarVisible",handle.isTpsBarVisible);
|
|
+ //Luminol end
|
|
// Paper start - persist for use in offline save data
|
|
if (!nbttagcompound.contains("Paper")) {
|
|
nbttagcompound.put("Paper", new CompoundTag());
|