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

Implement replace functionality for /cghostblock fill #585

Merged
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 @@ -6,13 +6,17 @@
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.block.BlockState;
import net.minecraft.block.pattern.CachedBlockPosition;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockBox;
import net.minecraft.util.math.BlockPos;

import java.util.function.Predicate;

import static dev.xpple.clientarguments.arguments.CBlockPosArgumentType.*;
import static dev.xpple.clientarguments.arguments.CBlockPredicateArgumentType.*;
import static dev.xpple.clientarguments.arguments.CBlockStateArgumentType.*;
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.*;

Expand All @@ -31,7 +35,10 @@ public static void register(CommandDispatcher<FabricClientCommandSource> dispatc
.then(argument("from", blockPos())
.then(argument("to", blockPos())
.then(argument("block", blockState(registryAccess))
.executes(ctx -> fillGhostBlocks(ctx.getSource(), getCBlockPos(ctx, "from"), getCBlockPos(ctx, "to"), getCBlockState(ctx, "block").getBlockState())))))));
.executes(ctx -> fillGhostBlocks(ctx.getSource(), getCBlockPos(ctx, "from"), getCBlockPos(ctx, "to"), getCBlockState(ctx, "block").getBlockState(), pos -> true))
.then(literal("replace")
.then(argument("filter", blockPredicate(registryAccess))
.executes(ctx -> fillGhostBlocks(ctx.getSource(), getCBlockPos(ctx, "from"), getCBlockPos(ctx, "to"), getCBlockState(ctx, "block").getBlockState(), getCBlockPredicate(ctx, "filter"))))))))));
}

private static int setGhostBlock(FabricClientCommandSource source, BlockPos pos, BlockState state) throws CommandSyntaxException {
Expand All @@ -49,7 +56,7 @@ private static int setGhostBlock(FabricClientCommandSource source, BlockPos pos,
}
}

private static int fillGhostBlocks(FabricClientCommandSource source, BlockPos from, BlockPos to, BlockState state) throws CommandSyntaxException {
private static int fillGhostBlocks(FabricClientCommandSource source, BlockPos from, BlockPos to, BlockState state, Predicate<CachedBlockPosition> filter) throws CommandSyntaxException {
ClientWorld world = source.getWorld();
assert world != null;

Expand All @@ -59,8 +66,10 @@ private static int fillGhostBlocks(FabricClientCommandSource source, BlockPos fr
BlockBox range = BlockBox.create(from, to);
int successCount = 0;
for (BlockPos pos : BlockPos.iterate(range.getMinX(), range.getMinY(), range.getMinZ(), range.getMaxX(), range.getMaxY(), range.getMaxZ())) {
if (world.setBlockState(pos, state, 18)) {
successCount++;
if (filter.test(new CachedBlockPosition(world, pos, true))) {
if (world.setBlockState(pos, state, 18)) {
successCount++;
}
}
}

Expand Down