Skip to content

Commit

Permalink
Allow custom GameSpaceList registration
Browse files Browse the repository at this point in the history
  • Loading branch information
Gegy committed Dec 3, 2022
1 parent f007a85 commit 7e065d7
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 18 deletions.
8 changes: 2 additions & 6 deletions src/main/java/xyz/nucleoid/plasmid/command/GameCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -289,7 +285,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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
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"));

public static RequiredArgumentBuilder<ServerCommandSource, Identifier> 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
);
});
Expand All @@ -31,7 +31,7 @@ public static RequiredArgumentBuilder<ServerCommandSource, Identifier> argument(
public static ListedGameSpace get(CommandContext<ServerCommandSource> 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();
}
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/xyz/nucleoid/plasmid/command/ui/GameJoinUi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/xyz/nucleoid/plasmid/game/GameSpaceList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends ListedGameSpace> getOpenGameSpaces() {
return List.of();
}

@Override
@Nullable
public ListedGameSpace byId(UUID id) {
return null;
}

@Override
@Nullable
public ListedGameSpace byUserId(Identifier userId) {
return null;
}
};

Collection<? extends ListedGameSpace> getOpenGameSpaces();

@Nullable
Expand Down
68 changes: 68 additions & 0 deletions src/main/java/xyz/nucleoid/plasmid/game/GameSpaceLists.java
Original file line number Diff line number Diff line change
@@ -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<GameSpaceList> 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<GameSpaceList> registry) {
return new GameSpaceList() {
@Override
public Collection<? extends ListedGameSpace> getOpenGameSpaces() {
var result = new ArrayList<ListedGameSpace>();
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;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ManagedGameSpace> open(GameConfig<?> config) {
Expand Down

0 comments on commit 7e065d7

Please sign in to comment.