Skip to content

Commit 7e065d7

Browse files
committed
Allow custom GameSpaceList registration
1 parent f007a85 commit 7e065d7

6 files changed

Lines changed: 101 additions & 18 deletions

File tree

src/main/java/xyz/nucleoid/plasmid/command/GameCommand.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@
1919
import xyz.nucleoid.plasmid.command.argument.GameConfigArgument;
2020
import xyz.nucleoid.plasmid.command.argument.GameSpaceArgument;
2121
import xyz.nucleoid.plasmid.command.ui.GameJoinUi;
22-
import xyz.nucleoid.plasmid.game.GameCloseReason;
23-
import xyz.nucleoid.plasmid.game.GameOpenException;
24-
import xyz.nucleoid.plasmid.game.GameSpace;
25-
import xyz.nucleoid.plasmid.game.GameTexts;
26-
import xyz.nucleoid.plasmid.game.ListedGameSpace;
22+
import xyz.nucleoid.plasmid.game.*;
2723
import xyz.nucleoid.plasmid.game.config.GameConfig;
2824
import xyz.nucleoid.plasmid.game.config.GameConfigList;
2925
import xyz.nucleoid.plasmid.game.config.GameConfigLists;
@@ -289,7 +285,7 @@ private static void tryJoinGame(ServerPlayerEntity player, ListedGameSpace gameS
289285
}
290286

291287
private static ListedGameSpace getJoinableGameSpace() throws CommandSyntaxException {
292-
return GameSpaceManager.get().getOpenGameSpaces().stream()
288+
return GameSpaceLists.composite().getOpenGameSpaces().stream()
293289
.max(Comparator.comparingInt(space -> space.getPlayers().size()))
294290
.orElseThrow(NO_GAME_OPEN::create);
295291
}

src/main/java/xyz/nucleoid/plasmid/command/argument/GameSpaceArgument.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@
1010
import net.minecraft.server.command.ServerCommandSource;
1111
import net.minecraft.text.Text;
1212
import net.minecraft.util.Identifier;
13+
import xyz.nucleoid.plasmid.game.GameSpaceLists;
1314
import xyz.nucleoid.plasmid.game.ListedGameSpace;
14-
import xyz.nucleoid.plasmid.game.manager.GameSpaceManager;
1515

