55import com .mojang .brigadier .arguments .ArgumentType ;
66import com .mojang .brigadier .arguments .IntegerArgumentType ;
77import com .mojang .brigadier .builder .LiteralArgumentBuilder ;
8+ import net .minecraft .entity .Entity ;
9+ import net .minecraft .server .world .ServerWorld ;
810import net .minecraft .util .Formatting ;
9- import net .minecraft .ResourceLocationException ;
10- import net .minecraft .server .command .CommandManagerourceStack ;
11- import net .minecraft .core .registries .Registries ;
12- import net .minecraft .text .Component ;
13- import net .minecraft .text .MutableComponent ;
11+ import net .minecraft .util .InvalidIdentifierException ;
12+ import net .minecraft .server .command .ServerCommandSource ;
13+ import net .minecraft .entity .EntityType ;
1414import net .minecraft .util .Identifier ;
15+ import net .minecraft .util .registry .Registry ;
16+ import net .minecraft .text .Text ;
17+ import net .minecraft .text .MutableText ;
1518import net .minecraft .server .MinecraftServer ;
16- import net .minecraft .world .entity .EntityType ;
1719import org .apache .logging .log4j .LogManager ;
1820import org .apache .logging .log4j .Logger ;
1921
@@ -41,13 +43,13 @@ public class StatsCommand {
4143
4244 public static LiteralArgumentBuilder <ServerCommandSource > registerStatus (LiteralArgumentBuilder <ServerCommandSource > root ) {
4345 return root .then (literal ("stats" )
44- .requires (cmdSrc -> cmdSrc .hasPermission (4 ))
46+ .requires (cmdSrc -> cmdSrc .hasPermissionLevel (4 ))
4547 .executes (cmdCtx -> {
4648 showGeneralStats (cmdCtx .getSource ());
4749 return 1 ;
4850 })
4951 .then (literal ("entity" )
50- .requires (cmdSrc -> cmdSrc .hasPermission (4 ))
52+ .requires (cmdSrc -> cmdSrc .hasPermissionLevel (4 ))
5153 .executes (cmdCtx -> {
5254 showEntityStats (cmdCtx .getSource (), 0 );
5355 return 1 ;
@@ -65,18 +67,18 @@ private static void showGeneralStats(ServerCommandSource source) {
6567 double avgThreads = calculateAverageThreads ();
6668 double threadUtilization = (avgThreads / availableProcessors ) * 100.0 ;
6769
68- MutableComponent message = prefix .copy ()
69- .append (Component .literal ("Performance Statistics " ).withStyle (style -> style .withColor (ChatFormatting .GOLD )))
70- .append (Component .literal ("\n Active Processing Threads: " ).withStyle (style -> style .withColor (ChatFormatting .WHITE )))
71- .append (Component .literal (DECIMAL_FORMAT .format (avgThreads )).withStyle (style -> style .withColor (ChatFormatting .GREEN )))
72- .append (Component .literal (" / " + availableProcessors ).withStyle (style -> style .withColor (ChatFormatting .GRAY )))
73- .append (Component .literal ("\n Thread Utilization: " ).withStyle (style -> style .withColor (ChatFormatting .WHITE )))
74- .append (Component .literal (DECIMAL_FORMAT .format (threadUtilization ) + "%" ).withStyle (style -> style .withColor (ChatFormatting .GREEN )))
75- .append (Component .literal ("\n Async Status: " ).withStyle (style -> style .withColor (ChatFormatting .WHITE )))
76- .append (Component .literal (AsyncConfig .disabled ? "Disabled" : "Enabled" ).withStyle (style ->
77- style .withColor (AsyncConfig .disabled ? ChatFormatting .RED : ChatFormatting .GREEN )));
78-
79- source .sendSuccess (() -> message , true );
70+ MutableText message = prefix .copy ()
71+ .append (Text .literal ("Performance Statistics " ).styled (style -> style .withColor (Formatting .GOLD )))
72+ .append (Text .literal ("\n Active Processing Threads: " ).styled (style -> style .withColor (Formatting .WHITE )))
73+ .append (Text .literal (DECIMAL_FORMAT .format (avgThreads )).styled (style -> style .withColor (Formatting .GREEN )))
74+ .append (Text .literal (" / " + availableProcessors ).styled (style -> style .withColor (Formatting .GRAY )))
75+ .append (Text .literal ("\n Thread Utilization: " ).styled (style -> style .withColor (Formatting .WHITE )))
76+ .append (Text .literal (DECIMAL_FORMAT .format (threadUtilization ) + "%" ).styled (style -> style .withColor (Formatting .GREEN )))
77+ .append (Text .literal ("\n Async Status: " ).styled (style -> style .withColor (Formatting .WHITE )))
78+ .append (Text .literal (AsyncConfig .disabled ? "Disabled" : "Enabled" ).styled (style ->
79+ style .withColor (AsyncConfig .disabled ? Formatting .RED : Formatting .GREEN )));
80+
81+ source .sendFeedback (() -> message , true );
8082 }
8183
8284 private static void showEntityStats (ServerCommandSource source , int topCount ) {
@@ -87,15 +89,15 @@ private static void showEntityStats(ServerCommandSource source, int topCount) {
8789 AtomicInteger totalEntities = new AtomicInteger (0 );
8890 AtomicInteger totalAsyncEntities = new AtomicInteger (0 );
8991
90- MutableComponent message = prefix .copy ()
91- .append (Component .literal ("Entity Statistics " ).withStyle (style -> style .withColor (ChatFormatting .GOLD )));
92+ MutableText message = prefix .copy ()
93+ .append (Text .literal ("Entity Statistics " ).styled (style -> style .withColor (Formatting .GOLD )));
9294
93- server .getAllLevels (). forEach ( world -> {
94- String worldName = world .dimension ().location ().toString ();
95+ for ( ServerWorld world : server .getWorlds ()) {
96+ String worldName = world .getRegistryKey ().getValue ().toString ();
9597 AtomicInteger worldCount = new AtomicInteger (0 );
9698 AtomicInteger asyncCount = new AtomicInteger (0 );
9799
98- world . entityTickList . forEach ( entity -> {
100+ for ( Entity entity : world . iterateEntities ()) {
99101 if (entity != null && entity .isAlive ()) {
100102 EntityType <?> entityType = entity .getType ();
101103
@@ -109,20 +111,20 @@ private static void showEntityStats(ServerCommandSource source, int topCount) {
109111 asyncEntityTypeCounts .merge (entityType , 1 , Integer ::sum );
110112 }
111113 }
112- }) ;
114+ };
113115
114- message .append (Component .literal ("\n " + worldName + ": " ).withStyle (style -> style .withColor (ChatFormatting .YELLOW )))
115- .append (Component .literal (String .valueOf (worldCount .get ())).withStyle (style -> style .withColor (ChatFormatting .GREEN )))
116- .append (Component .literal (" entities (" ).withStyle (style -> style .withColor (ChatFormatting .GRAY )))
117- .append (Component .literal (String .valueOf (asyncCount .get ())).withStyle (style -> style .withColor (ChatFormatting .AQUA )))
118- .append (Component .literal (" async)" ).withStyle (style -> style .withColor (ChatFormatting .GRAY )));
119- }) ;
116+ message .append (Text .literal ("\n " + worldName + ": " ).styled (style -> style .withColor (Formatting .YELLOW )))
117+ .append (Text .literal (String .valueOf (worldCount .get ())).styled (style -> style .withColor (Formatting .GREEN )))
118+ .append (Text .literal (" entities (" ).styled (style -> style .withColor (Formatting .GRAY )))
119+ .append (Text .literal (String .valueOf (asyncCount .get ())).styled (style -> style .withColor (Formatting .AQUA )))
120+ .append (Text .literal (" async)" ).styled (style -> style .withColor (Formatting .GRAY )));
121+ };
120122
121- message .append (Component .literal ("\n Total Entities: " ).withStyle (style -> style .withColor (ChatFormatting .WHITE )))
122- .append (Component .literal (String .valueOf (totalEntities .get ())).withStyle (style -> style .withColor (ChatFormatting .GOLD )))
123- .append (Component .literal (" (" ).withStyle (style -> style .withColor (ChatFormatting .GRAY )))
124- .append (Component .literal (String .valueOf (totalAsyncEntities .get ())).withStyle (style -> style .withColor (ChatFormatting .AQUA )))
125- .append (Component .literal (" async)" ).withStyle (style -> style .withColor (ChatFormatting .GRAY )));
123+ message .append (Text .literal ("\n Total Entities: " ).styled (style -> style .withColor (Formatting .WHITE )))
124+ .append (Text .literal (String .valueOf (totalEntities .get ())).styled (style -> style .withColor (Formatting .GOLD )))
125+ .append (Text .literal (" (" ).styled (style -> style .withColor (Formatting .GRAY )))
126+ .append (Text .literal (String .valueOf (totalAsyncEntities .get ())).styled (style -> style .withColor (Formatting .AQUA )))
127+ .append (Text .literal (" async)" ).styled (style -> style .withColor (Formatting .GRAY )));
126128
127129 if (topCount > 0 ) {
128130 List <Map .Entry <EntityType <?>, Integer >> sortedEntities = new ArrayList <>(entityTypeCounts .entrySet ());
@@ -133,30 +135,30 @@ private static void showEntityStats(ServerCommandSource source, int topCount) {
133135 }
134136
135137 if (!sortedEntities .isEmpty ()) {
136- message .append (Component .literal ("\n \n Top " + sortedEntities .size () + " Entity Types:" ).withStyle (style -> style .withColor (ChatFormatting .GOLD )));
138+ message .append (Text .literal ("\n \n Top " + sortedEntities .size () + " Entity Types:" ).styled (style -> style .withColor (Formatting .GOLD )));
137139
138140 int rank = 1 ;
139141 for (Map .Entry <EntityType <?>, Integer > entry : sortedEntities ) {
140142 EntityType <?> type = entry .getKey ();
141143 int count = entry .getValue ();
142144 int asyncCount = asyncEntityTypeCounts .getOrDefault (type , 0 );
143145
144- ResourceLocation typeId = EntityType . getKey (type );
146+ Identifier typeId = Registry . ENTITY_TYPE . getId (type );
145147 String name = typeId .toString ();
146148
147- message .append (Component .literal ("\n " + rank + ". " ).withStyle (style -> style .withColor (ChatFormatting .GRAY )))
148- .append (Component .literal (name ).withStyle (style -> style .withColor (ChatFormatting .YELLOW )))
149- .append (Component .literal (": " ).withStyle (style -> style .withColor (ChatFormatting .GRAY )))
150- .append (Component .literal (String .valueOf (count )).withStyle (style -> style .withColor (ChatFormatting .GREEN )))
151- .append (Component .literal (" (" ).withStyle (style -> style .withColor (ChatFormatting .GRAY )))
152- .append (Component .literal (String .valueOf (asyncCount )).withStyle (style -> style .withColor (ChatFormatting .AQUA )))
153- .append (Component .literal (" async)" ).withStyle (style -> style .withColor (ChatFormatting .GRAY )));
149+ message .append (Text .literal ("\n " + rank + ". " ).styled (style -> style .withColor (Formatting .GRAY )))
150+ .append (Text .literal (name ).styled (style -> style .withColor (Formatting .YELLOW )))
151+ .append (Text .literal (": " ).styled (style -> style .withColor (Formatting .GRAY )))
152+ .append (Text .literal (String .valueOf (count )).styled (style -> style .withColor (Formatting .GREEN )))
153+ .append (Text .literal (" (" ).styled (style -> style .withColor (Formatting .GRAY )))
154+ .append (Text .literal (String .valueOf (asyncCount )).styled (style -> style .withColor (Formatting .AQUA )))
155+ .append (Text .literal (" async)" ).styled (style -> style .withColor (Formatting .GRAY )));
154156
155157 rank ++;
156158 }
157159 }
158160 }
159- source .sendSuccess (() -> message , true );
161+ source .sendFeedback (() -> message , true );
160162 });
161163 }
162164
0 commit comments