Skip to content

Commit

Permalink
Extract GameSpaceManager into api interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Nov 6, 2024
1 parent 61ae525 commit 09a29f2
Show file tree
Hide file tree
Showing 21 changed files with 131 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import net.minecraft.server.network.ServerPlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import xyz.nucleoid.plasmid.impl.manager.GameSpaceManager;
import xyz.nucleoid.plasmid.impl.manager.GameSpaceManagerImpl;

@Mixin(IntegratedPlayerManager.class)
public abstract class IntegratedPlayerManagerMixin {
Expand All @@ -15,6 +15,6 @@ public abstract class IntegratedPlayerManagerMixin {
at = @At(value = "INVOKE", target = "Lnet/minecraft/server/integrated/IntegratedServer;isHost(Lcom/mojang/authlib/GameProfile;)Z")
)
private boolean canSavePlayerData(boolean original, ServerPlayerEntity player) {
return original && !GameSpaceManager.get().inGame(player);
return original && !GameSpaceManagerImpl.get().inGame(player);
}
}
42 changes: 42 additions & 0 deletions src/main/java/xyz/nucleoid/plasmid/api/game/GameSpaceManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package xyz.nucleoid.plasmid.api.game;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.util.Identifier;
import net.minecraft.world.World;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.plasmid.api.game.config.GameConfig;
import xyz.nucleoid.plasmid.impl.manager.GameSpaceManagerImpl;
import xyz.nucleoid.plasmid.impl.manager.ManagedGameSpace;

import java.util.Collection;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

