Skip to content

Commit f913b22

Browse files
Implement ram and rambar commands
1 parent 478d55e commit f913b22

1 file changed

Lines changed: 360 additions & 0 deletions

File tree

Lines changed: 360 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,360 @@
1+
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2+
From: BillyGalbreath <blake.galbreath@gmail.com>
3+
Date: Mon, 26 Sep 2022 07:43:30 -0500
4+
Subject: [PATCH] Implement ram and rambar commands
5+
6+
7+
diff --git a/src/main/java/net/minecraft/commands/Commands.java b/src/main/java/net/minecraft/commands/Commands.java
8+
index eb4dac2239592d680ef31edf47f1ab660299762d..1b956c3f50750a00eaf7851112004115e9572395 100644
9+
--- a/src/main/java/net/minecraft/commands/Commands.java
10+
+++ b/src/main/java/net/minecraft/commands/Commands.java
11+
@@ -213,6 +213,8 @@ public class Commands {
12+
org.purpurmc.purpur.command.UptimeCommand.register(this.dispatcher); // Purpur
13+
org.purpurmc.purpur.command.TPSBarCommand.register(this.dispatcher); // Purpur
14+
org.purpurmc.purpur.command.CompassCommand.register(this.dispatcher); // Purpur
15+
+ org.purpurmc.purpur.command.RamBarCommand.register(this.dispatcher); // Purpur
16+
+ org.purpurmc.purpur.command.RamCommand.register(this.dispatcher); // Purpur
17+
}
18+
19+
if (environment.includeIntegrated) {
20+
diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java
21+
index 6979305279996be59756d0424e5bc16a19fde6b0..12637019da44019475042e4cf88d3e0ebd0fd068 100644
22+
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
23+
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
24+
@@ -263,6 +263,7 @@ public class ServerPlayer extends Player {
25+
public PlayerNaturallySpawnCreaturesEvent playerNaturallySpawnedEvent; // Paper
26+
public boolean purpurClient = false; // Purpur
27+
public boolean acceptingResourcePack = false; // Purpur
28+
+ private boolean ramBar = false; // Purpur
29+
private boolean tpsBar = false; // Purpur
30+
private boolean compassBar = false; // Purpur
31+
32+
@@ -472,6 +473,7 @@ public class ServerPlayer extends Player {
33+
}
34+
}
35+
36+
+ if (nbt.contains("Purpur.RamBar")) { this.ramBar = nbt.getBoolean("Purpur.RamBar"); } // Purpur
37+
if (nbt.contains("Purpur.TPSBar")) { this.tpsBar = nbt.getBoolean("Purpur.TPSBar"); } // Purpur
38+
if (nbt.contains("Purpur.CompassBar")) { this.compassBar = nbt.getBoolean("Purpur.CompassBar"); } // Purpur
39+
}
40+
@@ -534,6 +536,7 @@ public class ServerPlayer extends Player {
41+
}
42+
this.getBukkitEntity().setExtraData(nbt); // CraftBukkit
43+
44+
+ nbt.putBoolean("Purpur.RamBar", this.ramBar); // Purpur
45+
nbt.putBoolean("Purpur.TPSBar", this.tpsBar); // Purpur
46+
nbt.putBoolean("Purpur.CompassBar", this.compassBar); // Purpur
47+
}
48+
@@ -2607,6 +2610,14 @@ public class ServerPlayer extends Player {
49+
}
50+
}
51+
52+
+ public boolean ramBar() {
53+
+ return this.ramBar;
54+
+ }
55+
+
56+
+ public void ramBar(boolean ramBar) {
57+
+ this.ramBar = ramBar;
58+
+ }
59+
+
60+
public boolean tpsBar() {
61+
return this.tpsBar;
62+
}
63+
diff --git a/src/main/java/org/purpurmc/purpur/PurpurConfig.java b/src/main/java/org/purpurmc/purpur/PurpurConfig.java
64+
index d44b8ce90db7c6c27f71aa841d6fb64b159b7b42..f45b0002efd30b45633b31cf9995cfcbdf0f7504 100644
65+
--- a/src/main/java/org/purpurmc/purpur/PurpurConfig.java
66+
+++ b/src/main/java/org/purpurmc/purpur/PurpurConfig.java
67+
@@ -176,6 +176,8 @@ public class PurpurConfig {
68+
public static String creditsCommandOutput = "<green>%s has been shown the end credits";
69+
public static String demoCommandOutput = "<green>%s has been shown the demo screen";
70+
public static String pingCommandOutput = "<green>%s's ping is %sms";
71+
+ public static String ramCommandOutput = "<green>Ram Usage: <used>/<xmx> (<percent>)";
72+
+ public static String rambarCommandOutput = "<green>Rambar toggled <onoff> for <target>";
73+
public static String tpsbarCommandOutput = "<green>Tpsbar toggled <onoff> for <target>";
74+
public static String dontRunWithScissors = "<red><italic>Don't run with scissors!";
75+
public static String uptimeCommandOutput = "<green>Server uptime is <uptime>";
76+
@@ -191,6 +193,8 @@ public class PurpurConfig {
77+
creditsCommandOutput = getString("settings.messages.credits-command-output", creditsCommandOutput);
78+
demoCommandOutput = getString("settings.messages.demo-command-output", demoCommandOutput);
79+
pingCommandOutput = getString("settings.messages.ping-command-output", pingCommandOutput);
80+
+ ramCommandOutput = getString("settings.messages.ram-command-output", ramCommandOutput);
81+
+ rambarCommandOutput = getString("settings.messages.rambar-command-output", rambarCommandOutput);
82+
tpsbarCommandOutput = getString("settings.messages.tpsbar-command-output", tpsbarCommandOutput);
83+
dontRunWithScissors = getString("settings.messages.dont-run-with-scissors", dontRunWithScissors);
84+
uptimeCommandOutput = getString("settings.messages.uptime-command-output", uptimeCommandOutput);
85+
@@ -255,6 +259,15 @@ public class PurpurConfig {
86+
disableGiveCommandDrops = getBoolean("settings.disable-give-dropping", disableGiveCommandDrops);
87+
}
88+
89+
+ public static String commandRamBarTitle = "<gray>Ram<yellow>:</yellow> <used>/<xmx> (<percent>)";
90+
+ public static BossBar.Overlay commandRamBarProgressOverlay = BossBar.Overlay.NOTCHED_20;
91+
+ public static BossBar.Color commandRamBarProgressColorGood = BossBar.Color.GREEN;
92+
+ public static BossBar.Color commandRamBarProgressColorMedium = BossBar.Color.YELLOW;
93+
+ public static BossBar.Color commandRamBarProgressColorLow = BossBar.Color.RED;
94+
+ public static String commandRamBarTextColorGood = "<gradient:#55ff55:#00aa00><text></gradient>";
95+
+ public static String commandRamBarTextColorMedium = "<gradient:#ffff55:#ffaa00><text></gradient>";
96+
+ public static String commandRamBarTextColorLow = "<gradient:#ff5555:#aa0000><text></gradient>";
97+
+ public static int commandRamBarTickInterval = 20;
98+
public static String commandTPSBarTitle = "<gray>TPS<yellow>:</yellow> <tps> MSPT<yellow>:</yellow> <mspt> Ping<yellow>:</yellow> <ping>ms";
99+
public static BossBar.Overlay commandTPSBarProgressOverlay = BossBar.Overlay.NOTCHED_20;
100+
public static TPSBarTask.FillMode commandTPSBarProgressFillMode = TPSBarTask.FillMode.MSPT;
101+
@@ -283,6 +296,16 @@ public class PurpurConfig {
102+
public static String uptimeSecond = "%02d second";
103+
public static String uptimeSeconds = "%02d seconds";
104+
private static void commandSettings() {
105+
+ commandRamBarTitle = getString("settings.command.rambar.title", commandRamBarTitle);
106+
+ commandRamBarProgressOverlay = BossBar.Overlay.valueOf(getString("settings.command.rambar.overlay", commandRamBarProgressOverlay.name()));
107+
+ commandRamBarProgressColorGood = BossBar.Color.valueOf(getString("settings.command.rambar.progress-color.good", commandRamBarProgressColorGood.name()));
108+
+ commandRamBarProgressColorMedium = BossBar.Color.valueOf(getString("settings.command.rambar.progress-color.medium", commandRamBarProgressColorMedium.name()));
109+
+ commandRamBarProgressColorLow = BossBar.Color.valueOf(getString("settings.command.rambar.progress-color.low", commandRamBarProgressColorLow.name()));
110+
+ commandRamBarTextColorGood = getString("settings.command.rambar.text-color.good", commandRamBarTextColorGood);
111+
+ commandRamBarTextColorMedium = getString("settings.command.rambar.text-color.medium", commandRamBarTextColorMedium);
112+
+ commandRamBarTextColorLow = getString("settings.command.rambar.text-color.low", commandRamBarTextColorLow);
113+
+ commandRamBarTickInterval = getInt("settings.command.rambar.tick-interval", commandRamBarTickInterval);
114+
+
115+
commandTPSBarTitle = getString("settings.command.tpsbar.title", commandTPSBarTitle);
116+
commandTPSBarProgressOverlay = BossBar.Overlay.valueOf(getString("settings.command.tpsbar.overlay", commandTPSBarProgressOverlay.name()));
117+
commandTPSBarProgressFillMode = TPSBarTask.FillMode.valueOf(getString("settings.command.tpsbar.fill-mode", commandTPSBarProgressFillMode.name()));
118+
diff --git a/src/main/java/org/purpurmc/purpur/command/RamBarCommand.java b/src/main/java/org/purpurmc/purpur/command/RamBarCommand.java
119+
new file mode 100644
120+
index 0000000000000000000000000000000000000000..9d3f7b4acab1e4502e6ab5d5b2cc400d948e1cef
121+
--- /dev/null
122+
+++ b/src/main/java/org/purpurmc/purpur/command/RamBarCommand.java
123+
@@ -0,0 +1,43 @@
124+
+package org.purpurmc.purpur.command;
125+
+
126+
+import com.mojang.brigadier.CommandDispatcher;
127+
+import net.kyori.adventure.text.Component;
128+
+import net.kyori.adventure.text.format.NamedTextColor;
129+
+import net.kyori.adventure.text.minimessage.MiniMessage;
130+
+import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
131+
+import net.minecraft.commands.CommandSourceStack;
132+
+import net.minecraft.commands.Commands;
133+
+import net.minecraft.commands.arguments.EntityArgument;
134+
+import net.minecraft.server.level.ServerPlayer;
135+
+import org.purpurmc.purpur.PurpurConfig;
136+
+import org.purpurmc.purpur.task.RamBarTask;
137+
+
138+
+import java.util.Collection;
139+
+import java.util.Collections;
140+
+
141+
+public class RamBarCommand {
142+
+ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
143+
+ dispatcher.register(Commands.literal("rambar")
144+
+ .requires(listener -> listener.hasPermission(2))
145+
+ .executes(context -> execute(context.getSource(), Collections.singleton(context.getSource().getPlayerOrException())))
146+
+ .then(Commands.argument("targets", EntityArgument.players())
147+
+ .executes((context) -> execute(context.getSource(), EntityArgument.getPlayers(context, "targets")))
148+
+ )
149+
+ ).setPermission("bukkit.command.rambar");
150+
+ }
151+
+
152+
+ private static int execute(CommandSourceStack sender, Collection<ServerPlayer> targets) {
153+
+ for (ServerPlayer player : targets) {
154+
+ boolean result = RamBarTask.instance().togglePlayer(player.getBukkitEntity());
155+
+ player.ramBar(result);
156+
+
157+
+ Component output = MiniMessage.miniMessage().deserialize(PurpurConfig.rambarCommandOutput,
158+
+ Placeholder.component("onoff", Component.translatable(result ? "options.on" : "options.off")
159+
+ .color(result ? NamedTextColor.GREEN : NamedTextColor.RED)),
160+
+ Placeholder.parsed("target", player.getGameProfile().getName()));
161+
+
162+
+ sender.sendSuccess(output, false);
163+
+ }
164+
+ return targets.size();
165+
+ }
166+
+}
167+
diff --git a/src/main/java/org/purpurmc/purpur/command/RamCommand.java b/src/main/java/org/purpurmc/purpur/command/RamCommand.java
168+
new file mode 100644
169+
index 0000000000000000000000000000000000000000..d16263d4ddf6e28fb99a5cd32a28be262f4c36a7
170+
--- /dev/null
171+
+++ b/src/main/java/org/purpurmc/purpur/command/RamCommand.java
172+
@@ -0,0 +1,30 @@
173+
+package org.purpurmc.purpur.command;
174+
+
175+
+import com.mojang.brigadier.CommandDispatcher;
176+
+import io.papermc.paper.adventure.PaperAdventure;
177+
+import net.kyori.adventure.text.minimessage.MiniMessage;
178+
+import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
179+
+import net.minecraft.commands.CommandSourceStack;
180+
+import net.minecraft.commands.Commands;
181+
+import org.purpurmc.purpur.PurpurConfig;
182+
+import org.purpurmc.purpur.task.RamBarTask;
183+
+
184+
+public class RamCommand {
185+
+ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
186+
+ dispatcher.register(Commands.literal("ram")
187+
+ .requires(listener -> listener.hasPermission(2))
188+
+ .executes(context -> {
189+
+ CommandSourceStack sender = context.getSource();
190+
+ RamBarTask ramBar = RamBarTask.instance();
191+
+ sender.sendSuccess(PaperAdventure.asVanilla(MiniMessage.miniMessage().deserialize(PurpurConfig.ramCommandOutput,
192+
+ Placeholder.component("allocated", ramBar.format(ramBar.getAllocated())),
193+
+ Placeholder.component("used", ramBar.format(ramBar.getUsed())),
194+
+ Placeholder.component("xmx", ramBar.format(ramBar.getXmx())),
195+
+ Placeholder.component("xms", ramBar.format(ramBar.getXms())),
196+
+ Placeholder.unparsed("percent", ((int) (ramBar.getPercent() * 100)) + "%")
197+
+ )), false);
198+
+ return 1;
199+
+ })
200+
+ ).setPermission("bukkit.command.ram");
201+
+ }
202+
+}
203+
diff --git a/src/main/java/org/purpurmc/purpur/task/BossBarTask.java b/src/main/java/org/purpurmc/purpur/task/BossBarTask.java
204+
index d333334f323049ca97e756324cff0b23eddacd2a..114f273dd7f8b8a3c02f0651f6944859b33a65d4 100644
205+
--- a/src/main/java/org/purpurmc/purpur/task/BossBarTask.java
206+
+++ b/src/main/java/org/purpurmc/purpur/task/BossBarTask.java
207+
@@ -89,17 +89,22 @@ public abstract class BossBarTask extends BukkitRunnable {
208+
}
209+
210+
public static void startAll() {
211+
+ RamBarTask.instance().start();
212+
TPSBarTask.instance().start();
213+
CompassTask.instance().start();
214+
}
215+
216+
public static void stopAll() {
217+
+ RamBarTask.instance().stop();
218+
TPSBarTask.instance().stop();
219+
CompassTask.instance().stop();
220+
}
221+
222+
public static void addToAll(ServerPlayer player) {
223+
Player bukkit = player.getBukkitEntity();
224+
+ if (player.ramBar()) {
225+
+ RamBarTask.instance().addPlayer(bukkit);
226+
+ }
227+
if (player.tpsBar()) {
228+
TPSBarTask.instance().addPlayer(bukkit);
229+
}
230+
@@ -109,6 +114,7 @@ public abstract class BossBarTask extends BukkitRunnable {
231+
}
232+
233+
public static void removeFromAll(Player player) {
234+
+ RamBarTask.instance().removePlayer(player);
235+
TPSBarTask.instance().removePlayer(player);
236+
CompassTask.instance().removePlayer(player);
237+
}
238+
diff --git a/src/main/java/org/purpurmc/purpur/task/RamBarTask.java b/src/main/java/org/purpurmc/purpur/task/RamBarTask.java
239+
new file mode 100644
240+
index 0000000000000000000000000000000000000000..8e98c0ae73e2c40002a72b5d0d246ffa0c3ab38f
241+
--- /dev/null
242+
+++ b/src/main/java/org/purpurmc/purpur/task/RamBarTask.java
243+
@@ -0,0 +1,117 @@
244+
+package org.purpurmc.purpur.task;
245+
+
246+
+import net.kyori.adventure.bossbar.BossBar;
247+
+import net.kyori.adventure.text.Component;
248+
+import net.kyori.adventure.text.minimessage.MiniMessage;
249+
+import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
250+
+import org.bukkit.entity.Player;
251+
+import org.purpurmc.purpur.PurpurConfig;
252+
+
253+
+import java.lang.management.ManagementFactory;
254+
+import java.lang.management.MemoryUsage;
255+
+
256+
+public class RamBarTask extends BossBarTask {
257+
+ private static RamBarTask instance;
258+
+ private long allocated = 0L;
259+
+ private long used = 0L;
260+
+ private long xmx = 0L;
261+
+ private long xms = 0L;
262+
+ private float percent = 0F;
263+
+ private int tick = 0;
264+
+
265+
+ public static RamBarTask instance() {
266+
+ if (instance == null) {
267+
+ instance = new RamBarTask();
268+
+ }
269+
+ return instance;
270+
+ }
271+
+
272+
+ @Override
273+
+ BossBar createBossBar() {
274+
+ return BossBar.bossBar(Component.text(""), 0.0F, instance().getBossBarColor(), PurpurConfig.commandRamBarProgressOverlay);
275+
+ }
276+
+
277+
+ @Override
278+
+ void updateBossBar(BossBar bossbar, Player player) {
279+
+ bossbar.progress(getBossBarProgress());
280+
+ bossbar.color(getBossBarColor());
281+
+ bossbar.name(MiniMessage.miniMessage().deserialize(PurpurConfig.commandRamBarTitle,
282+
+ Placeholder.component("allocated", format(this.allocated)),
283+
+ Placeholder.component("used", format(this.used)),
284+
+ Placeholder.component("xmx", format(this.xmx)),
285+
+ Placeholder.component("xms", format(this.xms)),
286+
+ Placeholder.unparsed("percent", ((int) (this.percent * 100)) + "%")
287+
+ ));
288+
+ }
289+
+
290+
+ @Override
291+
+ public void run() {
292+
+ if (++this.tick < PurpurConfig.commandRamBarTickInterval) {
293+
+ return;
294+
+ }
295+
+ this.tick = 0;
296+
+
297+
+ MemoryUsage heap = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
298+
+
299+
+ this.allocated = heap.getCommitted();
300+
+ this.used = heap.getUsed();
301+
+ this.xmx = heap.getMax();
302+
+ this.xms = heap.getInit();
303+
+ this.percent = Math.max(Math.min((float) this.used / this.xmx, 1.0F), 0.0F);
304+
+
305+
+ super.run();
306+
+ }
307+
+
308+
+ private float getBossBarProgress() {
309+
+ return this.percent;
310+
+ }
311+
+
312+
+ private BossBar.Color getBossBarColor() {
313+
+ if (this.percent < 0.5F) {
314+
+ return PurpurConfig.commandRamBarProgressColorGood;
315+
+ } else if (this.percent < 0.75F) {
316+
+ return PurpurConfig.commandRamBarProgressColorMedium;
317+
+ } else {
318+
+ return PurpurConfig.commandRamBarProgressColorLow;
319+
+ }
320+
+ }
321+
+
322+
+ public Component format(long v) {
323+
+ String color;
324+
+ if (this.percent < 0.60F) {
325+
+ color = PurpurConfig.commandRamBarTextColorGood;
326+
+ } else if (this.percent < 0.85F) {
327+
+ color = PurpurConfig.commandRamBarTextColorMedium;
328+
+ } else {
329+
+ color = PurpurConfig.commandRamBarTextColorLow;
330+
+ }
331+
+ String value;
332+
+ if (v < 1024) {
333+
+ value = v + "B";
334+
+ } else {
335+
+ int z = (63 - Long.numberOfLeadingZeros(v)) / 10;
336+
+ value = String.format("%.1f%s", (double) v / (1L << (z * 10)), "BKMGTPE".charAt(z));
337+
+ }
338+
+ return MiniMessage.miniMessage().deserialize(color, Placeholder.unparsed("text", value));
339+
+ }
340+
+
341+
+ public long getAllocated() {
342+
+ return this.allocated;
343+
+ }
344+
+
345+
+ public long getUsed() {
346+
+ return this.used;
347+
+ }
348+
+
349+
+ public long getXmx() {
350+
+ return this.xmx;
351+
+ }
352+
+
353+
+ public long getXms() {
354+
+ return this.xms;
355+
+ }
356+
+
357+
+ public float getPercent() {
358+
+ return this.percent;
359+
+ }
360+
+}

0 commit comments

Comments
 (0)