|
| 1 | +package anticope.rejects.commands; |
| 2 | + |
| 3 | +import anticope.rejects.arguments.ClientPosArgumentType; |
| 4 | +import com.mojang.brigadier.arguments.StringArgumentType; |
| 5 | +import com.mojang.brigadier.builder.LiteralArgumentBuilder; |
| 6 | +import meteordevelopment.meteorclient.commands.Command; |
| 7 | +import net.minecraft.block.BlockState; |
| 8 | +import net.minecraft.command.CommandSource; |
| 9 | +import net.minecraft.command.argument.BlockStateArgument; |
| 10 | +import net.minecraft.command.argument.BlockStateArgumentType; |
| 11 | +import net.minecraft.util.math.BlockPos; |
| 12 | +import net.minecraft.util.math.Vec3d; |
| 13 | + |
| 14 | +public class FillCommand extends Command { |
| 15 | + |
| 16 | + public FillCommand() { |
| 17 | + super("fill", "Fills a specified area with a block"); |
| 18 | + } |
| 19 | + @Override |
| 20 | + public void build(LiteralArgumentBuilder<CommandSource> builder) { |
| 21 | + builder.then(argument("from-pos", ClientPosArgumentType.pos()).then(argument("to-pos", ClientPosArgumentType.pos()).then(argument("block", BlockStateArgumentType.blockState(REGISTRY_ACCESS)).executes(ctx -> { |
| 22 | + Vec3d fromPos = ClientPosArgumentType.getPos(ctx, "from-pos"); |
| 23 | + Vec3d toPos = ClientPosArgumentType.getPos(ctx, "to-pos"); |
| 24 | + BlockState blockState = ctx.getArgument("block", BlockStateArgument.class).getBlockState(); |
| 25 | + |
| 26 | + fillArea(fromPos, toPos, blockState, null); |
| 27 | + |
| 28 | + return SINGLE_SUCCESS; |
| 29 | + })))); |
| 30 | + |
| 31 | + builder.then(argument("from-pos", ClientPosArgumentType.pos()).then(argument("to-pos", ClientPosArgumentType.pos()).then(argument("block", BlockStateArgumentType.blockState(REGISTRY_ACCESS)).then(argument("replace", StringArgumentType.string()).then(argument("filter", BlockStateArgumentType.blockState(REGISTRY_ACCESS)).executes(ctx -> { |
| 32 | + Vec3d fromPos = ClientPosArgumentType.getPos(ctx, "from-pos"); |
| 33 | + Vec3d toPos = ClientPosArgumentType.getPos(ctx, "to-pos"); |
| 34 | + BlockState findBlock = ctx.getArgument("block", BlockStateArgument.class).getBlockState(); |
| 35 | + BlockState filterBlock = ctx.getArgument("filter", BlockStateArgument.class).getBlockState(); |
| 36 | + |
| 37 | + fillArea(fromPos, toPos, findBlock, filterBlock); |
| 38 | + |
| 39 | + return SINGLE_SUCCESS; |
| 40 | + })))))); |
| 41 | + } |
| 42 | + |
| 43 | + private void fillArea(Vec3d fromPos, Vec3d toPos, BlockState blockState, BlockState filterBlock) { |
| 44 | + MinMaxCoords coords = getMinMaxCoords(fromPos, toPos); |
| 45 | + |
| 46 | + for (int x = coords.minX; x <= coords.maxX; x++) { |
| 47 | + for (int y = coords.minY; y <= coords.maxY; y++) { |
| 48 | + for (int z = coords.minZ; z <= coords.maxZ; z++) { |
| 49 | + BlockPos pos = new BlockPos(x, y, z); |
| 50 | + |
| 51 | + if (filterBlock == null || mc.world.getBlockState(pos).equals(filterBlock)) { |
| 52 | + mc.world.setBlockState(pos, blockState); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + //Send help |
| 60 | + private MinMaxCoords getMinMaxCoords(Vec3d fromPos, Vec3d toPos) { |
| 61 | + int minX = Math.min((int) fromPos.getX(), (int) toPos.getX()); |
| 62 | + int maxX = Math.max((int) fromPos.getX(), (int) toPos.getX()); |
| 63 | + int minY = Math.min((int) fromPos.getY(), (int) toPos.getY()); |
| 64 | + int maxY = Math.max((int) fromPos.getY(), (int) toPos.getY()); |
| 65 | + int minZ = Math.min((int) fromPos.getZ(), (int) toPos.getZ()); |
| 66 | + int maxZ = Math.max((int) fromPos.getZ(), (int) toPos.getZ()); |
| 67 | + |
| 68 | + return new MinMaxCoords(minX, maxX, minY, maxY, minZ, maxZ); |
| 69 | + } |
| 70 | + //gotta have that clean code |
| 71 | + private static class MinMaxCoords { |
| 72 | + public final int minX, maxX, minY, maxY, minZ, maxZ; |
| 73 | + |
| 74 | + public MinMaxCoords(int minX, int maxX, int minY, int maxY, int minZ, int maxZ) { |
| 75 | + this.minX = minX; |
| 76 | + this.maxX = maxX; |
| 77 | + this.minY = minY; |
| 78 | + this.maxY = maxY; |
| 79 | + this.minZ = minZ; |
| 80 | + this.maxZ = maxZ; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments