From eafe4f780dd718aba0794eab0a5256758f3d831d Mon Sep 17 00:00:00 2001 From: Zak Shearman <34372536+ZakShearman@users.noreply.github.com> Date: Sun, 10 Mar 2024 19:01:14 +0000 Subject: [PATCH] feat: queue utils for any gamemode --- .../core/utils/KurushimiMinestomUtils.java | 67 ++++++++++++++----- 1 file changed, 49 insertions(+), 18 deletions(-) diff --git a/src/main/java/dev/emortal/minestom/core/utils/KurushimiMinestomUtils.java b/src/main/java/dev/emortal/minestom/core/utils/KurushimiMinestomUtils.java index caf3021..696d953 100644 --- a/src/main/java/dev/emortal/minestom/core/utils/KurushimiMinestomUtils.java +++ b/src/main/java/dev/emortal/minestom/core/utils/KurushimiMinestomUtils.java @@ -1,6 +1,8 @@ package dev.emortal.minestom.core.utils; import dev.emortal.api.service.matchmaker.MatchmakerService; +import dev.emortal.api.service.matchmaker.QueueOptions; +import dev.emortal.api.service.matchmaker.QueuePlayerResult; import dev.emortal.api.utils.GrpcStubCollection; import io.grpc.StatusRuntimeException; import net.minestom.server.MinecraftServer; @@ -33,6 +35,31 @@ public final class KurushimiMinestomUtils { MinecraftServer.getGlobalEventHandler().addChild(EVENT_NODE); } + @NonBlocking + public static void sendToGamemode(@NotNull Collection players, @NotNull String gamemodeId, @NotNull Runnable successRunnable, + @NotNull Runnable failureRunnable, int retries) { + test(players, successRunnable, failureRunnable); + + for (Player player : players) { + Thread.startVirtualThread(() -> sendToGamemode(player, gamemodeId, () -> LOGGER.warn("Failed to create ticket to send '{}' to gamemode", player.getUsername()))); + } + } + + @Blocking + private static void sendToGamemode(@NotNull Player player, @NotNull String gamemodeId, @NotNull Runnable failureRunnable) { + try { + QueuePlayerResult result = MATCHMAKER.queuePlayer(gamemodeId, player.getUuid()); + switch (result) { + case INVALID_GAME_MODE, GAME_MODE_DISABLED, ALREADY_IN_QUEUE, INVALID_MAP, PARTY_TOO_LARGE -> { + failureRunnable.run(); + } + } + } catch (StatusRuntimeException exception) { + LOGGER.error("Failed to queue '{}' for '{}'", player.getUsername(), gamemodeId, exception); + failureRunnable.run(); + } + } + /** * Note: The failure runnable is only run at the end of the time if players are not sent. * If there are other errors, they may only affect one player and resolve with retries. @@ -47,6 +74,28 @@ public final class KurushimiMinestomUtils { @NonBlocking public static void sendToLobby(@NotNull Collection players, @NotNull Runnable successRunnable, @NotNull Runnable failureRunnable, int retries) { + test(players, successRunnable, failureRunnable); + + for (Player player : players) { + Thread.startVirtualThread(() -> sendToLobby(player, () -> LOGGER.warn("Failed to create ticket to send '{}' to lobby", player.getUsername()))); + } + } + + @NonBlocking + public static void sendToLobby(@NotNull Collection players, @NotNull Runnable successRunnable, @NotNull Runnable failureRunnable) { + sendToLobby(players, successRunnable, failureRunnable, 1); + } + + @Blocking + private static void sendToLobby(@NotNull Player player, @NotNull Runnable failureRunnable) { + try { + MATCHMAKER.sendPlayerToLobby(player.getUuid(), false); + } catch (StatusRuntimeException exception) { + failureRunnable.run(); + } + } + + private static void test(@NotNull Collection players, @NotNull Runnable successRunnable, @NotNull Runnable failureRunnable) { if (MATCHMAKER == null) throw new IllegalStateException("Kurushimi stub is not present."); Set remainingPlayers = new HashSet<>(players); @@ -72,23 +121,5 @@ public static void sendToLobby(@NotNull Collection players, @N successRunnable.run(); } }); - - for (Player player : players) { - Thread.startVirtualThread(() -> sendToLobby(player, () -> LOGGER.warn("Failed to create ticket to send '{}' to lobby", player.getUsername()))); - } - } - - @NonBlocking - public static void sendToLobby(@NotNull Collection players, @NotNull Runnable successRunnable, @NotNull Runnable failureRunnable) { - sendToLobby(players, successRunnable, failureRunnable, 1); - } - - @Blocking - private static void sendToLobby(@NotNull Player player, @NotNull Runnable failureRunnable) { - try { - MATCHMAKER.sendPlayerToLobby(player.getUuid(), false); - } catch (StatusRuntimeException exception) { - failureRunnable.run(); - } } }