This commit is contained in:
@ -0,0 +1,77 @@
|
||||
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 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) // Ensure correct method name
|
||||
.executes(SpectatorCommand::execute));
|
||||
}
|
||||
|
||||
private static int execute(CommandContext<ServerCommandSource> context) {
|
||||
ServerCommandSource source = context.getSource();
|
||||
if (source.getEntity() == null) { // Ensure correct method name
|
||||
source.sendMessage(Text.literal("Console cannot go into spectator mode!"));
|
||||
return 0;
|
||||
}
|
||||
ServerPlayerEntity player = (ServerPlayerEntity) source.getEntity(); // Ensure correct method name
|
||||
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);
|
||||
} else {
|
||||
|
||||
spectating.put(player, new LocationData(player
|
||||
.getX(), player
|
||||
.getY(), player
|
||||
.getZ(), player
|
||||
.getYaw(), player
|
||||
.getPitch(), player
|
||||
.getServerWorld()));
|
||||
player.changeGameMode(GameMode.SPECTATOR);
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user