From 8bda683435dbd8174b30a852678d837a6d0dfc46 Mon Sep 17 00:00:00 2001 From: Zak Shearman <34372536+ZakShearman@users.noreply.github.com> Date: Sun, 10 Mar 2024 19:02:45 +0000 Subject: [PATCH] feat: fully implement requeue option --- build.gradle.kts | 2 +- .../minestom/gamesdk/config/GameSdkConfig.java | 16 ++++++++++------ .../gamesdk/internal/AgonesGameListener.java | 4 ++-- .../minestom/gamesdk/internal/GameManager.java | 10 ++++++---- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 2e563c9..d657e69 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -17,7 +17,7 @@ repositories { } dependencies { - api("dev.emortal.minestom:core:87615bb") + api("dev.emortal.minestom:core:eafe4f7") } java { diff --git a/src/main/java/dev/emortal/minestom/gamesdk/config/GameSdkConfig.java b/src/main/java/dev/emortal/minestom/gamesdk/config/GameSdkConfig.java index bb7b0a2..1405668 100644 --- a/src/main/java/dev/emortal/minestom/gamesdk/config/GameSdkConfig.java +++ b/src/main/java/dev/emortal/minestom/gamesdk/config/GameSdkConfig.java @@ -12,7 +12,7 @@ * @param gameCreator a function that can be called to create a game instance */ public record GameSdkConfig(int minPlayers, int maxGames, int minTrackingInterval, int maxTrackingInterval, - boolean lobbyOnFinish, @NotNull GameCreator gameCreator) { + FinishBehaviour finishBehaviour, @NotNull GameCreator gameCreator) { public static @NotNull Builder builder() { return new BuilderImpl(); @@ -33,7 +33,7 @@ interface GameCreatorStep { @NotNull GameCreatorStep maxTrackingInterval(int interval); - @NotNull GameCreatorStep lobbyOnFinish(boolean lobbyOnFinish); + @NotNull GameCreatorStep finishBehaviour(FinishBehaviour finishBehaviour); @NotNull EndStep gameCreator(@NotNull GameCreator creator); } @@ -50,7 +50,7 @@ private static final class BuilderImpl implements Builder, Builder.MaxGamesStep, private int maxGames; private int minTrackingInterval = GameTracker.DEFAULT_MIN_UPDATE_INTERVAL; private int maxTrackingInterval = GameTracker.DEFAULT_MAX_UPDATE_INTERVAL; - private boolean lobbyOnFinish = true; + private FinishBehaviour finishBehaviour = FinishBehaviour.LOBBY; private GameCreator gameCreator; @Override @@ -78,8 +78,8 @@ private static final class BuilderImpl implements Builder, Builder.MaxGamesStep, } @Override - public @NotNull GameCreatorStep lobbyOnFinish(boolean lobbyOnFinish) { - this.lobbyOnFinish = lobbyOnFinish; + public @NotNull GameCreatorStep finishBehaviour(FinishBehaviour finishBehaviour) { + this.finishBehaviour = finishBehaviour; return this; } @@ -92,7 +92,11 @@ private static final class BuilderImpl implements Builder, Builder.MaxGamesStep, @Override public @NotNull GameSdkConfig build() { return new GameSdkConfig(this.minPlayers, this.maxGames, this.minTrackingInterval, this.maxTrackingInterval, - this.lobbyOnFinish, this.gameCreator); + this.finishBehaviour, this.gameCreator); } } + + public enum FinishBehaviour { + LOBBY, REQUEUE + } } diff --git a/src/main/java/dev/emortal/minestom/gamesdk/internal/AgonesGameListener.java b/src/main/java/dev/emortal/minestom/gamesdk/internal/AgonesGameListener.java index b2e24f2..497a894 100644 --- a/src/main/java/dev/emortal/minestom/gamesdk/internal/AgonesGameListener.java +++ b/src/main/java/dev/emortal/minestom/gamesdk/internal/AgonesGameListener.java @@ -76,10 +76,10 @@ private void movePlayersOnThisServer(@NotNull Game newGame, @NotNull Set p Player player = connectionManager.getOnlinePlayerByUuid(playerId); if (player == null) continue; + // The old game will be null if the player is queued when the game is finishing. See REQUEUE logic Game oldGame = this.gameManager.findGame(player); - if (oldGame == null) continue; + if (oldGame != null) GamePlayerTracker.removePlayer(oldGame, player); - GamePlayerTracker.removePlayer(oldGame, player); GamePlayerTracker.addPlayer(newGame, player); // Increment after using the index diff --git a/src/main/java/dev/emortal/minestom/gamesdk/internal/GameManager.java b/src/main/java/dev/emortal/minestom/gamesdk/internal/GameManager.java index 6c78d2b..c018b38 100644 --- a/src/main/java/dev/emortal/minestom/gamesdk/internal/GameManager.java +++ b/src/main/java/dev/emortal/minestom/gamesdk/internal/GameManager.java @@ -97,10 +97,12 @@ private void onGameFinish(@NotNull GameFinishedEvent event) { } this.removeGame(game); - if (this.config.lobbyOnFinish()) { - KurushimiMinestomUtils.sendToLobby(game.getPlayers(), () -> this.cleanUpGame(game), () -> this.cleanUpGame(game)); - } else { - this.cleanUpGame(game); + switch (this.config.finishBehaviour()) { + case LOBBY -> + KurushimiMinestomUtils.sendToLobby(game.getPlayers(), () -> this.cleanUpGame(game), () -> this.cleanUpGame(game)); + case REQUEUE -> + KurushimiMinestomUtils.sendToGamemode(game.getPlayers(), game.getCreationInfo().gameModeId(), + () -> this.cleanUpGame(game), () -> this.cleanUpGame(game), 1); } }