diff --git a/src/main/java/xyz/nucleoid/plasmid/command/GameCommand.java b/src/main/java/xyz/nucleoid/plasmid/command/GameCommand.java index adf69c99..3e8e77ae 100644 --- a/src/main/java/xyz/nucleoid/plasmid/command/GameCommand.java +++ b/src/main/java/xyz/nucleoid/plasmid/command/GameCommand.java @@ -19,11 +19,7 @@ import xyz.nucleoid.plasmid.command.argument.GameConfigArgument; import xyz.nucleoid.plasmid.command.argument.GameSpaceArgument; import xyz.nucleoid.plasmid.command.ui.GameJoinUi; -import xyz.nucleoid.plasmid.game.GameCloseReason; -import xyz.nucleoid.plasmid.game.GameOpenException; -import xyz.nucleoid.plasmid.game.GameSpace; -import xyz.nucleoid.plasmid.game.GameTexts; -import xyz.nucleoid.plasmid.game.ListedGameSpace; +import xyz.nucleoid.plasmid.game.*; import xyz.nucleoid.plasmid.game.config.GameConfig; import xyz.nucleoid.plasmid.game.config.GameConfigList; import xyz.nucleoid.plasmid.game.config.GameConfigLists; @@ -312,7 +308,7 @@ private static void tryJoinGame(ServerPlayerEntity player, ListedGameSpace gameS } private static ListedGameSpace getJoinableGameSpace() throws CommandSyntaxException { - return GameSpaceManager.get().getOpenGameSpaces().stream() + return GameSpaceLists.composite().getOpenGameSpaces().stream() .max(Comparator.comparingInt(space -> space.getPlayers().size())) .orElseThrow(NO_GAME_OPEN::create); } diff --git a/src/main/java/xyz/nucleoid/plasmid/command/argument/GameSpaceArgument.java b/src/main/java/xyz/nucleoid/plasmid/command/argument/GameSpaceArgument.java index 02812baa..81f2403f 100644 --- a/src/main/java/xyz/nucleoid/plasmid/command/argument/GameSpaceArgument.java +++ b/src/main/java/xyz/nucleoid/plasmid/command/argument/GameSpaceArgument.java @@ -10,8 +10,8 @@ import net.minecraft.server.command.ServerCommandSource; import net.minecraft.text.Text; import net.minecraft.util.Identifier; +import xyz.nucleoid.plasmid.game.GameSpaceLists; import xyz.nucleoid.plasmid.game.ListedGameSpace; -import xyz.nucleoid.plasmid.game.manager.GameSpaceManager; public final class GameSpaceArgument { private static final SimpleCommandExceptionType GAME_NOT_FOUND = new SimpleCommandExceptionType(Text.translatable("text.plasmid.game.not_found")); @@ -19,10 +19,10 @@ public final class GameSpaceArgument { public static RequiredArgumentBuilder argument(String name) { return CommandManager.argument(name, IdentifierArgumentType.identifier()) .suggests((context, builder) -> { - var gameSpaceManager = GameSpaceManager.get(); + var gameSpaceList = GameSpaceLists.composite(); return CommandSource.suggestIdentifiers( - gameSpaceManager.getOpenGameSpaces().stream().map(space -> space.getMetadata().userId()), + gameSpaceList.getOpenGameSpaces().stream().map(space -> space.getMetadata().userId()), builder ); }); @@ -31,7 +31,7 @@ public static RequiredArgumentBuilder argument( public static ListedGameSpace get(CommandContext context, String name) throws CommandSyntaxException { var identifier = IdentifierArgumentType.getIdentifier(context, name); - var gameSpace = GameSpaceManager.get().byUserId(identifier); + var gameSpace = GameSpaceLists.composite().byUserId(identifier); if (gameSpace == null) { throw GAME_NOT_FOUND.create(); } diff --git a/src/main/java/xyz/nucleoid/plasmid/command/ui/GameJoinUi.java b/src/main/java/xyz/nucleoid/plasmid/command/ui/GameJoinUi.java index 1ae41b76..fe718be2 100644 --- a/src/main/java/xyz/nucleoid/plasmid/command/ui/GameJoinUi.java +++ b/src/main/java/xyz/nucleoid/plasmid/command/ui/GameJoinUi.java @@ -10,10 +10,8 @@ import net.minecraft.text.Text; import net.minecraft.util.Formatting; import net.minecraft.util.math.MathHelper; -import xyz.nucleoid.plasmid.game.GameSpace; +import xyz.nucleoid.plasmid.game.GameSpaceLists; import xyz.nucleoid.plasmid.game.ListedGameSpace; -import xyz.nucleoid.plasmid.game.manager.GameSpaceManager; -import xyz.nucleoid.plasmid.game.manager.ManagedGameSpace; import xyz.nucleoid.plasmid.game.player.GamePlayerJoiner; import xyz.nucleoid.plasmid.util.Guis; @@ -54,7 +52,7 @@ private void updateUi() { int i = 0; int gameI = 0; - var games = new ArrayList<>(GameSpaceManager.get().getOpenGameSpaces()); + var games = new ArrayList<>(GameSpaceLists.composite().getOpenGameSpaces()); games.sort(Comparator.comparingInt(space -> -space.getPlayers().size())); int limit = this.size; diff --git a/src/main/java/xyz/nucleoid/plasmid/game/GameSpaceList.java b/src/main/java/xyz/nucleoid/plasmid/game/GameSpaceList.java index 5c075d8e..d6cc66f4 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/GameSpaceList.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/GameSpaceList.java @@ -4,9 +4,29 @@ import org.jetbrains.annotations.Nullable; import java.util.Collection; +import java.util.List; import java.util.UUID; public interface GameSpaceList { + GameSpaceList EMPTY = new GameSpaceList() { + @Override + public Collection getOpenGameSpaces() { + return List.of(); + } + + @Override + @Nullable + public ListedGameSpace byId(UUID id) { + return null; + } + + @Override + @Nullable + public ListedGameSpace byUserId(Identifier userId) { + return null; + } + }; + Collection getOpenGameSpaces(); @Nullable diff --git a/src/main/java/xyz/nucleoid/plasmid/game/GameSpaceLists.java b/src/main/java/xyz/nucleoid/plasmid/game/GameSpaceLists.java new file mode 100644 index 00000000..a50c7b32 --- /dev/null +++ b/src/main/java/xyz/nucleoid/plasmid/game/GameSpaceLists.java @@ -0,0 +1,68 @@ +package xyz.nucleoid.plasmid.game; + +import net.minecraft.util.Identifier; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.UUID; + +public final class GameSpaceLists { + private static final List REGISTRY = new ArrayList<>(); + private static GameSpaceList composite = GameSpaceList.EMPTY; + + private GameSpaceLists() { + } + + public static GameSpaceList composite() { + return composite; + } + + public static void register(GameSpaceList list) { + REGISTRY.add(list); + composite = buildCompositeList(List.copyOf(REGISTRY)); + } + + public static void unregister(GameSpaceList list) { + REGISTRY.remove(list); + composite = buildCompositeList(List.copyOf(REGISTRY)); + } + + private static GameSpaceList buildCompositeList(List registry) { + return new GameSpaceList() { + @Override + public Collection getOpenGameSpaces() { + var result = new ArrayList(); + for (var list : registry) { + result.addAll(list.getOpenGameSpaces()); + } + return result; + } + + @Override + @Nullable + public ListedGameSpace byId(UUID id) { + for (var list : registry) { + var gameSpace = list.byId(id); + if (gameSpace != null) { + return gameSpace; + } + } + return null; + } + + @Override + @Nullable + public ListedGameSpace byUserId(Identifier userId) { + for (var list : registry) { + var gameSpace = list.byUserId(userId); + if (gameSpace != null) { + return gameSpace; + } + } + return null; + } + }; + } +} diff --git a/src/main/java/xyz/nucleoid/plasmid/game/manager/GameSpaceManager.java b/src/main/java/xyz/nucleoid/plasmid/game/manager/GameSpaceManager.java index e3aec98d..35f8abdc 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/manager/GameSpaceManager.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/manager/GameSpaceManager.java @@ -14,10 +14,7 @@ import org.jetbrains.annotations.Nullable; import xyz.nucleoid.plasmid.Plasmid; import xyz.nucleoid.plasmid.event.GameEvents; -import xyz.nucleoid.plasmid.game.GameCloseReason; -import xyz.nucleoid.plasmid.game.GameOpenProcedure; -import xyz.nucleoid.plasmid.game.GameSpaceList; -import xyz.nucleoid.plasmid.game.GameSpaceMetadata; +import xyz.nucleoid.plasmid.game.*; import xyz.nucleoid.plasmid.game.config.GameConfig; import xyz.nucleoid.stimuli.EventSource; import xyz.nucleoid.stimuli.Stimuli; @@ -75,7 +72,11 @@ public static GameSpaceManager get() { } private static void updateInstance(@Nullable GameSpaceManager instance) { + if (GameSpaceManager.instance != null) { + GameSpaceLists.unregister(GameSpaceManager.instance); + } GameSpaceManager.instance = instance; + GameSpaceLists.register(instance); } public CompletableFuture open(GameConfig config) {