From f3438d8b4885f41b46ded4150d12e364c6633481 Mon Sep 17 00:00:00 2001 From: Patbox Date: Thu, 7 Nov 2024 16:46:44 +0100 Subject: [PATCH] Comment out JoinIntent.ANY, add /game spectate command, add methods for PlayerRef to work on GameSpace --- .../nucleoid/plasmid/api/util/PlayerRef.java | 17 ++++++++++++ .../plasmid/impl/command/GameCommand.java | 26 ++++++++++++------- .../plasmid/impl/command/ui/GameJoinUi.java | 12 +++++---- .../game/ConcurrentGamePortalBackend.java | 4 +-- .../portal/game/NewGamePortalBackend.java | 2 +- .../portal/game/SingleGamePortalBackend.java | 2 +- 6 files changed, 44 insertions(+), 19 deletions(-) diff --git a/src/main/java/xyz/nucleoid/plasmid/api/util/PlayerRef.java b/src/main/java/xyz/nucleoid/plasmid/api/util/PlayerRef.java index 088135ce..46f84cd0 100644 --- a/src/main/java/xyz/nucleoid/plasmid/api/util/PlayerRef.java +++ b/src/main/java/xyz/nucleoid/plasmid/api/util/PlayerRef.java @@ -6,6 +6,7 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.world.ServerWorld; import org.jetbrains.annotations.Nullable; +import xyz.nucleoid.plasmid.api.game.GameSpace; import java.util.UUID; import java.util.function.Consumer; @@ -23,6 +24,11 @@ public static PlayerRef ofUnchecked(UUID id) { return new PlayerRef(id); } + @Nullable + public ServerPlayerEntity getEntity(GameSpace gameSpace) { + return gameSpace.getPlayers().getEntity(this.id); + } + @Nullable public ServerPlayerEntity getEntity(ServerWorld world) { return this.getEntity(world.getServer()); @@ -33,6 +39,10 @@ public ServerPlayerEntity getEntity(MinecraftServer server) { return server.getPlayerManager().getPlayer(this.id); } + public boolean isOnline(GameSpace gameSpace) { + return this.getEntity(gameSpace) != null; + } + public boolean isOnline(ServerWorld world) { return this.getEntity(world) != null; } @@ -41,6 +51,13 @@ public boolean isOnline(MinecraftServer server) { return this.getEntity(server) != null; } + public void ifOnline(GameSpace gameSpace, Consumer consumer) { + ServerPlayerEntity player = this.getEntity(gameSpace); + if (player != null) { + consumer.accept(player); + } + } + public void ifOnline(ServerWorld world, Consumer consumer) { ServerPlayerEntity player = this.getEntity(world); if (player != null) { diff --git a/src/main/java/xyz/nucleoid/plasmid/impl/command/GameCommand.java b/src/main/java/xyz/nucleoid/plasmid/impl/command/GameCommand.java index c5570e94..02164882 100644 --- a/src/main/java/xyz/nucleoid/plasmid/impl/command/GameCommand.java +++ b/src/main/java/xyz/nucleoid/plasmid/impl/command/GameCommand.java @@ -96,11 +96,17 @@ public static void register(CommandDispatcher dispatcher) { ) ) .then(literal("join") - .executes(GameCommand::joinGame) + .executes(ctx -> GameCommand.joinGame(ctx, JoinIntent.PLAY)) .then(GameSpaceArgument.argument("game_space") - .executes(GameCommand::joinQualifiedGame) + .executes(ctx -> GameCommand.joinQualifiedGame(ctx, JoinIntent.PLAY)) ) ) + .then(literal("spectate") + .executes(ctx -> GameCommand.joinGame(ctx, JoinIntent.SPECTATE)) + .then(GameSpaceArgument.argument("game_space") + .executes(ctx -> GameCommand.joinQualifiedGame(ctx, JoinIntent.SPECTATE)) + ) + ) .then(literal("joinall") .requires(source -> source.hasPermissionLevel(2)) .executes(GameCommand::joinAllGame) @@ -198,7 +204,7 @@ private static void onOpenSuccess(ServerCommandSource source, GameSpace gameSpac gameSpace.getPlayers().sendMessage(error); } } else if (player != null) { - tryJoinGame(player, gameSpace); + tryJoinGame(player, gameSpace, JoinIntent.PLAY); } } @@ -243,14 +249,14 @@ private static int proposeGame(ServerCommandSource source, GameSpace gameSpace) return Command.SINGLE_SUCCESS; } - private static int joinGame(CommandContext context) throws CommandSyntaxException { - new GameJoinUi(context.getSource().getPlayerOrThrow()).open(); + private static int joinGame(CommandContext context, JoinIntent intent) throws CommandSyntaxException { + new GameJoinUi(context.getSource().getPlayerOrThrow(), intent).open(); return Command.SINGLE_SUCCESS; } - private static int joinQualifiedGame(CommandContext context) throws CommandSyntaxException { + private static int joinQualifiedGame(CommandContext context, JoinIntent intent) throws CommandSyntaxException { var gameSpace = GameSpaceArgument.get(context, "game_space"); - tryJoinGame(context.getSource().getPlayerOrThrow(), gameSpace); + tryJoinGame(context.getSource().getPlayerOrThrow(), gameSpace, intent); return Command.SINGLE_SUCCESS; } @@ -286,15 +292,15 @@ private static void joinAllPlayersToGame(ServerCommandSource source, GameSpace g .filter(player -> !GameSpaceManagerImpl.get().inGame(player)) .collect(Collectors.toList()); - var intent = JoinIntent.ANY; + var intent = JoinIntent.PLAY; var result = gameSpace.getPlayers().offer(players, intent); if (result.isError()) { source.sendError(result.errorCopy().formatted(Formatting.RED)); } } - private static void tryJoinGame(ServerPlayerEntity player, GameSpace gameSpace) { - var result = GamePlayerJoiner.tryJoin(player, gameSpace, JoinIntent.ANY); + private static void tryJoinGame(ServerPlayerEntity player, GameSpace gameSpace, JoinIntent intent) { + var result = GamePlayerJoiner.tryJoin(player, gameSpace, intent); if (result.isError()) { player.sendMessage(result.errorCopy().formatted(Formatting.RED)); } diff --git a/src/main/java/xyz/nucleoid/plasmid/impl/command/ui/GameJoinUi.java b/src/main/java/xyz/nucleoid/plasmid/impl/command/ui/GameJoinUi.java index 17a48b96..a0e39a01 100644 --- a/src/main/java/xyz/nucleoid/plasmid/impl/command/ui/GameJoinUi.java +++ b/src/main/java/xyz/nucleoid/plasmid/impl/command/ui/GameJoinUi.java @@ -22,24 +22,26 @@ import java.util.Comparator; public class GameJoinUi extends SimpleGui { - private static final GuiElementInterface EMPTY = new GuiElementBuilder(Items.GRAY_STAINED_GLASS_PANE).setName(Text.empty()).build(); + private static final GuiElementInterface EMPTY = new GuiElementBuilder(Items.GRAY_STAINED_GLASS_PANE).hideTooltip().build(); private static final int NAVBAR_POS = 81; private final ServerPlayerEntity player; + private final JoinIntent joinIntent; private int tick; private int page = 0; private int pageSize; - public GameJoinUi(ServerPlayerEntity player) { + public GameJoinUi(ServerPlayerEntity player, JoinIntent intent) { super(ScreenHandlerType.GENERIC_9X6, player, true); + this.joinIntent = intent; this.player = player; this.setTitle(Text.translatable("text.plasmid.ui.game_join.title")); this.updateUi(); } - private static void tryJoinGame(ServerPlayerEntity player, GameSpace gameSpace) { + private static void tryJoinGame(ServerPlayerEntity player, GameSpace gameSpace, JoinIntent joinIntent) { player.server.execute(() -> { - var result = GamePlayerJoiner.tryJoin(player, gameSpace, JoinIntent.ANY); + var result = GamePlayerJoiner.tryJoin(player, gameSpace, joinIntent); if (result.isError()) { player.sendMessage(result.errorCopy().formatted(Formatting.RED)); } @@ -140,7 +142,7 @@ private GuiElementBuilder createIconFor(GameSpace gameSpace) { ); element.hideDefaultTooltip(); - element.setCallback((a, b, c, d) -> tryJoinGame(this.getPlayer(), gameSpace)); + element.setCallback((a, b, c, d) -> tryJoinGame(this.getPlayer(), gameSpace, joinIntent)); return element; } diff --git a/src/main/java/xyz/nucleoid/plasmid/impl/portal/game/ConcurrentGamePortalBackend.java b/src/main/java/xyz/nucleoid/plasmid/impl/portal/game/ConcurrentGamePortalBackend.java index df7ad5a0..837a69d3 100644 --- a/src/main/java/xyz/nucleoid/plasmid/impl/portal/game/ConcurrentGamePortalBackend.java +++ b/src/main/java/xyz/nucleoid/plasmid/impl/portal/game/ConcurrentGamePortalBackend.java @@ -31,7 +31,7 @@ public RegistryEntry> game() { public void applyTo(ServerPlayerEntity player) { for (var gameSpace : GameSpaceManagerImpl.get().getOpenGameSpaces()) { if (gameSpace.getMetadata().sourceConfig().equals(this.game)) { - var result = GamePlayerJoiner.tryJoin(player, gameSpace, JoinIntent.ANY); + var result = GamePlayerJoiner.tryJoin(player, gameSpace, JoinIntent.PLAY); if (result.isOk()) { return; @@ -45,7 +45,7 @@ public void applyTo(ServerPlayerEntity player) { this.gameFuture = null; GameResult result; if (gameSpace != null) { - result = GamePlayerJoiner.tryJoin(player, gameSpace, JoinIntent.ANY); + result = GamePlayerJoiner.tryJoin(player, gameSpace, JoinIntent.PLAY); } else { result = GamePlayerJoiner.handleJoinException(throwable); } diff --git a/src/main/java/xyz/nucleoid/plasmid/impl/portal/game/NewGamePortalBackend.java b/src/main/java/xyz/nucleoid/plasmid/impl/portal/game/NewGamePortalBackend.java index a8e6f51d..1a93ea1d 100644 --- a/src/main/java/xyz/nucleoid/plasmid/impl/portal/game/NewGamePortalBackend.java +++ b/src/main/java/xyz/nucleoid/plasmid/impl/portal/game/NewGamePortalBackend.java @@ -22,7 +22,7 @@ public void applyTo(ServerPlayerEntity player) { .handleAsync((gameSpace, throwable) -> { GameResult result; if (gameSpace != null) { - result = GamePlayerJoiner.tryJoin(player, gameSpace, JoinIntent.ANY); + result = GamePlayerJoiner.tryJoin(player, gameSpace, JoinIntent.PLAY); } else { result = GamePlayerJoiner.handleJoinException(throwable); } diff --git a/src/main/java/xyz/nucleoid/plasmid/impl/portal/game/SingleGamePortalBackend.java b/src/main/java/xyz/nucleoid/plasmid/impl/portal/game/SingleGamePortalBackend.java index ec3c6d91..a9391a5b 100644 --- a/src/main/java/xyz/nucleoid/plasmid/impl/portal/game/SingleGamePortalBackend.java +++ b/src/main/java/xyz/nucleoid/plasmid/impl/portal/game/SingleGamePortalBackend.java @@ -31,7 +31,7 @@ public void applyTo(ServerPlayerEntity player) { .handleAsync((gameSpace, throwable) -> { GameResult result; if (gameSpace != null) { - result = GamePlayerJoiner.tryJoin(player, gameSpace, JoinIntent.ANY); + result = GamePlayerJoiner.tryJoin(player, gameSpace, JoinIntent.PLAY); } else { result = GamePlayerJoiner.handleJoinException(throwable); }