Files
SurvivalFabric/src/main/java/wtf/hak/survivalfabric/commands/SpectatorCommand.java

81 lines
2.5 KiB
Java

package wtf.hak.survivalfabric.commands;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.world.GameMode;
import wtf.hak.survivalfabric.utils.Utils;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class SpectatorCommand {
public static final Map<ServerPlayerEntity, LocationData> spectating = new HashMap<>();
public static void register(CommandDispatcher<ServerCommandSource> dispatcher, String... aliases) {
for (String str : aliases)
dispatcher.register(CommandManager.literal(str)
.executes(SpectatorCommand::execute));
}
private static int execute(CommandContext<ServerCommandSource> context) {
ServerCommandSource source = context.getSource();
if (source.getEntity() == null) {
source.sendMessage(Text.literal("Console cannot go into spectator mode!"));
return 0;
}
ServerPlayerEntity player = (ServerPlayerEntity) source.getEntity();
if (spectating.containsKey(player)) {
LocationData data = spectating.get(player);
player.teleport(data.world, data.x, data.y, data.z, Set.of(), data.yaw, data.pitch, false);
player.changeGameMode(GameMode.SURVIVAL);
spectating.remove(player);
Utils.updateListNames(player);
} else {
spectating.put(player, new LocationData(player
.getX(), player
.getY(), player
.getZ(), player
.getYaw(), player
.getPitch(), player
.getServerWorld()));
player.changeGameMode(GameMode.SPECTATOR);
Utils.updateListNames(player);
}
return 1;
}
public static class LocationData {
public final double x;
public final double y;
public final double z;
public final float yaw;
public final float pitch;
public final ServerWorld world;
LocationData(double x, double y, double z, float yaw, float pitch, ServerWorld world) {
this.x = x;
this.y = y;
this.z = z;
this.yaw = yaw;
this.pitch = pitch;
this.world = world;
}
}
}