Skip to content

Commit a37edaa

Browse files
RestiosonGegy
authored andcommitted
feat: remove all regions by name
1 parent 1d82c90 commit a37edaa

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

src/main/java/xyz/nucleoid/creator_tools/command/MapMetadataCommand.java

+35-27
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import net.minecraft.util.Formatting;
2929
import net.minecraft.util.Identifier;
3030
import net.minecraft.util.Pair;
31-
import net.minecraft.util.math.BlockPos;
3231
import org.jetbrains.annotations.NotNull;
3332
import org.jetbrains.annotations.Nullable;
3433
import xyz.nucleoid.creator_tools.workspace.MapWorkspace;
@@ -37,7 +36,7 @@
3736
import xyz.nucleoid.map_templates.BlockBounds;
3837

3938
import java.util.Collection;
40-
import java.util.stream.Collectors;
39+
import java.util.function.Predicate;
4140

4241
import static net.minecraft.server.command.CommandManager.argument;
4342
import static net.minecraft.server.command.CommandManager.literal;
@@ -82,15 +81,27 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
8281
.then(literal("all")
8382
.then(argument("old", StringArgumentType.word()).suggests(regionSuggestions())
8483
.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+
})
8692
)))
8793
.then(literal("here")
8894
.then(argument("old", StringArgumentType.word()).suggests(localRegionSuggestions())
8995
.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+
})
94105
)))
95106
)
96107
.then(literal("bounds")
@@ -121,6 +132,13 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
121132
.then(argument("pos", BlockPosArgumentType.blockPos())
122133
.executes(MapMetadataCommand::removeRegionAt)
123134
))
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+
))
124142
)
125143
.then(literal("commit")
126144
.then(argument("marker", StringArgumentType.word())
@@ -211,16 +229,12 @@ private static BlockBounds getPlayerBounds(ServerPlayerEntity player) {
211229
return BlockBounds.of(player.getBlockPos(), player.getBlockPos().add(0, 1, 0));
212230
}
213231

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 {
215233
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-
220234
var map = getWorkspaceForSource(source);
221235

222236
var regions = map.getRegions().stream()
223-
.filter(region -> predicate.test(region, oldMarker, playerBounds))
237+
.filter(predicate)
224238
.toList();
225239

226240
for (var region : regions) {
@@ -262,11 +276,9 @@ private static Text formatNbt(final NbtElement data) {
262276
}
263277

264278
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);
270282
return false;
271283
}
272284

@@ -288,19 +300,20 @@ private static boolean executeRegionDataRemove(CommandContext<ServerCommandSourc
288300
}
289301

290302
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));
292305
}
293306

294307
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")));
296309
}
297310

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 {
299312
var source = context.getSource();
300313
var map = getWorkspaceForSource(source);
301314

302315
var regions = map.getRegions().stream()
303-
.filter(region -> region.bounds().intersects(removalBouns))
316+
.filter(predicate)
304317
.toList();
305318

306319
for (var region : regions) {
@@ -626,9 +639,4 @@ private static Text withMapPrefix(MapWorkspace map, @Nullable Text text) {
626639
private interface RegionExecutor {
627640
boolean execute(CommandContext<ServerCommandSource> context, MapWorkspace map, WorkspaceRegion region);
628641
}
629-
630-
@FunctionalInterface
631-
private interface RegionPredicate {
632-
boolean test(WorkspaceRegion region, String marker, BlockBounds playerBounds);
633-
}
634642
}

0 commit comments

Comments
 (0)