Skip to content
This repository was archived by the owner on Apr 13, 2025. It is now read-only.

Commit e13b74d

Browse files
BillyGalbreathgranny
authored andcommitted
Implement ram and rambar commands
1 parent 0082c63 commit e13b74d

8 files changed

Lines changed: 240 additions & 365 deletions

File tree

patches/server/0255-Implement-ram-and-rambar-commands.patch

Lines changed: 0 additions & 360 deletions
This file was deleted.

purpur-server/minecraft-patches/sources/net/minecraft/commands/Commands.java.patch

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import org.slf4j.Logger;
99

1010
public class Commands {
11-
@@ -245,6 +_,12 @@
11+
@@ -245,6 +_,14 @@
1212
StopCommand.register(this.dispatcher);
1313
TransferCommand.register(this.dispatcher);
1414
WhitelistCommand.register(this.dispatcher);
@@ -18,6 +18,8 @@
1818
+ org.purpurmc.purpur.command.UptimeCommand.register(this.dispatcher); // Purpur - Add uptime command
1919
+ org.purpurmc.purpur.command.TPSBarCommand.register(this.dispatcher); // Purpur - Implement TPSBar
2020
+ org.purpurmc.purpur.command.CompassCommand.register(this.dispatcher); // Purpur - Add compass command
21+
+ org.purpurmc.purpur.command.RamBarCommand.register(this.dispatcher); // Purpur - Add rambar command
22+
+ org.purpurmc.purpur.command.RamCommand.register(this.dispatcher); // Purpur - Add ram command
2123
}
2224

2325
if (selection.includeIntegrated) {

purpur-server/minecraft-patches/sources/net/minecraft/server/level/ServerPlayer.java.patch

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
11
--- a/net/minecraft/server/level/ServerPlayer.java
22
+++ b/net/minecraft/server/level/ServerPlayer.java
3-
@@ -393,6 +_,9 @@
3+
@@ -393,6 +_,10 @@
44
public com.destroystokyo.paper.event.entity.PlayerNaturallySpawnCreaturesEvent playerNaturallySpawnedEvent; // Paper - PlayerNaturallySpawnCreaturesEvent
55
public @Nullable String clientBrandName = null; // Paper - Brand support
66
public org.bukkit.event.player.PlayerQuitEvent.QuitReason quitReason = null; // Paper - Add API for quit reason; there are a lot of changes to do if we change all methods leading to the event
77
+ public boolean purpurClient = false; // Purpur - Purpur client support
88
+ private boolean tpsBar = false; // Purpur - Implement TPSBar
99
+ private boolean compassBar = false; // Purpur - Add compass command
10+
+ private boolean ramBar = false; // Purpur - Implement rambar commands
1011

1112
// Paper start - rewrite chunk system
1213
private ca.spottedleaf.moonrise.patches.chunk_system.player.RegionizedPlayerChunkLoader.PlayerChunkLoaderData chunkLoader;
13-
@@ -561,6 +_,9 @@
14+
@@ -561,6 +_,10 @@
1415
if (tag != null) {
1516
BlockPos.CODEC.parse(NbtOps.INSTANCE, tag).resultOrPartial(LOGGER::error).ifPresent(pos -> this.raidOmenPosition = pos);
1617
}
1718
+
1819
+ if (compound.contains("Purpur.TPSBar")) { this.tpsBar = compound.getBoolean("Purpur.TPSBar"); } // Purpur - Implement TPSBar
1920
+ if (compound.contains("Purpur.CompassBar")) { this.compassBar = compound.getBoolean("Purpur.CompassBar"); } // Purpur - Add compass command
21+
+ if (compound.contains("Purpur.RamBar")) { this.ramBar = compound.getBoolean("Purpur.RamBar"); } // Purpur - Implement rambar command
2022
}
2123

2224
@Override
23-
@@ -605,6 +_,8 @@
25+
@@ -605,6 +_,9 @@
2426
}
2527

