| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 5 | import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType;
|
| 6 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 7 | import com.mojang.datafixers.util.Either;
|
| 8 | import java.util.ArrayList;
|
| 9 | import java.util.Iterator;
|
| 10 | import java.util.List;
|
| 11 | import java.util.Optional;
|
| 12 | import java.util.function.Consumer;
|
| 13 | import java.util.function.Predicate;
|
| 14 | import java.util.function.Supplier;
|
| 15 | import net.minecraft.commands.CommandBuildContext;
|
| 16 | import net.minecraft.commands.CommandSourceStack;
|
| 17 | import net.minecraft.commands.Commands;
|
| 18 | import net.minecraft.commands.arguments.ResourceArgument;
|
| 19 | import net.minecraft.commands.arguments.ResourceOrTagArgument;
|
| 20 | import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
|
| 21 | import net.minecraft.core.BlockPos;
|
| 22 | import net.minecraft.core.Holder;
|
| 23 | import net.minecraft.core.QuartPos;
|
| 24 | import net.minecraft.core.SectionPos;
|
| 25 | import net.minecraft.core.registries.Registries;
|
| 26 | import net.minecraft.network.chat.Component;
|
| 27 | import net.minecraft.server.level.ServerLevel;
|
| 28 | import net.minecraft.world.level.biome.Biome;
|
| 29 | import net.minecraft.world.level.biome.BiomeResolver;
|
| 30 | import net.minecraft.world.level.chunk.ChunkAccess;
|
| 31 | import net.minecraft.world.level.chunk.status.ChunkStatus;
|
| 32 | import net.minecraft.world.level.gamerules.GameRules;
|
| 33 | import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
| 34 | import org.apache.commons.lang3.mutable.MutableInt;
|
| 35 |
|
| 36 | public class FillBiomeCommand {
|
| 37 | public static final SimpleCommandExceptionType ERROR_NOT_LOADED = new SimpleCommandExceptionType(Component.translatable("argument.pos.unloaded"));
|
| 38 | private static final Dynamic2CommandExceptionType ERROR_VOLUME_TOO_LARGE = new Dynamic2CommandExceptionType(
|
| 39 | (max, count) -> Component.translatableEscape("commands.fillbiome.toobig", max, count)
|
| 40 | );
|
| 41 | public static final SimpleCommandExceptionType ERROR_NO_BIOMES_SET = new SimpleCommandExceptionType(Component.translatable("commands.fillbiome.no_changes"));
|
| 42 |
|
| 43 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context) {
|
| 44 | dispatcher.register(
|
| 45 | Commands.literal("fillbiome")
|
| 46 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 47 | .then(
|
| 48 | Commands.argument("from", BlockPosArgument.blockPos())
|
| 49 | .then(
|
| 50 | Commands.argument("to", BlockPosArgument.blockPos())
|
| 51 | .then(
|
| 52 | Commands.argument("biome", ResourceArgument.resource(context, Registries.BIOME))
|
| 53 | .executes(
|
| 54 | c -> fill(
|
| 55 | c.getSource(),
|
| 56 | BlockPosArgument.getLoadedBlockPos(c, "from"),
|
| 57 | BlockPosArgument.getLoadedBlockPos(c, "to"),
|
| 58 | ResourceArgument.getResource(c, "biome", Registries.BIOME),
|
| 59 | b -> true
|
| 60 | )
|
| 61 | )
|
| 62 | .then(
|
| 63 | Commands.literal("replace")
|
| 64 | .then(
|
| 65 | Commands.argument("filter", ResourceOrTagArgument.resourceOrTag(context, Registries.BIOME))
|
| 66 | .executes(
|
| 67 | c -> fill(
|
| 68 | c.getSource(),
|
| 69 | BlockPosArgument.getLoadedBlockPos(c, "from"),
|
| 70 | BlockPosArgument.getLoadedBlockPos(c, "to"),
|
| 71 | ResourceArgument.getResource(c, "biome", Registries.BIOME),
|
| 72 | ResourceOrTagArgument.getResourceOrTag(c, "filter", Registries.BIOME)
|
| 73 | )
|
| 74 | )
|
| 75 | )
|
| 76 | )
|
| 77 | )
|
| 78 | )
|
| 79 | )
|
| 80 | );
|
| 81 | }
|
| 82 |
|
| 83 | private static int quantize(final int blockCoord) {
|
| 84 | return QuartPos.toBlock(QuartPos.fromBlock(blockCoord));
|
| 85 | }
|
| 86 |
|
| 87 | private static BlockPos quantize(final BlockPos block) {
|
| 88 | return new BlockPos(quantize(block.getX()), quantize(block.getY()), quantize(block.getZ()));
|
| 89 | }
|
| 90 |
|
| 91 | private static BiomeResolver makeResolver(
|
| 92 | final MutableInt count, final ChunkAccess chunk, final BoundingBox region, final Holder<Biome> toFill, final Predicate<Holder<Biome>> filter
|
| 93 | ) {
|
| 94 | return (quartX, quartY, quartZ) -> {
|
| 95 | int blockX = QuartPos.toBlock(quartX);
|
| 96 | int blockY = QuartPos.toBlock(quartY);
|
| 97 | int blockZ = QuartPos.toBlock(quartZ);
|
| 98 | Holder<Biome> currentBiome = chunk.getNoiseBiome(quartX, quartY, quartZ);
|
| 99 | if (region.isInside(blockX, blockY, blockZ) && filter.test(currentBiome)) {
|
| 100 | if (!currentBiome.is(toFill)) {
|
| 101 | count.increment();
|
| 102 | }
|
| 103 |
|
| 104 | return toFill;
|
| 105 | } else {
|
| 106 | return currentBiome;
|
| 107 | }
|
| 108 | };
|
| 109 | }
|
| 110 |
|
| 111 | public static Either<Integer, CommandSyntaxException> fill(final ServerLevel level, final BlockPos rawFrom, final BlockPos rawTo, final Holder<Biome> biome) {
|
| 112 | return fill(level, rawFrom, rawTo, biome, var0 -> true, var0 -> {});
|
| 113 | }
|
| 114 |
|
| 115 | public static Either<Integer, CommandSyntaxException> fill(
|
| 116 | final ServerLevel level,
|
| 117 | final BlockPos rawFrom,
|
| 118 | final BlockPos rawTo,
|
| 119 | final Holder<Biome> biome,
|
| 120 | final Predicate<Holder<Biome>> filter,
|
| 121 | final Consumer<Supplier<Component>> successMessageConsumer
|
| 122 | ) {
|
| 123 | BlockPos from = quantize(rawFrom);
|
| 124 | BlockPos to = quantize(rawTo);
|
| 125 | BoundingBox region = BoundingBox.fromCorners(from, to);
|
| 126 | long volume = (long)region.getXSpan() * region.getYSpan() * region.getZSpan();
|
| 127 | int limit = level.getGameRules().get(GameRules.MAX_BLOCK_MODIFICATIONS);
|
| 128 | if (volume > limit) {
|
| 129 | return Either.right(ERROR_VOLUME_TOO_LARGE.create(limit, volume));
|
| 130 | }
|
| 131 |
|
| 132 | List<ChunkAccess> chunks = new ArrayList<>();
|
| 133 |
|
| 134 | for (int chunkZ = SectionPos.blockToSectionCoord(region.minZ()); chunkZ <= SectionPos.blockToSectionCoord(region.maxZ()); chunkZ++) {
|
| 135 | for (int chunkX = SectionPos.blockToSectionCoord(region.minX()); chunkX <= SectionPos.blockToSectionCoord(region.maxX()); chunkX++) {
|
| 136 | ChunkAccess chunk = level.getChunk(chunkX, chunkZ, ChunkStatus.FULL, false);
|
| 137 | if (chunk == null) {
|
| 138 | return Either.right(ERROR_NOT_LOADED.create());
|
| 139 | }
|
| 140 |
|
| 141 | chunks.add(chunk);
|
| 142 | }
|
| 143 | }
|
| 144 |
|
| 145 | MutableInt changedCount = new MutableInt(0);
|
| 146 | Iterator<ChunkAccess> iterator = chunks.iterator();
|
| 147 |
|
| 148 | while (iterator.hasNext()) {
|
| 149 | ChunkAccess chunk = iterator.next();
|
| 150 | int previousChangedCount = changedCount.intValue();
|
| 151 | chunk.fillBiomesFromNoise(makeResolver(changedCount, chunk, region, biome, filter));
|
| 152 | if (previousChangedCount != changedCount.intValue()) {
|
| 153 | chunk.markUnsaved();
|
| 154 | } else {
|
| 155 | iterator.remove();
|
| 156 | }
|
| 157 | }
|
| 158 |
|
| 159 | int finalChangedCount = changedCount.intValue();
|
| 160 | if (finalChangedCount > 0) {
|
| 161 | level.getChunkSource().chunkMap.resendBiomesForChunks(chunks);
|
| 162 | successMessageConsumer.accept(
|
| 163 | () -> Component.translatable(
|
| 164 | "commands.fillbiome.success.count", finalChangedCount, region.minX(), region.minY(), region.minZ(), region.maxX(), region.maxY(), region.maxZ()
|
| 165 | )
|
| 166 | );
|
| 167 | return Either.left(finalChangedCount);
|
| 168 | } else {
|
| 169 | return Either.right(ERROR_NO_BIOMES_SET.create());
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | private static int fill(
|
| 174 | final CommandSourceStack source, final BlockPos rawFrom, final BlockPos rawTo, final Holder.Reference<Biome> biome, final Predicate<Holder<Biome>> filter
|
| 175 | ) throws CommandSyntaxException {
|
| 176 | Either<Integer, CommandSyntaxException> result = fill(source.getLevel(), rawFrom, rawTo, biome, filter, m -> source.sendSuccess(m, true));
|
| 177 | Optional<CommandSyntaxException> exception = result.right();
|
| 178 | if (exception.isPresent()) {
|
| 179 | throw (CommandSyntaxException)exception.get();
|
| 180 | } else {
|
| 181 | return result.left().get();
|
| 182 | }
|
| 183 | }
|
| 184 | }
|
| 185 |
|