20
20
import net .minecraft .network .chat .ClickEvent ;
21
21
import net .minecraft .network .chat .Component ;
22
22
import net .minecraft .network .chat .HoverEvent ;
23
+ import net .minecraft .network .chat .MutableComponent ;
23
24
import net .minecraft .resources .ResourceKey ;
24
25
import net .minecraft .resources .ResourceLocation ;
25
26
import net .minecraft .server .MinecraftServer ;
@@ -39,45 +40,66 @@ public class ListLoadersCommand {
39
40
public static ArgumentBuilder <CommandSourceStack , ?> register () {
40
41
return Commands .literal ("list" )
41
42
.requires (cs -> cs .hasPermission (2 ))
42
- .then (
43
- Commands .argument ("type" , EnumArgument .enumArgument (LoaderMode .class ))
44
- .then (
45
- Commands .literal ("limit" )
46
- .then (
47
- Commands .argument ("limit" , IntegerArgumentType .integer (1 ))
48
- .executes (handler (true , true ))
49
- )
50
- )
51
- .executes (handler (true , false ))
43
+ .then (Commands .argument ("type" , EnumArgument .enumArgument (LoaderMode .class ))
44
+ .then (Commands .literal ("active" )
45
+ .then (Commands .literal ("limit" )
46
+ .then (Commands .argument ("limit" , IntegerArgumentType .integer (1 ))
47
+ .executes (handler (true , true , true ))
48
+ )
49
+ ).executes (handler (true , false , true ))
50
+ )
51
+ .then (Commands .literal ("all" )
52
+ .then (Commands .literal ("limit" )
53
+ .then (Commands .argument ("limit" , IntegerArgumentType .integer (1 ))
54
+ .executes (handler (true , true , false ))
55
+ )
56
+ ).executes (handler (true , false , false ))
57
+ )
52
58
)
53
- .then (
54
- Commands .literal ("limit" )
55
- .then (
56
- Commands .argument ("limit" , IntegerArgumentType .integer (1 ))
57
- .executes (handler (false , true ))
58
- )
59
- )
60
- .executes (handler (false , false ));
59
+ .then (Commands .literal ("all" )
60
+ .then (Commands .literal ("active" )
61
+ .then (Commands .literal ("limit" )
62
+ .then (Commands .argument ("limit" , IntegerArgumentType .integer (1 ))
63
+ .executes (handler (false , true , true ))
64
+ )
65
+ ).executes (handler (false , false , true ))
66
+ )
67
+ .then (Commands .literal ("all" )
68
+ .then (Commands .literal ("limit" )
69
+ .then (Commands .argument ("limit" , IntegerArgumentType .integer (1 ))
70
+ .executes (handler (false , true , false ))
71
+ )
72
+ ).executes (handler (false , false , false ))
73
+ )
74
+ );
61
75
}
62
76
63
- private static Command <CommandSourceStack > handler (boolean hasMode , boolean hasLimit ) {
77
+ private static Command <CommandSourceStack > handler (boolean hasMode , boolean hasLimit , boolean activeOnly ) {
64
78
return ctx -> {
65
79
CommandSourceStack source = ctx .getSource ();
66
80
fillReport (source .getLevel (), source .getPosition (),
67
81
hasMode ? ctx .getArgument ("type" , LoaderMode .class ) : null ,
68
- hasLimit ? ctx .getArgument ("limit" , Integer .class ) : Integer .MAX_VALUE ,
82
+ hasLimit ? ctx .getArgument ("limit" , Integer .class ) : 20 ,
83
+ activeOnly ,
69
84
(s , f ) -> source .sendSuccess (() -> Components .literal (s ).withStyle (st -> st .withColor (f )), false ),
70
85
(c ) -> source .sendSuccess (() -> c , false ));
71
86
return Command .SINGLE_SUCCESS ;
72
87
};
73
88
}
74
89
75
- private static void fillReport (ServerLevel level , Vec3 location , @ Nullable LoaderMode mode , int limit , BiConsumer <String , Integer > chat ,
90
+ private static void fillReport (ServerLevel level ,
91
+ Vec3 location ,
92
+ @ Nullable LoaderMode mode ,
93
+ int limit ,
94
+ boolean activeOnly ,
95
+ BiConsumer <String , Integer > chat ,
76
96
Consumer <Component > chatRaw ) {
77
97
int white = ChatFormatting .WHITE .getColor ();
98
+ int gray = ChatFormatting .GRAY .getColor ();
78
99
int blue = 0xD3DEDC ;
79
- int bright = 0xFFEFEF ;
100
+ int darkBlue = 0x5955A1 ;
80
101
int orange = 0xFFAD60 ;
102
+ int darkOrange = 0xB27943 ;
81
103
82
104
List <ChunkLoader > loaders = new LinkedList <>();
83
105
if (mode == null ) {
@@ -87,7 +109,20 @@ private static void fillReport(ServerLevel level, Vec3 location, @Nullable Loade
87
109
} else {
88
110
loaders .addAll (ChunkLoadManager .allLoaders .get (mode ));
89
111
}
90
- loaders .removeIf (loader -> loader .getForcedChunks ().size () == 0 );
112
+ loaders .removeIf (loader -> {
113
+ if (loader instanceof TrainChunkLoader trainLoader ) {
114
+ for (CarriageChunkLoader carriageLoader : trainLoader .carriageLoaders ) {
115
+ if (carriageLoader .known && (carriageLoader .brass || carriageLoader .andesite )) return false ;
116
+ }
117
+ return true ;
118
+ } else if (loader instanceof StationChunkLoader stationLoader ) {
119
+ return stationLoader .attachments .size () == 0 ;
120
+ } else {
121
+ return false ;
122
+ }
123
+ });
124
+ if (activeOnly )
125
+ loaders .removeIf (loader -> loader .getForcedChunks ().size () == 0 );
91
126
92
127
Map <ResourceLocation , DimensionType > typeCache = new HashMap <>();
93
128
MinecraftServer server = level .getServer ();
@@ -106,62 +141,78 @@ private static void fillReport(ServerLevel level, Vec3 location, @Nullable Loade
106
141
107
142
chat .accept ("" , white );
108
143
chat .accept ("-+------<< Chunk Loader List >>------+-" , white );
109
- chat .accept (pairs .size () + " out of " + loaders .size () + " nearest" + (mode != null ? " " + mode .getSerializedName () : "" ) + " loaders" , blue );
144
+ chat .accept (pairs .size () + " out of " + loaders .size () + " nearest" + (activeOnly ? " active" : "" ) + (mode != null ? " " + mode .getSerializedName () : "" ) + " loaders" , blue );
145
+ chat .accept ("" , white );
110
146
for (Pair <ChunkLoader , Pair <ResourceLocation , Vec3 >> pair : pairs ) {
111
147
ChunkLoader loader = pair .getFirst ();
112
148
ResourceLocation dimension = pair .getSecond ().getFirst ();
113
149
BlockPos pos = BlockPos .containing (pair .getSecond ().getSecond ());
114
150
115
- chatRaw .accept (createTpButton (dimension , pos ,
116
- (mode == null ? " " + loader .getLoaderMode ().getSerializedName () + " " : "" )
117
- + "[" + pos .toShortString () + "]"
118
- + (!dimension .equals (level .dimension ().location ()) ? " in " + dimension : "" )
119
- , white ));
120
-
121
- chat .accept (
122
- " "
123
- + loader .getLoaderType ().getSerializedName () + " - "
124
- + loader .getForcedChunks ().size () + " chunks"
125
- , orange );
151
+ chatRaw .accept (
152
+ text (mode == null ? loader .getLoaderMode ().getSerializedName () + " - " : "" , white )
153
+ .append (text (loader .getLoaderType ().getSerializedName () + " - " , orange ))
154
+ .append (text (loader .getForcedChunks ().size () + " chunks" , colorForCount (loader .getForcedChunks ().size ())))
155
+ );
156
+
157
+ chatRaw .accept (
158
+ text (" ↳ " , darkBlue )
159
+ .append (createTpButton (level .dimension ().location (), dimension , pos , darkBlue ))
160
+ );
126
161
if (loader instanceof TrainChunkLoader trainLoader ) {
127
162
for (int i = 0 ; i < trainLoader .carriageLoaders .size (); i ++) {
128
163
CarriageChunkLoader carriageLoader = trainLoader .carriageLoaders .get (i );
129
164
if (carriageLoader .getForcedChunks ().isEmpty ()) continue ;
130
165
Pair <ResourceLocation , BlockPos > carriageLocation = carriageLoader .getLocation ();
131
- chatRaw .accept (createTpButton (carriageLocation .getFirst (), carriageLocation .getSecond (),
132
- " Carriage " + (i + 1 ) + " - "
133
- + "[" + carriageLocation .getSecond ().toShortString () + "]"
134
- + (!carriageLocation .getFirst ().equals (level .dimension ().location ()) ? " in " + carriageLocation .getFirst ().toString () : "" )
135
- , blue ));
136
- chat .accept (
137
- " "
138
- + carriageLoader .getLoaderType ().getSerializedName () + " - "
139
- + carriageLoader .getForcedChunks ().size () + " chunks"
140
- , orange );
166
+ chatRaw .accept (
167
+ text (" Carriage " + (i + 1 ) + " - " , gray )
168
+ .append (text (carriageLoader .getLoaderType ().getSerializedName () + " - " , darkOrange ))
169
+ .append (text (carriageLoader .getForcedChunks ().size () + " chunks" , colorForCount (carriageLoader .getForcedChunks ().size ())))
170
+ );
171
+ chatRaw .accept (
172
+ text (" ↳ " , darkBlue )
173
+ .append (createTpButton (level .dimension ().location (), carriageLocation .getFirst (), carriageLocation .getSecond (), darkBlue ))
174
+ );
141
175
}
142
176
} else if (loader instanceof StationChunkLoader stationLoader ) {
143
177
for (StationChunkLoader .AttachedLoader attachment : stationLoader .attachments ) {
144
- chatRaw .accept (createTpButton (stationLoader .getLocation ().getFirst (), attachment .pos (),
145
- " "
146
- + attachment .type ().getSerializedName ()
147
- + " - "
148
- + "[" + attachment .pos ().toShortString () + "]"
149
- , blue ));
178
+ chatRaw .accept (
179
+ text (" " , gray )
180
+ .append (text ("Attached - " , gray ))
181
+ .append (text (attachment .type ().getSerializedName (), darkOrange ))
182
+ );
183
+ chatRaw .accept (
184
+ text (" ↳ " , darkBlue )
185
+ .append (createTpButton (level .dimension ().location (), stationLoader .getLocation ().getFirst (), attachment .pos (), darkBlue ))
186
+ );
150
187
}
151
188
}
152
189
}
153
190
chat .accept ("-+--------------------------------+-" , white );
154
191
}
155
192
156
- private static Component createTpButton (ResourceLocation dimension , BlockPos blockPos , String text , int color ) {
193
+ private static int colorForCount (int count ) {
194
+ if (count == 0 ) return ChatFormatting .DARK_GRAY .getColor ();
195
+ if (count < 5 ) return ChatFormatting .GRAY .getColor ();
196
+ if (count < 10 ) return ChatFormatting .YELLOW .getColor ();
197
+ return ChatFormatting .RED .getColor ();
198
+ }
199
+
200
+ private static String shortString (ResourceLocation location ) {
201
+ if (location .getNamespace ().equals (ResourceLocation .DEFAULT_NAMESPACE )) return location .getPath ();
202
+ return location .toString ();
203
+ }
204
+
205
+ private static MutableComponent text (String text , int color ) {
206
+ return Components .literal (text ).withStyle (style -> style .withColor (color ));
207
+ }
208
+
209
+ private static MutableComponent createTpButton (ResourceLocation origin , ResourceLocation dimension , BlockPos blockPos , int color ) {
157
210
String teleport = "/execute in " + dimension .toString () + " run tp @s " + blockPos .getX () + " " + blockPos .getY () + " " + blockPos .getZ ();
158
- return Components .literal (text ).withStyle ((p_180514_ ) -> {
159
- return p_180514_ .withColor (color )
160
- .withClickEvent (new ClickEvent (ClickEvent .Action .RUN_COMMAND , teleport ))
161
- .withHoverEvent (new HoverEvent (HoverEvent .Action .SHOW_TEXT ,
162
- Components .literal ("Click to teleport" )))
163
- .withInsertion (teleport );
164
- });
211
+ return Components .literal ("[" + blockPos .toShortString () + "]" + (!origin .equals (dimension ) ? " in " + shortString (dimension ) : "" )).withStyle ((style ) -> style
212
+ .withColor (color )
213
+ .withClickEvent (new ClickEvent (ClickEvent .Action .RUN_COMMAND , teleport ))
214
+ .withHoverEvent (new HoverEvent (HoverEvent .Action .SHOW_TEXT , Components .literal ("Click to teleport" )))
215
+ .withInsertion (teleport ));
165
216
}
166
217
167
218
}
0 commit comments