Skip to content

Commit

Permalink
Make /game join ui into game portal, split configs from backend more
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Dec 23, 2024
1 parent 2d1831c commit 274e227
Show file tree
Hide file tree
Showing 28 changed files with 432 additions and 356 deletions.
22 changes: 15 additions & 7 deletions src/main/java/xyz/nucleoid/plasmid/api/game/player/JoinIntent.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package xyz.nucleoid.plasmid.api.game.player;

import net.minecraft.util.StringIdentifiable;
import xyz.nucleoid.plasmid.api.game.GameSpace;
import xyz.nucleoid.plasmid.api.game.event.GamePlayerEvents;

Expand All @@ -8,23 +9,25 @@
* It is up to the game implementation to respect this intent in the way that is appropriate for their game. This may be
* accomplished by handling the {@link GamePlayerEvents#OFFER 'Join Offer'} events.
*/
public enum JoinIntent {
/**
* The player has no particular intention. Generally, this should be considered as a preference to participate.
*/
//ANY,
public enum JoinIntent implements StringIdentifiable {
/**
* The player intends to join the game to participate. If they cannot be joined as a participant, they should not
* be allowed to join.
*/
PLAY,
PLAY("play"),
/**
* The player intends to join the game to spectate. Unless the game does not support spectators, this player should
* generally always be accepted.
*/
SPECTATE,
SPECTATE("spectate"),
;

private final String name;

JoinIntent(String name) {
this.name = name;
}

/**
* @return {@code true} if the player may join as a participant under any circumstances
*/
Expand All @@ -38,4 +41,9 @@ public boolean canPlay() {
public boolean canSpectate() {
return this != PLAY;
}

@Override
public String asString() {
return this.name;
}
}
55 changes: 46 additions & 9 deletions src/main/java/xyz/nucleoid/plasmid/impl/Plasmid.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package xyz.nucleoid.plasmid.impl;

import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.sun.net.httpserver.HttpServer;
import net.fabricmc.api.ModInitializer;
Expand All @@ -12,33 +11,37 @@
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.resource.ResourceManager;
import net.minecraft.screen.ScreenTexts;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.Unit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.nucleoid.plasmid.api.event.GameEvents;
import xyz.nucleoid.plasmid.api.game.GameOpenContext;
import xyz.nucleoid.plasmid.api.game.GameOpenException;
import xyz.nucleoid.plasmid.api.game.GameSpaceManager;
import xyz.nucleoid.plasmid.api.game.GameType;
import xyz.nucleoid.plasmid.impl.game.composite.RandomGame;
import xyz.nucleoid.plasmid.impl.game.composite.RandomGameConfig;
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.game.manager.GameSpaceManagerImpl;
import xyz.nucleoid.plasmid.impl.portal.GamePortalConfig;
import xyz.nucleoid.plasmid.impl.portal.backend.game.ConcurrentGamePortalBackend;
import xyz.nucleoid.plasmid.impl.portal.backend.game.NewGamePortalBackend;
import xyz.nucleoid.plasmid.impl.portal.backend.game.SingleGamePortalBackend;
import xyz.nucleoid.plasmid.impl.portal.backend.menu.ActiveGamePortalBackend;
import xyz.nucleoid.plasmid.impl.portal.backend.menu.AdvancedMenuPortalBackend;
import xyz.nucleoid.plasmid.impl.portal.backend.menu.MenuPortalBackend;
import xyz.nucleoid.plasmid.impl.portal.config.GamePortalConfig;
import xyz.nucleoid.plasmid.impl.portal.GamePortalInterface;
import xyz.nucleoid.plasmid.impl.portal.GamePortalManager;
import xyz.nucleoid.plasmid.impl.portal.game.ConcurrentGamePortalConfig;
import xyz.nucleoid.plasmid.impl.portal.game.LegacyOnDemandPortalConfig;
import xyz.nucleoid.plasmid.impl.portal.game.NewGamePortalConfig;
import xyz.nucleoid.plasmid.impl.portal.game.SingleGamePortalConfig;
import xyz.nucleoid.plasmid.impl.portal.config.*;
import xyz.nucleoid.plasmid.impl.command.*;
import xyz.nucleoid.plasmid.impl.compatibility.TrinketsCompatibility;
import xyz.nucleoid.plasmid.impl.portal.menu.GameMenuEntryConfig;
import xyz.nucleoid.plasmid.impl.portal.menu.*;

public final class Plasmid implements ModInitializer {
Expand All @@ -53,14 +56,48 @@ public void onInitialize() {
GamePortalConfig.register(Identifier.of(ID, "single_game"), SingleGamePortalConfig.CODEC);
GamePortalConfig.register(Identifier.of(ID, "new_game"), NewGamePortalConfig.CODEC);
GamePortalConfig.register(Identifier.of(ID, "concurrent_game"), ConcurrentGamePortalConfig.CODEC);
GamePortalConfig.register(Identifier.of(ID, "on_demand"), LegacyOnDemandPortalConfig.CODEC); // old one

GamePortalConfig.register(Identifier.of(ID, "active_games"), ActiveGamePortalConfig.CODEC);
GamePortalConfig.register(Identifier.of(ID, "menu"), MenuPortalConfig.CODEC);
GamePortalConfig.register(Identifier.of(ID, "advanced_menu"), AdvancedMenuPortalConfig.CODEC);

MenuEntryConfig.register(Identifier.of(ID, "game"), GameMenuEntryConfig.CODEC);
MenuEntryConfig.register(Identifier.of(ID, "portal"), PortalEntryConfig.CODEC);

GamePortalConfig.registerFactory(SingleGamePortalConfig.class, ((server, id, config) -> new SingleGamePortalBackend(config.game())));
GamePortalConfig.registerFactory(NewGamePortalConfig.class, ((server, id, config) -> new NewGamePortalBackend(config.game())));
GamePortalConfig.registerFactory(ConcurrentGamePortalConfig.class, ((server, id, config) -> new ConcurrentGamePortalBackend(config.game())));
GamePortalConfig.registerFactory(MenuPortalConfig.class, ((server, id, config) -> {
Text name;
if (config.name() != null && config.name() != ScreenTexts.EMPTY) {
name = config.name();
} else {
name = Text.literal(id.toString());
}

return new MenuPortalBackend(name, config.description(), config.icon(), config.games());
}));
GamePortalConfig.registerFactory(AdvancedMenuPortalConfig.class, ((server, id, config) -> {
Text name;
if (config.name() != null && config.name() != ScreenTexts.EMPTY) {
name = config.name();
} else {
name = Text.literal(id.toString());
}
return new AdvancedMenuPortalBackend(name, config.description(), config.icon(), config.entries());
}));

GamePortalConfig.registerFactory(ActiveGamePortalConfig.class, ((server, id, config) -> {
Text name;
if (config.name() != null && config.name() != ScreenTexts.EMPTY) {
name = config.name();
} else {
name = Text.literal(id.toString());
}
return new ActiveGamePortalBackend(name, config.description(), config.icon(), GameSpaceManager.get(), config.joinIntent());
}));


GameType.register(Identifier.of(Plasmid.ID, "random"), RandomGameConfig.CODEC, RandomGame::open);
GameType.register(Identifier.of(Plasmid.ID, "invalid"), MapCodec.unit(""), (context) -> {
var id = context.server().getRegistryManager().getOrThrow(GameConfigs.REGISTRY_KEY).getId(context.game());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import org.slf4j.Logger;
import xyz.nucleoid.plasmid.impl.Plasmid;
import xyz.nucleoid.plasmid.impl.command.argument.GameConfigArgument;
import xyz.nucleoid.plasmid.impl.command.argument.GameSpaceArgument;
import xyz.nucleoid.plasmid.impl.command.ui.GameJoinUi;
import xyz.nucleoid.plasmid.api.game.GameCloseReason;
import xyz.nucleoid.plasmid.api.game.GameOpenException;
import xyz.nucleoid.plasmid.api.game.GameSpace;
Expand All @@ -31,6 +31,8 @@
import xyz.nucleoid.plasmid.api.game.player.GamePlayerJoiner;
import xyz.nucleoid.plasmid.api.game.player.JoinIntent;
import xyz.nucleoid.plasmid.api.util.Scheduler;
import xyz.nucleoid.plasmid.impl.portal.config.ActiveGamePortalConfig;
import xyz.nucleoid.plasmid.impl.portal.config.GamePortalConfig;

import java.util.Comparator;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -250,7 +252,8 @@ private static int proposeGame(ServerCommandSource source, GameSpace gameSpace)
}

private static int joinGame(CommandContext<ServerCommandSource> context, JoinIntent intent) throws CommandSyntaxException {
new GameJoinUi(context.getSource().getPlayerOrThrow(), intent).open();
GamePortalConfig.create(context.getSource().getServer(), Identifier.of("plasmid", "join_command"),
ActiveGamePortalConfig.of(Text.translatable("text.plasmid.ui.game_join.title"), intent)).applyTo(context.getSource().getPlayerOrThrow(), false);
return Command.SINGLE_SUCCESS;
}

Expand Down
187 changes: 0 additions & 187 deletions src/main/java/xyz/nucleoid/plasmid/impl/command/ui/GameJoinUi.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import net.minecraft.util.Identifier;
import xyz.nucleoid.plasmid.api.game.GameSpace;
import xyz.nucleoid.plasmid.api.game.config.CustomValuesConfig;
import xyz.nucleoid.plasmid.impl.portal.backend.GamePortalBackend;
import xyz.nucleoid.plasmid.impl.portal.config.GamePortalConfig;

import java.util.List;
import java.util.Set;
Expand All @@ -24,9 +26,9 @@ public final class GamePortal {
private GamePortalDisplay lastDisplay = new GamePortalDisplay();
private GamePortalDisplay currentDisplay = new GamePortalDisplay();

public GamePortal(MinecraftServer server, Identifier id, GamePortalBackend.Factory backendFactory) {
public GamePortal(Identifier id, GamePortalBackend backend) {
this.id = id;
this.backend = backendFactory.create(server, id);
this.backend = backend;
}

void setCustom(CustomValuesConfig custom) {
Expand Down
Loading

0 comments on commit 274e227

Please sign in to comment.