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: broaden region intersection with player #30

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 @@ -23,6 +23,7 @@
import net.minecraft.nbt.visitor.NbtTextFormatter;
import net.minecraft.registry.Registries;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
Expand Down Expand Up @@ -81,14 +82,14 @@ 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, pos) -> region.marker().equals(oldMarker)))
.executes(context -> renameRegions(context, (region, oldMarker, playerBounds) -> region.marker().equals(oldMarker)))
)))
.then(literal("here")
.then(argument("old", StringArgumentType.word()).suggests(localRegionSuggestions())
.then(argument("new", StringArgumentType.word())
.executes(
context -> renameRegions(context, (region, oldMarker, pos) -> region.marker().equals(oldMarker)
&& region.bounds().contains(pos))
context -> renameRegions(context, (region, oldMarker, playerBounds) -> region.marker().equals(oldMarker)
&& region.bounds().intersects(playerBounds))
)
)))
)
Expand Down Expand Up @@ -206,17 +207,20 @@ private static int addRegion(CommandContext<ServerCommandSource> context, NbtCom
return Command.SINGLE_SUCCESS;
}

private static BlockBounds getPlayerBounds(ServerPlayerEntity player) {
Restioson marked this conversation as resolved.
Show resolved Hide resolved
return BlockBounds.of(player.getBlockPos(), player.getBlockPos().add(0, 1, 0));
}

private static int renameRegions(CommandContext<ServerCommandSource> context, RegionPredicate predicate) throws CommandSyntaxException {
var source = context.getSource();
var pos = source.getPlayerOrThrow().getBlockPos();

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, pos))
.filter(region -> predicate.test(region, oldMarker, playerBounds))
.toList();

for (var region : regions) {
Expand Down Expand Up @@ -284,19 +288,19 @@ private static boolean executeRegionDataRemove(CommandContext<ServerCommandSourc
}

private static int removeRegionHere(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
return removeRegion(context, context.getSource().getPlayer().getBlockPos());
return removeRegion(context, getPlayerBounds(context.getSource().getPlayer()));
}

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

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

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

for (var region : regions) {
Expand Down Expand Up @@ -561,9 +565,9 @@ private static SuggestionProvider<ServerCommandSource> regionSuggestions() {
private static SuggestionProvider<ServerCommandSource> localRegionSuggestions() {
return (context, builder) -> {
var map = getWorkspaceForSource(context.getSource());
var sourcePos = context.getSource().getPlayerOrThrow().getBlockPos();
var sourceBounds = getPlayerBounds(context.getSource().getPlayerOrThrow());
return CommandSource.suggestMatching(
map.getRegions().stream().filter(region -> region.bounds().contains(sourcePos))
map.getRegions().stream().filter(region -> region.bounds().intersects(sourceBounds))
.map(WorkspaceRegion::marker),
builder
);
Expand All @@ -583,13 +587,13 @@ private static SuggestionProvider<ServerCommandSource> localRegionSuggestions()
private static Command<ServerCommandSource> executeInRegions(String message, RegionExecutor executor) {
return context -> {
var source = context.getSource();
var pos = source.getPlayerOrThrow().getBlockPos();
var playerBounds = getPlayerBounds(source.getPlayerOrThrow());

var marker = StringArgumentType.getString(context, "marker");

var map = getWorkspaceForSource(context.getSource());
var regions = map.getRegions().stream()
.filter(region -> region.bounds().contains(pos) && region.marker().equals(marker))
.filter(region -> region.bounds().intersects(playerBounds) && region.marker().equals(marker))
.toList();

int count = 0;
Expand Down Expand Up @@ -625,6 +629,6 @@ private interface RegionExecutor {

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