2628
this.saveEnderPearls(compound);
2729
+ compound.putBoolean("Purpur.TPSBar", this.tpsBar); // Purpur - Implement TPSBar
2830
+ compound.putBoolean("Purpur.CompassBar", this.compassBar); // Purpur - Add compass command
31+
+ compound.putBoolean("Purpur.CompassBar", this.compassBar); // Purpur - Add rambar command
2932
}
3033

3134
private void saveParentVehicle(CompoundTag tag) {
@@ -214,7 +217,7 @@
214217

215218
public ServerStatsCounter getStats() {
216219
return this.stats;
217-
@@ -3077,4 +_,46 @@
220+
@@ -3077,4 +_,56 @@
218221
return (org.bukkit.craftbukkit.entity.CraftPlayer) super.getBukkitEntity();
219222
}
220223
// CraftBukkit end
@@ -260,4 +263,14 @@
260263
+ this.compassBar = compassBar;
261264
+ }
262265
+ // Purpur end - Add compass command
266+
+
267+
+ // Purpur start - Add rambar command
268+
+ public boolean ramBar() {
269+
+ return this.ramBar;
270+
+ }
271+
+
272+
+ public void ramBar(boolean ramBar) {
273+
+ this.ramBar = ramBar;
274+
+ }
275+
+ // Purpur end - Add rambar command
263276
}

