mirror of
https://github.com/Winds-Studio/Leaf.git
synced 2025-12-19 15:09:25 +00:00
347 lines
16 KiB
Diff
347 lines
16 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: BillyGalbreath <blake.galbreath@gmail.com>
|
|
Date: Mon, 26 Sep 2022 07:43:30 -0500
|
|
Subject: [PATCH] Purpur: Implement ram and rambar commands
|
|
|
|
Original license: MIT
|
|
Original project: https://github.com/PurpurMC/Purpur
|
|
|
|
diff --git a/src/main/java/net/minecraft/commands/Commands.java b/src/main/java/net/minecraft/commands/Commands.java
|
|
index 5ddaaf3b50b6483143e21f278d3fd732429e64d4..e94bcacae678b70a61c428dd307b860ab2382f50 100644
|
|
--- a/src/main/java/net/minecraft/commands/Commands.java
|
|
+++ b/src/main/java/net/minecraft/commands/Commands.java
|
|
@@ -223,6 +223,8 @@ public class Commands {
|
|
WhitelistCommand.register(this.dispatcher);
|
|
org.dreeam.leaf.commands.TPSBarCommand.register(this.dispatcher); // Purpur
|
|
org.dreeam.leaf.commands.CompassCommand.register(this.dispatcher); // Purpur
|
|
+ org.dreeam.leaf.commands.RamBarCommand.register(this.dispatcher); // Purpur
|
|
+ org.dreeam.leaf.commands.RamCommand.register(this.dispatcher); // Purpur
|
|
}
|
|
|
|
if (environment.includeIntegrated) {
|
|
diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java
|
|
index f0cf0f82920e09a984c8c3dbb131103beb327163..807bae553cf0008e3ca328b85fb210dc6a929fde 100644
|
|
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
|
|
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
|
|
@@ -277,6 +277,7 @@ public class ServerPlayer extends Player {
|
|
public org.bukkit.event.player.PlayerQuitEvent.QuitReason quitReason = null; // Paper - there are a lot of changes to do if we change all methods leading to the event
|
|
private boolean tpsBar = false; // Purpur
|
|
private boolean compassBar = false; // Purpur
|
|
+ private boolean ramBar = false; // Purpur
|
|
|
|
public ServerPlayer(MinecraftServer server, ServerLevel world, GameProfile profile) {
|
|
super(world, world.getSharedSpawnPos(), world.getSharedSpawnAngle(), profile);
|
|
@@ -515,6 +516,7 @@ public class ServerPlayer extends Player {
|
|
}
|
|
}
|
|
|
|
+ if (nbt.contains("Purpur.RamBar")) { this.ramBar = nbt.getBoolean("Purpur.RamBar"); } // Purpur
|
|
if (nbt.contains("Purpur.TPSBar")) { this.tpsBar = nbt.getBoolean("Purpur.TPSBar"); } // Purpur
|
|
if (nbt.contains("Purpur.CompassBar")) { this.compassBar = nbt.getBoolean("Purpur.CompassBar"); } // Purpur
|
|
}
|
|
@@ -583,6 +585,7 @@ public class ServerPlayer extends Player {
|
|
}
|
|
this.getBukkitEntity().setExtraData(nbt); // CraftBukkit
|
|
|
|
+ nbt.putBoolean("Purpur.RamBar", this.ramBar); // Purpur
|
|
nbt.putBoolean("Purpur.TPSBar", this.tpsBar); // Purpur
|
|
nbt.putBoolean("Purpur.CompassBar", this.compassBar); // Purpur
|
|
}
|
|
@@ -2566,5 +2569,14 @@ public class ServerPlayer extends Player {
|
|
public void compassBar(boolean compassBar) {
|
|
this.compassBar = compassBar;
|
|
}
|
|
+
|
|
+ public boolean ramBar() {
|
|
+ return this.ramBar;
|
|
+ }
|
|
+
|
|
+ public void ramBar(boolean ramBar) {
|
|
+ this.ramBar = ramBar;
|
|
+ }
|
|
+
|
|
// Purpur end
|
|
}
|
|
diff --git a/src/main/java/org/dreeam/leaf/LeafConfig.java b/src/main/java/org/dreeam/leaf/LeafConfig.java
|
|
index 41b79bdda2f643fdb85577b2b52327d4b7fa83a8..b9ade5d89b8dd48b075338e0f61c54aee5fa72ae 100644
|
|
--- a/src/main/java/org/dreeam/leaf/LeafConfig.java
|
|
+++ b/src/main/java/org/dreeam/leaf/LeafConfig.java
|
|
@@ -199,6 +199,17 @@ public class LeafConfig {
|
|
public static BossBar.Color commandCompassBarProgressColor = BossBar.Color.BLUE;
|
|
public static float commandCompassBarProgressPercent = 1.0F;
|
|
public static int commandCompassBarTickInterval = 5;
|
|
+ public static String commandRamOutput = "<green>Ram Usage: <used>/<xmx> (<percent>)";
|
|
+ public static String commandRamBarOutput = "<green>Rambar toggled <onoff> for <target>";
|
|
+ public static String commandRamBarTitle = "<gray>Ram<yellow>:</yellow> <used>/<xmx> (<percent>)";
|
|
+ public static BossBar.Overlay commandRamBarProgressOverlay = BossBar.Overlay.NOTCHED_20;
|
|
+ public static BossBar.Color commandRamBarProgressColorGood = BossBar.Color.GREEN;
|
|
+ public static BossBar.Color commandRamBarProgressColorMedium = BossBar.Color.YELLOW;
|
|
+ public static BossBar.Color commandRamBarProgressColorLow = BossBar.Color.RED;
|
|
+ public static String commandRamBarTextColorGood = "<gradient:#55ff55:#00aa00><text></gradient>";
|
|
+ public static String commandRamBarTextColorMedium = "<gradient:#ffff55:#ffaa00><text></gradient>";
|
|
+ public static String commandRamBarTextColorLow = "<gradient:#ff5555:#aa0000><text></gradient>";
|
|
+ public static int commandRamBarTickInterval = 20;
|
|
|
|
private static void commandSettings() {
|
|
commandTPSBarOutput = getString("command.messages.tpsbar-command-output", commandTPSBarOutput);
|
|
@@ -218,6 +229,18 @@ public class LeafConfig {
|
|
commandCompassBarProgressColor = BossBar.Color.valueOf(getString("command.compass.progress-color", commandCompassBarProgressColor.name()));
|
|
commandCompassBarProgressPercent = (float) getDouble("command.compass.percent", commandCompassBarProgressPercent);
|
|
commandCompassBarTickInterval = getInt("command.compass.tick-interval", commandCompassBarTickInterval);
|
|
+
|
|
+ commandRamOutput = getString("command.messages.ram-command-output", commandRamOutput);
|
|
+ commandRamBarOutput = getString("command.messages.rambar-command-output", commandRamBarOutput);
|
|
+ commandRamBarTitle = getString("command.rambar.title", commandRamBarTitle);
|
|
+ commandRamBarProgressOverlay = BossBar.Overlay.valueOf(getString("command.rambar.overlay", commandRamBarProgressOverlay.name()));
|
|
+ commandRamBarProgressColorGood = BossBar.Color.valueOf(getString("command.rambar.progress-color.good", commandRamBarProgressColorGood.name()));
|
|
+ commandRamBarProgressColorMedium = BossBar.Color.valueOf(getString("command.rambar.progress-color.medium", commandRamBarProgressColorMedium.name()));
|
|
+ commandRamBarProgressColorLow = BossBar.Color.valueOf(getString("command.rambar.progress-color.low", commandRamBarProgressColorLow.name()));
|
|
+ commandRamBarTextColorGood = getString("command.rambar.text-color.good", commandRamBarTextColorGood);
|
|
+ commandRamBarTextColorMedium = getString("command.rambar.text-color.medium", commandRamBarTextColorMedium);
|
|
+ commandRamBarTextColorLow = getString("command.rambar.text-color.low", commandRamBarTextColorLow);
|
|
+ commandRamBarTickInterval = getInt("command.rambar.tick-interval", commandRamBarTickInterval);
|
|
}
|
|
|
|
public static boolean compassItemShowsBossBar = false;
|
|
diff --git a/src/main/java/org/dreeam/leaf/commands/RamBarCommand.java b/src/main/java/org/dreeam/leaf/commands/RamBarCommand.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..900a5ab318d5b17cea7129088aa84c015dde0b53
|
|
--- /dev/null
|
|
+++ b/src/main/java/org/dreeam/leaf/commands/RamBarCommand.java
|
|
@@ -0,0 +1,43 @@
|
|
+package org.dreeam.leaf.commands;
|
|
+
|
|
+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.dreeam.leaf.LeafConfig;
|
|
+import org.dreeam.leaf.tasks.RamBarTask;
|
|
+
|
|
+import java.util.Collection;
|
|
+import java.util.Collections;
|
|
+
|
|
+public class RamBarCommand {
|
|
+ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
|
|
+ dispatcher.register(Commands.literal("rambar")
|
|
+ .requires(listener -> listener.hasPermission(2))
|
|
+ .executes(context -> execute(context.getSource(), Collections.singleton(context.getSource().getPlayerOrException())))
|
|
+ .then(Commands.argument("targets", EntityArgument.players())
|
|
+ .executes((context) -> execute(context.getSource(), EntityArgument.getPlayers(context, "targets")))
|
|
+ )
|
|
+ ).setPermission("bukkit.command.rambar");
|
|
+ }
|
|
+
|
|
+ private static int execute(CommandSourceStack sender, Collection<ServerPlayer> targets) {
|
|
+ for (ServerPlayer player : targets) {
|
|
+ boolean result = RamBarTask.instance().togglePlayer(player.getBukkitEntity());
|
|
+ player.ramBar(result);
|
|
+
|
|
+ Component output = MiniMessage.miniMessage().deserialize(LeafConfig.commandRamBarOutput,
|
|
+ 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();
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/org/dreeam/leaf/commands/RamCommand.java b/src/main/java/org/dreeam/leaf/commands/RamCommand.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..dd9492ffb7f92c69dd37bf197ec1bacb5f206f05
|
|
--- /dev/null
|
|
+++ b/src/main/java/org/dreeam/leaf/commands/RamCommand.java
|
|
@@ -0,0 +1,30 @@
|
|
+package org.dreeam.leaf.commands;
|
|
+
|
|
+import com.mojang.brigadier.CommandDispatcher;
|
|
+import io.papermc.paper.adventure.PaperAdventure;
|
|
+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 org.dreeam.leaf.LeafConfig;
|
|
+import org.dreeam.leaf.tasks.RamBarTask;
|
|
+
|
|
+public class RamCommand {
|
|
+ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
|
|
+ dispatcher.register(Commands.literal("ram")
|
|
+ .requires(listener -> listener.hasPermission(2))
|
|
+ .executes(context -> {
|
|
+ CommandSourceStack sender = context.getSource();
|
|
+ RamBarTask ramBar = RamBarTask.instance();
|
|
+ sender.sendSuccess(PaperAdventure.asVanilla(MiniMessage.miniMessage().deserialize(LeafConfig.commandRamOutput,
|
|
+ Placeholder.component("allocated", ramBar.format(ramBar.getAllocated())),
|
|
+ Placeholder.component("used", ramBar.format(ramBar.getUsed())),
|
|
+ Placeholder.component("xmx", ramBar.format(ramBar.getXmx())),
|
|
+ Placeholder.component("xms", ramBar.format(ramBar.getXms())),
|
|
+ Placeholder.unparsed("percent", ((int) (ramBar.getPercent() * 100)) + "%")
|
|
+ )), false);
|
|
+ return 1;
|
|
+ })
|
|
+ ).setPermission("bukkit.command.ram");
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/org/dreeam/leaf/tasks/BossBarTask.java b/src/main/java/org/dreeam/leaf/tasks/BossBarTask.java
|
|
index 7dacf4f21d082dd9a19db6e5122d06b5bda8fc95..46644c72bf1db3cb86ffbf50dd7d723cb04e04e3 100644
|
|
--- a/src/main/java/org/dreeam/leaf/tasks/BossBarTask.java
|
|
+++ b/src/main/java/org/dreeam/leaf/tasks/BossBarTask.java
|
|
@@ -91,11 +91,13 @@ public abstract class BossBarTask extends BukkitRunnable {
|
|
public static void startAll() {
|
|
TPSBarTask.instance().start();
|
|
CompassTask.instance().start();
|
|
+ RamBarTask.instance().start();
|
|
}
|
|
|
|
public static void stopAll() {
|
|
TPSBarTask.instance().stop();
|
|
CompassTask.instance().stop();
|
|
+ RamBarTask.instance().stop();
|
|
}
|
|
|
|
public static void addToAll(ServerPlayer player) {
|
|
@@ -106,10 +108,14 @@ public abstract class BossBarTask extends BukkitRunnable {
|
|
if (player.compassBar()) {
|
|
CompassTask.instance().addPlayer(bukkit);
|
|
}
|
|
+ if (player.ramBar()) {
|
|
+ RamBarTask.instance().addPlayer(bukkit);
|
|
+ }
|
|
}
|
|
|
|
public static void removeFromAll(Player player) {
|
|
TPSBarTask.instance().removePlayer(player);
|
|
CompassTask.instance().removePlayer(player);
|
|
+ RamBarTask.instance().removePlayer(player);
|
|
}
|
|
}
|
|
diff --git a/src/main/java/org/dreeam/leaf/tasks/RamBarTask.java b/src/main/java/org/dreeam/leaf/tasks/RamBarTask.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..c0d2f4f9fdd60f89376a18c88cf82012acd62da0
|
|
--- /dev/null
|
|
+++ b/src/main/java/org/dreeam/leaf/tasks/RamBarTask.java
|
|
@@ -0,0 +1,117 @@
|
|
+package org.dreeam.leaf.tasks;
|
|
+
|
|
+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.bukkit.entity.Player;
|
|
+import org.dreeam.leaf.LeafConfig;
|
|
+
|
|
+import java.lang.management.ManagementFactory;
|
|
+import java.lang.management.MemoryUsage;
|
|
+
|
|
+public class RamBarTask extends BossBarTask {
|
|
+ private static RamBarTask instance;
|
|
+ private long allocated = 0L;
|
|
+ private long used = 0L;
|
|
+ private long xmx = 0L;
|
|
+ private long xms = 0L;
|
|
+ private float percent = 0F;
|
|
+ private int tick = 0;
|
|
+
|
|
+ public static RamBarTask instance() {
|
|
+ if (instance == null) {
|
|
+ instance = new RamBarTask();
|
|
+ }
|
|
+ return instance;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ BossBar createBossBar() {
|
|
+ return BossBar.bossBar(Component.text(""), 0.0F, instance().getBossBarColor(), LeafConfig.commandRamBarProgressOverlay);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ void updateBossBar(BossBar bossbar, Player player) {
|
|
+ bossbar.progress(getBossBarProgress());
|
|
+ bossbar.color(getBossBarColor());
|
|
+ bossbar.name(MiniMessage.miniMessage().deserialize(LeafConfig.commandRamBarTitle,
|
|
+ Placeholder.component("allocated", format(this.allocated)),
|
|
+ Placeholder.component("used", format(this.used)),
|
|
+ Placeholder.component("xmx", format(this.xmx)),
|
|
+ Placeholder.component("xms", format(this.xms)),
|
|
+ Placeholder.unparsed("percent", ((int) (this.percent * 100)) + "%")
|
|
+ ));
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public void run() {
|
|
+ if (++this.tick < LeafConfig.commandRamBarTickInterval) {
|
|
+ return;
|
|
+ }
|
|
+ this.tick = 0;
|
|
+
|
|
+ MemoryUsage heap = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
|
|
+
|
|
+ this.allocated = heap.getCommitted();
|
|
+ this.used = heap.getUsed();
|
|
+ this.xmx = heap.getMax();
|
|
+ this.xms = heap.getInit();
|
|
+ this.percent = Math.max(Math.min((float) this.used / this.xmx, 1.0F), 0.0F);
|
|
+
|
|
+ super.run();
|
|
+ }
|
|
+
|
|
+ private float getBossBarProgress() {
|
|
+ return this.percent;
|
|
+ }
|
|
+
|
|
+ private BossBar.Color getBossBarColor() {
|
|
+ if (this.percent < 0.5F) {
|
|
+ return LeafConfig.commandRamBarProgressColorGood;
|
|
+ } else if (this.percent < 0.75F) {
|
|
+ return LeafConfig.commandRamBarProgressColorMedium;
|
|
+ } else {
|
|
+ return LeafConfig.commandRamBarProgressColorLow;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public Component format(long v) {
|
|
+ String color;
|
|
+ if (this.percent < 0.60F) {
|
|
+ color = LeafConfig.commandRamBarTextColorGood;
|
|
+ } else if (this.percent < 0.85F) {
|
|
+ color = LeafConfig.commandRamBarTextColorMedium;
|
|
+ } else {
|
|
+ color = LeafConfig.commandRamBarTextColorLow;
|
|
+ }
|
|
+ String value;
|
|
+ if (v < 1024) {
|
|
+ value = v + "B";
|
|
+ } else {
|
|
+ int z = (63 - Long.numberOfLeadingZeros(v)) / 10;
|
|
+ value = String.format("%.1f%s", (double) v / (1L << (z * 10)), "BKMGTPE".charAt(z));
|
|
+ }
|
|
+ return MiniMessage.miniMessage().deserialize(color, Placeholder.unparsed("text", value));
|
|
+ }
|
|
+
|
|
+ public long getAllocated() {
|
|
+ return this.allocated;
|
|
+ }
|
|
+
|
|
+ public long getUsed() {
|
|
+ return this.used;
|
|
+ }
|
|
+
|
|
+ public long getXmx() {
|
|
+ return this.xmx;
|
|
+ }
|
|
+
|
|
+ public long getXms() {
|
|
+ return this.xms;
|
|
+ }
|
|
+
|
|
+ public float getPercent() {
|
|
+ return this.percent;
|
|
+ }
|
|
+}
|