| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.brigadier.CommandDispatcher;
|
| 5 | import com.mojang.brigadier.builder.ArgumentBuilder;
|
| 6 | import com.mojang.brigadier.context.CommandContext;
|
| 7 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 8 | import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType;
|
| 9 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 10 | import java.util.Collections;
|
| 11 | import java.util.List;
|
| 12 | import java.util.function.Predicate;
|
| 13 | import net.minecraft.commands.CommandBuildContext;
|
| 14 | import net.minecraft.commands.CommandSourceStack;
|
| 15 | import net.minecraft.commands.Commands;
|
| 16 | import net.minecraft.commands.arguments.blocks.BlockInput;
|
| 17 | import net.minecraft.commands.arguments.blocks.BlockPredicateArgument;
|
| 18 | import net.minecraft.commands.arguments.blocks.BlockStateArgument;
|
| 19 | import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
|
| 20 | import net.minecraft.core.BlockPos;
|
| 21 | import net.minecraft.network.chat.Component;
|
| 22 | import net.minecraft.server.level.ServerLevel;
|
| 23 | import net.minecraft.world.level.block.Blocks;
|
| 24 | import net.minecraft.world.level.block.state.BlockState;
|
| 25 | import net.minecraft.world.level.block.state.pattern.BlockInWorld;
|
| 26 | import net.minecraft.world.level.gamerules.GameRules;
|
| 27 | import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
| 28 | import org.jspecify.annotations.Nullable;
|
| 29 |
|
| 30 | public class FillCommand {
|
| 31 | private static final Dynamic2CommandExceptionType ERROR_AREA_TOO_LARGE = new Dynamic2CommandExceptionType(
|
| 32 | (max, count) -> Component.translatableEscape("commands.fill.toobig", max, count)
|
| 33 | );
|
| 34 | private static final BlockInput HOLLOW_CORE = new BlockInput(Blocks.AIR.defaultBlockState(), Collections.emptySet(), null);
|
| 35 | private static final SimpleCommandExceptionType ERROR_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.fill.failed"));
|
| 36 |
|
| 37 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context) {
|
| 38 | dispatcher.register(
|
| 39 | Commands.literal("fill")
|
| 40 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 41 | .then(
|
| 42 | Commands.argument("from", BlockPosArgument.blockPos())
|
| 43 | .then(
|
| 44 | Commands.argument("to", BlockPosArgument.blockPos())
|
| 45 | .then(
|
| 46 | wrapWithMode(
|
| 47 | context,
|
| 48 | Commands.argument("block", BlockStateArgument.block(context)),
|
| 49 | c -> BlockPosArgument.getLoadedBlockPos(c, "from"),
|
| 50 | c -> BlockPosArgument.getLoadedBlockPos(c, "to"),
|
| 51 | c -> BlockStateArgument.getBlock(c, "block"),
|
| 52 | c -> null
|
| 53 | )
|
| 54 | .then(
|
| 55 | Commands.literal("replace")
|
| 56 | .executes(
|
| 57 | c -> fillBlocks(
|
| 58 | c.getSource(),
|
| 59 | BoundingBox.fromCorners(BlockPosArgument.getLoadedBlockPos(c, "from"), BlockPosArgument.getLoadedBlockPos(c, "to")),
|
| 60 | BlockStateArgument.getBlock(c, "block"),
|
| 61 | FillCommand.Mode.REPLACE,
|
| 62 | null,
|
| 63 | false
|
| 64 | )
|
| 65 | )
|
| 66 | .then(
|
| 67 | wrapWithMode(
|
| 68 | context,
|
| 69 | Commands.argument("filter", BlockPredicateArgument.blockPredicate(context)),
|
| 70 | c -> BlockPosArgument.getLoadedBlockPos(c, "from"),
|
| 71 | c -> BlockPosArgument.getLoadedBlockPos(c, "to"),
|
| 72 | c -> BlockStateArgument.getBlock(c, "block"),
|
| 73 | c -> BlockPredicateArgument.getBlockPredicate(c, "filter")
|
| 74 | )
|
| 75 | )
|
| 76 | )
|
| 77 | .then(
|
| 78 | Commands.literal("keep")
|
| 79 | .executes(
|
| 80 | c -> fillBlocks(
|
| 81 | c.getSource(),
|
| 82 | BoundingBox.fromCorners(BlockPosArgument.getLoadedBlockPos(c, "from"), BlockPosArgument.getLoadedBlockPos(c, "to")),
|
| 83 | BlockStateArgument.getBlock(c, "block"),
|
| 84 | FillCommand.Mode.REPLACE,
|
| 85 | b -> b.getLevel().isEmptyBlock(b.getPos()),
|
| 86 | false
|
| 87 | )
|
| 88 | )
|
| 89 | )
|
| 90 | )
|
| 91 | )
|
| 92 | )
|
| 93 | );
|
| 94 | }
|
| 95 |
|
| 96 | private static ArgumentBuilder<CommandSourceStack, ?> wrapWithMode(
|
| 97 | final CommandBuildContext context,
|
| 98 | final ArgumentBuilder<CommandSourceStack, ?> builder,
|
| 99 | final InCommandFunction<CommandContext<CommandSourceStack>, BlockPos> from,
|
| 100 | final InCommandFunction<CommandContext<CommandSourceStack>, BlockPos> to,
|
| 101 | final InCommandFunction<CommandContext<CommandSourceStack>, BlockInput> block,
|
| 102 | final FillCommand.NullableCommandFunction<CommandContext<CommandSourceStack>, Predicate<BlockInWorld>> filter
|
| 103 | ) {
|
| 104 | return builder.executes(
|
| 105 | c -> fillBlocks(c.getSource(), BoundingBox.fromCorners(from.apply(c), to.apply(c)), block.apply(c), FillCommand.Mode.REPLACE, filter.apply(c), false)
|
| 106 | )
|
| 107 | .then(
|
| 108 | Commands.literal("outline")
|
| 109 | .executes(
|
| 110 | c -> fillBlocks(c.getSource(), BoundingBox.fromCorners(from.apply(c), to.apply(c)), block.apply(c), FillCommand.Mode.OUTLINE, filter.apply(c), false)
|
| 111 | )
|
| 112 | )
|
| 113 | .then(
|
| 114 | Commands.literal("hollow")
|
| 115 | .executes(
|
| 116 | c -> fillBlocks(c.getSource(), BoundingBox.fromCorners(from.apply(c), to.apply(c)), block.apply(c), FillCommand.Mode.HOLLOW, filter.apply(c), false)
|
| 117 | )
|
| 118 | )
|
| 119 | .then(
|
| 120 | Commands.literal("destroy")
|
| 121 | .executes(
|
| 122 | c -> fillBlocks(c.getSource(), BoundingBox.fromCorners(from.apply(c), to.apply(c)), block.apply(c), FillCommand.Mode.DESTROY, filter.apply(c), false)
|
| 123 | )
|
| 124 | )
|
| 125 | .then(
|
| 126 | Commands.literal("strict")
|
| 127 | .executes(
|
| 128 | c -> fillBlocks(c.getSource(), BoundingBox.fromCorners(from.apply(c), to.apply(c)), block.apply(c), FillCommand.Mode.REPLACE, filter.apply(c), true)
|
| 129 | )
|
| 130 | );
|
| 131 | }
|
| 132 |
|
| 133 | private static int fillBlocks(
|
| 134 | final CommandSourceStack source,
|
| 135 | final BoundingBox region,
|
| 136 | final BlockInput target,
|
| 137 | final FillCommand.Mode mode,
|
| 138 | final @Nullable Predicate<BlockInWorld> predicate,
|
| 139 | final boolean strict
|
| 140 | ) throws CommandSyntaxException {
|
| 141 | long area = (long)region.getXSpan() * region.getYSpan() * region.getZSpan();
|
| 142 | int limit = source.getLevel().getGameRules().get(GameRules.MAX_BLOCK_MODIFICATIONS);
|
| 143 | if (area > limit) {
|
| 144 | throw ERROR_AREA_TOO_LARGE.create(limit, area);
|
| 145 | }
|
| 146 |
|
| 147 | record UpdatedPosition(BlockPos pos, BlockState oldState) {
|
| 148 | }
|
| 149 |
|
| 150 | List<UpdatedPosition> updatePositions = Lists.newArrayList();
|
| 151 | ServerLevel level = source.getLevel();
|
| 152 | if (level.isDebug()) {
|
| 153 | throw ERROR_FAILED.create();
|
| 154 | }
|
| 155 |
|
| 156 | int count = 0;
|
| 157 |
|
| 158 | for (BlockPos pos : BlockPos.betweenClosed(region.minX(), region.minY(), region.minZ(), region.maxX(), region.maxY(), region.maxZ())) {
|
| 159 | if (predicate == null || predicate.test(new BlockInWorld(level, pos, true))) {
|
| 160 | BlockState oldState = level.getBlockState(pos);
|
| 161 | boolean affected = false;
|
| 162 | if (mode.affector.affect(level, pos)) {
|
| 163 | affected = true;
|
| 164 | }
|
| 165 |
|
| 166 | BlockInput block = mode.filter.filter(region, pos, target, level);
|
| 167 | if (block == null) {
|
| 168 | if (affected) {
|
| 169 | count++;
|
| 170 | }
|
| 171 | } else if (!block.place(level, pos, 2 | (strict ? 816 : 256))) {
|
| 172 | if (affected) {
|
| 173 | count++;
|
| 174 | }
|
| 175 | } else {
|
| 176 | if (!strict) {
|
| 177 | updatePositions.add(new UpdatedPosition(pos.immutable(), oldState));
|
| 178 | }
|
| 179 |
|
| 180 | count++;
|
| 181 | }
|
| 182 | }
|
| 183 | }
|
| 184 |
|
| 185 | for (UpdatedPosition pos : updatePositions) {
|
| 186 | level.updateNeighboursOnBlockSet(pos.pos, pos.oldState);
|
| 187 | }
|
| 188 |
|
| 189 | if (count == 0) {
|
| 190 | throw ERROR_FAILED.create();
|
| 191 | }
|
| 192 |
|
| 193 | int finalCount = count;
|
| 194 | source.sendSuccess(() -> Component.translatable("commands.fill.success", finalCount), true);
|
| 195 | return count;
|
| 196 | }
|
| 197 |
|
| 198 | @FunctionalInterface
|
| 199 | public interface Affector {
|
| 200 | FillCommand.Affector NOOP = (l, p) -> false;
|
| 201 |
|
| 202 | boolean affect(final ServerLevel level, final BlockPos pos);
|
| 203 | }
|
| 204 |
|
| 205 | @FunctionalInterface
|
| 206 | public interface Filter {
|
| 207 | FillCommand.Filter NOOP = (r, p, b, l) -> b;
|
| 208 |
|
| 209 | @Nullable BlockInput filter(final BoundingBox region, final BlockPos pos, final BlockInput block, final ServerLevel level);
|
| 210 | }
|
| 211 |
|
| 212 | private enum Mode {
|
| 213 | REPLACE(FillCommand.Affector.NOOP, FillCommand.Filter.NOOP),
|
| 214 | OUTLINE(
|
| 215 | FillCommand.Affector.NOOP,
|
| 216 | (r, p, b, l) -> p.getX() != r.minX() && p.getX() != r.maxX() && p.getY() != r.minY() && p.getY() != r.maxY() && p.getZ() != r.minZ() && p.getZ() != r.maxZ()
|
| 217 | ? null
|
| 218 | : b
|
| 219 | ),
|
| 220 | HOLLOW(
|
| 221 | FillCommand.Affector.NOOP,
|
| 222 | (r, p, b, l) -> p.getX() != r.minX() && p.getX() != r.maxX() && p.getY() != r.minY() && p.getY() != r.maxY() && p.getZ() != r.minZ() && p.getZ() != r.maxZ()
|
| 223 | ? FillCommand.HOLLOW_CORE
|
| 224 | : b
|
| 225 | ),
|
| 226 | DESTROY((l, p) -> l.destroyBlock(p, true), FillCommand.Filter.NOOP);
|
| 227 |
|
| 228 | public final FillCommand.Filter filter;
|
| 229 | public final FillCommand.Affector affector;
|
| 230 |
|
| 231 | Mode(final FillCommand.Affector affector, final FillCommand.Filter filter) {
|
| 232 | this.affector = affector;
|
| 233 | this.filter = filter;
|
| 234 | }
|
| 235 | }
|
| 236 |
|
| 237 | @FunctionalInterface
|
| 238 | private interface NullableCommandFunction<T, R> {
|
| 239 | @Nullable R apply(T t) throws CommandSyntaxException;
|
| 240 | }
|
| 241 | }
|
| 242 |
|