| 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.DynamicCommandExceptionType;
|
| 6 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 7 | import net.minecraft.commands.CommandSourceStack;
|
| 8 | import net.minecraft.commands.Commands;
|
| 9 | import net.minecraft.commands.arguments.EntityArgument;
|
| 10 | import net.minecraft.network.chat.Component;
|
| 11 | import net.minecraft.server.level.ServerPlayer;
|
| 12 | import net.minecraft.world.entity.Entity;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 |
|
| 15 | public class SpectateCommand {
|
| 16 | private static final SimpleCommandExceptionType ERROR_SELF = new SimpleCommandExceptionType(Component.translatable("commands.spectate.self"));
|
| 17 | private static final DynamicCommandExceptionType ERROR_NOT_SPECTATOR = new DynamicCommandExceptionType(
|
| 18 | s -> Component.translatableEscape("commands.spectate.not_spectator", s)
|
| 19 | );
|
| 20 | private static final DynamicCommandExceptionType ERROR_CANNOT_SPECTATE = new DynamicCommandExceptionType(
|
| 21 | s -> Component.translatableEscape("commands.spectate.cannot_spectate", s)
|
| 22 | );
|
| 23 |
|
| 24 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 25 | dispatcher.register(
|
| 26 | Commands.literal("spectate")
|
| 27 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 28 | .executes(c -> spectate(c.getSource(), null, c.getSource().getPlayerOrException()))
|
| 29 | .then(
|
| 30 | Commands.argument("target", EntityArgument.entity())
|
| 31 | .executes(c -> spectate(c.getSource(), EntityArgument.getEntity(c, "target"), c.getSource().getPlayerOrException()))
|
| 32 | .then(
|
| 33 | Commands.argument("player", EntityArgument.player())
|
| 34 | .executes(c -> spectate(c.getSource(), EntityArgument.getEntity(c, "target"), EntityArgument.getPlayer(c, "player")))
|
| 35 | )
|
| 36 | )
|
| 37 | );
|
| 38 | }
|
| 39 |
|
| 40 | private static int spectate(final CommandSourceStack source, final @Nullable Entity target, final ServerPlayer player) throws CommandSyntaxException {
|
| 41 | if (player == target) {
|
| 42 | throw ERROR_SELF.create();
|
| 43 | }
|
| 44 |
|
| 45 | if (!player.isSpectator()) {
|
| 46 | throw ERROR_NOT_SPECTATOR.create(player.getDisplayName());
|
| 47 | }
|
| 48 |
|
| 49 | if (target != null && target.getType().clientTrackingRange() == 0) {
|
| 50 | throw ERROR_CANNOT_SPECTATE.create(target.getDisplayName());
|
| 51 | }
|
| 52 |
|
| 53 | player.setCamera(target);
|
| 54 | if (target != null) {
|
| 55 | source.sendSuccess(() -> Component.translatable("commands.spectate.success.started", target.getDisplayName()), false);
|
| 56 | } else {
|
| 57 | source.sendSuccess(() -> Component.translatable("commands.spectate.success.stopped"), false);
|
| 58 | }
|
| 59 |
|
| 60 | return 1;
|
| 61 | }
|
| 62 | }
|
| 63 |
|