mirror of
https://github.com/PurpurMC/Purpur.git
synced 2026-02-18 08:57:44 +01:00
Implement TPSBar
This commit is contained in:
committed by
granny
parent
55eb63b89c
commit
42bf7db018
@@ -2,6 +2,7 @@ package org.purpurmc.purpur;
|
||||
|
||||
import com.google.common.base.Throwables;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import net.kyori.adventure.bossbar.BossBar;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.world.entity.EntityDimensions;
|
||||
@@ -24,6 +25,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import org.purpurmc.purpur.task.TPSBarTask;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class PurpurConfig {
|
||||
@@ -168,6 +170,7 @@ public class PurpurConfig {
|
||||
public static String creditsCommandOutput = "<green>%s has been shown the end credits";
|
||||
public static String demoCommandOutput = "<green>%s has been shown the demo screen";
|
||||
public static String pingCommandOutput = "<green>%s's ping is %sms";
|
||||
public static String tpsbarCommandOutput = "<green>Tpsbar toggled <onoff> for <target>";
|
||||
private static void messages() {
|
||||
cannotRideMob = getString("settings.messages.cannot-ride-mob", cannotRideMob);
|
||||
afkBroadcastAway = getString("settings.messages.afk-broadcast-away", afkBroadcastAway);
|
||||
@@ -178,6 +181,7 @@ public class PurpurConfig {
|
||||
creditsCommandOutput = getString("settings.messages.credits-command-output", creditsCommandOutput);
|
||||
demoCommandOutput = getString("settings.messages.demo-command-output", demoCommandOutput);
|
||||
pingCommandOutput = getString("settings.messages.ping-command-output", pingCommandOutput);
|
||||
tpsbarCommandOutput = getString("settings.messages.tpsbar-command-output", tpsbarCommandOutput);
|
||||
}
|
||||
|
||||
public static String serverModName = io.papermc.paper.ServerBuildInfo.buildInfo().brandName();
|
||||
@@ -200,6 +204,29 @@ public class PurpurConfig {
|
||||
disableGiveCommandDrops = getBoolean("settings.disable-give-dropping", disableGiveCommandDrops);
|
||||
}
|
||||
|
||||
public static String commandTPSBarTitle = "<gray>TPS<yellow>:</yellow> <tps> MSPT<yellow>:</yellow> <mspt> Ping<yellow>:</yellow> <ping>ms";
|
||||
public static BossBar.Overlay commandTPSBarProgressOverlay = BossBar.Overlay.NOTCHED_20;
|
||||
public static TPSBarTask.FillMode commandTPSBarProgressFillMode = TPSBarTask.FillMode.MSPT;
|
||||
public static BossBar.Color commandTPSBarProgressColorGood = BossBar.Color.GREEN;
|
||||
public static BossBar.Color commandTPSBarProgressColorMedium = BossBar.Color.YELLOW;
|
||||
public static BossBar.Color commandTPSBarProgressColorLow = BossBar.Color.RED;
|
||||
public static String commandTPSBarTextColorGood = "<gradient:#55ff55:#00aa00><text></gradient>";
|
||||
public static String commandTPSBarTextColorMedium = "<gradient:#ffff55:#ffaa00><text></gradient>";
|
||||
public static String commandTPSBarTextColorLow = "<gradient:#ff5555:#aa0000><text></gradient>";
|
||||
public static int commandTPSBarTickInterval = 20;
|
||||
private static void commandSettings() {
|
||||
commandTPSBarTitle = getString("settings.command.tpsbar.title", commandTPSBarTitle);
|
||||
commandTPSBarProgressOverlay = BossBar.Overlay.valueOf(getString("settings.command.tpsbar.overlay", commandTPSBarProgressOverlay.name()));
|
||||
commandTPSBarProgressFillMode = TPSBarTask.FillMode.valueOf(getString("settings.command.tpsbar.fill-mode", commandTPSBarProgressFillMode.name()));
|
||||
commandTPSBarProgressColorGood = BossBar.Color.valueOf(getString("settings.command.tpsbar.progress-color.good", commandTPSBarProgressColorGood.name()));
|
||||
commandTPSBarProgressColorMedium = BossBar.Color.valueOf(getString("settings.command.tpsbar.progress-color.medium", commandTPSBarProgressColorMedium.name()));
|
||||
commandTPSBarProgressColorLow = BossBar.Color.valueOf(getString("settings.command.tpsbar.progress-color.low", commandTPSBarProgressColorLow.name()));
|
||||
commandTPSBarTextColorGood = getString("settings.command.tpsbar.text-color.good", commandTPSBarTextColorGood);
|
||||
commandTPSBarTextColorMedium = getString("settings.command.tpsbar.text-color.medium", commandTPSBarTextColorMedium);
|
||||
commandTPSBarTextColorLow = getString("settings.command.tpsbar.text-color.low", commandTPSBarTextColorLow);
|
||||
commandTPSBarTickInterval = getInt("settings.command.tpsbar.tick-interval", commandTPSBarTickInterval);
|
||||
}
|
||||
|
||||
public static int barrelRows = 3;
|
||||
public static boolean enderChestSixRows = false;
|
||||
public static boolean enderChestPermissionRows = false;
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package org.purpurmc.purpur.command;
|
||||
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.Commands;
|
||||
import net.minecraft.commands.arguments.EntityArgument;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import org.purpurmc.purpur.PurpurConfig;
|
||||
import org.purpurmc.purpur.task.TPSBarTask;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
public class TPSBarCommand {
|
||||
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
|
||||
dispatcher.register(Commands.literal("tpsbar")
|
||||
.requires(listener -> listener.hasPermission(2, "bukkit.command.tpsbar"))
|
||||
.executes(context -> execute(context.getSource(), Collections.singleton(context.getSource().getPlayerOrException())))
|
||||
.then(Commands.argument("targets", EntityArgument.players())
|
||||
.requires(listener -> listener.hasPermission(2, "bukkit.command.tpsbar.other"))
|
||||
.executes((context) -> execute(context.getSource(), EntityArgument.getPlayers(context, "targets")))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private static int execute(CommandSourceStack sender, Collection<ServerPlayer> targets) {
|
||||
for (ServerPlayer player : targets) {
|
||||
boolean result = TPSBarTask.instance().togglePlayer(player.getBukkitEntity());
|
||||
player.tpsBar(result);
|
||||
|
||||
Component output = MiniMessage.miniMessage().deserialize(PurpurConfig.tpsbarCommandOutput,
|
||||
Placeholder.component("onoff", Component.translatable(result ? "options.on" : "options.off")
|
||||
.color(result ? NamedTextColor.GREEN : NamedTextColor.RED)),
|
||||
Placeholder.parsed("target", player.getGameProfile().getName()));
|
||||
|
||||
sender.sendSuccess(output, false);
|
||||
}
|
||||
return targets.size();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package org.purpurmc.purpur.task;
|
||||
|
||||
import net.kyori.adventure.bossbar.BossBar;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import org.purpurmc.purpur.util.MinecraftInternalPlugin;
|
||||
|
||||
public abstract class BossBarTask extends BukkitRunnable {
|
||||
private final Map<UUID, BossBar> bossbars = new HashMap<>();
|
||||
private boolean started;
|
||||
|
||||
abstract BossBar createBossBar();
|
||||
|
||||
abstract void updateBossBar(BossBar bossbar, Player player);
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Iterator<Map.Entry<UUID, BossBar>> iter = bossbars.entrySet().iterator();
|
||||
while (iter.hasNext()) {
|
||||
Map.Entry<UUID, BossBar> entry = iter.next();
|
||||
Player player = Bukkit.getPlayer(entry.getKey());
|
||||
if (player == null) {
|
||||
iter.remove();
|
||||
continue;
|
||||
}
|
||||
updateBossBar(entry.getValue(), player);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancel() {
|
||||
super.cancel();
|
||||
new HashSet<>(this.bossbars.keySet()).forEach(uuid -> {
|
||||
Player player = Bukkit.getPlayer(uuid);
|
||||
if (player != null) {
|
||||
removePlayer(player);
|
||||
}
|
||||
});
|
||||
this.bossbars.clear();
|
||||
}
|
||||
|
||||
public boolean removePlayer(Player player) {
|
||||
BossBar bossbar = this.bossbars.remove(player.getUniqueId());
|
||||
if (bossbar != null) {
|
||||
player.hideBossBar(bossbar);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void addPlayer(Player player) {
|
||||
removePlayer(player);
|
||||
BossBar bossbar = createBossBar();
|
||||
this.bossbars.put(player.getUniqueId(), bossbar);
|
||||
this.updateBossBar(bossbar, player);
|
||||
player.showBossBar(bossbar);
|
||||
}
|
||||
|
||||
public boolean hasPlayer(UUID uuid) {
|
||||
return this.bossbars.containsKey(uuid);
|
||||
}
|
||||
|
||||
public boolean togglePlayer(Player player) {
|
||||
if (removePlayer(player)) {
|
||||
return false;
|
||||
}
|
||||
addPlayer(player);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
stop();
|
||||
this.runTaskTimerAsynchronously(new MinecraftInternalPlugin(), 1, 1);
|
||||
started = true;
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
if (started) {
|
||||
cancel();
|
||||
}
|
||||
}
|
||||
|
||||
public static void startAll() {
|
||||
TPSBarTask.instance().start();
|
||||
}
|
||||
|
||||
public static void stopAll() {
|
||||
TPSBarTask.instance().stop();
|
||||
}
|
||||
|
||||
public static void addToAll(ServerPlayer player) {
|
||||
Player bukkit = player.getBukkitEntity();
|
||||
if (player.tpsBar()) {
|
||||
TPSBarTask.instance().addPlayer(bukkit);
|
||||
}
|
||||
}
|
||||
|
||||
public static void removeFromAll(Player player) {
|
||||
TPSBarTask.instance().removePlayer(player);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
package org.purpurmc.purpur.task;
|
||||
|
||||
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 org.purpurmc.purpur.PurpurConfig;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class TPSBarTask extends BossBarTask {
|
||||
private static TPSBarTask instance;
|
||||
private double tps = 20.0D;
|
||||
private double mspt = 0.0D;
|
||||
private int tick = 0;
|
||||
|
||||
public static TPSBarTask instance() {
|
||||
if (instance == null) {
|
||||
instance = new TPSBarTask();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
BossBar createBossBar() {
|
||||
return BossBar.bossBar(Component.text(""), 0.0F, instance().getBossBarColor(), PurpurConfig.commandTPSBarProgressOverlay);
|
||||
}
|
||||
|
||||
@Override
|
||||
void updateBossBar(BossBar bossbar, Player player) {
|
||||
bossbar.progress(getBossBarProgress());
|
||||
bossbar.color(getBossBarColor());
|
||||
bossbar.name(MiniMessage.miniMessage().deserialize(PurpurConfig.commandTPSBarTitle,
|
||||
Placeholder.component("tps", getTPSColor()),
|
||||
Placeholder.component("mspt", getMSPTColor()),
|
||||
Placeholder.component("ping", getPingColor(player.getPing()))
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (++tick < PurpurConfig.commandTPSBarTickInterval) {
|
||||
return;
|
||||
}
|
||||
tick = 0;
|
||||
|
||||
this.tps = Math.max(Math.min(Bukkit.getTPS()[0], 20.0D), 0.0D);
|
||||
this.mspt = Bukkit.getAverageTickTime();
|
||||
|
||||
super.run();
|
||||
}
|
||||
|
||||
private float getBossBarProgress() {
|
||||
if (PurpurConfig.commandTPSBarProgressFillMode == FillMode.MSPT) {
|
||||
return Math.max(Math.min((float) mspt / 50.0F, 1.0F), 0.0F);
|
||||
} else {
|
||||
return Math.max(Math.min((float) tps / 20.0F, 1.0F), 0.0F);
|
||||
}
|
||||
}
|
||||
|
||||
private BossBar.Color getBossBarColor() {
|
||||
if (isGood(PurpurConfig.commandTPSBarProgressFillMode)) {
|
||||
return PurpurConfig.commandTPSBarProgressColorGood;
|
||||
} else if (isMedium(PurpurConfig.commandTPSBarProgressFillMode)) {
|
||||
return PurpurConfig.commandTPSBarProgressColorMedium;
|
||||
} else {
|
||||
return PurpurConfig.commandTPSBarProgressColorLow;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isGood(FillMode mode) {
|
||||
return isGood(mode, 0);
|
||||
}
|
||||
|
||||
private boolean isGood(FillMode mode, int ping) {
|
||||
if (mode == FillMode.MSPT) {
|
||||
return mspt < 40;
|
||||
} else if (mode == FillMode.TPS) {
|
||||
return tps >= 19;
|
||||
} else if (mode == FillMode.PING) {
|
||||
return ping < 100;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isMedium(FillMode mode) {
|
||||
return isMedium(mode, 0);
|
||||
}
|
||||
|
||||
private boolean isMedium(FillMode mode, int ping) {
|
||||
if (mode == FillMode.MSPT) {
|
||||
return mspt < 50;
|
||||
} else if (mode == FillMode.TPS) {
|
||||
return tps >= 15;
|
||||
} else if (mode == FillMode.PING) {
|
||||
return ping < 200;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private Component getTPSColor() {
|
||||
String color;
|
||||
if (isGood(FillMode.TPS)) {
|
||||
color = PurpurConfig.commandTPSBarTextColorGood;
|
||||
} else if (isMedium(FillMode.TPS)) {
|
||||
color = PurpurConfig.commandTPSBarTextColorMedium;
|
||||
} else {
|
||||
color = PurpurConfig.commandTPSBarTextColorLow;
|
||||
}
|
||||
return MiniMessage.miniMessage().deserialize(color, Placeholder.parsed("text", String.format("%.2f", tps)));
|
||||
}
|
||||
|
||||
private Component getMSPTColor() {
|
||||
String color;
|
||||
if (isGood(FillMode.MSPT)) {
|
||||
color = PurpurConfig.commandTPSBarTextColorGood;
|
||||
} else if (isMedium(FillMode.MSPT)) {
|
||||
color = PurpurConfig.commandTPSBarTextColorMedium;
|
||||
} else {
|
||||
color = PurpurConfig.commandTPSBarTextColorLow;
|
||||
}
|
||||
return MiniMessage.miniMessage().deserialize(color, Placeholder.parsed("text", String.format("%.2f", mspt)));
|
||||
}
|
||||
|
||||
private Component getPingColor(int ping) {
|
||||
String color;
|
||||
if (isGood(FillMode.PING, ping)) {
|
||||
color = PurpurConfig.commandTPSBarTextColorGood;
|
||||
} else if (isMedium(FillMode.PING, ping)) {
|
||||
color = PurpurConfig.commandTPSBarTextColorMedium;
|
||||
} else {
|
||||
color = PurpurConfig.commandTPSBarTextColorLow;
|
||||
}
|
||||
return MiniMessage.miniMessage().deserialize(color, Placeholder.parsed("text", String.format("%s", ping)));
|
||||
}
|
||||
|
||||
public enum FillMode {
|
||||
TPS, MSPT, PING
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user