| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import com.mojang.brigadier.arguments.FloatArgumentType;
|
| 5 | import com.mojang.brigadier.arguments.IntegerArgumentType;
|
| 6 | import com.mojang.brigadier.builder.RequiredArgumentBuilder;
|
| 7 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 8 | import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
|
| 9 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 10 | import com.mojang.brigadier.suggestion.SuggestionProvider;
|
| 11 | import java.util.Optional;
|
| 12 | import net.minecraft.IdentifierException;
|
| 13 | import net.minecraft.commands.CommandBuildContext;
|
| 14 | import net.minecraft.commands.CommandSourceStack;
|
| 15 | import net.minecraft.commands.Commands;
|
| 16 | import net.minecraft.commands.SharedSuggestionProvider;
|
| 17 | import net.minecraft.commands.arguments.IdentifierArgument;
|
| 18 | import net.minecraft.commands.arguments.ResourceKeyArgument;
|
| 19 | import net.minecraft.commands.arguments.ResourceOrIdArgument;
|
| 20 | import net.minecraft.commands.arguments.TemplateMirrorArgument;
|
| 21 | import net.minecraft.commands.arguments.TemplateRotationArgument;
|
| 22 | import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
|
| 23 | import net.minecraft.core.BlockPos;
|
| 24 | import net.minecraft.core.Holder;
|
| 25 | import net.minecraft.core.SectionPos;
|
| 26 | import net.minecraft.core.registries.Registries;
|
| 27 | import net.minecraft.network.chat.Component;
|
| 28 | import net.minecraft.resources.Identifier;
|
| 29 | import net.minecraft.server.level.ServerLevel;
|
| 30 | import net.minecraft.world.level.ChunkPos;
|
| 31 | import net.minecraft.world.level.block.Mirror;
|
| 32 | import net.minecraft.world.level.block.Rotation;
|
| 33 | import net.minecraft.world.level.block.entity.StructureBlockEntity;
|
| 34 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 35 | import net.minecraft.world.level.levelgen.RandomState;
|
| 36 | import net.minecraft.world.level.levelgen.densityfunction.SamplerContext;
|
| 37 | import net.minecraft.world.level.levelgen.feature.Feature;
|
| 38 | import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
| 39 | import net.minecraft.world.level.levelgen.structure.Structure;
|
| 40 | import net.minecraft.world.level.levelgen.structure.StructureStart;
|
| 41 | import net.minecraft.world.level.levelgen.structure.pools.JigsawPlacement;
|
| 42 | import net.minecraft.world.level.levelgen.structure.pools.StructureTemplatePool;
|
| 43 | import net.minecraft.world.level.levelgen.structure.templatesystem.BlockRotProcessor;
|
| 44 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlaceSettings;
|
| 45 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
|
| 46 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplateManager;
|
| 47 |
|
| 48 | public class PlaceCommand {
|
| 49 | private static final SimpleCommandExceptionType ERROR_FEATURE_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.place.feature.failed"));
|
| 50 | private static final SimpleCommandExceptionType ERROR_JIGSAW_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.place.jigsaw.failed"));
|
| 51 | private static final SimpleCommandExceptionType ERROR_STRUCTURE_FAILED = new SimpleCommandExceptionType(
|
| 52 | Component.translatable("commands.place.structure.failed")
|
| 53 | );
|
| 54 | private static final DynamicCommandExceptionType ERROR_TEMPLATE_INVALID = new DynamicCommandExceptionType(
|
| 55 | value -> Component.translatableEscape("commands.place.template.invalid", value)
|
| 56 | );
|
| 57 | private static final SimpleCommandExceptionType ERROR_TEMPLATE_FAILED = new SimpleCommandExceptionType(
|
| 58 | Component.translatable("commands.place.template.failed")
|
| 59 | );
|
| 60 | private static final SuggestionProvider<CommandSourceStack> SUGGEST_TEMPLATES = (context, builder) -> {
|
| 61 | StructureTemplateManager structureManager = context.getSource().getLevel().getStructureTemplateManager();
|
| 62 | return SharedSuggestionProvider.suggestResource(structureManager.listTemplates(), builder);
|
| 63 | };
|
| 64 |
|
| 65 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context) {
|
| 66 | dispatcher.register(
|
| 67 | Commands.literal("place")
|
| 68 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 69 | .then(
|
| 70 | Commands.literal("feature")
|
| 71 | .then(
|
| 72 | ((RequiredArgumentBuilder)Commands.argument("feature", ResourceOrIdArgument.feature(context))
|
| 73 | .executes(
|
| 74 | c -> placeFeature(
|
| 75 | (CommandSourceStack)c.getSource(),
|
| 76 | ResourceOrIdArgument.getFeature(c, "feature"),
|
| 77 | BlockPos.containing(((CommandSourceStack)c.getSource()).getPosition())
|
| 78 | )
|
| 79 | ))
|
| 80 | .then(
|
| 81 | Commands.argument("pos", BlockPosArgument.blockPos())
|
| 82 | .executes(c -> placeFeature(c.getSource(), ResourceOrIdArgument.getFeature(c, "feature"), BlockPosArgument.getLoadedBlockPos(c, "pos")))
|
| 83 | )
|
| 84 | )
|
| 85 | )
|
| 86 | .then(
|
| 87 | Commands.literal("jigsaw")
|
| 88 | .then(
|
| 89 | Commands.argument("pool", ResourceKeyArgument.key(Registries.TEMPLATE_POOL))
|
| 90 | .then(
|
| 91 | Commands.argument("target", IdentifierArgument.id())
|
| 92 | .then(
|
| 93 | Commands.argument("max_depth", IntegerArgumentType.integer(1, 20))
|
| 94 | .executes(
|
| 95 | c -> placeJigsaw(
|
| 96 | c.getSource(),
|
| 97 | ResourceKeyArgument.getStructureTemplatePool(c, "pool"),
|
| 98 | IdentifierArgument.getId(c, "target"),
|
| 99 | IntegerArgumentType.getInteger(c, "max_depth"),
|
| 100 | BlockPos.containing(c.getSource().getPosition())
|
| 101 | )
|
| 102 | )
|
| 103 | .then(
|
| 104 | Commands.argument("position", BlockPosArgument.blockPos())
|
| 105 | .executes(
|
| 106 | c -> placeJigsaw(
|
| 107 | c.getSource(),
|
| 108 | ResourceKeyArgument.getStructureTemplatePool(c, "pool"),
|
| 109 | IdentifierArgument.getId(c, "target"),
|
| 110 | IntegerArgumentType.getInteger(c, "max_depth"),
|
| 111 | BlockPosArgument.getLoadedBlockPos(c, "position")
|
| 112 | )
|
| 113 | )
|
| 114 | )
|
| 115 | )
|
| 116 | )
|
| 117 | )
|
| 118 | )
|
| 119 | .then(
|
| 120 | Commands.literal("structure")
|
| 121 | .then(
|
| 122 | Commands.argument("structure", ResourceKeyArgument.key(Registries.STRUCTURE))
|
| 123 | .executes(c -> placeStructure(c.getSource(), ResourceKeyArgument.getStructure(c, "structure"), BlockPos.containing(c.getSource().getPosition())))
|
| 124 | .then(
|
| 125 | Commands.argument("pos", BlockPosArgument.blockPos())
|
| 126 | .executes(c -> placeStructure(c.getSource(), ResourceKeyArgument.getStructure(c, "structure"), BlockPosArgument.getLoadedBlockPos(c, "pos")))
|
| 127 | )
|
| 128 | )
|
| 129 | )
|
| 130 | .then(
|
| 131 | Commands.literal("template")
|
| 132 | .then(
|
| 133 | Commands.argument("template", IdentifierArgument.id())
|
| 134 | .suggests(SUGGEST_TEMPLATES)
|
| 135 | .executes(
|
| 136 | c -> placeTemplate(
|
| 137 | c.getSource(), IdentifierArgument.getId(c, "template"), BlockPos.containing(c.getSource().getPosition()), Rotation.NONE, Mirror.NONE, 1.0F, 0, false
|
| 138 | )
|
| 139 | )
|
| 140 | .then(
|
| 141 | Commands.argument("pos", BlockPosArgument.blockPos())
|
| 142 | .executes(
|
| 143 | c -> placeTemplate(
|
| 144 | c.getSource(), IdentifierArgument.getId(c, "template"), BlockPosArgument.getLoadedBlockPos(c, "pos"), Rotation.NONE, Mirror.NONE, 1.0F, 0, false
|
| 145 | )
|
| 146 | )
|
| 147 | .then(
|
| 148 | Commands.argument("rotation", TemplateRotationArgument.templateRotation())
|
| 149 | .executes(
|
| 150 | c -> placeTemplate(
|
| 151 | c.getSource(),
|
| 152 | IdentifierArgument.getId(c, "template"),
|
| 153 | BlockPosArgument.getLoadedBlockPos(c, "pos"),
|
| 154 | TemplateRotationArgument.getRotation(c, "rotation"),
|
| 155 | Mirror.NONE,
|
| 156 | 1.0F,
|
| 157 | 0,
|
| 158 | false
|
| 159 | )
|
| 160 | )
|
| 161 | .then(
|
| 162 | Commands.argument("mirror", TemplateMirrorArgument.templateMirror())
|
| 163 | .executes(
|
| 164 | c -> placeTemplate(
|
| 165 | c.getSource(),
|
| 166 | IdentifierArgument.getId(c, "template"),
|
| 167 | BlockPosArgument.getLoadedBlockPos(c, "pos"),
|
| 168 | TemplateRotationArgument.getRotation(c, "rotation"),
|
| 169 | TemplateMirrorArgument.getMirror(c, "mirror"),
|
| 170 | 1.0F,
|
| 171 | 0,
|
| 172 | false
|
| 173 | )
|
| 174 | )
|
| 175 | .then(
|
| 176 | Commands.argument("integrity", FloatArgumentType.floatArg(0.0F, 1.0F))
|
| 177 | .executes(
|
| 178 | c -> placeTemplate(
|
| 179 | c.getSource(),
|
| 180 | IdentifierArgument.getId(c, "template"),
|
| 181 | BlockPosArgument.getLoadedBlockPos(c, "pos"),
|
| 182 | TemplateRotationArgument.getRotation(c, "rotation"),
|
| 183 | TemplateMirrorArgument.getMirror(c, "mirror"),
|
| 184 | FloatArgumentType.getFloat(c, "integrity"),
|
| 185 | 0,
|
| 186 | false
|
| 187 | )
|
| 188 | )
|
| 189 | .then(
|
| 190 | Commands.argument("seed", IntegerArgumentType.integer())
|
| 191 | .executes(
|
| 192 | c -> placeTemplate(
|
| 193 | c.getSource(),
|
| 194 | IdentifierArgument.getId(c, "template"),
|
| 195 | BlockPosArgument.getLoadedBlockPos(c, "pos"),
|
| 196 | TemplateRotationArgument.getRotation(c, "rotation"),
|
| 197 | TemplateMirrorArgument.getMirror(c, "mirror"),
|
| 198 | FloatArgumentType.getFloat(c, "integrity"),
|
| 199 | IntegerArgumentType.getInteger(c, "seed"),
|
| 200 | false
|
| 201 | )
|
| 202 | )
|
| 203 | .then(
|
| 204 | Commands.literal("strict")
|
| 205 | .executes(
|
| 206 | c -> placeTemplate(
|
| 207 | c.getSource(),
|
| 208 | IdentifierArgument.getId(c, "template"),
|
| 209 | BlockPosArgument.getLoadedBlockPos(c, "pos"),
|
| 210 | TemplateRotationArgument.getRotation(c, "rotation"),
|
| 211 | TemplateMirrorArgument.getMirror(c, "mirror"),
|
| 212 | FloatArgumentType.getFloat(c, "integrity"),
|
| 213 | IntegerArgumentType.getInteger(c, "seed"),
|
| 214 | true
|
| 215 | )
|
| 216 | )
|
| 217 | )
|
| 218 | )
|
| 219 | )
|
| 220 | )
|
| 221 | )
|
| 222 | )
|
| 223 | )
|
| 224 | )
|
| 225 | );
|
| 226 | }
|
| 227 |
|
| 228 | public static int placeFeature(final CommandSourceStack source, final Holder<Feature> featureHolder, final BlockPos pos) throws CommandSyntaxException {
|
| 229 | ServerLevel level = source.getLevel();
|
| 230 | Feature feature = featureHolder.value();
|
| 231 | ChunkPos chunkPos = ChunkPos.containing(pos);
|
| 232 | checkLoaded(level, new ChunkPos(chunkPos.x() - 1, chunkPos.z() - 1), new ChunkPos(chunkPos.x() + 1, chunkPos.z() + 1));
|
| 233 | if (!feature.place(level, level.getChunkSource().getGenerator(), level.getRandom(), pos)) {
|
| 234 | throw ERROR_FEATURE_FAILED.create();
|
| 235 | }
|
| 236 |
|
| 237 | Optional<String> key = featureHolder.getRegisteredNameIfPresent();
|
| 238 | if (key.isPresent()) {
|
| 239 | String id = key.get();
|
| 240 | source.sendSuccess(() -> Component.translatable("commands.place.feature.success", id, pos.getX(), pos.getY(), pos.getZ()), true);
|
| 241 | } else {
|
| 242 | source.sendSuccess(() -> Component.translatable("commands.place.feature.success.inline", pos.getX(), pos.getY(), pos.getZ()), true);
|
| 243 | }
|
| 244 |
|
| 245 | return 1;
|
| 246 | }
|
| 247 |
|
| 248 | public static int placeJigsaw(
|
| 249 | final CommandSourceStack source, final Holder<StructureTemplatePool> pool, final Identifier target, final int maxDepth, final BlockPos pos
|
| 250 | ) throws CommandSyntaxException {
|
| 251 | ServerLevel level = source.getLevel();
|
| 252 | ChunkPos chunk = ChunkPos.containing(pos);
|
| 253 | checkLoaded(level, chunk, chunk);
|
| 254 | if (!JigsawPlacement.generateJigsaw(level, pool, target, maxDepth, pos, false)) {
|
| 255 | throw ERROR_JIGSAW_FAILED.create();
|
| 256 | }
|
| 257 |
|
| 258 | source.sendSuccess(() -> Component.translatable("commands.place.jigsaw.success", pos.getX(), pos.getY(), pos.getZ()), true);
|
| 259 | return 1;
|
| 260 | }
|
| 261 |
|
| 262 | public static int placeStructure(final CommandSourceStack source, final Holder.Reference<Structure> structureHolder, final BlockPos pos) throws CommandSyntaxException {
|
| 263 | ServerLevel level = source.getLevel();
|
| 264 | Structure structure = structureHolder.value();
|
| 265 | ChunkGenerator chunkGenerator = level.getChunkSource().getGenerator();
|
| 266 | RandomState randomState = level.getChunkSource().randomState();
|
| 267 | StructureStart start = structure.generate(
|
| 268 | structureHolder,
|
| 269 | level.dimension(),
|
| 270 | source.registryAccess(),
|
| 271 | chunkGenerator,
|
| 272 | chunkGenerator.getBiomeSource(),
|
| 273 | randomState.createClimateSampler(SamplerContext.EMPTY_UNCACHED),
|
| 274 | randomState,
|
| 275 | level.getStructureTemplateManager(),
|
| 276 | level.getSeed(),
|
| 277 | ChunkPos.containing(pos),
|
| 278 | 0,
|
| 279 | level,
|
| 280 | b -> true
|
| 281 | );
|
| 282 | if (!start.isValid()) {
|
| 283 | throw ERROR_STRUCTURE_FAILED.create();
|
| 284 | }
|
| 285 |
|
| 286 | BoundingBox boundingBox = start.getBoundingBox();
|
| 287 | ChunkPos chunkMin = new ChunkPos(SectionPos.blockToSectionCoord(boundingBox.minX()), SectionPos.blockToSectionCoord(boundingBox.minZ()));
|
| 288 | ChunkPos chunkMax = new ChunkPos(SectionPos.blockToSectionCoord(boundingBox.maxX()), SectionPos.blockToSectionCoord(boundingBox.maxZ()));
|
| 289 | checkLoaded(level, chunkMin, chunkMax);
|
| 290 | ChunkPos.rangeClosed(chunkMin, chunkMax)
|
| 291 | .forEach(
|
| 292 | c -> start.placeInChunk(
|
| 293 | level,
|
| 294 | level.structureManager(),
|
| 295 | chunkGenerator,
|
| 296 | level.getRandom(),
|
| 297 | new BoundingBox(c.getMinBlockX(), level.getMinY(), c.getMinBlockZ(), c.getMaxBlockX(), level.getMaxY() + 1, c.getMaxBlockZ()),
|
| 298 | c
|
| 299 | )
|
| 300 | );
|
| 301 | String id = structureHolder.key().identifier().toString();
|
| 302 | source.sendSuccess(() -> Component.translatable("commands.place.structure.success", id, pos.getX(), pos.getY(), pos.getZ()), true);
|
| 303 | return 1;
|
| 304 | }
|
| 305 |
|
| 306 | public static int placeTemplate(
|
| 307 | final CommandSourceStack source,
|
| 308 | final Identifier template,
|
| 309 | final BlockPos pos,
|
| 310 | final Rotation rotation,
|
| 311 | final Mirror mirror,
|
| 312 | final float integrity,
|
| 313 | final int seed,
|
| 314 | final boolean strict
|
| 315 | ) throws CommandSyntaxException {
|
| 316 | ServerLevel level = source.getLevel();
|
| 317 | StructureTemplateManager manager = level.getStructureTemplateManager();
|
| 318 |
|
| 319 | Optional<StructureTemplate> maybeStructureTemplate;
|
| 320 | try {
|
| 321 | maybeStructureTemplate = manager.get(template);
|
| 322 | } catch (IdentifierException e) {
|
| 323 | throw ERROR_TEMPLATE_INVALID.create(template);
|
| 324 | }
|
| 325 |
|
| 326 | if (maybeStructureTemplate.isEmpty()) {
|
| 327 | throw ERROR_TEMPLATE_INVALID.create(template);
|
| 328 | }
|
| 329 |
|
| 330 | StructureTemplate structureTemplate = maybeStructureTemplate.get();
|
| 331 | checkLoaded(level, ChunkPos.containing(pos), ChunkPos.containing(pos.offset(structureTemplate.getSize())));
|
| 332 | StructurePlaceSettings placeSettings = new StructurePlaceSettings().setMirror(mirror).setRotation(rotation).setKnownShape(strict);
|
| 333 | if (integrity < 1.0F) {
|
| 334 | placeSettings.clearProcessors().addProcessor(new BlockRotProcessor(integrity)).setRandom(StructureBlockEntity.createRandom(seed));
|
| 335 | }
|
| 336 |
|
| 337 | boolean placed = structureTemplate.placeInWorld(level, pos, pos, placeSettings, StructureBlockEntity.createRandom(seed), 2 | (strict ? 816 : 0));
|
| 338 | if (!placed) {
|
| 339 | throw ERROR_TEMPLATE_FAILED.create();
|
| 340 | }
|
| 341 |
|
| 342 | source.sendSuccess(
|
| 343 | () -> Component.translatable("commands.place.template.success", Component.translationArg(template), pos.getX(), pos.getY(), pos.getZ()), true
|
| 344 | );
|
| 345 | return 1;
|
| 346 | }
|
| 347 |
|
| 348 | private static void checkLoaded(final ServerLevel level, final ChunkPos chunkMin, final ChunkPos chunkMax) throws CommandSyntaxException {
|
| 349 | if (ChunkPos.rangeClosed(chunkMin, chunkMax).filter(c -> !level.isLoaded(c.getWorldPosition())).findAny().isPresent()) {
|
| 350 | throw BlockPosArgument.ERROR_NOT_LOADED.create();
|
| 351 | }
|
| 352 | }
|
| 353 | }
|
| 354 |
|