From cfbd454f6d45f97eedc9c88bce55386fa20d5c1e Mon Sep 17 00:00:00 2001 From: Patbox <39821509+Patbox@users.noreply.github.com> Date: Tue, 31 Dec 2024 14:58:32 +0100 Subject: [PATCH] Give portals more context about click, user PortalUserContext instead of player entity --- .../plasmid/impl/command/GameCommand.java | 4 +- .../impl/command/GamePortalCommand.java | 4 +- .../plasmid/impl/portal/GamePortal.java | 7 +- .../portal/backend/GamePortalBackend.java | 22 ++- .../portal/backend/PortalUserContext.java | 66 +++++++++ .../game/ConcurrentGamePortalBackend.java | 71 ++-------- .../game/GameConfigGamePortalBackend.java | 5 +- .../game/InvalidGamePortalBackend.java | 6 +- .../backend/game/NewGamePortalBackend.java | 36 +---- .../backend/game/SingleGamePortalBackend.java | 128 ++---------------- .../backend/menu/ActiveGamePortalBackend.java | 17 +-- .../menu/AdvancedMenuPortalBackend.java | 12 +- .../backend/menu/MenuPortalBackend.java | 12 +- .../impl/portal/menu/GameMenuEntry.java | 11 +- .../impl/portal/menu/InvalidMenuEntry.java | 6 +- .../plasmid/impl/portal/menu/MenuEntry.java | 32 ++--- .../plasmid/impl/portal/menu/PortalEntry.java | 16 +-- .../mixin/game/portal/EntityMixin.java | 4 +- .../game/portal/SignBlockEntityMixin.java | 4 +- 19 files changed, 172 insertions(+), 291 deletions(-) create mode 100644 src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/PortalUserContext.java 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 249b8a89..5bb44f56 100644 --- a/src/main/java/xyz/nucleoid/plasmid/impl/command/GameCommand.java +++ b/src/main/java/xyz/nucleoid/plasmid/impl/command/GameCommand.java @@ -7,6 +7,7 @@ import com.mojang.brigadier.exceptions.DynamicCommandExceptionType; import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; import com.mojang.logging.LogUtils; +import eu.pb4.sgui.api.ClickType; import net.minecraft.command.argument.EntityArgumentType; import net.minecraft.command.argument.NbtCompoundArgumentType; import net.minecraft.nbt.NbtOps; @@ -31,6 +32,7 @@ import xyz.nucleoid.plasmid.api.game.player.GamePlayerJoiner; import xyz.nucleoid.plasmid.api.game.player.JoinIntent; import xyz.nucleoid.plasmid.api.util.Scheduler; +import xyz.nucleoid.plasmid.impl.portal.backend.PortalUserContext; import xyz.nucleoid.plasmid.impl.portal.config.ActiveGamePortalConfig; import xyz.nucleoid.plasmid.impl.portal.config.GamePortalConfig; @@ -253,7 +255,7 @@ private static int proposeGame(ServerCommandSource source, GameSpace gameSpace) private static int joinGame(CommandContext context, JoinIntent intent) throws CommandSyntaxException { GamePortalConfig.create(context.getSource().getServer(), Identifier.of("plasmid", "join_command"), - ActiveGamePortalConfig.of(Text.translatable("text.plasmid.ui.game_join.title"), intent)).applyTo(context.getSource().getPlayerOrThrow(), false); + ActiveGamePortalConfig.of(Text.translatable("text.plasmid.ui.game_join.title"), intent)).applyTo(PortalUserContext.of(context.getSource().getPlayerOrThrow()), ClickType.MOUSE_RIGHT); return Command.SINGLE_SUCCESS; } diff --git a/src/main/java/xyz/nucleoid/plasmid/impl/command/GamePortalCommand.java b/src/main/java/xyz/nucleoid/plasmid/impl/command/GamePortalCommand.java index 4264cdf7..554cd9b3 100644 --- a/src/main/java/xyz/nucleoid/plasmid/impl/command/GamePortalCommand.java +++ b/src/main/java/xyz/nucleoid/plasmid/impl/command/GamePortalCommand.java @@ -5,6 +5,7 @@ import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; +import eu.pb4.sgui.api.ClickType; import net.minecraft.command.argument.BlockPosArgumentType; import net.minecraft.command.argument.EntityArgumentType; import net.minecraft.server.command.ServerCommandSource; @@ -12,6 +13,7 @@ import net.minecraft.util.Formatting; import xyz.nucleoid.plasmid.impl.command.argument.GamePortalArgument; import xyz.nucleoid.plasmid.impl.portal.GamePortalInterface; +import xyz.nucleoid.plasmid.impl.portal.backend.PortalUserContext; import static net.minecraft.server.command.CommandManager.argument; import static net.minecraft.server.command.CommandManager.literal; @@ -51,7 +53,7 @@ public static void register(CommandDispatcher dispatcher) { private static int openPortal(CommandContext context) throws CommandSyntaxException { var portal = GamePortalArgument.get(context, "portal"); - portal.requestJoin(context.getSource().getPlayer(), false); + portal.applyTo(PortalUserContext.of(context.getSource().getPlayerOrThrow()), ClickType.MOUSE_RIGHT); return 1; } diff --git a/src/main/java/xyz/nucleoid/plasmid/impl/portal/GamePortal.java b/src/main/java/xyz/nucleoid/plasmid/impl/portal/GamePortal.java index 9f1e70db..8b2c7760 100644 --- a/src/main/java/xyz/nucleoid/plasmid/impl/portal/GamePortal.java +++ b/src/main/java/xyz/nucleoid/plasmid/impl/portal/GamePortal.java @@ -1,16 +1,15 @@ package xyz.nucleoid.plasmid.impl.portal; +import eu.pb4.sgui.api.ClickType; import eu.pb4.sgui.api.elements.GuiElementInterface; import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; import net.minecraft.item.ItemStack; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import net.minecraft.util.Identifier; import xyz.nucleoid.plasmid.api.game.GameSpace; import xyz.nucleoid.plasmid.api.game.config.CustomValuesConfig; +import xyz.nucleoid.plasmid.impl.portal.backend.PortalUserContext; import xyz.nucleoid.plasmid.impl.portal.backend.GamePortalBackend; -import xyz.nucleoid.plasmid.impl.portal.config.GamePortalConfig; import java.util.List; import java.util.Set; @@ -67,7 +66,7 @@ public int getMaxPlayerCount() { return this.backend.getMaxPlayerCount(); } - public void requestJoin(ServerPlayerEntity player, boolean alt) { + public void applyTo(PortalUserContext player, ClickType alt) { this.backend.applyTo(player, alt); } diff --git a/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/GamePortalBackend.java b/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/GamePortalBackend.java index 2a875340..a8227311 100644 --- a/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/GamePortalBackend.java +++ b/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/GamePortalBackend.java @@ -1,9 +1,9 @@ package xyz.nucleoid.plasmid.impl.portal.backend; +import eu.pb4.sgui.api.ClickType; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.server.MinecraftServer; -import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import net.minecraft.util.Formatting; import net.minecraft.util.Identifier; @@ -23,8 +23,6 @@ default void populateDisplay(GamePortalDisplay display) { display.set(GamePortalDisplay.SPECTATOR_COUNT, this.getSpectatorCount()); } - void applyTo(ServerPlayerEntity player, boolean alt); - default Text getName() { return Text.literal("༼ つ ◕_◕ ༽つ (Unnamed)"); } @@ -49,22 +47,20 @@ default int getSpectatorCount() { return -1; } - default ActionType getActionType() { - return ActionType.NONE; - } - default ActionType getAltActionType() { - return ActionType.NONE; - } + void applyTo(PortalUserContext context, ClickType clickType); + default List getActions(PortalUserContext context) { + return List.of(); + } default void provideGameSpaces(Consumer consumer) {} interface Factory { GamePortalBackend create(MinecraftServer server, Identifier id, T config); } - record ActionType(Text text, Text textAlt) { - public static ActionType NONE = new ActionType(Text.empty(), Text.empty()); - public static ActionType PLAY = new ActionType(Text.translatable("text.plasmid.ui.game_join.action.play"), Text.translatable("text.plasmid.ui.game_join.action.play.alt")); - public static ActionType SPECTATE = new ActionType(Text.translatable("text.plasmid.ui.game_join.action.spectate"), Text.translatable("text.plasmid.ui.game_join.action.spectate.alt")); + record Action(Text text, Text textAlt) { + public static Action NONE = new Action(Text.empty(), Text.empty()); + public static Action PLAY = new Action(Text.translatable("text.plasmid.ui.game_join.action.play"), Text.translatable("text.plasmid.ui.game_join.action.play.alt")); + public static Action SPECTATE = new Action(Text.translatable("text.plasmid.ui.game_join.action.spectate"), Text.translatable("text.plasmid.ui.game_join.action.spectate.alt")); } } diff --git a/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/PortalUserContext.java b/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/PortalUserContext.java new file mode 100644 index 00000000..4c9a7027 --- /dev/null +++ b/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/PortalUserContext.java @@ -0,0 +1,66 @@ +package xyz.nucleoid.plasmid.impl.portal.backend; + +import net.minecraft.registry.entry.RegistryEntry; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.util.Formatting; +import xyz.nucleoid.plasmid.api.game.GameResult; +import xyz.nucleoid.plasmid.api.game.GameSpace; +import xyz.nucleoid.plasmid.api.game.config.GameConfig; +import xyz.nucleoid.plasmid.api.game.player.GamePlayerJoiner; +import xyz.nucleoid.plasmid.api.game.player.JoinIntent; +import xyz.nucleoid.plasmid.impl.game.manager.GameSpaceManagerImpl; + +import java.util.concurrent.CompletableFuture; +import java.util.function.Consumer; +import java.util.function.Function; + +public interface PortalUserContext { + static PortalUserContext of(ServerPlayerEntity serverPlayer) { + return new Player(serverPlayer); + } + + void openUi(Consumer guiOpener); + + boolean canJoinExisting(); + + GameResult tryJoin(GameSpace gameSpace, JoinIntent joinIntent); + + void tryOpening(RegistryEntry> game); + + record Player(ServerPlayerEntity player) implements PortalUserContext { + @Override + public void openUi(Consumer guiOpener) { + guiOpener.accept(this.player); + } + + @Override + public boolean canJoinExisting() { + return true; + } + + @Override + public GameResult tryJoin(GameSpace gameSpace, JoinIntent joinIntent) { + return GamePlayerJoiner.tryJoin(this.player, gameSpace, joinIntent); + } + + @Override + public void tryOpening(RegistryEntry> game) { + CompletableFuture.supplyAsync(() -> GameSpaceManagerImpl.get().open(game)) + .thenCompose(Function.identity()) + .handleAsync((gameSpace, throwable) -> { + GameResult result; + if (gameSpace != null) { + result = GamePlayerJoiner.tryJoin(this.player, gameSpace, JoinIntent.PLAY); + } else { + result = GamePlayerJoiner.handleJoinException(throwable); + } + + if (result.isError()) { + this.player.sendMessage(result.errorCopy().formatted(Formatting.RED), false); + } + + return null; + }, this.player.server); + } + } +} diff --git a/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/game/ConcurrentGamePortalBackend.java b/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/game/ConcurrentGamePortalBackend.java index 26d221ab..2248d12f 100644 --- a/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/game/ConcurrentGamePortalBackend.java +++ b/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/game/ConcurrentGamePortalBackend.java @@ -1,72 +1,27 @@ package xyz.nucleoid.plasmid.impl.portal.backend.game; +import eu.pb4.sgui.api.ClickType; import net.minecraft.registry.entry.RegistryEntry; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.util.Formatting; -import xyz.nucleoid.plasmid.api.game.GameResult; -import xyz.nucleoid.plasmid.api.game.GameSpace; import xyz.nucleoid.plasmid.api.game.config.GameConfig; import xyz.nucleoid.plasmid.impl.game.manager.GameSpaceManagerImpl; -import xyz.nucleoid.plasmid.api.game.player.GamePlayerJoiner; import xyz.nucleoid.plasmid.api.game.player.JoinIntent; +import xyz.nucleoid.plasmid.impl.portal.backend.PortalUserContext; -import java.util.concurrent.CompletableFuture; -import java.util.function.Function; - -public final class ConcurrentGamePortalBackend implements GameConfigGamePortalBackend { - private final RegistryEntry> game; - private CompletableFuture gameFuture; - - public ConcurrentGamePortalBackend(RegistryEntry> game) { - this.game = game; - } - +public record ConcurrentGamePortalBackend(RegistryEntry> game) implements GameConfigGamePortalBackend { @Override - public RegistryEntry> game() { - return this.game; - } - - @Override - public void applyTo(ServerPlayerEntity player, boolean alt) { - for (var gameSpace : GameSpaceManagerImpl.get().getOpenGameSpaces()) { - if (gameSpace.getMetadata().sourceConfig().equals(this.game)) { - var result = GamePlayerJoiner.tryJoin(player, gameSpace, alt ? JoinIntent.SPECTATE : JoinIntent.PLAY); - - if (result.isOk()) { - return; + public void applyTo(PortalUserContext context, ClickType type) { + if (context.canJoinExisting()) { + for (var gameSpace : GameSpaceManagerImpl.get().getOpenGameSpaces()) { + if (gameSpace.getMetadata().sourceConfig().equals(this.game)) { + var result = context.tryJoin(gameSpace, JoinIntent.PLAY); + + if (result.isOk()) { + return; + } } } } - CompletableFuture.supplyAsync(() -> this.getOrOpenNew(player.server)) - .thenCompose(Function.identity()) - .handleAsync((gameSpace, throwable) -> { - this.gameFuture = null; - GameResult result; - if (gameSpace != null) { - result = GamePlayerJoiner.tryJoin(player, gameSpace, JoinIntent.PLAY); - } else { - result = GamePlayerJoiner.handleJoinException(throwable); - } - - if (result.isError()) { - player.sendMessage(result.errorCopy().formatted(Formatting.RED), false); - } - - return null; - }, player.server); - } - - public CompletableFuture getOrOpenNew(MinecraftServer server) { - var future = this.gameFuture; - if (future == null || future.isCompletedExceptionally()) { - this.gameFuture = future = this.openGame(server); - } - return future; - } - - private CompletableFuture openGame(MinecraftServer server) { - return GameSpaceManagerImpl.get().open(this.game); + context.tryOpening(this.game); } } diff --git a/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/game/GameConfigGamePortalBackend.java b/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/game/GameConfigGamePortalBackend.java index 25c51327..3d2da76d 100644 --- a/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/game/GameConfigGamePortalBackend.java +++ b/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/game/GameConfigGamePortalBackend.java @@ -6,6 +6,7 @@ import xyz.nucleoid.plasmid.api.game.GameSpace; import xyz.nucleoid.plasmid.api.game.config.GameConfig; import xyz.nucleoid.plasmid.impl.game.manager.GameSpaceManagerImpl; +import xyz.nucleoid.plasmid.impl.portal.backend.PortalUserContext; import xyz.nucleoid.plasmid.impl.portal.backend.GamePortalBackend; import java.util.List; @@ -61,7 +62,7 @@ default Text getName() { } @Override - default ActionType getActionType() { - return ActionType.PLAY; + default List getActions(PortalUserContext context) { + return List.of(Action.PLAY); } } diff --git a/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/game/InvalidGamePortalBackend.java b/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/game/InvalidGamePortalBackend.java index 65d43da8..385e9736 100644 --- a/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/game/InvalidGamePortalBackend.java +++ b/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/game/InvalidGamePortalBackend.java @@ -1,11 +1,11 @@ package xyz.nucleoid.plasmid.impl.portal.backend.game; import com.mojang.serialization.MapCodec; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.network.ServerPlayerEntity; +import eu.pb4.sgui.api.ClickType; import net.minecraft.text.Text; import net.minecraft.util.Identifier; import xyz.nucleoid.plasmid.api.game.config.CustomValuesConfig; +import xyz.nucleoid.plasmid.impl.portal.backend.PortalUserContext; import xyz.nucleoid.plasmid.impl.portal.backend.GamePortalBackend; import xyz.nucleoid.plasmid.impl.portal.config.GamePortalConfig; @@ -28,7 +28,7 @@ public Text getName() { } @Override - public void applyTo(ServerPlayerEntity player, boolean alt) { + public void applyTo(PortalUserContext context, ClickType clickType) { } } diff --git a/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/game/NewGamePortalBackend.java b/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/game/NewGamePortalBackend.java index 307c70fb..36bd4f18 100644 --- a/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/game/NewGamePortalBackend.java +++ b/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/game/NewGamePortalBackend.java @@ -1,41 +1,13 @@ package xyz.nucleoid.plasmid.impl.portal.backend.game; +import eu.pb4.sgui.api.ClickType; import net.minecraft.registry.entry.RegistryEntry; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.util.Formatting; -import xyz.nucleoid.plasmid.api.game.GameResult; -import xyz.nucleoid.plasmid.api.game.GameSpace; import xyz.nucleoid.plasmid.api.game.config.GameConfig; -import xyz.nucleoid.plasmid.impl.game.manager.GameSpaceManagerImpl; -import xyz.nucleoid.plasmid.api.game.player.GamePlayerJoiner; -import xyz.nucleoid.plasmid.api.game.player.JoinIntent; - -import java.util.concurrent.CompletableFuture; -import java.util.function.Function; +import xyz.nucleoid.plasmid.impl.portal.backend.PortalUserContext; public record NewGamePortalBackend(RegistryEntry> game) implements GameConfigGamePortalBackend { @Override - public void applyTo(ServerPlayerEntity player, boolean alt) { - CompletableFuture.supplyAsync(() -> this.openGame(player.server)) - .thenCompose(Function.identity()) - .handleAsync((gameSpace, throwable) -> { - GameResult result; - if (gameSpace != null) { - result = GamePlayerJoiner.tryJoin(player, gameSpace, JoinIntent.PLAY); - } else { - result = GamePlayerJoiner.handleJoinException(throwable); - } - - if (result.isError()) { - player.sendMessage(result.errorCopy().formatted(Formatting.RED), false); - } - - return null; - }, player.server); - } - - private CompletableFuture openGame(MinecraftServer server) { - return GameSpaceManagerImpl.get().open(this.game); + public void applyTo(PortalUserContext context, ClickType clickType) { + context.tryOpening(this.game); } } diff --git a/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/game/SingleGamePortalBackend.java b/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/game/SingleGamePortalBackend.java index 95639da2..9e6b93f6 100644 --- a/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/game/SingleGamePortalBackend.java +++ b/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/game/SingleGamePortalBackend.java @@ -1,127 +1,29 @@ package xyz.nucleoid.plasmid.impl.portal.backend.game; +import eu.pb4.sgui.api.ClickType; import net.minecraft.registry.entry.RegistryEntry; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.util.Formatting; -import xyz.nucleoid.plasmid.api.game.GameCloseReason; -import xyz.nucleoid.plasmid.api.game.GameLifecycle; -import xyz.nucleoid.plasmid.api.game.GameResult; -import xyz.nucleoid.plasmid.api.game.GameSpace; import xyz.nucleoid.plasmid.api.game.config.GameConfig; import xyz.nucleoid.plasmid.impl.game.manager.GameSpaceManagerImpl; -import xyz.nucleoid.plasmid.api.game.player.GamePlayerJoiner; import xyz.nucleoid.plasmid.api.game.player.JoinIntent; +import xyz.nucleoid.plasmid.impl.portal.backend.PortalUserContext; -import java.util.concurrent.CompletableFuture; -import java.util.function.Consumer; -import java.util.function.Function; - -public final class SingleGamePortalBackend implements GameConfigGamePortalBackend { - private final RegistryEntry> game; - private CompletableFuture gameFuture; - - public SingleGamePortalBackend(RegistryEntry> game) { - this.game = game; - } - - @Override - public void applyTo(ServerPlayerEntity player, boolean alt) { - CompletableFuture.supplyAsync(() -> this.getOrOpen(player.server)) - .thenCompose(Function.identity()) - .handleAsync((gameSpace, throwable) -> { - GameResult result; - if (gameSpace != null) { - result = GamePlayerJoiner.tryJoin(player, gameSpace, alt ? JoinIntent.SPECTATE : JoinIntent.PLAY); - } else { - result = GamePlayerJoiner.handleJoinException(throwable); - } - - if (result.isError()) { - player.sendMessage(result.errorCopy().formatted(Formatting.RED), false); - } - - return null; - }, player.server); - } - +public record SingleGamePortalBackend(RegistryEntry> game) implements GameConfigGamePortalBackend { @Override - public void provideGameSpaces(Consumer consumer) { - if (this.gameFuture != null && this.gameFuture.isDone()) { - try { - consumer.accept(this.gameFuture.get()); - } catch (Throwable e) { - throw new RuntimeException(e); - } - } - } + public void applyTo(PortalUserContext context, ClickType clickType) { + var x = GameSpaceManagerImpl.get().getOpenGameSpaces(); - @Override - public int getPlayerCount() { - if (this.gameFuture != null && this.gameFuture.isDone()) { - try { - return this.gameFuture.get().getState().players(); - } catch (Throwable e) { - throw new RuntimeException(e); - } - } - return 0; - } + if (!x.isEmpty() && context.canJoinExisting()) { + for (var gameSpace : x) { + if (gameSpace.getMetadata().sourceConfig().equals(this.game)) { + var result = context.tryJoin(gameSpace, JoinIntent.PLAY); - @Override - public int getSpectatorCount() { - if (this.gameFuture != null && this.gameFuture.isDone()) { - try { - return this.gameFuture.get().getState().spectators(); - } catch (Throwable e) { - throw new RuntimeException(e); - } - } - return 0; - } - - @Override - public int getMaxPlayerCount() { - if (this.gameFuture != null && this.gameFuture.isDone()) { - try { - return this.gameFuture.get().getState().maxPlayers(); - } catch (Throwable e) { - throw new RuntimeException(e); + if (result.isOk()) { + return; + } + } } - } - return 0; - } - - public CompletableFuture getOrOpen(MinecraftServer server) { - var future = this.gameFuture; - if (future == null) { - this.gameFuture = future = this.openGame(server); - } - return future; - } - - private void onClose() { - this.gameFuture = null; - } - - private CompletableFuture openGame(MinecraftServer server) { - return GameSpaceManagerImpl.get().open(this.game).thenApplyAsync(gameSpace -> { - var lifecycle = gameSpace.getLifecycle(); - lifecycle.addListeners(new LifecycleListeners()); - - return gameSpace; - }, server); - } - - @Override - public RegistryEntry> game() { - return this.game; - } - - private class LifecycleListeners implements GameLifecycle.Listeners { - @Override - public void onClosing(GameSpace gameSpace, GameCloseReason reason) { - SingleGamePortalBackend.this.onClose(); + } else { + context.tryOpening(this.game); } } } diff --git a/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/menu/ActiveGamePortalBackend.java b/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/menu/ActiveGamePortalBackend.java index 895e6ee0..d30f1e36 100644 --- a/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/menu/ActiveGamePortalBackend.java +++ b/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/menu/ActiveGamePortalBackend.java @@ -1,9 +1,9 @@ package xyz.nucleoid.plasmid.impl.portal.backend.menu; +import eu.pb4.sgui.api.ClickType; import eu.pb4.sgui.api.elements.GuiElementBuilder; import eu.pb4.sgui.api.elements.GuiElementInterface; import eu.pb4.sgui.api.gui.SimpleGui; -import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.screen.ScreenHandlerType; @@ -20,11 +20,8 @@ import xyz.nucleoid.plasmid.api.game.player.JoinIntent; import xyz.nucleoid.plasmid.api.util.Guis; import xyz.nucleoid.plasmid.impl.game.manager.GameSpaceManagerImpl; +import xyz.nucleoid.plasmid.impl.portal.backend.PortalUserContext; import xyz.nucleoid.plasmid.impl.portal.backend.GamePortalBackend; -import xyz.nucleoid.plasmid.impl.portal.backend.game.ConcurrentGamePortalBackend; -import xyz.nucleoid.plasmid.impl.portal.config.MenuPortalConfig; -import xyz.nucleoid.plasmid.impl.portal.menu.GameMenuEntry; -import xyz.nucleoid.plasmid.impl.portal.menu.MenuEntry; import java.util.ArrayList; import java.util.Comparator; @@ -67,8 +64,8 @@ public void provideGameSpaces(Consumer consumer) { } @Override - public void applyTo(ServerPlayerEntity player, boolean alt) { - new GameJoinUi(player, this.name, this.intent).open(); + public void applyTo(PortalUserContext context, ClickType type) { + context.openUi(player -> new GameJoinUi(player, context, this.name, this.intent).open()); } private static final class GameJoinUi extends SimpleGui { @@ -81,7 +78,7 @@ private static final class GameJoinUi extends SimpleGui { private int page = 0; private int pageSize; - public GameJoinUi(ServerPlayerEntity player, Text name, JoinIntent intent) { + public GameJoinUi(ServerPlayerEntity player, PortalUserContext context, Text name, JoinIntent intent) { super(ScreenHandlerType.GENERIC_9X6, player, true); this.joinIntent = intent; this.player = player; @@ -219,9 +216,9 @@ private GuiElementBuilder createIconFor(GameSpace gameSpace) { ); } - var actionType = this.joinIntent == JoinIntent.PLAY ? GamePortalBackend.ActionType.PLAY : GamePortalBackend.ActionType.SPECTATE; + var actionType = this.joinIntent == JoinIntent.PLAY ? Action.PLAY : Action.SPECTATE; - if (actionType != GamePortalBackend.ActionType.NONE) { + if (actionType != Action.NONE) { element.addLoreLine(Text.empty().append(Text.literal(" [ ").formatted(Formatting.GRAY)) .append(actionType.text()) .append(Text.literal(" ]").formatted(Formatting.GRAY)).setStyle(Style.EMPTY.withColor(0x76ed6f))); diff --git a/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/menu/AdvancedMenuPortalBackend.java b/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/menu/AdvancedMenuPortalBackend.java index 561a3121..60a7e2f2 100644 --- a/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/menu/AdvancedMenuPortalBackend.java +++ b/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/menu/AdvancedMenuPortalBackend.java @@ -1,11 +1,12 @@ package xyz.nucleoid.plasmid.impl.portal.backend.menu; +import eu.pb4.sgui.api.ClickType; import eu.pb4.sgui.api.elements.GuiElementInterface; import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet; import net.minecraft.item.ItemStack; -import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import xyz.nucleoid.plasmid.api.game.GameSpace; +import xyz.nucleoid.plasmid.impl.portal.backend.PortalUserContext; import xyz.nucleoid.plasmid.impl.portal.backend.GamePortalBackend; import xyz.nucleoid.plasmid.api.util.Guis; import xyz.nucleoid.plasmid.impl.portal.menu.MenuEntry; @@ -77,11 +78,11 @@ public void provideGameSpaces(Consumer consumer) { } } - private List getGuiElements() { + private List getGuiElements(PortalUserContext context) { List elements = new ArrayList<>(); for (var entry : this.getEntries()) { - var uiEntry = entry.createGuiElement(); + var uiEntry = entry.createGuiElement(context); elements.add(uiEntry); } @@ -100,8 +101,7 @@ private List getEntries() { } @Override - public void applyTo(ServerPlayerEntity player, boolean alt) { - var ui = Guis.createSelectorGui(player, this.name.copy(), true, this.getGuiElements()); - ui.open(); + public void applyTo(PortalUserContext context, ClickType type) { + context.openUi(player -> Guis.createSelectorGui(player, this.name.copy(), true, this.getGuiElements(context)).open()); } } diff --git a/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/menu/MenuPortalBackend.java b/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/menu/MenuPortalBackend.java index 335b87c0..9da81b57 100644 --- a/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/menu/MenuPortalBackend.java +++ b/src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/menu/MenuPortalBackend.java @@ -1,11 +1,12 @@ package xyz.nucleoid.plasmid.impl.portal.backend.menu; +import eu.pb4.sgui.api.ClickType; import eu.pb4.sgui.api.elements.GuiElementInterface; import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet; import net.minecraft.item.ItemStack; -import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import xyz.nucleoid.plasmid.api.game.config.GameConfig; +import xyz.nucleoid.plasmid.impl.portal.backend.PortalUserContext; import xyz.nucleoid.plasmid.impl.portal.backend.GamePortalBackend; import xyz.nucleoid.plasmid.api.game.GameSpace; import xyz.nucleoid.plasmid.impl.portal.backend.game.ConcurrentGamePortalBackend; @@ -77,11 +78,11 @@ public void provideGameSpaces(Consumer consumer) { } } - private List getGuiElements() { + private List getGuiElements(PortalUserContext context) { List elements = new ArrayList<>(); for (var game : this.games) { - var uiEntry = game.createGuiElement(); + var uiEntry = game.createGuiElement(context); elements.add(uiEntry); } @@ -105,8 +106,7 @@ private List buildGames(List configs) { } @Override - public void applyTo(ServerPlayerEntity player, boolean alt) { - var ui = Guis.createSelectorGui(player, this.name.copy(), true, this.getGuiElements()); - ui.open(); + public void applyTo(PortalUserContext context, ClickType type) { + context.openUi(player -> Guis.createSelectorGui(player, this.name.copy(), true, this.getGuiElements(context)).open()); } } diff --git a/src/main/java/xyz/nucleoid/plasmid/impl/portal/menu/GameMenuEntry.java b/src/main/java/xyz/nucleoid/plasmid/impl/portal/menu/GameMenuEntry.java index 2a220a99..182e1130 100644 --- a/src/main/java/xyz/nucleoid/plasmid/impl/portal/menu/GameMenuEntry.java +++ b/src/main/java/xyz/nucleoid/plasmid/impl/portal/menu/GameMenuEntry.java @@ -3,9 +3,10 @@ import java.util.List; import java.util.function.Consumer; +import eu.pb4.sgui.api.ClickType; import net.minecraft.item.ItemStack; -import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; +import xyz.nucleoid.plasmid.impl.portal.backend.PortalUserContext; import xyz.nucleoid.plasmid.impl.portal.backend.GamePortalBackend; import xyz.nucleoid.plasmid.api.game.GameSpace; import xyz.nucleoid.plasmid.impl.portal.backend.game.ConcurrentGamePortalBackend; @@ -17,8 +18,8 @@ public record GameMenuEntry( ItemStack icon ) implements MenuEntry { @Override - public void click(ServerPlayerEntity player, boolean alt) { - game.applyTo(player, alt); + public void click(PortalUserContext context, ClickType type) { + game.applyTo(context, type); } @Override @@ -27,8 +28,8 @@ public int getPlayerCount() { } @Override - public GamePortalBackend.ActionType getActionType() { - return this.game.getActionType(); + public List getActions(PortalUserContext context) { + return this.game.getActions(context); } @Override diff --git a/src/main/java/xyz/nucleoid/plasmid/impl/portal/menu/InvalidMenuEntry.java b/src/main/java/xyz/nucleoid/plasmid/impl/portal/menu/InvalidMenuEntry.java index 8ce020fb..0af4da76 100644 --- a/src/main/java/xyz/nucleoid/plasmid/impl/portal/menu/InvalidMenuEntry.java +++ b/src/main/java/xyz/nucleoid/plasmid/impl/portal/menu/InvalidMenuEntry.java @@ -1,10 +1,11 @@ package xyz.nucleoid.plasmid.impl.portal.menu; +import eu.pb4.sgui.api.ClickType; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; -import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import net.minecraft.util.Formatting; +import xyz.nucleoid.plasmid.impl.portal.backend.PortalUserContext; import java.util.Collections; import java.util.List; @@ -30,7 +31,8 @@ public ItemStack icon() { } @Override - public void click(ServerPlayerEntity player, boolean alt) { + public void click(PortalUserContext context, ClickType type) { + } @Override diff --git a/src/main/java/xyz/nucleoid/plasmid/impl/portal/menu/MenuEntry.java b/src/main/java/xyz/nucleoid/plasmid/impl/portal/menu/MenuEntry.java index c40d798d..ff8262f4 100644 --- a/src/main/java/xyz/nucleoid/plasmid/impl/portal/menu/MenuEntry.java +++ b/src/main/java/xyz/nucleoid/plasmid/impl/portal/menu/MenuEntry.java @@ -1,14 +1,15 @@ package xyz.nucleoid.plasmid.impl.portal.menu; +import eu.pb4.sgui.api.ClickType; import eu.pb4.sgui.api.elements.GuiElement; import eu.pb4.sgui.api.elements.GuiElementBuilder; import net.minecraft.item.ItemStack; import net.minecraft.screen.ScreenTexts; -import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Style; import net.minecraft.text.Text; import net.minecraft.util.Formatting; import org.jetbrains.annotations.Nullable; +import xyz.nucleoid.plasmid.impl.portal.backend.PortalUserContext; import xyz.nucleoid.plasmid.impl.portal.backend.GamePortalBackend; import xyz.nucleoid.plasmid.api.game.GameSpace; @@ -22,7 +23,7 @@ public interface MenuEntry { ItemStack icon(); - void click(ServerPlayerEntity player, boolean alt); + void click(PortalUserContext context, ClickType type); default int getPlayerCount() { return -1; @@ -45,20 +46,15 @@ default boolean isHidden() { return false; } - default GamePortalBackend.ActionType getActionType() { - return GamePortalBackend.ActionType.NONE; + default List getActions(PortalUserContext context) { + return List.of(); } - default GamePortalBackend.ActionType getAltActionType() { - return GamePortalBackend.ActionType.NONE; - } - - default void provideGameSpaces(Consumer consumer) { } - default GuiElement createGuiElement() { + default GuiElement createGuiElement(PortalUserContext context) { var element = GuiElementBuilder.from(this.icon().copy()).hideDefaultTooltip() .setName(Text.empty().append(this.name())); @@ -109,24 +105,14 @@ default GuiElement createGuiElement() { ); } - var actionType = this.getActionType(); - - if (actionType != GamePortalBackend.ActionType.NONE) { - element.addLoreLine(Text.empty().append(Text.literal(" [ ").formatted(Formatting.GRAY)) - .append(actionType.text()) - .append(Text.literal(" ]").formatted(Formatting.GRAY)).setStyle(Style.EMPTY.withColor(0x76ed6f))); - } - - var altActionType = this.getAltActionType(); - - if (altActionType != GamePortalBackend.ActionType.NONE) { + for (var type : this.getActions(context)) { element.addLoreLine(Text.empty().append(Text.literal(" [ ").formatted(Formatting.GRAY)) - .append(actionType.text()) + .append(type.text()) .append(Text.literal(" ]").formatted(Formatting.GRAY)).setStyle(Style.EMPTY.withColor(0x76ed6f))); } element.setCallback((a, b, c, gui) -> { - this.click(gui.getPlayer(), b.shift); + this.click(context, b); }); return element.build(); diff --git a/src/main/java/xyz/nucleoid/plasmid/impl/portal/menu/PortalEntry.java b/src/main/java/xyz/nucleoid/plasmid/impl/portal/menu/PortalEntry.java index 34f6b066..84fb6260 100644 --- a/src/main/java/xyz/nucleoid/plasmid/impl/portal/menu/PortalEntry.java +++ b/src/main/java/xyz/nucleoid/plasmid/impl/portal/menu/PortalEntry.java @@ -3,9 +3,10 @@ import java.util.List; import java.util.function.Consumer; +import eu.pb4.sgui.api.ClickType; import net.minecraft.item.ItemStack; -import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; +import xyz.nucleoid.plasmid.impl.portal.backend.PortalUserContext; import xyz.nucleoid.plasmid.impl.portal.backend.GamePortalBackend; import xyz.nucleoid.plasmid.api.game.GameSpace; import xyz.nucleoid.plasmid.impl.portal.GamePortal; @@ -21,8 +22,8 @@ public PortalEntry(GamePortal portal) { } @Override - public void click(ServerPlayerEntity player, boolean alt) { - this.portal.requestJoin(player, alt); + public void click(PortalUserContext context, ClickType type) { + this.portal.applyTo(context, type); } @Override @@ -46,12 +47,7 @@ public void provideGameSpaces(Consumer consumer) { } @Override - public GamePortalBackend.ActionType getActionType() { - return this.portal.getBackend().getActionType(); - } - - @Override - public GamePortalBackend.ActionType getAltActionType() { - return this.portal.getBackend().getAltActionType(); + public List getActions(PortalUserContext context) { + return this.portal.getBackend().getActions(context); } } \ No newline at end of file diff --git a/src/main/java/xyz/nucleoid/plasmid/mixin/game/portal/EntityMixin.java b/src/main/java/xyz/nucleoid/plasmid/mixin/game/portal/EntityMixin.java index df7be049..a7204ba0 100644 --- a/src/main/java/xyz/nucleoid/plasmid/mixin/game/portal/EntityMixin.java +++ b/src/main/java/xyz/nucleoid/plasmid/mixin/game/portal/EntityMixin.java @@ -3,6 +3,7 @@ import eu.pb4.polymer.virtualentity.api.ElementHolder; import eu.pb4.polymer.virtualentity.api.attachment.EntityAttachment; import eu.pb4.polymer.virtualentity.api.elements.TextDisplayElement; +import eu.pb4.sgui.api.ClickType; import net.minecraft.entity.Entity; import net.minecraft.entity.decoration.Brightness; import net.minecraft.entity.decoration.DisplayEntity; @@ -24,6 +25,7 @@ import xyz.nucleoid.plasmid.impl.portal.GamePortalDisplay; import xyz.nucleoid.plasmid.impl.portal.GamePortalInterface; import xyz.nucleoid.plasmid.impl.compatibility.DisguiseLibCompatibility; +import xyz.nucleoid.plasmid.impl.portal.backend.PortalUserContext; @Mixin(Entity.class) public abstract class EntityMixin implements GamePortalInterface { @@ -45,7 +47,7 @@ public abstract class EntityMixin implements GamePortalInterface { @Override public boolean interactWithPortal(ServerPlayerEntity player) { if (this.portal != null) { - this.portal.requestJoin(player, false); + this.portal.applyTo(PortalUserContext.of(player), ClickType.MOUSE_RIGHT); return true; } return false; diff --git a/src/main/java/xyz/nucleoid/plasmid/mixin/game/portal/SignBlockEntityMixin.java b/src/main/java/xyz/nucleoid/plasmid/mixin/game/portal/SignBlockEntityMixin.java index 3f04a00c..eaeadc24 100644 --- a/src/main/java/xyz/nucleoid/plasmid/mixin/game/portal/SignBlockEntityMixin.java +++ b/src/main/java/xyz/nucleoid/plasmid/mixin/game/portal/SignBlockEntityMixin.java @@ -1,5 +1,6 @@ package xyz.nucleoid.plasmid.mixin.game.portal; +import eu.pb4.sgui.api.ClickType; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntity; @@ -27,6 +28,7 @@ import xyz.nucleoid.plasmid.impl.portal.GamePortal; import xyz.nucleoid.plasmid.impl.portal.GamePortalDisplay; import xyz.nucleoid.plasmid.impl.portal.GamePortalInterface; +import xyz.nucleoid.plasmid.impl.portal.backend.PortalUserContext; @Mixin(SignBlockEntity.class) public abstract class SignBlockEntityMixin extends BlockEntity implements GamePortalInterface { @@ -106,7 +108,7 @@ private void canRunCommandClickEvent(CallbackInfoReturnable ci) { @Inject(method = "runCommandClickEvent", at = @At("HEAD"), cancellable = true) private void runCommandClickEvent(PlayerEntity player, World world, BlockPos pos, boolean front, CallbackInfoReturnable ci) { if (this.portal != null && player instanceof ServerPlayerEntity serverPlayer) { - this.portal.requestJoin(serverPlayer, false); + this.portal.applyTo(PortalUserContext.of(serverPlayer), ClickType.MOUSE_RIGHT); ci.setReturnValue(true); } }