1616
public final class GameSpaceArgument {
1717
private static final SimpleCommandExceptionType GAME_NOT_FOUND = new SimpleCommandExceptionType(Text.translatable("text.plasmid.game.not_found"));
1818

1919
public static RequiredArgumentBuilder<ServerCommandSource, Identifier> argument(String name) {
2020
return CommandManager.argument(name, IdentifierArgumentType.identifier())
2121
.suggests((context, builder) -> {
22-
var gameSpaceManager = GameSpaceManager.get();
22+
var gameSpaceList = GameSpaceLists.composite();
2323

2424
return CommandSource.suggestIdentifiers(
25-
gameSpaceManager.getOpenGameSpaces().stream().map(space -> space.getMetadata().userId()),
25+
gameSpaceList.getOpenGameSpaces().stream().map(space -> space.getMetadata().userId()),
2626
builder
2727
);
2828
});
@@ -31,7 +31,7 @@ public static RequiredArgumentBuilder<ServerCommandSource, Identifier> argument(
3131
public static ListedGameSpace get(CommandContext<ServerCommandSource> context, String name) throws CommandSyntaxException {
3232
var identifier = IdentifierArgumentType.getIdentifier(context, name);
3333

34-
var gameSpace = GameSpaceManager.get().byUserId(identifier);
34+
var gameSpace = GameSpaceLists.composite().byUserId(identifier);
3535
if (gameSpace == null) {
3636
throw GAME_NOT_FOUND.create();
3737
}

src/main/java/xyz/nucleoid/plasmid/command/ui/GameJoinUi.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
import net.minecraft.text.Text;
1111
import net.minecraft.util.Formatting;
1212
import net.minecraft.util.math.MathHelper;
13-
import xyz.nucleoid.plasmid.game.GameSpace;
13+
import xyz.nucleoid.plasmid.game.GameSpaceLists;
1414
import xyz.nucleoid.plasmid.game.ListedGameSpace;
15-
import xyz.nucleoid.plasmid.game.manager.GameSpaceManager;
16-
import xyz.nucleoid.plasmid.game.manager.ManagedGameSpace;
1715
import xyz.nucleoid.plasmid.game.player.GamePlayerJoiner;
1816
import xyz.nucleoid.plasmid.util.Guis;
1917

@@ -54,7 +52,7 @@ private void updateUi() {
5452
int i = 0;
5553
int gameI = 0;
5654

57-
var games = new ArrayList<>(GameSpaceManager.get().getOpenGameSpaces());
55+
var games = new ArrayList<>(GameSpaceLists.composite().getOpenGameSpaces());
5856
games.sort(Comparator.comparingInt(space -> -space.getPlayers().size()));
5957

6058
int limit = this.size;

src/main/java/xyz/nucleoid/plasmid/game/GameSpaceList.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,29 @@
44
import org.jetbrains.annotations.Nullable;
55

66
import java.util.Collection;
7+
import java.util.List;
78
import java.util.UUID;
89

910
public interface GameSpaceList {
11+
GameSpaceList EMPTY = new GameSpaceList() {
12+
@Override
13+
public Collection<? extends ListedGameSpace> getOpenGameSpaces() {
14+
return List.of();
15+
}
16+
17+
@Override
18+
@Nullable
19+
public ListedGameSpace byId(UUID id) {
20+
return null;
21+
}
22+
23+
@Override
24+
@Nullable
25+
public ListedGameSpace byUserId(Identifier userId) {
26+
return null;
27+
}
28+
};
29+
1030
Collection<? extends ListedGameSpace> getOpenGameSpaces();
1131

1232
@Nullable
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package xyz.nucleoid.plasmid.game;
2+
3+
import net.minecraft.util.Identifier;
4+
import org.jetbrains.annotations.Nullable;
5+
6+
import java.util.ArrayList;
7+
import java.util.Collection;
8+
import java.util.List;
9+
import java.util.UUID;
10+
11+
public final class GameSpaceLists {
12+
private static final List<GameSpaceList> REGISTRY = new ArrayList<>();
13+
private static GameSpaceList composite = GameSpaceList.EMPTY;
14+
15+
private GameSpaceLists() {
16+
}
17+
18+
public static GameSpaceList composite() {
19+
return composite;
20+
}
21+
22+
public static void register(GameSpaceList list) {
23+
REGISTRY.add(list);
24+
composite = buildCompositeList(List.copyOf(REGISTRY));
25+
}
26+
27+
public static void unregister(GameSpaceList list) {
28+
REGISTRY.remove(list);
29+
composite = buildCompositeList(List.copyOf(REGISTRY));
30+
}
31+
32+
private static GameSpaceList buildCompositeList(List<GameSpaceList> registry) {
33+
return new GameSpaceList() {
34+
@Override
35+
public Collection<? extends ListedGameSpace> getOpenGameSpaces() {
36+
var result = new ArrayList<ListedGameSpace>();
37+
for (var list : registry) {
38+
result.addAll(list.getOpenGameSpaces());
39+
}
40+
return result;
41+
}
42+
43+
@Override
44+
@Nullable
45+
public ListedGameSpace byId(UUID id) {
46+
for (var list : registry) {
47+
var gameSpace = list.byId(id);
48+
if (gameSpace != null) {
49+
return gameSpace;
50+
}
51+
}
52+
return null;
53+
}
54+
55+
@Override
56+
@Nullable
57+
public ListedGameSpace byUserId(Identifier userId) {
58+
for (var list : registry) {
59+
var gameSpace = list.byUserId(userId);
60+
if (gameSpace != null) {
61+
return gameSpace;
62+
}
63+
}
64+
return null;
65+
}
66+
};
67+
}
68+
}

src/main/java/xyz/nucleoid/plasmid/game/manager/GameSpaceManager.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
import org.jetbrains.annotations.Nullable;
1515
import xyz.nucleoid.plasmid.Plasmid;
1616
import xyz.nucleoid.plasmid.event.GameEvents;
17-
import xyz.nucleoid.plasmid.game.GameCloseReason;
18-
import xyz.nucleoid.plasmid.game.GameOpenProcedure;
19-
import xyz.nucleoid.plasmid.game.GameSpaceList;
20-
import xyz.nucleoid.plasmid.game.GameSpaceMetadata;
17+
import xyz.nucleoid.plasmid.game.*;
2118
import xyz.nucleoid.plasmid.game.config.GameConfig;
2219
import xyz.nucleoid.stimuli.EventSource;
2320
import xyz.nucleoid.stimuli.Stimuli;
@@ -75,7 +72,11 @@ public static GameSpaceManager get() {
7572
}
7673

7774
private static void updateInstance(@Nullable GameSpaceManager instance) {
75+
if (GameSpaceManager.instance != null) {
76+
GameSpaceLists.unregister(GameSpaceManager.instance);
77+
}
7878
GameSpaceManager.instance = instance;
79+
GameSpaceLists.register(instance);
7980
}
8081

8182
public CompletableFuture<ManagedGameSpace> open(GameConfig<?> config) {

0 commit comments

Comments
 (0)