| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 5 | import java.util.Collection;
|
| 6 | import java.util.Collections;
|
| 7 | import net.minecraft.commands.CommandSourceStack;
|
| 8 | import net.minecraft.commands.Commands;
|
| 9 | import net.minecraft.commands.arguments.EntityArgument;
|
| 10 | import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
|
| 11 | import net.minecraft.commands.arguments.coordinates.Coordinates;
|
| 12 | import net.minecraft.commands.arguments.coordinates.RotationArgument;
|
| 13 | import net.minecraft.commands.arguments.coordinates.WorldCoordinates;
|
| 14 | import net.minecraft.core.BlockPos;
|
| 15 | import net.minecraft.network.chat.Component;
|
| 16 | import net.minecraft.resources.ResourceKey;
|
| 17 | import net.minecraft.server.level.ServerPlayer;
|
| 18 | import net.minecraft.util.Mth;
|
| 19 | import net.minecraft.world.level.Level;
|
| 20 | import net.minecraft.world.level.storage.LevelData;
|
| 21 | import net.minecraft.world.phys.Vec2;
|
| 22 |
|
| 23 | public class SetSpawnCommand {
|
| 24 | private static final CommandResponseTracker.MessagesWithArg<ServerPlayer, LevelData.RespawnData> RESPONSE_SET = CommandResponseTracker.messages(
|
| 25 | (player, var1, respawnData) -> Component.translatable(
|
| 26 | "commands.spawnpoint.success.single",
|
| 27 | respawnData.pos().getX(),
|
| 28 | respawnData.pos().getY(),
|
| 29 | respawnData.pos().getZ(),
|
| 30 | respawnData.yaw(),
|
| 31 | respawnData.pitch(),
|
| 32 | Component.translationArg(respawnData.dimension().identifier()),
|
| 33 | player.getDisplayName()
|
| 34 | ),
|
| 35 | (playerCount, var1, respawnData) -> Component.translatable(
|
| 36 | "commands.spawnpoint.success.multiple",
|
| 37 | respawnData.pos().getX(),
|
| 38 | respawnData.pos().getY(),
|
| 39 | respawnData.pos().getZ(),
|
| 40 | respawnData.yaw(),
|
| 41 | respawnData.pitch(),
|
| 42 | Component.translationArg(respawnData.dimension().identifier()),
|
| 43 | playerCount
|
| 44 | )
|
| 45 | );
|
| 46 |
|
| 47 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 48 | dispatcher.register(
|
| 49 | Commands.literal("spawnpoint")
|
| 50 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 51 | .executes(
|
| 52 | c -> setSpawn(
|
| 53 | c.getSource(),
|
| 54 | Collections.singleton(c.getSource().getPlayerOrException()),
|
| 55 | BlockPos.containing(c.getSource().getPosition()),
|
| 56 | WorldCoordinates.ZERO_ROTATION
|
| 57 | )
|
| 58 | )
|
| 59 | .then(
|
| 60 | Commands.argument("targets", EntityArgument.players())
|
| 61 | .executes(
|
| 62 | c -> setSpawn(c.getSource(), EntityArgument.getPlayers(c, "targets"), BlockPos.containing(c.getSource().getPosition()), WorldCoordinates.ZERO_ROTATION)
|
| 63 | )
|
| 64 | .then(
|
| 65 | Commands.argument("pos", BlockPosArgument.blockPos())
|
| 66 | .executes(
|
| 67 | c -> setSpawn(c.getSource(), EntityArgument.getPlayers(c, "targets"), BlockPosArgument.getSpawnablePos(c, "pos"), WorldCoordinates.ZERO_ROTATION)
|
| 68 | )
|
| 69 | .then(
|
| 70 | Commands.argument("rotation", RotationArgument.rotation())
|
| 71 | .executes(
|
| 72 | c -> setSpawn(
|
| 73 | c.getSource(), EntityArgument.getPlayers(c, "targets"), BlockPosArgument.getSpawnablePos(c, "pos"), RotationArgument.getRotation(c, "rotation")
|
| 74 | )
|
| 75 | )
|
| 76 | )
|
| 77 | )
|
| 78 | )
|
| 79 | );
|
| 80 | }
|
| 81 |
|
| 82 | private static int setSpawn(final CommandSourceStack source, final Collection<ServerPlayer> targets, final BlockPos pos, final Coordinates rotation) throws CommandSyntaxException {
|
| 83 | CommandResponseTracker<ServerPlayer> tracker = CommandResponseTracker.create();
|
| 84 | ResourceKey<Level> dimension = source.getLevel().dimension();
|
| 85 | Vec2 rotationVector = rotation.getRotation(source);
|
| 86 | float yaw = Mth.wrapDegrees(rotationVector.y);
|
| 87 | float pitch = Mth.clamp(rotationVector.x, -90.0F, 90.0F);
|
| 88 | LevelData.RespawnData respawnData = LevelData.RespawnData.of(dimension, pos, yaw, pitch);
|
| 89 | ServerPlayer.RespawnConfig respawnConfig = new ServerPlayer.RespawnConfig(respawnData, true);
|
| 90 |
|
| 91 | for (ServerPlayer target : targets) {
|
| 92 | target.setRespawnPosition(respawnConfig, false);
|
| 93 | tracker.track(target);
|
| 94 | }
|
| 95 |
|
| 96 | return tracker.sendFeedback(source, true, RESPONSE_SET, respawnData);
|
| 97 | }
|
| 98 | }
|
| 99 |
|