purpur-server/src/main/java/org/purpurmc/purpur/PurpurConfig.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ private static Map<String, Object> toMap(ConfigurationSection section) {
178178
public static String creditsCommandOutput = "<green>%s has been shown the end credits";
179179
public static String demoCommandOutput = "<green>%s has been shown the demo screen";
180180
public static String pingCommandOutput = "<green>%s's ping is %sms";
181+
public static String ramCommandOutput = "<green>Ram Usage: <used>/<xmx> (<percent>)";
182+
public static String rambarCommandOutput = "<green>Rambar toggled <onoff> for <target>";
181183
public static String tpsbarCommandOutput = "<green>Tpsbar toggled <onoff> for <target>";
182184
public static String dontRunWithScissors = "<red><italic>Don't run with scissors!";
183185
public static String uptimeCommandOutput = "<green>Server uptime is <uptime>";
@@ -195,6 +197,8 @@ private static void messages() {
195197
creditsCommandOutput = getString("settings.messages.credits-command-output", creditsCommandOutput);
196198
demoCommandOutput = getString("settings.messages.demo-command-output", demoCommandOutput);
197199
pingCommandOutput = getString("settings.messages.ping-command-output", pingCommandOutput);
200+
ramCommandOutput = getString("settings.messages.ram-command-output", ramCommandOutput);
201+
rambarCommandOutput = getString("settings.messages.rambar-command-output", rambarCommandOutput);
198202
tpsbarCommandOutput = getString("settings.messages.tpsbar-command-output", tpsbarCommandOutput);
199203
dontRunWithScissors = getString("settings.messages.dont-run-with-scissors", dontRunWithScissors);
200204
uptimeCommandOutput = getString("settings.messages.uptime-command-output", uptimeCommandOutput);
@@ -243,6 +247,15 @@ private static void disableGiveCommandDrops() {
243247
disableGiveCommandDrops = getBoolean("settings.disable-give-dropping", disableGiveCommandDrops);
244248
}
245249

250+
public static String commandRamBarTitle = "<gray>Ram<yellow>:</yellow> <used>/<xmx> (<percent>)";
251+
public static BossBar.Overlay commandRamBarProgressOverlay = BossBar.Overlay.NOTCHED_20;
252+
public static BossBar.Color commandRamBarProgressColorGood = BossBar.Color.GREEN;
253+
public static BossBar.Color commandRamBarProgressColorMedium = BossBar.Color.YELLOW;
254+
public static BossBar.Color commandRamBarProgressColorLow = BossBar.Color.RED;
255+
public static String commandRamBarTextColorGood = "<gradient:#55ff55:#00aa00><text></gradient>";
256+
public static String commandRamBarTextColorMedium = "<gradient:#ffff55:#ffaa00><text></gradient>";
257+
public static String commandRamBarTextColorLow = "<gradient:#ff5555:#aa0000><text></gradient>";
258+
public static int commandRamBarTickInterval = 20;
246259
public static String commandTPSBarTitle = "<gray>TPS<yellow>:</yellow> <tps> MSPT<yellow>:</yellow> <mspt> Ping<yellow>:</yellow> <ping>ms";
247260
public static BossBar.Overlay commandTPSBarProgressOverlay = BossBar.Overlay.NOTCHED_20;
248261
public static TPSBarTask.FillMode commandTPSBarProgressFillMode = TPSBarTask.FillMode.MSPT;
@@ -270,6 +283,16 @@ private static void disableGiveCommandDrops() {
270283
public static String uptimeSecond = "%02d second";
271284
public static String uptimeSeconds = "%02d seconds";
272285
private static void commandSettings() {
286+
commandRamBarTitle = getString("settings.command.rambar.title", commandRamBarTitle);
287+
commandRamBarProgressOverlay = BossBar.Overlay.valueOf(getString("settings.command.rambar.overlay", commandRamBarProgressOverlay.name()));
288+
commandRamBarProgressColorGood = BossBar.Color.valueOf(getString("settings.command.rambar.progress-color.good", commandRamBarProgressColorGood.name()));
289+
commandRamBarProgressColorMedium = BossBar.Color.valueOf(getString("settings.command.rambar.progress-color.medium", commandRamBarProgressColorMedium.name()));
290+
commandRamBarProgressColorLow = BossBar.Color.valueOf(getString("settings.command.rambar.progress-color.low", commandRamBarProgressColorLow.name()));
291+
commandRamBarTextColorGood = getString("settings.command.rambar.text-color.good", commandRamBarTextColorGood);
292+
commandRamBarTextColorMedium = getString("settings.command.rambar.text-color.medium", commandRamBarTextColorMedium);
293+
commandRamBarTextColorLow = getString("settings.command.rambar.text-color.low", commandRamBarTextColorLow);
294+
commandRamBarTickInterval = getInt("settings.command.rambar.tick-interval", commandRamBarTickInterval);
295+
273296
commandTPSBarTitle = getString("settings.command.tpsbar.title", commandTPSBarTitle);
274297
commandTPSBarProgressOverlay = BossBar.Overlay.valueOf(getString("settings.command.tpsbar.overlay", commandTPSBarProgressOverlay.name()));
275298
commandTPSBarProgressFillMode = TPSBarTask.FillMode.valueOf(getString("settings.command.tpsbar.fill-mode", commandTPSBarProgressFillMode.name()));
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.purpurmc.purpur.command;
2+
3+
import com.mojang.brigadier.CommandDispatcher;
4+
import net.kyori.adventure.text.Component;
5+
import net.kyori.adventure.text.format.NamedTextColor;
6+
import net.kyori.adventure.text.minimessage.MiniMessage;
7+
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
8+
import net.minecraft.commands.CommandSourceStack;
9+
import net.minecraft.commands.Commands;
10+
import net.minecraft.commands.arguments.EntityArgument;
11+
import net.minecraft.server.level.ServerPlayer;
12+
import org.purpurmc.purpur.PurpurConfig;
13+
import org.purpurmc.purpur.task.RamBarTask;
14+
15+
import java.util.Collection;
16+
import java.util.Collections;
17+
18+
public class RamBarCommand {
19+
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
20+
dispatcher.register(Commands.literal("rambar")
21+
.requires(listener -> listener.hasPermission(2, "bukkit.command.rambar"))
22+
.executes(context -> execute(context.getSource(), Collections.singleton(context.getSource().getPlayerOrException())))
23+
.then(Commands.argument("targets", EntityArgument.players())
24+
.requires(listener -> listener.hasPermission(2, "bukkit.command.rambar.other"))
25+
.executes((context) -> execute(context.getSource(), EntityArgument.getPlayers(context, "targets")))
26+
)
27+
);
28+
}
29+
30+
private static int execute(CommandSourceStack sender, Collection<ServerPlayer> targets) {
31+
for (ServerPlayer player : targets) {
32+
boolean result = RamBarTask.instance().togglePlayer(player.getBukkitEntity());
33+
player.ramBar(result);
34+
35+
Component output = MiniMessage.miniMessage().deserialize(PurpurConfig.rambarCommandOutput,
36+
Placeholder.component("onoff", Component.translatable(result ? "options.on" : "options.off")
37+
.color(result ? NamedTextColor.GREEN : NamedTextColor.RED)),
38+
Placeholder.parsed("target", player.getGameProfile().getName()));
39+
40+
sender.sendSuccess(output, false);
41+
}
42+
return targets.size();
43+
}
44+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.purpurmc.purpur.command;
2+
3+
import com.mojang.brigadier.CommandDispatcher;
4+
import io.papermc.paper.adventure.PaperAdventure;
5+
import net.kyori.adventure.text.minimessage.MiniMessage;
6+
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
7+
import net.minecraft.commands.CommandSourceStack;
8+
import net.minecraft.commands.Commands;
9+
import org.purpurmc.purpur.PurpurConfig;
10+
import org.purpurmc.purpur.task.RamBarTask;
11+
12+
public class RamCommand {
13+
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
14+
dispatcher.register(Commands.literal("ram")
15+
.requires(listener -> listener.hasPermission(2, "bukkit.command.ram"))
16+
.executes(context -> {
17+
CommandSourceStack sender = context.getSource();
18+
RamBarTask ramBar = RamBarTask.instance();
19+
sender.sendSuccess(() -> PaperAdventure.asVanilla(MiniMessage.miniMessage().deserialize(PurpurConfig.ramCommandOutput,
20+
Placeholder.component("allocated", ramBar.format(ramBar.getAllocated())),
21+
Placeholder.component("used", ramBar.format(ramBar.getUsed())),
22+
Placeholder.component("xmx", ramBar.format(ramBar.getXmx())),
23+
Placeholder.component("xms", ramBar.format(ramBar.getXms())),
24+
Placeholder.unparsed("percent", ((int) (ramBar.getPercent() * 100)) + "%")
25+
)), false);
26+
return 1;
27+
})
28+
);
29+
}
30+
}

purpur-server/src/main/java/org/purpurmc/purpur/task/BossBarTask.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,22 @@ public void stop() {
8989
}
9090

9191
public static void startAll() {
92+
RamBarTask.instance().start();
9293
TPSBarTask.instance().start();
9394
CompassTask.instance().start();
9495
}
9596

9697
public static void stopAll() {
98+
RamBarTask.instance().stop();
9799
TPSBarTask.instance().stop();
98100
CompassTask.instance().stop();
99101
}
100102

101103
public static void addToAll(ServerPlayer player) {
102104
Player bukkit = player.getBukkitEntity();
105+
if (player.ramBar()) {
106+
RamBarTask.instance().addPlayer(bukkit);
107+
}
103108
if (player.tpsBar()) {
104109
TPSBarTask.instance().addPlayer(bukkit);
105110
}
@@ -109,6 +114,7 @@ public static void addToAll(ServerPlayer player) {
109114
}
110115

111116
public static void removeFromAll(Player player) {
117+
RamBarTask.instance().removePlayer(player);
112118
TPSBarTask.instance().removePlayer(player);
113119
CompassTask.instance().removePlayer(player);
114120
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package org.purpurmc.purpur.task;
2+
3+
import net.kyori.adventure.bossbar.BossBar;
4+
import net.kyori.adventure.text.Component;
5+
import net.kyori.adventure.text.minimessage.MiniMessage;
6+
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
7+
import org.bukkit.entity.Player;
8+
import org.purpurmc.purpur.PurpurConfig;
9+
10+
import java.lang.management.ManagementFactory;
11+
import java.lang.management.MemoryUsage;
12+
13+
public class RamBarTask extends BossBarTask {
14+
private static RamBarTask instance;
15+
private long allocated = 0L;
16+
private long used = 0L;
17+
private long xmx = 0L;
18+
private long xms = 0L;
19+
private float percent = 0F;
20+
private int tick = 0;
21+
22+
public static RamBarTask instance() {
23+
if (instance == null) {
24+
instance = new RamBarTask();
25+
}
26+
return instance;
27+
}
28+
29+
@Override
30+
BossBar createBossBar() {
31+
return BossBar.bossBar(Component.text(""), 0.0F, instance().getBossBarColor(), PurpurConfig.commandRamBarProgressOverlay);
32+
}
33+
34+
@Override
35+
void updateBossBar(BossBar bossbar, Player player) {
36+
bossbar.progress(getBossBarProgress());
37+
bossbar.color(getBossBarColor());
38+
bossbar.name(MiniMessage.miniMessage().deserialize(PurpurConfig.commandRamBarTitle,
39+
Placeholder.component("allocated", format(this.allocated)),
40+
Placeholder.component("used", format(this.used)),
41+
Placeholder.component("xmx", format(this.xmx)),
42+
Placeholder.component("xms", format(this.xms)),
43+
Placeholder.unparsed("percent", ((int) (this.percent * 100)) + "%")
44+
));
45+
}
46+
47+
@Override
48+
public void run() {
49+
if (++this.tick < PurpurConfig.commandRamBarTickInterval) {
50+
return;
51+
}
52+
this.tick = 0;
53+
54+
MemoryUsage heap = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
55+
56+
this.allocated = heap.getCommitted();
57+
this.used = heap.getUsed();
58+
this.xmx = heap.getMax();
59+
this.xms = heap.getInit();
60+
this.percent = Math.max(Math.min((float) this.used / this.xmx, 1.0F), 0.0F);
61+
62+
super.run();
63+
}
64+
65+
private float getBossBarProgress() {
66+
return this.percent;
67+
}
68+
69+
private BossBar.Color getBossBarColor() {
70+
if (this.percent < 0.5F) {
71+
return PurpurConfig.commandRamBarProgressColorGood;
72+
} else if (this.percent < 0.75F) {
73+
return PurpurConfig.commandRamBarProgressColorMedium;
74+
} else {
75+
return PurpurConfig.commandRamBarProgressColorLow;
76+
}
77+
}
78+
79+
public Component format(long v) {
80+
String color;
81+
if (this.percent < 0.60F) {
82+
color = PurpurConfig.commandRamBarTextColorGood;
83+
} else if (this.percent < 0.85F) {
84+
color = PurpurConfig.commandRamBarTextColorMedium;
85+
} else {
86+
color = PurpurConfig.commandRamBarTextColorLow;
87+
}
88+
String value;
89+
if (v < 1024) {
90+
value = v + "B";
91+
} else {
92+
int z = (63 - Long.numberOfLeadingZeros(v)) / 10;
93+
value = String.format("%.1f%s", (double) v / (1L << (z * 10)), "BKMGTPE".charAt(z));
94+
}
95+
return MiniMessage.miniMessage().deserialize(color, Placeholder.unparsed("text", value));
96+
}
97+
98+
public long getAllocated() {
99+
return this.allocated;
100+
}
101+
102+
public long getUsed() {
103+
return this.used;
104+
}
105+
106+
public long getXmx() {
107+
return this.xmx;
108+
}
109+
110+
public long getXms() {
111+
return this.xms;
112+
}
113+
114+
public float getPercent() {
115+
return this.percent;
116+
}
117+
}

0 commit comments

Comments
 (0)