From f5d35acf45734cb4ef79fabab8d1ff27afb41bd6 Mon Sep 17 00:00:00 2001 From: Patbox <39821509+Patbox@users.noreply.github.com> Date: Fri, 15 Nov 2024 21:14:04 +0100 Subject: [PATCH] Update to new waiting lobby ui Signed-off-by: Patbox <39821509+Patbox@users.noreply.github.com> --- .../destroythemonument/game/SpawnLogic.java | 3 ++- .../destroythemonument/game/WaitingLobby.java | 26 ++++++++++++++++--- .../game/logic/BaseGameLogic.java | 5 ++-- .../game/logic/CaptureGameLogic.java | 5 ++-- .../game/logic/DebugGameLogic.java | 5 ++-- .../game/logic/StandardGameLogic.java | 5 ++-- .../ui/ClassSelectorUI.java | 14 ++++++++++ 7 files changed, 51 insertions(+), 12 deletions(-) diff --git a/src/main/java/eu/pb4/destroythemonument/game/SpawnLogic.java b/src/main/java/eu/pb4/destroythemonument/game/SpawnLogic.java index a2e4fb1..ea5ea14 100644 --- a/src/main/java/eu/pb4/destroythemonument/game/SpawnLogic.java +++ b/src/main/java/eu/pb4/destroythemonument/game/SpawnLogic.java @@ -10,12 +10,13 @@ import net.minecraft.util.math.Vec3d; import net.minecraft.world.GameMode; import xyz.nucleoid.plasmid.api.game.GameSpace; +import xyz.nucleoid.plasmid.api.util.PlayerMap; import xyz.nucleoid.plasmid.api.util.PlayerRef; import java.util.Set; public record SpawnLogic(GameSpace gameSpace, GameMap map, - Object2ObjectMap participants, + PlayerMap participants, Teams teams) { public void resetPlayer(ServerPlayerEntity player, GameMode gameMode) { diff --git a/src/main/java/eu/pb4/destroythemonument/game/WaitingLobby.java b/src/main/java/eu/pb4/destroythemonument/game/WaitingLobby.java index f7ffb00..da1abfe 100644 --- a/src/main/java/eu/pb4/destroythemonument/game/WaitingLobby.java +++ b/src/main/java/eu/pb4/destroythemonument/game/WaitingLobby.java @@ -14,10 +14,14 @@ import eu.pb4.destroythemonument.game.map.GameMap; import eu.pb4.destroythemonument.other.DtmUtil; import eu.pb4.destroythemonument.ui.ClassSelectorUI; +import eu.pb4.sgui.api.ClickType; +import eu.pb4.sgui.api.elements.GuiElement; +import eu.pb4.sgui.api.elements.GuiElementInterface; import it.unimi.dsi.fastutil.objects.Object2ObjectMap; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import net.minecraft.entity.damage.DamageSource; import net.minecraft.item.ItemStack; +import net.minecraft.screen.slot.SlotActionType; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import net.minecraft.util.ActionResult; @@ -29,8 +33,12 @@ import xyz.nucleoid.plasmid.api.game.common.GameWaitingLobby; import xyz.nucleoid.plasmid.api.game.common.team.GameTeamKey; import xyz.nucleoid.plasmid.api.game.common.team.TeamSelectionLobby; +import xyz.nucleoid.plasmid.api.game.common.ui.WaitingLobbyUiElement; +import xyz.nucleoid.plasmid.api.game.common.ui.WaitingLobbyUiLayout; import xyz.nucleoid.plasmid.api.game.event.GameActivityEvents; import xyz.nucleoid.plasmid.api.game.event.GamePlayerEvents; +import xyz.nucleoid.plasmid.api.game.event.GameWaitingLobbyEvents; +import xyz.nucleoid.plasmid.api.util.PlayerMap; import xyz.nucleoid.plasmid.api.util.PlayerRef; import xyz.nucleoid.stimuli.event.EventResult; import xyz.nucleoid.stimuli.event.item.ItemUseEvent; @@ -43,7 +51,7 @@ public class WaitingLobby { private final GameConfig config; private final SpawnLogic spawnLogic; private final Teams teams; - private final Object2ObjectMap participants = new Object2ObjectOpenHashMap<>(); + private final PlayerMap participants = PlayerMap.of(new Object2ObjectOpenHashMap<>()); private final TeamSelectionLobby teamSelection; private final PlayerClass defaultKit; @@ -88,7 +96,6 @@ public static GameOpenProcedure open(GameOpenContext context) { return context.openWithWorld(worldConfig, (game, world) -> { map.world = world; GameWaitingLobby.addTo(game, config.players()); - TeamSelectionLobby teamSelection = TeamSelectionLobby.addTo(game, config.teams()); WaitingLobby waiting = new WaitingLobby(game.getGameSpace(), map, context.config(), teamSelection); @@ -100,6 +107,8 @@ public static GameOpenProcedure open(GameOpenContext context) { game.listen(PlayerDeathEvent.EVENT, waiting::onPlayerDeath); game.listen(PlayerDamageEvent.EVENT, waiting::onPlayerDamage); game.listen(ItemUseEvent.EVENT, waiting::onUseItem); + + game.listen(GameWaitingLobbyEvents.BUILD_UI_LAYOUT, waiting::buildUiLayout); }); } @@ -121,6 +130,17 @@ private ActionResult onUseItem(ServerPlayerEntity player, Hand hand) { return ActionResult.PASS; } + + private void buildUiLayout(WaitingLobbyUiLayout layout, ServerPlayerEntity player) { + if (this.gameSpace.getPlayers().participants().contains(player)) { + layout.addTrailing(() -> new GuiElement(DtmItems.CLASS_SELECTOR.getDefaultStack(), (index, type, action) -> { + if (type.isRight) { + ClassSelectorUI.openSelector(player, this.participants.get(player), this.config.kits()); + } + })); + } + } + private GameResult requestStart() { Multimap playerTeams = HashMultimap.create(); this.teamSelection.allocate(this.gameSpace.getPlayers().participants(), playerTeams::put); @@ -163,6 +183,6 @@ private EventResult onPlayerDeath(ServerPlayerEntity player, DamageSource source private void spawnPlayer(ServerPlayerEntity player) { this.spawnLogic.resetPlayer(player, GameMode.ADVENTURE, false); this.spawnLogic.spawnPlayer(player); - player.getInventory().setStack(8, new ItemStack(DtmItems.CLASS_SELECTOR)); + //player.getInventory().setStack(8, new ItemStack(DtmItems.CLASS_SELECTOR)); } } diff --git a/src/main/java/eu/pb4/destroythemonument/game/logic/BaseGameLogic.java b/src/main/java/eu/pb4/destroythemonument/game/logic/BaseGameLogic.java index 0c6c457..a52a2da 100644 --- a/src/main/java/eu/pb4/destroythemonument/game/logic/BaseGameLogic.java +++ b/src/main/java/eu/pb4/destroythemonument/game/logic/BaseGameLogic.java @@ -67,6 +67,7 @@ import xyz.nucleoid.plasmid.api.game.rule.GameRuleType; import xyz.nucleoid.plasmid.api.game.stats.GameStatisticBundle; import xyz.nucleoid.plasmid.api.game.stats.StatisticKeys; +import xyz.nucleoid.plasmid.api.util.PlayerMap; import xyz.nucleoid.plasmid.api.util.PlayerRef; import xyz.nucleoid.stimuli.event.EventResult; import xyz.nucleoid.stimuli.event.block.BlockBreakEvent; @@ -91,7 +92,7 @@ public abstract class BaseGameLogic { public final GameSpace gameSpace; public final GameMap gameMap; - public final Object2ObjectMap participants; + public final PlayerMap participants; public final Object2IntMap deadPlayers = new Object2IntArrayMap<>(); public final Teams teams; public final ArrayList kits = new ArrayList<>(); @@ -106,7 +107,7 @@ public abstract class BaseGameLogic { protected long closeTime = -1; protected boolean setSpectator = false; - public BaseGameLogic(GameSpace gameSpace, GameMap map, GameConfig config, Object2ObjectMap participants, Teams teams) { + public BaseGameLogic(GameSpace gameSpace, GameMap map, GameConfig config, PlayerMap participants, Teams teams) { this.gameSpace = gameSpace; this.config = config; this.gameMap = map; diff --git a/src/main/java/eu/pb4/destroythemonument/game/logic/CaptureGameLogic.java b/src/main/java/eu/pb4/destroythemonument/game/logic/CaptureGameLogic.java index 48bbd20..745a216 100644 --- a/src/main/java/eu/pb4/destroythemonument/game/logic/CaptureGameLogic.java +++ b/src/main/java/eu/pb4/destroythemonument/game/logic/CaptureGameLogic.java @@ -32,6 +32,7 @@ import xyz.nucleoid.plasmid.api.game.GameSpace; import xyz.nucleoid.plasmid.api.game.common.team.GameTeamKey; import xyz.nucleoid.plasmid.api.util.ColoredBlocks; +import xyz.nucleoid.plasmid.api.util.PlayerMap; import xyz.nucleoid.plasmid.api.util.PlayerRef; import xyz.nucleoid.stimuli.event.EventResult; @@ -43,7 +44,7 @@ public class CaptureGameLogic extends BaseGameLogic { protected int sidebarTeamPos = 0; protected List sidebarTeams; - public CaptureGameLogic(GameSpace gameSpace, GameMap map, GameConfig config, Object2ObjectMap participants, Teams teams) { + public CaptureGameLogic(GameSpace gameSpace, GameMap map, GameConfig config, PlayerMap participants, Teams teams) { super(gameSpace, map, config, participants, teams); List texts = new ArrayList<>(); @@ -57,7 +58,7 @@ public CaptureGameLogic(GameSpace gameSpace, GameMap map, GameConfig config, Obj } } - public static void open(GameSpace gameSpace, GameMap map, GameConfig config, Multimap playerTeams, Object2ObjectMap participants, Teams teams) { + public static void open(GameSpace gameSpace, GameMap map, GameConfig config, Multimap playerTeams, PlayerMap participants, Teams teams) { gameSpace.setActivity(game -> { BaseGameLogic active = new CaptureGameLogic(gameSpace, map, config, participants, teams); active.setupGame(game, map, config, playerTeams); diff --git a/src/main/java/eu/pb4/destroythemonument/game/logic/DebugGameLogic.java b/src/main/java/eu/pb4/destroythemonument/game/logic/DebugGameLogic.java index b868a96..343d328 100644 --- a/src/main/java/eu/pb4/destroythemonument/game/logic/DebugGameLogic.java +++ b/src/main/java/eu/pb4/destroythemonument/game/logic/DebugGameLogic.java @@ -12,18 +12,19 @@ import xyz.nucleoid.plasmid.api.game.GameSpace; import xyz.nucleoid.plasmid.api.game.common.team.GameTeam; import xyz.nucleoid.plasmid.api.game.common.team.GameTeamKey; +import xyz.nucleoid.plasmid.api.util.PlayerMap; import xyz.nucleoid.plasmid.api.util.PlayerRef; public class DebugGameLogic extends StandardGameLogic { - public DebugGameLogic(GameSpace gameSpace, GameMap map, GameConfig config, Object2ObjectMap participants, Teams teams) { + public DebugGameLogic(GameSpace gameSpace, GameMap map, GameConfig config, PlayerMap participants, Teams teams) { super(gameSpace, map, config, participants, teams); Text text = Text.literal("+-----------------DEBUG----------------+").formatted(Formatting.AQUA); this.gameSpace.getPlayers().sendMessage(text); } - public static void open(GameSpace gameSpace, GameMap map, GameConfig config, Multimap playerTeams, Object2ObjectMap participants, Teams teams) { + public static void open(GameSpace gameSpace, GameMap map, GameConfig config, Multimap playerTeams, PlayerMap participants, Teams teams) { gameSpace.setActivity(game -> { BaseGameLogic active = new DebugGameLogic(gameSpace, map, config, participants, teams); active.setupGame(game, map, config, playerTeams); diff --git a/src/main/java/eu/pb4/destroythemonument/game/logic/StandardGameLogic.java b/src/main/java/eu/pb4/destroythemonument/game/logic/StandardGameLogic.java index af807a0..2b95a96 100644 --- a/src/main/java/eu/pb4/destroythemonument/game/logic/StandardGameLogic.java +++ b/src/main/java/eu/pb4/destroythemonument/game/logic/StandardGameLogic.java @@ -28,6 +28,7 @@ import xyz.nucleoid.plasmid.api.game.GameActivity; import xyz.nucleoid.plasmid.api.game.GameSpace; import xyz.nucleoid.plasmid.api.game.common.team.GameTeamKey; +import xyz.nucleoid.plasmid.api.util.PlayerMap; import xyz.nucleoid.plasmid.api.util.PlayerRef; import xyz.nucleoid.stimuli.event.EventResult; @@ -40,7 +41,7 @@ public class StandardGameLogic extends BaseGameLogic { protected int sidebarTeamPos = 0; protected List sidebarTeams; - public StandardGameLogic(GameSpace gameSpace, GameMap map, GameConfig config, Object2ObjectMap participants, Teams teams) { + public StandardGameLogic(GameSpace gameSpace, GameMap map, GameConfig config, PlayerMap participants, Teams teams) { super(gameSpace, map, config, participants, teams); List texts = new ArrayList<>(); @@ -54,7 +55,7 @@ public StandardGameLogic(GameSpace gameSpace, GameMap map, GameConfig config, Ob } } - public static void open(GameSpace gameSpace, GameMap map, GameConfig config, Multimap playerTeams, Object2ObjectMap participants, Teams teams) { + public static void open(GameSpace gameSpace, GameMap map, GameConfig config, Multimap playerTeams, PlayerMap participants, Teams teams) { gameSpace.setActivity(game -> { BaseGameLogic active = new StandardGameLogic(gameSpace, map, config, participants, teams); active.setupGame(game, map, config, playerTeams); diff --git a/src/main/java/eu/pb4/destroythemonument/ui/ClassSelectorUI.java b/src/main/java/eu/pb4/destroythemonument/ui/ClassSelectorUI.java index 1cc925e..0b6f810 100644 --- a/src/main/java/eu/pb4/destroythemonument/ui/ClassSelectorUI.java +++ b/src/main/java/eu/pb4/destroythemonument/ui/ClassSelectorUI.java @@ -6,7 +6,9 @@ import eu.pb4.destroythemonument.game.playerclass.ClassRegistry; import eu.pb4.destroythemonument.other.DtmUtil; import eu.pb4.destroythemonument.other.FormattingUtil; +import eu.pb4.sgui.api.GuiHelpers; import eu.pb4.sgui.api.elements.GuiElementBuilder; +import eu.pb4.sgui.api.gui.GuiInterface; import eu.pb4.sgui.api.gui.SimpleGui; import net.minecraft.screen.ScreenHandlerType; import net.minecraft.server.network.ServerPlayerEntity; @@ -17,6 +19,7 @@ import net.minecraft.text.Text; import net.minecraft.util.Formatting; import net.minecraft.util.Identifier; +import org.jetbrains.annotations.Nullable; import xyz.nucleoid.map_templates.BlockBounds; import xyz.nucleoid.plasmid.api.util.PlayerRef; @@ -27,12 +30,15 @@ public class ClassSelectorUI extends SimpleGui { private final PlayerData playerData; private final BaseGameLogic game; private final List kits; + @Nullable + private final GuiInterface previousUi; public ClassSelectorUI(ServerPlayerEntity player, PlayerData data, BaseGameLogic game, List kits) { super(getType(kits.size()), player, kits.size() > 53); this.playerData = data; this.game = game; this.kits = kits; + this.previousUi = GuiHelpers.getCurrentGui(player); this.setTitle(DtmUtil.getText("ui", "select_class")); this.updateIcons(); } @@ -53,6 +59,14 @@ private static ScreenHandlerType getType(int size) { } } + @Override + public void onClose() { + super.onClose(); + if (this.previousUi != null) { + this.previousUi.open(); + } + } + public static void openSelector(ServerPlayerEntity player, BaseGameLogic logic) { new ClassSelectorUI(player, logic.participants.get(PlayerRef.of(player)), logic, logic.kits).open(); }