Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove all regions by name #31

Merged
merged 1 commit into from
Jun 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import net.minecraft.util.Pair;
import net.minecraft.util.math.BlockPos;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.creator_tools.workspace.MapWorkspace;
Expand All @@ -37,7 +36,7 @@
import xyz.nucleoid.map_templates.BlockBounds;

import java.util.Collection;
import java.util.stream.Collectors;
import java.util.function.Predicate;

import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;
Expand Down Expand Up @@ -82,15 +81,27 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
.then(literal("all")
.then(argument("old", StringArgumentType.word()).suggests(regionSuggestions())
.then(argument("new", StringArgumentType.word())
.executes(context -> renameRegions(context, (region, oldMarker, playerBounds) -> region.marker().equals(oldMarker)))
.executes(context -> {
var oldMarker = StringArgumentType.getString(context, "old");
return renameRegions(
context,
StringArgumentType.getString(context, "new"),
(r) -> r.marker().equals(oldMarker)
);
})
)))
.then(literal("here")
.then(argument("old", StringArgumentType.word()).suggests(localRegionSuggestions())
.then(argument("new", StringArgumentType.word())
.executes(
context -> renameRegions(context, (region, oldMarker, playerBounds) -> region.marker().equals(oldMarker)
&& region.bounds().intersects(playerBounds))
)
.executes(context -> {
var oldMarker = StringArgumentType.getString(context, "old");
var playerBounds = getPlayerBounds(context.getSource().getPlayerOrThrow());
return renameRegions(
context,
StringArgumentType.getString(context, "new"),
(r) -> r.marker().equals(oldMarker) && r.bounds().intersects(playerBounds)
);
})
)))
)
.then(literal("bounds")
Expand Down Expand Up @@ -121,6 +132,13 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
.then(argument("pos", BlockPosArgumentType.blockPos())
.executes(MapMetadataCommand::removeRegionAt)
))
.then(literal("all")
.then(argument("marker", StringArgumentType.word()).suggests(regionSuggestions())
.executes(context -> {
final var marker = StringArgumentType.getString(context, "marker");
return removeRegions(context, r -> r.marker().equals(marker));
})
))
)
.then(literal("commit")
.then(argument("marker", StringArgumentType.word())
Expand Down Expand Up @@ -211,16 +229,12 @@ private static BlockBounds getPlayerBounds(ServerPlayerEntity player) {
return BlockBounds.of(player.getBlockPos(), player.getBlockPos().add(0, 1, 0));
}

private static int renameRegions(CommandContext<ServerCommandSource> context, RegionPredicate predicate) throws CommandSyntaxException {
private static int renameRegions(CommandContext<ServerCommandSource> context, String newMarker, Predicate<WorkspaceRegion> predicate) throws CommandSyntaxException {
var source = context.getSource();
var playerBounds = getPlayerBounds(source.getPlayerOrThrow());
var oldMarker = StringArgumentType.getString(context, "old");
var newMarker = StringArgumentType.getString(context, "new");

var map = getWorkspaceForSource(source);

var regions = map.getRegions().stream()
.filter(region -> predicate.test(region, oldMarker, playerBounds))
.filter(predicate)
.toList();

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

private static boolean executeRegionDataGet(CommandContext<ServerCommandSource> context, MapWorkspace map, WorkspaceRegion region) {
context.getSource().sendFeedback(() -> {
return withMapPrefix(map,
Text.translatable("text.nucleoid_creator_tools.map.region.data.get", region.marker(), formatNbt(region.data()))
);
}, false);
context.getSource().sendFeedback(() -> withMapPrefix(map,
Text.translatable("text.nucleoid_creator_tools.map.region.data.get", region.marker(), formatNbt(region.data()))
), false);
return false;
}

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

private static int removeRegionHere(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
return removeRegion(context, getPlayerBounds(context.getSource().getPlayer()));
final var playerBounds = getPlayerBounds(context.getSource().getPlayerOrThrow());
return removeRegions(context, region -> region.bounds().intersects(playerBounds));
}

private static int removeRegionAt(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
return removeRegion(context, BlockBounds.ofBlock(BlockPosArgumentType.getBlockPos(context, "pos")));
return removeRegions(context, region -> region.bounds().contains(BlockPosArgumentType.getBlockPos(context, "pos")));
}

private static int removeRegion(CommandContext<ServerCommandSource> context, BlockBounds removalBouns) throws CommandSyntaxException {
private static int removeRegions(CommandContext<ServerCommandSource> context, Predicate<WorkspaceRegion> predicate) throws CommandSyntaxException {
var source = context.getSource();
var map = getWorkspaceForSource(source);

var regions = map.getRegions().stream()
.filter(region -> region.bounds().intersects(removalBouns))
.filter(predicate)
.toList();

for (var region : regions) {
Expand Down Expand Up @@ -626,9 +639,4 @@ private static Text withMapPrefix(MapWorkspace map, @Nullable Text text) {
private interface RegionExecutor {
boolean execute(CommandContext<ServerCommandSource> context, MapWorkspace map, WorkspaceRegion region);
}

@FunctionalInterface
private interface RegionPredicate {
boolean test(WorkspaceRegion region, String marker, BlockBounds playerBounds);
}
}
Loading