| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import net.minecraft.commands.CommandSourceStack;
|
| 5 | import net.minecraft.commands.Commands;
|
| 6 | import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
|
| 7 | import net.minecraft.commands.arguments.coordinates.Coordinates;
|
| 8 | import net.minecraft.commands.arguments.coordinates.RotationArgument;
|
| 9 | import net.minecraft.commands.arguments.coordinates.WorldCoordinates;
|
| 10 | import net.minecraft.core.BlockPos;
|
| 11 | import net.minecraft.network.chat.Component;
|
| 12 | import net.minecraft.server.level.ServerLevel;
|
| 13 | import net.minecraft.world.level.storage.LevelData;
|
| 14 | import net.minecraft.world.phys.Vec2;
|
| 15 |
|
| 16 | public class SetWorldSpawnCommand {
|
| 17 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 18 | dispatcher.register(
|
| 19 | Commands.literal("setworldspawn")
|
| 20 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 21 | .executes(c -> setSpawn(c.getSource(), BlockPos.containing(c.getSource().getPosition()), WorldCoordinates.ZERO_ROTATION))
|
| 22 | .then(
|
| 23 | Commands.argument("pos", BlockPosArgument.blockPos())
|
| 24 | .executes(c -> setSpawn(c.getSource(), BlockPosArgument.getSpawnablePos(c, "pos"), WorldCoordinates.ZERO_ROTATION))
|
| 25 | .then(
|
| 26 | Commands.argument("rotation", RotationArgument.rotation())
|
| 27 | .executes(c -> setSpawn(c.getSource(), BlockPosArgument.getSpawnablePos(c, "pos"), RotationArgument.getRotation(c, "rotation")))
|
| 28 | )
|
| 29 | )
|
| 30 | );
|
| 31 | }
|
| 32 |
|
| 33 | private static int setSpawn(final CommandSourceStack source, final BlockPos pos, final Coordinates rotation) {
|
| 34 | ServerLevel level = source.getLevel();
|
| 35 | Vec2 rotationVector = rotation.getRotation(source);
|
| 36 | float yaw = rotationVector.y;
|
| 37 | float pitch = rotationVector.x;
|
| 38 | LevelData.RespawnData respawnData = LevelData.RespawnData.of(level.dimension(), pos, yaw, pitch);
|
| 39 | level.setRespawnData(respawnData);
|
| 40 | source.sendSuccess(
|
| 41 | () -> Component.translatable(
|
| 42 | "commands.setworldspawn.success", pos.getX(), pos.getY(), pos.getZ(), respawnData.yaw(), respawnData.pitch(), level.dimension().identifier().toString()
|
| 43 | ),
|
| 44 | true
|
| 45 | );
|
| 46 | return 1;
|
| 47 | }
|
| 48 | }
|
| 49 |
|