@ApiStatus.NonExtendable
public interface GameSpaceManager {
static GameSpaceManagerImpl get() {
return GameSpaceManagerImpl.get();
}

CompletableFuture<GameSpace> open(RegistryEntry<GameConfig<?>> config);

Collection<ManagedGameSpace> getOpenGameSpaces();

@Nullable
GameSpace byId(UUID id);

@Nullable
GameSpace byUserId(Identifier userId);

@Nullable
GameSpace byWorld(World world);

@Nullable
GameSpace byPlayer(PlayerEntity player);

boolean hasGame(World world);

boolean inGame(PlayerEntity player);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import xyz.nucleoid.plasmid.api.game.GameResult;
import xyz.nucleoid.plasmid.api.game.GameTexts;
import xyz.nucleoid.plasmid.api.game.common.widget.SidebarWidget;
import xyz.nucleoid.plasmid.impl.manager.GameSpaceManager;
import xyz.nucleoid.plasmid.impl.manager.GameSpaceManagerImpl;
import xyz.nucleoid.plasmid.impl.compatibility.AfkDisplayCompatibility;

import java.util.ArrayList;
Expand Down Expand Up @@ -311,7 +311,7 @@ private boolean isActiveFull() {

// if there are no players outside of a game on the server
for (var world : server.getWorlds()) {
if (hasActivePlayer(world) && !GameSpaceManager.get().hasGame(world)) {
if (hasActivePlayer(world) && !GameSpaceManagerImpl.get().hasGame(world)) {
return false;
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/xyz/nucleoid/plasmid/impl/Plasmid.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import xyz.nucleoid.plasmid.api.game.config.GameConfig;
import xyz.nucleoid.plasmid.api.game.config.GameConfigs;
import xyz.nucleoid.plasmid.api.game.event.GameActivityEvents;
import xyz.nucleoid.plasmid.impl.manager.GameSpaceManager;
import xyz.nucleoid.plasmid.impl.manager.GameSpaceManagerImpl;
import xyz.nucleoid.plasmid.impl.portal.GamePortalConfig;
import xyz.nucleoid.plasmid.impl.portal.GamePortalInterface;
import xyz.nucleoid.plasmid.impl.portal.GamePortalManager;
Expand Down Expand Up @@ -95,7 +95,7 @@ private void registerCallbacks() {
});

ServerTickEvents.END_WORLD_TICK.register(world -> {
var game = GameSpaceManager.get().byWorld(world);
var game = GameSpaceManagerImpl.get().byWorld(world);
if (game != null) {
try {
game.getBehavior().propagatingInvoker(GameActivityEvents.TICK).onTick();
Expand All @@ -110,7 +110,7 @@ private void registerCallbacks() {
});

ServerLifecycleEvents.SERVER_STARTING.register(server -> {
GameSpaceManager.openServer(server);
GameSpaceManagerImpl.openServer(server);
GamePortalManager.INSTANCE.setup(server);
loadData(server.getRegistryManager(), server.getResourceManager());
PlasmidConfig.get().webServerConfig().ifPresent(config -> {
Expand All @@ -119,15 +119,15 @@ private void registerCallbacks() {
});

ServerLifecycleEvents.SERVER_STOPPING.register(server -> {
GameSpaceManager.startClosing();
GameSpaceManagerImpl.startClosing();
GamePortalManager.INSTANCE.close(server);
if (httpServer != null) {
httpServer.stop(0);
}
});

ServerLifecycleEvents.SERVER_STOPPED.register(server -> {
GameSpaceManager.closeServer();
GameSpaceManagerImpl.closeServer();
});

ServerLifecycleEvents.END_DATA_PACK_RELOAD.register(((server, resourceManager, success) -> {
Expand Down
26 changes: 13 additions & 13 deletions src/main/java/xyz/nucleoid/plasmid/impl/command/GameCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import xyz.nucleoid.plasmid.api.game.GameTexts;
import xyz.nucleoid.plasmid.api.game.config.GameConfig;
import xyz.nucleoid.plasmid.api.game.config.GameConfigs;
import xyz.nucleoid.plasmid.impl.manager.GameSpaceManager;
import xyz.nucleoid.plasmid.impl.manager.GameSpaceManagerImpl;
import xyz.nucleoid.plasmid.api.game.player.GamePlayerJoiner;
import xyz.nucleoid.plasmid.api.game.player.JoinIntent;
import xyz.nucleoid.plasmid.api.util.Scheduler;
Expand Down Expand Up @@ -160,7 +160,7 @@ private static int openGame(CommandContext<ServerCommandSource> context, Registr
var player = source.getPlayer();

if (player != null) {
var currentGameSpace = GameSpaceManager.get().byPlayer(player);
var currentGameSpace = GameSpaceManagerImpl.get().byPlayer(player);
if (currentGameSpace != null) {
if (test) {
currentGameSpace.close(GameCloseReason.CANCELED);
Expand All @@ -170,7 +170,7 @@ private static int openGame(CommandContext<ServerCommandSource> context, Registr
}
}

GameSpaceManager.get().open(config).handleAsync((gameSpace, throwable) -> {
GameSpaceManagerImpl.get().open(config).handleAsync((gameSpace, throwable) -> {
if (throwable == null) {
onOpenSuccess(source, gameSpace, player, test);
} else {
Expand Down Expand Up @@ -226,7 +226,7 @@ private static int proposeGame(CommandContext<ServerCommandSource> context) thro
private static int proposeCurrentGame(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
var source = context.getSource();

var gameSpace = GameSpaceManager.get().byPlayer(source.getPlayerOrThrow());
var gameSpace = GameSpaceManagerImpl.get().byPlayer(source.getPlayerOrThrow());
if (gameSpace == null) {
throw NOT_IN_GAME.create();
}
Expand Down Expand Up @@ -260,7 +260,7 @@ private static int joinAllGame(CommandContext<ServerCommandSource> context) thro

var player = context.getSource().getPlayer();
if (player != null) {
gameSpace = GameSpaceManager.get().byPlayer(player);
gameSpace = GameSpaceManagerImpl.get().byPlayer(player);
}

if (gameSpace == null) {
Expand All @@ -283,7 +283,7 @@ private static void joinAllPlayersToGame(ServerCommandSource source, GameSpace g
var playerManager = source.getServer().getPlayerManager();

var players = playerManager.getPlayerList().stream()
.filter(player -> !GameSpaceManager.get().inGame(player))
.filter(player -> !GameSpaceManagerImpl.get().inGame(player))
.collect(Collectors.toList());

var intent = JoinIntent.ANY;
Expand All @@ -301,15 +301,15 @@ private static void tryJoinGame(ServerPlayerEntity player, GameSpace gameSpace)
}

private static GameSpace getJoinableGameSpace() throws CommandSyntaxException {
return GameSpaceManager.get().getOpenGameSpaces().stream()
return GameSpaceManagerImpl.get().getOpenGameSpaces().stream()
.max(Comparator.comparingInt(space -> space.getPlayers().size()))
.orElseThrow(NO_GAME_OPEN::create);
}

private static int locatePlayer(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
var player = EntityArgumentType.getPlayer(context, "player");

var gameSpace = GameSpaceManager.get().byPlayer(player);
var gameSpace = GameSpaceManagerImpl.get().byPlayer(player);
if (gameSpace == null) {
throw PLAYER_NOT_IN_GAME.create(player.getName());
}
Expand All @@ -323,7 +323,7 @@ private static int leaveGame(CommandContext<ServerCommandSource> context) throws
var source = context.getSource();
var player = source.getPlayerOrThrow();

var gameSpace = GameSpaceManager.get().byPlayer(player);
var gameSpace = GameSpaceManagerImpl.get().byPlayer(player);
if (gameSpace == null) {
throw NOT_IN_GAME.create();
}
Expand All @@ -338,7 +338,7 @@ private static int leaveGame(CommandContext<ServerCommandSource> context) throws
private static int startGame(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
var source = context.getSource();

var gameSpace = GameSpaceManager.get().byPlayer(source.getPlayerOrThrow());
var gameSpace = GameSpaceManagerImpl.get().byPlayer(source.getPlayerOrThrow());
if (gameSpace == null) {
throw NOT_IN_GAME.create();
}
Expand All @@ -359,7 +359,7 @@ private static int startGame(CommandContext<ServerCommandSource> context) throws

private static int stopGame(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
var source = context.getSource();
var gameSpace = GameSpaceManager.get().byPlayer(source.getPlayerOrThrow());
var gameSpace = GameSpaceManagerImpl.get().byPlayer(source.getPlayerOrThrow());
if (gameSpace == null) {
throw NOT_IN_GAME.create();
}
Expand All @@ -380,7 +380,7 @@ private static int stopGame(CommandContext<ServerCommandSource> context) throws

private static int stopGameConfirmed(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
var source = context.getSource();
var gameSpace = GameSpaceManager.get().byPlayer(source.getPlayerOrThrow());
var gameSpace = GameSpaceManagerImpl.get().byPlayer(source.getPlayerOrThrow());
if (gameSpace == null) {
throw NOT_IN_GAME.create();
}
Expand Down Expand Up @@ -430,7 +430,7 @@ private static int kickPlayers(CommandContext<ServerCommandSource> context) thro
int successes = 0;

for (var target : targets) {
var gameSpace = GameSpaceManager.get().byPlayer(target);
var gameSpace = GameSpaceManagerImpl.get().byPlayer(target);
if (gameSpace != null) {
var message = GameTexts.Kick.kick(source, target).formatted(Formatting.GRAY);
playerManager.broadcast(message, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import xyz.nucleoid.plasmid.api.game.GameSpace;
import xyz.nucleoid.plasmid.impl.manager.GameSpaceManager;
import xyz.nucleoid.plasmid.impl.manager.GameSpaceManagerImpl;

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 gameSpaceManager = GameSpaceManagerImpl.get();

return CommandSource.suggestIdentifiers(
gameSpaceManager.getOpenGameSpaces().stream().map(space -> space.getMetadata().userId()),
Expand All @@ -31,7 +31,7 @@ public static RequiredArgumentBuilder<ServerCommandSource, Identifier> argument(
public static GameSpace get(CommandContext<ServerCommandSource> context, String name) throws CommandSyntaxException {
var identifier = IdentifierArgumentType.getIdentifier(context, name);

var gameSpace = GameSpaceManager.get().byUserId(identifier);
var gameSpace = GameSpaceManagerImpl.get().byUserId(identifier);
if (gameSpace == null) {
throw GAME_NOT_FOUND.create();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import net.minecraft.util.math.MathHelper;
import xyz.nucleoid.plasmid.api.game.GameSpace;
import xyz.nucleoid.plasmid.api.game.config.GameConfig;
import xyz.nucleoid.plasmid.impl.manager.GameSpaceManager;
import xyz.nucleoid.plasmid.impl.manager.GameSpaceManagerImpl;
import xyz.nucleoid.plasmid.impl.manager.ManagedGameSpace;
import xyz.nucleoid.plasmid.api.game.player.GamePlayerJoiner;
import xyz.nucleoid.plasmid.api.game.player.JoinIntent;
Expand Down Expand Up @@ -59,7 +59,7 @@ private void updateUi() {
int i = 0;
int gameI = 0;

var games = new ArrayList<>(GameSpaceManager.get().getOpenGameSpaces());
var games = new ArrayList<>(GameSpaceManagerImpl.get().getOpenGameSpaces());
games.sort(Comparator.comparingInt(space -> -space.getPlayers().size()));

int limit = this.size;
Expand Down
Loading

0 comments on commit 09a29f2

Please sign in to comment.