28
28
import net .minecraft .util .Formatting ;
29
29
import net .minecraft .util .Identifier ;
30
30
import net .minecraft .util .Pair ;
31
- import net .minecraft .util .math .BlockPos ;
32
31
import org .jetbrains .annotations .NotNull ;
33
32
import org .jetbrains .annotations .Nullable ;
34
33
import xyz .nucleoid .creator_tools .workspace .MapWorkspace ;
37
36
import xyz .nucleoid .map_templates .BlockBounds ;
38
37
39
38
import java .util .Collection ;
40
- import java .util .stream . Collectors ;
39
+ import java .util .function . Predicate ;
41
40
42
41
import static net .minecraft .server .command .CommandManager .argument ;
43
42
import static net .minecraft .server .command .CommandManager .literal ;
@@ -82,15 +81,27 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
82
81
.then (literal ("all" )
83
82
.then (argument ("old" , StringArgumentType .word ()).suggests (regionSuggestions ())
84
83
.then (argument ("new" , StringArgumentType .word ())
85
- .executes (context -> renameRegions (context , (region , oldMarker , playerBounds ) -> region .marker ().equals (oldMarker )))
84
+ .executes (context -> {
85
+ var oldMarker = StringArgumentType .getString (context , "old" );
86
+ return renameRegions (
87
+ context ,
88
+ StringArgumentType .getString (context , "new" ),
89
+ (r ) -> r .marker ().equals (oldMarker )
90
+ );
91
+ })
86
92
)))
87
93
.then (literal ("here" )
88
94
.then (argument ("old" , StringArgumentType .word ()).suggests (localRegionSuggestions ())
89
95
.then (argument ("new" , StringArgumentType .word ())
90
- .executes (
91
- context -> renameRegions (context , (region , oldMarker , playerBounds ) -> region .marker ().equals (oldMarker )
92
- && region .bounds ().intersects (playerBounds ))
93
- )
96
+ .executes (context -> {
97
+ var oldMarker = StringArgumentType .getString (context , "old" );
98
+ var playerBounds = getPlayerBounds (context .getSource ().getPlayerOrThrow ());
99
+ return renameRegions (
100
+ context ,
101
+ StringArgumentType .getString (context , "new" ),
102
+ (r ) -> r .marker ().equals (oldMarker ) && r .bounds ().intersects (playerBounds )
103
+ );
104
+ })
94
105
)))
95
106
)
96
107
.then (literal ("bounds" )
@@ -121,6 +132,13 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
121
132
.then (argument ("pos" , BlockPosArgumentType .blockPos ())
122
133
.executes (MapMetadataCommand ::removeRegionAt )
123
134
))
135
+ .then (literal ("all" )
136
+ .then (argument ("marker" , StringArgumentType .word ()).suggests (regionSuggestions ())
137
+ .executes (context -> {
138
+ final var marker = StringArgumentType .getString (context , "marker" );
139
+ return removeRegions (context , r -> r .marker ().equals (marker ));
140
+ })
141
+ ))
124
142
)
125
143
.then (literal ("commit" )
126
144
.then (argument ("marker" , StringArgumentType .word ())
@@ -211,16 +229,12 @@ private static BlockBounds getPlayerBounds(ServerPlayerEntity player) {
211
229
return BlockBounds .of (player .getBlockPos (), player .getBlockPos ().add (0 , 1 , 0 ));
212
230
}
213
231
214
- private static int renameRegions (CommandContext <ServerCommandSource > context , RegionPredicate predicate ) throws CommandSyntaxException {
232
+ private static int renameRegions (CommandContext <ServerCommandSource > context , String newMarker , Predicate < WorkspaceRegion > predicate ) throws CommandSyntaxException {
215
233
var source = context .getSource ();
216
- var playerBounds = getPlayerBounds (source .getPlayerOrThrow ());
217
- var oldMarker = StringArgumentType .getString (context , "old" );
218
- var newMarker = StringArgumentType .getString (context , "new" );
219
-
220
234
var map = getWorkspaceForSource (source );
221
235
222
236
var regions = map .getRegions ().stream ()
223
- .filter (region -> predicate . test ( region , oldMarker , playerBounds ) )
237
+ .filter (predicate )
224
238
.toList ();
225
239
226
240
for (var region : regions ) {
@@ -262,11 +276,9 @@ private static Text formatNbt(final NbtElement data) {
262
276
}
263
277
264
278
private static boolean executeRegionDataGet (CommandContext <ServerCommandSource > context , MapWorkspace map , WorkspaceRegion region ) {
265
- context .getSource ().sendFeedback (() -> {
266
- return withMapPrefix (map ,
267
- Text .translatable ("text.nucleoid_creator_tools.map.region.data.get" , region .marker (), formatNbt (region .data ()))
268
- );
269
- }, false );
279
+ context .getSource ().sendFeedback (() -> withMapPrefix (map ,
280
+ Text .translatable ("text.nucleoid_creator_tools.map.region.data.get" , region .marker (), formatNbt (region .data ()))
281
+ ), false );
270
282
return false ;
271
283
}
272
284
@@ -288,19 +300,20 @@ private static boolean executeRegionDataRemove(CommandContext<ServerCommandSourc
288
300
}
289
301
290
302
private static int removeRegionHere (CommandContext <ServerCommandSource > context ) throws CommandSyntaxException {
291
- return removeRegion (context , getPlayerBounds (context .getSource ().getPlayer ()));
303
+ final var playerBounds = getPlayerBounds (context .getSource ().getPlayerOrThrow ());
304
+ return removeRegions (context , region -> region .bounds ().intersects (playerBounds ));
292
305
}
293
306
294
307
private static int removeRegionAt (CommandContext <ServerCommandSource > context ) throws CommandSyntaxException {
295
- return removeRegion (context , BlockBounds . ofBlock (BlockPosArgumentType .getBlockPos (context , "pos" )));
308
+ return removeRegions (context , region -> region . bounds (). contains (BlockPosArgumentType .getBlockPos (context , "pos" )));
296
309
}
297
310
298
- private static int removeRegion (CommandContext <ServerCommandSource > context , BlockBounds removalBouns ) throws CommandSyntaxException {
311
+ private static int removeRegions (CommandContext <ServerCommandSource > context , Predicate < WorkspaceRegion > predicate ) throws CommandSyntaxException {
299
312
var source = context .getSource ();
300
313
var map = getWorkspaceForSource (source );
301
314
302
315
var regions = map .getRegions ().stream ()
303
- .filter (region -> region . bounds (). intersects ( removalBouns ) )
316
+ .filter (predicate )
304
317
.toList ();
305
318
306
319
for (var region : regions ) {
@@ -626,9 +639,4 @@ private static Text withMapPrefix(MapWorkspace map, @Nullable Text text) {
626
639
private interface RegionExecutor {
627
640
boolean execute (CommandContext <ServerCommandSource > context , MapWorkspace map , WorkspaceRegion region );
628
641
}
629
-
630
- @ FunctionalInterface
631
- private interface RegionPredicate {
632
- boolean test (WorkspaceRegion region , String marker , BlockBounds playerBounds );
633
- }
634
642
}
0 commit comments