61 lines
2.5 KiB
Java
61 lines
2.5 KiB
Java
package net.minecraft.server.commands;
|
|
|
|
import com.mojang.brigadier.CommandDispatcher;
|
|
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
|
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
|
|
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
|
import net.minecraft.commands.CommandSourceStack;
|
|
import net.minecraft.commands.Commands;
|
|
import net.minecraft.commands.arguments.EntityArgument;
|
|
import net.minecraft.network.chat.Component;
|
|
import net.minecraft.server.level.ServerPlayer;
|
|
import net.minecraft.world.entity.Entity;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
public class SpectateCommand {
|
|
private static final SimpleCommandExceptionType ERROR_SELF = new SimpleCommandExceptionType(Component.translatable("commands.spectate.self"));
|
|
private static final DynamicCommandExceptionType ERROR_NOT_SPECTATOR = new DynamicCommandExceptionType(
|
|
object -> Component.translatableEscape("commands.spectate.not_spectator", object)
|
|
);
|
|
|
|
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
|
|
dispatcher.register(
|
|
Commands.literal("spectate")
|
|
.requires(commandSourceStack -> commandSourceStack.hasPermission(2))
|
|
.executes(commandContext -> spectate(commandContext.getSource(), null, commandContext.getSource().getPlayerOrException()))
|
|
.then(
|
|
Commands.argument("target", EntityArgument.entity())
|
|
.executes(
|
|
commandContext -> spectate(
|
|
commandContext.getSource(), EntityArgument.getEntity(commandContext, "target"), commandContext.getSource().getPlayerOrException()
|
|
)
|
|
)
|
|
.then(
|
|
Commands.argument("player", EntityArgument.player())
|
|
.executes(
|
|
commandContext -> spectate(
|
|
commandContext.getSource(), EntityArgument.getEntity(commandContext, "target"), EntityArgument.getPlayer(commandContext, "player")
|
|
)
|
|
)
|
|
)
|
|
)
|
|
);
|
|
}
|
|
|
|
private static int spectate(CommandSourceStack source, @Nullable Entity target, ServerPlayer player) throws CommandSyntaxException {
|
|
if (player == target) {
|
|
throw ERROR_SELF.create();
|
|
} else if (!player.isSpectator()) {
|
|
throw ERROR_NOT_SPECTATOR.create(player.getDisplayName());
|
|
} else {
|
|
player.setCamera(target);
|
|
if (target != null) {
|
|
source.sendSuccess(() -> Component.translatable("commands.spectate.success.started", target.getDisplayName()), false);
|
|
} else {
|
|
source.sendSuccess(() -> Component.translatable("commands.spectate.success.stopped"), false);
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
}
|
|
}
|