diff --git a/gradle.properties b/gradle.properties index 089807a5..ed62de1d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,7 +7,7 @@ yarn_mappings=1.20.1+build.1 loader_version=0.14.21 #Fabric api -fabric_version=0.83.0+1.20.1 +fabric_version=0.87.0+1.20.1 # Mod Properties mod_version=0.6 diff --git a/src/main/java/xyz/nucleoid/plasmid/Plasmid.java b/src/main/java/xyz/nucleoid/plasmid/Plasmid.java index ace560b8..92297b78 100644 --- a/src/main/java/xyz/nucleoid/plasmid/Plasmid.java +++ b/src/main/java/xyz/nucleoid/plasmid/Plasmid.java @@ -6,6 +6,7 @@ import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.fabricmc.fabric.api.event.player.UseEntityCallback; +import net.fabricmc.fabric.api.event.registry.DynamicRegistries; import net.fabricmc.loader.api.FabricLoader; import net.minecraft.registry.DynamicRegistryManager; import net.minecraft.registry.Registries; @@ -15,7 +16,6 @@ import net.minecraft.util.ActionResult; import net.minecraft.util.Hand; import net.minecraft.util.Identifier; -import net.minecraft.util.dynamic.Codecs; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import xyz.nucleoid.plasmid.command.*; @@ -23,6 +23,7 @@ import xyz.nucleoid.plasmid.game.GameType; import xyz.nucleoid.plasmid.game.composite.RandomGame; import xyz.nucleoid.plasmid.game.composite.RandomGameConfig; +import xyz.nucleoid.plasmid.game.config.GameConfig; import xyz.nucleoid.plasmid.game.config.GameConfigs; import xyz.nucleoid.plasmid.game.event.GameActivityEvents; import xyz.nucleoid.plasmid.game.manager.GameSpaceManager; @@ -32,8 +33,8 @@ import xyz.nucleoid.plasmid.game.portal.game.ConcurrentGamePortalConfig; import xyz.nucleoid.plasmid.game.portal.game.LegacyOnDemandPortalConfig; import xyz.nucleoid.plasmid.game.portal.game.NewGamePortalConfig; -import xyz.nucleoid.plasmid.game.portal.menu.*; import xyz.nucleoid.plasmid.game.portal.game.SingleGamePortalConfig; +import xyz.nucleoid.plasmid.game.portal.menu.*; import xyz.nucleoid.plasmid.game.world.generator.GameChunkGenerator; public final class Plasmid implements ModInitializer { @@ -43,8 +44,9 @@ public final class Plasmid implements ModInitializer { @Override public void onInitialize() { - Registry.register(Registries.CHUNK_GENERATOR, new Identifier(ID, "game"), GameChunkGenerator.CODEC); + DynamicRegistries.register(GameConfigs.REGISTRY_KEY, GameConfig.DIRECT_CODEC); + Registry.register(Registries.CHUNK_GENERATOR, new Identifier(ID, "game"), GameChunkGenerator.CODEC); GamePortalConfig.register(new Identifier(ID, "single_game"), SingleGamePortalConfig.CODEC); GamePortalConfig.register(new Identifier(ID, "new_game"), NewGamePortalConfig.CODEC); @@ -64,8 +66,7 @@ public void onInitialize() { } private void loadData(DynamicRegistryManager registryManager, ResourceManager manager) { - GameConfigs.reload(registryManager, manager); - GamePortalManager.INSTANCE.reload(manager); + GamePortalManager.INSTANCE.reload(registryManager, manager); } private void registerCallbacks() { diff --git a/src/main/java/xyz/nucleoid/plasmid/command/GameCommand.java b/src/main/java/xyz/nucleoid/plasmid/command/GameCommand.java index 31548f75..02a76a76 100644 --- a/src/main/java/xyz/nucleoid/plasmid/command/GameCommand.java +++ b/src/main/java/xyz/nucleoid/plasmid/command/GameCommand.java @@ -11,6 +11,7 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.nbt.NbtOps; import net.minecraft.registry.RegistryOps; +import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.MutableText; @@ -121,7 +122,7 @@ private static int openGame(CommandContext context) throws protected static int openGame(CommandContext context, boolean test) throws CommandSyntaxException { try { var game = GameConfigArgument.get(context, "game_config"); - return openGame(context, game.getSecond(), test); + return openGame(context, game, test); } catch (CommandSyntaxException e) { throw e; } catch (Exception e) { @@ -138,13 +139,13 @@ private static int openAnonymousGame(CommandContext context protected static int openAnonymousGame(CommandContext context, boolean test) throws CommandSyntaxException { try { var configNbt = NbtCompoundArgumentType.getNbtCompound(context, "game_config_nbt"); - var result = GameConfig.CODEC.parse(RegistryOps.of(NbtOps.INSTANCE, context.getSource().getRegistryManager()), configNbt); + var result = GameConfig.DIRECT_CODEC.parse(RegistryOps.of(NbtOps.INSTANCE, context.getSource().getRegistryManager()), configNbt); if (result.error().isPresent()) { throw MALFORMED_CONFIG.create(result.error().get()); } var game = result.result().get(); - return openGame(context, game, test); + return openGame(context, RegistryEntry.of(game), test); } catch (CommandSyntaxException e) { throw e; } catch (Exception e) { @@ -154,7 +155,7 @@ protected static int openAnonymousGame(CommandContext conte } } - private static int openGame(CommandContext context, GameConfig config, boolean test) { + private static int openGame(CommandContext context, RegistryEntry> config, boolean test) { var source = context.getSource(); var server = source.getServer(); @@ -414,19 +415,21 @@ private static int stopGameConfirmed(CommandContext context } private static int listGames(CommandContext context) { + var registry = context.getSource().getRegistryManager().get(GameConfigs.REGISTRY_KEY); var source = context.getSource(); source.sendFeedback(() -> GameTexts.Command.gameList().formatted(Formatting.BOLD), false); - for (var id : GameConfigs.getKeys()) { + registry.streamEntries().forEach(game -> { + var id = game.registryKey().getValue(); source.sendFeedback(() -> { String command = "/game open " + id; - var link = GameConfig.name(GameConfigs.get(id)).copy() + var link = GameConfig.name(game).copy() .setStyle(GameTexts.commandLinkStyle(command)); return GameTexts.Command.listEntry(link); }, false); - } + }); return Command.SINGLE_SUCCESS; } diff --git a/src/main/java/xyz/nucleoid/plasmid/command/argument/GameConfigArgument.java b/src/main/java/xyz/nucleoid/plasmid/command/argument/GameConfigArgument.java index d7078160..cc12414c 100644 --- a/src/main/java/xyz/nucleoid/plasmid/command/argument/GameConfigArgument.java +++ b/src/main/java/xyz/nucleoid/plasmid/command/argument/GameConfigArgument.java @@ -7,6 +7,9 @@ import com.mojang.datafixers.util.Pair; import net.minecraft.command.CommandSource; import net.minecraft.command.argument.IdentifierArgumentType; +import net.minecraft.registry.Registry; +import net.minecraft.registry.RegistryKey; +import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.server.command.CommandManager; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.text.Text; @@ -15,6 +18,7 @@ import xyz.nucleoid.plasmid.game.config.GameConfigs; import java.util.Locale; +import java.util.Optional; import java.util.function.Function; public final class GameConfigArgument { @@ -25,24 +29,21 @@ public final class GameConfigArgument { public static RequiredArgumentBuilder argument(String name) { return CommandManager.argument(name, IdentifierArgumentType.identifier()) .suggests((ctx, builder) -> { - Iterable candidates = GameConfigs.getKeys().stream()::iterator; + var registry = ctx.getSource().getRegistryManager().get(GameConfigs.REGISTRY_KEY); var remaining = builder.getRemaining().toLowerCase(Locale.ROOT); - CommandSource.forEachMatching(candidates, remaining, Function.identity(), id -> { - builder.suggest(id.toString(), GameConfig.name(GameConfigs.get(id))); + CommandSource.forEachMatching(registry.getKeys(), remaining, RegistryKey::getValue, key -> { + registry.getEntry(key).ifPresent(entry -> { + builder.suggest(key.getValue().toString(), GameConfig.name(entry)); + }); }); return builder.buildFuture(); }); } - public static Pair> get(CommandContext context, String name) throws CommandSyntaxException { - var identifier = IdentifierArgumentType.getIdentifier(context, name); - - var config = GameConfigs.get(identifier); - if (config == null) { - throw GAME_NOT_FOUND.create(identifier); - } - - return new Pair<>(identifier, config); + public static RegistryEntry.Reference> get(CommandContext context, String name) throws CommandSyntaxException { + var key = RegistryKey.of(GameConfigs.REGISTRY_KEY, IdentifierArgumentType.getIdentifier(context, name)); + var registry = context.getSource().getRegistryManager().get(GameConfigs.REGISTRY_KEY); + return registry.getEntry(key).orElseThrow(() -> GAME_NOT_FOUND.create(key.getValue())); } } 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 82e0db3d..98b42fab 100644 --- a/src/main/java/xyz/nucleoid/plasmid/command/ui/GameJoinUi.java +++ b/src/main/java/xyz/nucleoid/plasmid/command/ui/GameJoinUi.java @@ -114,10 +114,10 @@ private void changePage(int change) { private GuiElementBuilder createIconFor(GameSpace gameSpace) { var sourceConfig = gameSpace.getMetadata().sourceConfig(); - var element = GuiElementBuilder.from(sourceConfig.icon().copy()).hideFlags() + var element = GuiElementBuilder.from(sourceConfig.value().icon().copy()).hideFlags() .setName(GameConfig.name(sourceConfig).copy()); - for (var line : sourceConfig.description()) { + for (var line : sourceConfig.value().description()) { var text = line.copy(); if (line.getStyle().getColor() == null) { diff --git a/src/main/java/xyz/nucleoid/plasmid/event/GameEvents.java b/src/main/java/xyz/nucleoid/plasmid/event/GameEvents.java index 0d9e81e0..419b83a7 100644 --- a/src/main/java/xyz/nucleoid/plasmid/event/GameEvents.java +++ b/src/main/java/xyz/nucleoid/plasmid/event/GameEvents.java @@ -2,6 +2,7 @@ import net.fabricmc.fabric.api.event.Event; import net.fabricmc.fabric.api.event.EventFactory; +import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.server.network.ServerPlayerEntity; import org.jetbrains.annotations.Nullable; import xyz.nucleoid.plasmid.game.GameActivity; @@ -75,7 +76,7 @@ public interface GameSpaceOpened { * @param game The game and its configuration * @param gameSpace The {@link GameSpace} the game is running in. */ - void onGameSpaceOpened(GameConfig game, GameSpace gameSpace); + void onGameSpaceOpened(RegistryEntry> game, GameSpace gameSpace); } public interface CreateActivity { diff --git a/src/main/java/xyz/nucleoid/plasmid/game/GameOpenProcedure.java b/src/main/java/xyz/nucleoid/plasmid/game/GameOpenProcedure.java index 286c72d0..0c0bf8f8 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/GameOpenProcedure.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/GameOpenProcedure.java @@ -1,5 +1,6 @@ package xyz.nucleoid.plasmid.game; +import net.minecraft.registry.entry.RegistryEntry; import xyz.nucleoid.plasmid.game.config.GameConfig; /** @@ -8,7 +9,7 @@ * @see GameOpenContext */ public interface GameOpenProcedure { - static GameOpenProcedure withOverride(GameOpenProcedure procedure, GameConfig game) { + static GameOpenProcedure withOverride(GameOpenProcedure procedure, RegistryEntry> game) { return new GameOpenProcedure() { @Override public void apply(GameSpace context) { @@ -16,7 +17,7 @@ public void apply(GameSpace context) { } @Override - public GameConfig configOverride() { + public RegistryEntry> configOverride() { return game; } }; @@ -24,7 +25,7 @@ public GameConfig configOverride() { void apply(GameSpace gameSpace); - default GameConfig configOverride() { + default RegistryEntry> configOverride() { return null; } } diff --git a/src/main/java/xyz/nucleoid/plasmid/game/GameSpaceMetadata.java b/src/main/java/xyz/nucleoid/plasmid/game/GameSpaceMetadata.java index be14f3e8..f68ed5e6 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/GameSpaceMetadata.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/GameSpaceMetadata.java @@ -1,5 +1,6 @@ package xyz.nucleoid.plasmid.game; +import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.util.Identifier; import xyz.nucleoid.plasmid.game.config.GameConfig; @@ -12,7 +13,7 @@ public record GameSpaceMetadata( UUID id, Identifier userId, - GameConfig sourceConfig + RegistryEntry> sourceConfig ) { /** * @return the globally unique ID for this {@link GameSpace} @@ -36,8 +37,7 @@ public Identifier userId() { /** * @return the {@link GameConfig} that was responsible for creating this {@link GameSpace} */ - @Override - public GameConfig sourceConfig() { + public RegistryEntry> sourceConfig() { return this.sourceConfig; } } diff --git a/src/main/java/xyz/nucleoid/plasmid/game/common/GameWaitingLobby.java b/src/main/java/xyz/nucleoid/plasmid/game/common/GameWaitingLobby.java index fcbddc4c..d537fb7f 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/common/GameWaitingLobby.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/common/GameWaitingLobby.java @@ -93,7 +93,7 @@ public static GameWaitingLobby addTo(GameActivity activity, PlayerConfig playerC activity.listen(GamePlayerEvents.REMOVE, lobby::onRemovePlayer); - lobby.setSidebarLines(sourceConfig.description()); + lobby.setSidebarLines(sourceConfig.value().description()); var title = GameConfig.shortName(sourceConfig).copy(); if (title.getStyle().getColor() == null) { diff --git a/src/main/java/xyz/nucleoid/plasmid/game/composite/GameListConfig.java b/src/main/java/xyz/nucleoid/plasmid/game/composite/GameListConfig.java deleted file mode 100644 index 16275025..00000000 --- a/src/main/java/xyz/nucleoid/plasmid/game/composite/GameListConfig.java +++ /dev/null @@ -1,44 +0,0 @@ -package xyz.nucleoid.plasmid.game.composite; - -import com.mojang.serialization.Codec; -import net.minecraft.util.Identifier; -import org.jetbrains.annotations.Nullable; -import xyz.nucleoid.codecs.MoreCodecs; -import xyz.nucleoid.plasmid.Plasmid; -import xyz.nucleoid.plasmid.game.config.GameConfig; -import xyz.nucleoid.plasmid.game.config.GameConfigs; - -import java.util.ArrayList; -import java.util.List; -import java.util.Random; - -public record GameListConfig(List games) { - public static final Codec CODEC = MoreCodecs.listOrUnit(Identifier.CODEC) - .xmap(GameListConfig::new, config -> config.games); - - public List> collectGames() { - var games = new ArrayList>(this.games.size()); - for (var gameId : this.games) { - var game = GameConfigs.get(gameId); - if (game == null) { - Plasmid.LOGGER.warn("Missing game config by id '{}'!", gameId); - continue; - } - games.add(game); - } - return games; - } - - @Nullable - public GameConfig selectGame(Random random) { - var games = this.collectGames(); - if (games.isEmpty()) { - return null; - } - return games.get(random.nextInt(games.size())); - } - - public boolean isEmpty() { - return this.games.isEmpty(); - } -} diff --git a/src/main/java/xyz/nucleoid/plasmid/game/composite/RandomGame.java b/src/main/java/xyz/nucleoid/plasmid/game/composite/RandomGame.java index c9a574d5..cabffee3 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/composite/RandomGame.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/composite/RandomGame.java @@ -1,21 +1,20 @@ package xyz.nucleoid.plasmid.game.composite; import net.minecraft.text.Text; +import net.minecraft.util.math.random.Random; import xyz.nucleoid.plasmid.game.GameOpenContext; import xyz.nucleoid.plasmid.game.GameOpenException; import xyz.nucleoid.plasmid.game.GameOpenProcedure; -import java.util.Random; - public final class RandomGame { public static GameOpenProcedure open(GameOpenContext context) { var config = context.config(); - var game = config.selectGame(new Random()); + var game = config.selectGame(Random.createLocal()); if (game == null) { throw new GameOpenException(Text.translatable("text.plasmid.random.empty_composite_game_config")); } - return GameOpenProcedure.withOverride(game.openProcedure(context.server()), game); + return GameOpenProcedure.withOverride(game.value().openProcedure(context.server()), game); } } diff --git a/src/main/java/xyz/nucleoid/plasmid/game/composite/RandomGameConfig.java b/src/main/java/xyz/nucleoid/plasmid/game/composite/RandomGameConfig.java index 2fe61ac2..4b67adcb 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/composite/RandomGameConfig.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/composite/RandomGameConfig.java @@ -2,24 +2,25 @@ import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; +import net.minecraft.registry.RegistryCodecs; +import net.minecraft.registry.entry.RegistryEntry; +import net.minecraft.registry.entry.RegistryEntryList; +import net.minecraft.util.math.random.Random; import org.jetbrains.annotations.Nullable; import xyz.nucleoid.plasmid.game.config.GameConfig; +import xyz.nucleoid.plasmid.game.config.GameConfigs; -import java.util.Random; - -public record RandomGameConfig(GameListConfig games) { - public static final Codec CODEC = RecordCodecBuilder.create(instance -> { - return instance.group( - GameListConfig.CODEC.fieldOf("games").forGetter(config -> config.games) - ).apply(instance, RandomGameConfig::new); - }); +public record RandomGameConfig(RegistryEntryList> games) { + public static final Codec CODEC = RecordCodecBuilder.create(i -> i.group( + RegistryCodecs.entryList(GameConfigs.REGISTRY_KEY).fieldOf("games").forGetter(config -> config.games) + ).apply(i, RandomGameConfig::new)); @Nullable - public GameConfig selectGame(Random random) { - return this.games.selectGame(random); + public RegistryEntry> selectGame(Random random) { + return this.games.getRandom(random).orElse(null); } public boolean isEmpty() { - return this.games.isEmpty(); + return this.games.size() == 0; } } diff --git a/src/main/java/xyz/nucleoid/plasmid/game/config/GameConfig.java b/src/main/java/xyz/nucleoid/plasmid/game/config/GameConfig.java index bb338b77..13ad81bc 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/config/GameConfig.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/config/GameConfig.java @@ -5,9 +5,10 @@ import com.mojang.serialization.codecs.RecordCodecBuilder; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; +import net.minecraft.registry.entry.RegistryElementCodec; +import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.server.MinecraftServer; import net.minecraft.text.Text; -import net.minecraft.util.Identifier; import net.minecraft.util.Util; import org.jetbrains.annotations.Nullable; import xyz.nucleoid.codecs.MoreCodecs; @@ -25,7 +26,6 @@ import java.util.stream.Stream; public record GameConfig( - @Nullable Identifier source, GameType type, @Nullable Text name, @Nullable Text shortName, @@ -34,11 +34,8 @@ public record GameConfig( CustomValuesConfig custom, C config ) { - public static final Codec> CODEC = codecFrom(null); - - public static Codec> codecFrom(Identifier source) { - return new ConfigCodec(source).codec(); - } + public static final Codec> DIRECT_CODEC = new ConfigCodec().codec(); + public static final Codec>> CODEC = RegistryElementCodec.of(GameConfigs.REGISTRY_KEY, DIRECT_CODEC); public GameOpenProcedure openProcedure(MinecraftServer server) { var context = new GameOpenContext(server, this); @@ -48,42 +45,34 @@ public GameOpenProcedure openProcedure(MinecraftServer server) { /** * @return the source location that this config was loaded from, if loaded from a file. */ - @Override - @Nullable - public Identifier source() { - return this.source; - } - - /** - * @return the source location that this config was loaded from, if loaded from a file. - */ - public static String sourceName(GameConfig config) { - return Optional.ofNullable(config.source).map(Identifier::toString).orElse("[unknown source]"); + public static String sourceName(RegistryEntry> config) { + return config.getKey().map(e -> e.getValue().toString()).orElse("[unknown source]"); } /** * @return the name for this game config, defaulted to the game type name if none is specified */ - public static Text name(final GameConfig config) { - var name = config.name; + public static Text name(final RegistryEntry> config) { + var name = config.value().name; if (name != null) { return name; } - var translationKey = config.translationKey(); - if (translationKey != null && hasTranslationFor(translationKey)) { - return Text.translatable(translationKey); + var translationKey = config.getKey().map(key -> Util.createTranslationKey("game", key.getValue())) + .filter(GameConfig::hasTranslationFor); + if (translationKey.isPresent()) { + return Text.translatable(translationKey.get()); } - return config.type.name(); + return config.value().type.name(); } /** * @return shortened version of the name, defaulted to standard name */ - public static Text shortName(final GameConfig config) { - if (config.shortName != null) { - return config.shortName; + public static Text shortName(final RegistryEntry> config) { + if (config.value().shortName != null) { + return config.value().shortName; } return name(config); } @@ -116,22 +105,7 @@ private static boolean hasTranslationFor(String translationKey) { return language.serverTranslations().contains(translationKey); } - @Nullable - public String translationKey() { - if (this.source != null) { - return Util.createTranslationKey("game", this.source); - } else { - return null; - } - } - static final class ConfigCodec extends MapCodec> { - private final Identifier source; - - ConfigCodec(@Nullable Identifier source) { - this.source = source; - } - @Override public Stream keys(DynamicOps ops) { return Stream.concat(Stream.of(ops.createString("type"), ops.createString("config")), Metadata.MAP_CODEC.keys(ops)); @@ -161,7 +135,7 @@ private DataResult decodeConfig(DynamicOps ops, MapLike input, Code @SuppressWarnings("unchecked") private GameConfig createConfigUnchecked(GameType type, Metadata metadata, Object config) { return new GameConfig<>( - this.source, type, + type, metadata.name.orElse(null), metadata.shortName.orElse(null), metadata.description.orElse(null), metadata.icon, metadata.custom, (C) config diff --git a/src/main/java/xyz/nucleoid/plasmid/game/config/GameConfigs.java b/src/main/java/xyz/nucleoid/plasmid/game/config/GameConfigs.java index c3072e4d..16272e89 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/config/GameConfigs.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/config/GameConfigs.java @@ -1,66 +1,10 @@ package xyz.nucleoid.plasmid.game.config; -import com.google.gson.JsonElement; -import com.google.gson.JsonParseException; -import com.google.gson.JsonParser; -import com.mojang.serialization.Codec; -import com.mojang.serialization.DataResult; -import com.mojang.serialization.DynamicOps; -import com.mojang.serialization.JsonOps; -import net.minecraft.registry.DynamicRegistryManager; -import net.minecraft.registry.RegistryOps; -import net.minecraft.resource.ResourceFinder; -import net.minecraft.resource.ResourceManager; +import net.minecraft.registry.Registry; +import net.minecraft.registry.RegistryKey; import net.minecraft.util.Identifier; -import org.jetbrains.annotations.Nullable; import xyz.nucleoid.plasmid.Plasmid; -import xyz.nucleoid.plasmid.registry.TinyRegistry; - -import java.io.IOException; -import java.util.Set; public final class GameConfigs { - private static final ResourceFinder FINDER = ResourceFinder.json("games"); - - private static final TinyRegistry> CONFIGS = TinyRegistry.create(); - - public static void reload(DynamicRegistryManager registryManager, ResourceManager manager) { - CONFIGS.clear(); - - DynamicOps ops = RegistryOps.of(JsonOps.INSTANCE, registryManager); - - FINDER.findResources(manager).forEach((path, resource) -> { - try { - try (var reader = resource.getReader()) { - JsonElement json = JsonParser.parseReader(reader); - - Identifier identifier = FINDER.toResourceId(path); - - Codec> codec = GameConfig.codecFrom(identifier); - DataResult> result = codec.parse(ops, json); - - result.result().ifPresent(game -> { - CONFIGS.register(identifier, game); - }); - - result.error().ifPresent(error -> { - Plasmid.LOGGER.error("Failed to parse game at {}: {}", path, error); - }); - } - } catch (IOException e) { - Plasmid.LOGGER.error("Failed to read configured game at {}", path, e); - } catch (JsonParseException e) { - Plasmid.LOGGER.error("Failed to parse game JSON at {}: {}", path, e); - } - }); - } - - @Nullable - public static GameConfig get(Identifier identifier) { - return CONFIGS.get(identifier); - } - - public static Set getKeys() { - return CONFIGS.keySet(); - } + public static final RegistryKey>> REGISTRY_KEY = RegistryKey.ofRegistry(new Identifier(Plasmid.ID, "games")); } 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 a3fac1b8..ab99057d 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/manager/GameSpaceManager.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/manager/GameSpaceManager.java @@ -6,6 +6,7 @@ import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.registry.RegistryKey; +import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.Identifier; @@ -79,13 +80,12 @@ public static GameSpaceManager get() { } } - public CompletableFuture open(GameConfig config) { + public CompletableFuture open(RegistryEntry> config) { if (this.server == null) { return CompletableFuture.failedFuture(new RuntimeException("Not initialized yet!")); } - return CompletableFuture.supplyAsync( - () -> config.openProcedure(this.server), + () -> config.value().openProcedure(this.server), Util.getMainWorkerExecutor() ).thenApplyAsync( procedure -> this.addGameSpace(procedure.configOverride() != null ? procedure.configOverride() : config, procedure), @@ -93,13 +93,13 @@ public CompletableFuture open(GameConfig config) { ); } - private ManagedGameSpace addGameSpace(GameConfig config, GameOpenProcedure procedure) { + private ManagedGameSpace addGameSpace(RegistryEntry> config, GameOpenProcedure procedure) { if (this.server == null) { throw new RuntimeException("Not initialized yet!"); } var id = UUID.randomUUID(); - var userId = this.userIds.acquire(config); + var userId = this.userIds.acquire(config.value()); Preconditions.checkState(!this.userIdToGameSpace.containsKey(userId), "duplicate GameSpace user id acquired"); var metadata = new GameSpaceMetadata(id, userId, config); diff --git a/src/main/java/xyz/nucleoid/plasmid/game/portal/GamePortalManager.java b/src/main/java/xyz/nucleoid/plasmid/game/portal/GamePortalManager.java index a452978b..87dc4dc9 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/portal/GamePortalManager.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/portal/GamePortalManager.java @@ -4,8 +4,8 @@ import com.google.gson.JsonParser; import com.mojang.serialization.JsonOps; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; -import net.fabricmc.fabric.api.resource.ResourceManagerHelper; -import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener; +import net.minecraft.registry.DynamicRegistryManager; +import net.minecraft.registry.RegistryOps; import net.minecraft.resource.ResourceManager; import net.minecraft.server.MinecraftServer; import net.minecraft.util.Identifier; @@ -16,7 +16,6 @@ import java.io.IOException; import java.util.Collection; -import java.util.List; import java.util.Map; import java.util.Set; @@ -55,10 +54,10 @@ public void close(MinecraftServer server) { } @ApiStatus.Internal - public void reload(ResourceManager manager) { + public void reload(DynamicRegistryManager registryManager, ResourceManager manager) { this.portals.clear(); - var configs = this.loadConfigs(manager); + var configs = this.loadConfigs(registryManager, manager); if (this.server != null) { this.loadPortalsFrom(this.server, configs); } else { @@ -78,7 +77,7 @@ private void loadPortalsFrom(MinecraftServer server, Map loadConfigs(ResourceManager manager) { + private Map loadConfigs(DynamicRegistryManager registryManager, ResourceManager manager) { var configs = new Object2ObjectOpenHashMap(); manager.findResources(PATH, path -> path.getPath().endsWith(".json")).forEach((path, resource) -> { @@ -87,7 +86,7 @@ private Map loadConfigs(ResourceManager manager) { var json = JsonParser.parseReader(reader); var identifier = identifierFromPath(path); - var result = GamePortalConfig.CODEC.parse(JsonOps.INSTANCE, json); + var result = GamePortalConfig.CODEC.parse(RegistryOps.of(JsonOps.INSTANCE, registryManager), json); result.result().ifPresent(config -> { configs.put(identifier, config); diff --git a/src/main/java/xyz/nucleoid/plasmid/game/portal/game/ConcurrentGamePortalBackend.java b/src/main/java/xyz/nucleoid/plasmid/game/portal/game/ConcurrentGamePortalBackend.java index 3419968b..1724c454 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/portal/game/ConcurrentGamePortalBackend.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/portal/game/ConcurrentGamePortalBackend.java @@ -1,41 +1,34 @@ package xyz.nucleoid.plasmid.game.portal.game; import net.minecraft.item.ItemStack; -import net.minecraft.item.Items; +import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; -import net.minecraft.util.Formatting; -import net.minecraft.util.Identifier; -import xyz.nucleoid.plasmid.Plasmid; -import xyz.nucleoid.plasmid.game.GameOpenException; import xyz.nucleoid.plasmid.game.GameSpace; import xyz.nucleoid.plasmid.game.config.GameConfig; -import xyz.nucleoid.plasmid.game.config.GameConfigs; 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.game.portal.GamePortalBackend; -import java.util.Collections; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.function.Consumer; import java.util.function.Function; public final class ConcurrentGamePortalBackend implements GamePortalBackend { - private final Identifier gameId; + private final RegistryEntry> game; private CompletableFuture gameFuture; - public ConcurrentGamePortalBackend(Identifier gameId) { - this.gameId = gameId; + public ConcurrentGamePortalBackend(RegistryEntry> game) { + this.game = game; } @Override public void provideGameSpaces(Consumer consumer) { - var gameConfig = GameConfigs.get(this.gameId); for (var gameSpace : GameSpaceManager.get().getOpenGameSpaces()) { - if (gameSpace.getMetadata().sourceConfig() == gameConfig) { + if (gameSpace.getMetadata().sourceConfig().equals(this.game)) { consumer.accept(gameSpace); } } @@ -44,9 +37,8 @@ public void provideGameSpaces(Consumer consumer) { @Override public int getPlayerCount() { int count = 0; - var gameConfig = GameConfigs.get(this.gameId); for (var gameSpace : GameSpaceManager.get().getOpenGameSpaces()) { - if (gameSpace.getMetadata().sourceConfig() == gameConfig) { + if (gameSpace.getMetadata().sourceConfig().equals(this.game)) { count += gameSpace.getPlayers().size(); } } @@ -55,22 +47,12 @@ public int getPlayerCount() { @Override public List getDescription() { - var config = GameConfigs.get(this.gameId); - if (config != null) { - return config.description(); - } - - return Collections.emptyList(); + return this.game.value().description(); } @Override public ItemStack getIcon() { - var config = GameConfigs.get(this.gameId); - if (config != null) { - return config.icon(); - } - - return Items.BARRIER.getDefaultStack(); + return this.game.value().icon(); } @Override @@ -80,9 +62,8 @@ public ActionType getActionType() { @Override public void applyTo(ServerPlayerEntity player) { - var gameConfig = GameConfigs.get(this.gameId); for (var gameSpace : GameSpaceManager.get().getOpenGameSpaces()) { - if (gameSpace.getMetadata().sourceConfig() == gameConfig) { + if (gameSpace.getMetadata().sourceConfig().equals(this.game)) { var results = GamePlayerJoiner.tryJoin(player, gameSpace); if (results.globalError == null && results.playerErrors.get(player) == null) { @@ -110,12 +91,7 @@ public void applyTo(ServerPlayerEntity player) { @Override public Text getName() { - var config = GameConfigs.get(this.gameId); - if (config != null) { - return GameConfig.name(config); - } else { - return Text.literal(this.gameId.toString()).formatted(Formatting.RED); - } + return GameConfig.name(this.game); } public CompletableFuture getOrOpenNew(MinecraftServer server) { @@ -127,17 +103,6 @@ public CompletableFuture getOrOpenNew(MinecraftServer server) } private CompletableFuture openGame(MinecraftServer server) { - var config = GameConfigs.get(this.gameId); - if (config == null) { - Plasmid.LOGGER.warn("Missing game config for concurent game with id '{}'", this.gameId); - - var future = new CompletableFuture(); - var error = Text.translatable("text.plasmid.game_config.game_config_does_not_exist", this.gameId); - future.completeExceptionally(new GameOpenException(error)); - - return future; - } - - return GameSpaceManager.get().open(config); + return GameSpaceManager.get().open(this.game); } } diff --git a/src/main/java/xyz/nucleoid/plasmid/game/portal/game/ConcurrentGamePortalConfig.java b/src/main/java/xyz/nucleoid/plasmid/game/portal/game/ConcurrentGamePortalConfig.java index 6d37c91e..b168136b 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/portal/game/ConcurrentGamePortalConfig.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/portal/game/ConcurrentGamePortalConfig.java @@ -2,23 +2,23 @@ import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; +import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.server.MinecraftServer; import net.minecraft.util.Identifier; import xyz.nucleoid.plasmid.game.config.CustomValuesConfig; +import xyz.nucleoid.plasmid.game.config.GameConfig; import xyz.nucleoid.plasmid.game.portal.GamePortalBackend; import xyz.nucleoid.plasmid.game.portal.GamePortalConfig; -public record ConcurrentGamePortalConfig(Identifier gameId, CustomValuesConfig custom) implements GamePortalConfig { - public static final Codec CODEC = RecordCodecBuilder.create(instance -> { - return instance.group( - Identifier.CODEC.fieldOf("game").forGetter(c -> c.gameId), - CustomValuesConfig.CODEC.optionalFieldOf("custom", CustomValuesConfig.empty()).forGetter(c -> c.custom) - ).apply(instance, ConcurrentGamePortalConfig::new); - }); +public record ConcurrentGamePortalConfig(RegistryEntry> game, CustomValuesConfig custom) implements GamePortalConfig { + public static final Codec CODEC = RecordCodecBuilder.create(i -> i.group( + GameConfig.CODEC.fieldOf("game").forGetter(c -> c.game), + CustomValuesConfig.CODEC.optionalFieldOf("custom", CustomValuesConfig.empty()).forGetter(c -> c.custom) + ).apply(i, ConcurrentGamePortalConfig::new)); @Override public GamePortalBackend createBackend(MinecraftServer server, Identifier id) { - return new ConcurrentGamePortalBackend(this.gameId); + return new ConcurrentGamePortalBackend(this.game); } @Override diff --git a/src/main/java/xyz/nucleoid/plasmid/game/portal/game/LegacyOnDemandPortalConfig.java b/src/main/java/xyz/nucleoid/plasmid/game/portal/game/LegacyOnDemandPortalConfig.java index 15f80195..10d061d9 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/portal/game/LegacyOnDemandPortalConfig.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/portal/game/LegacyOnDemandPortalConfig.java @@ -2,23 +2,23 @@ import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; +import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.server.MinecraftServer; import net.minecraft.util.Identifier; import xyz.nucleoid.plasmid.game.config.CustomValuesConfig; +import xyz.nucleoid.plasmid.game.config.GameConfig; import xyz.nucleoid.plasmid.game.portal.GamePortalBackend; import xyz.nucleoid.plasmid.game.portal.GamePortalConfig; -public record LegacyOnDemandPortalConfig(Identifier gameId, CustomValuesConfig custom) implements GamePortalConfig { - public static final Codec CODEC = RecordCodecBuilder.create(instance -> { - return instance.group( - Identifier.CODEC.fieldOf("game").forGetter(c -> c.gameId), - CustomValuesConfig.CODEC.optionalFieldOf("custom", CustomValuesConfig.empty()).forGetter(c -> c.custom) - ).apply(instance, LegacyOnDemandPortalConfig::new); - }); +public record LegacyOnDemandPortalConfig(RegistryEntry> game, CustomValuesConfig custom) implements GamePortalConfig { + public static final Codec CODEC = RecordCodecBuilder.create(i -> i.group( + GameConfig.CODEC.fieldOf("game").forGetter(c -> c.game), + CustomValuesConfig.CODEC.optionalFieldOf("custom", CustomValuesConfig.empty()).forGetter(c -> c.custom) + ).apply(i, LegacyOnDemandPortalConfig::new)); @Override public GamePortalBackend createBackend(MinecraftServer server, Identifier id) { - return new ConcurrentGamePortalBackend(this.gameId); + return new ConcurrentGamePortalBackend(this.game); } @Override diff --git a/src/main/java/xyz/nucleoid/plasmid/game/portal/game/NewGamePortalBackend.java b/src/main/java/xyz/nucleoid/plasmid/game/portal/game/NewGamePortalBackend.java index 07b68b5a..c106caee 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/portal/game/NewGamePortalBackend.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/portal/game/NewGamePortalBackend.java @@ -1,35 +1,27 @@ package xyz.nucleoid.plasmid.game.portal.game; import net.minecraft.item.ItemStack; -import net.minecraft.item.Items; +import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; -import net.minecraft.util.Formatting; -import net.minecraft.util.Identifier; -import xyz.nucleoid.plasmid.Plasmid; -import xyz.nucleoid.plasmid.game.GameOpenException; import xyz.nucleoid.plasmid.game.GameSpace; import xyz.nucleoid.plasmid.game.config.GameConfig; -import xyz.nucleoid.plasmid.game.config.GameConfigs; 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.game.portal.GamePortalBackend; -import java.util.Collections; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.function.Consumer; import java.util.function.Function; -public record NewGamePortalBackend(Identifier gameId) implements GamePortalBackend { - +public record NewGamePortalBackend(RegistryEntry> game) implements GamePortalBackend { @Override public void provideGameSpaces(Consumer consumer) { - var gameConfig = GameConfigs.get(this.gameId); for (var gameSpace : GameSpaceManager.get().getOpenGameSpaces()) { - if (gameSpace.getMetadata().sourceConfig() == gameConfig) { + if (gameSpace.getMetadata().sourceConfig().equals(this.game)) { consumer.accept(gameSpace); } } @@ -38,9 +30,8 @@ public void provideGameSpaces(Consumer consumer) { @Override public int getPlayerCount() { int count = 0; - var gameConfig = GameConfigs.get(this.gameId); for (var gameSpace : GameSpaceManager.get().getOpenGameSpaces()) { - if (gameSpace.getMetadata().sourceConfig() == gameConfig) { + if (gameSpace.getMetadata().sourceConfig().equals(this.game)) { count += gameSpace.getPlayers().size(); } } @@ -72,46 +63,20 @@ public void applyTo(ServerPlayerEntity player) { @Override public Text getName() { - var config = GameConfigs.get(this.gameId); - if (config != null) { - return GameConfig.name(config); - } else { - return Text.literal(this.gameId.toString()).formatted(Formatting.RED); - } + return GameConfig.name(this.game); } @Override public List getDescription() { - var config = GameConfigs.get(this.gameId); - if (config != null) { - return config.description(); - } - - return Collections.emptyList(); + return this.game.value().description(); } @Override public ItemStack getIcon() { - var config = GameConfigs.get(this.gameId); - if (config != null) { - return config.icon(); - } - - return Items.BARRIER.getDefaultStack(); + return this.game.value().icon(); } private CompletableFuture openGame(MinecraftServer server) { - var config = GameConfigs.get(this.gameId); - if (config == null) { - Plasmid.LOGGER.warn("Missing game config for on-demand game with id '{}'", this.gameId); - - var future = new CompletableFuture(); - var error = Text.translatable("text.plasmid.game_config.game_config_does_not_exist", this.gameId); - future.completeExceptionally(new GameOpenException(error)); - - return future; - } - - return GameSpaceManager.get().open(config); + return GameSpaceManager.get().open(this.game); } } diff --git a/src/main/java/xyz/nucleoid/plasmid/game/portal/game/NewGamePortalConfig.java b/src/main/java/xyz/nucleoid/plasmid/game/portal/game/NewGamePortalConfig.java index dfbb94cd..047c515d 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/portal/game/NewGamePortalConfig.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/portal/game/NewGamePortalConfig.java @@ -2,23 +2,23 @@ import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; +import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.server.MinecraftServer; import net.minecraft.util.Identifier; import xyz.nucleoid.plasmid.game.config.CustomValuesConfig; +import xyz.nucleoid.plasmid.game.config.GameConfig; import xyz.nucleoid.plasmid.game.portal.GamePortalBackend; import xyz.nucleoid.plasmid.game.portal.GamePortalConfig; -public record NewGamePortalConfig(Identifier gameId, CustomValuesConfig custom) implements GamePortalConfig { - public static final Codec CODEC = RecordCodecBuilder.create(instance -> { - return instance.group( - Identifier.CODEC.fieldOf("game").forGetter(c -> c.gameId), - CustomValuesConfig.CODEC.optionalFieldOf("custom", CustomValuesConfig.empty()).forGetter(c -> c.custom) - ).apply(instance, NewGamePortalConfig::new); - }); +public record NewGamePortalConfig(RegistryEntry> game, CustomValuesConfig custom) implements GamePortalConfig { + public static final Codec CODEC = RecordCodecBuilder.create(i -> i.group( + GameConfig.CODEC.fieldOf("game").forGetter(c -> c.game), + CustomValuesConfig.CODEC.optionalFieldOf("custom", CustomValuesConfig.empty()).forGetter(c -> c.custom) + ).apply(i, NewGamePortalConfig::new)); @Override public GamePortalBackend createBackend(MinecraftServer server, Identifier id) { - return new NewGamePortalBackend(this.gameId); + return new NewGamePortalBackend(this.game); } @Override diff --git a/src/main/java/xyz/nucleoid/plasmid/game/portal/game/SingleGamePortalBackend.java b/src/main/java/xyz/nucleoid/plasmid/game/portal/game/SingleGamePortalBackend.java index 51f76d23..858ff234 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/portal/game/SingleGamePortalBackend.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/portal/game/SingleGamePortalBackend.java @@ -1,36 +1,30 @@ package xyz.nucleoid.plasmid.game.portal.game; import net.minecraft.item.ItemStack; -import net.minecraft.item.Items; +import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; -import net.minecraft.util.Formatting; -import net.minecraft.util.Identifier; -import xyz.nucleoid.plasmid.Plasmid; import xyz.nucleoid.plasmid.game.GameCloseReason; import xyz.nucleoid.plasmid.game.GameLifecycle; -import xyz.nucleoid.plasmid.game.GameOpenException; import xyz.nucleoid.plasmid.game.GameSpace; import xyz.nucleoid.plasmid.game.config.GameConfig; -import xyz.nucleoid.plasmid.game.config.GameConfigs; 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.game.portal.GamePortalBackend; -import java.util.Collections; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.function.Consumer; import java.util.function.Function; public final class SingleGamePortalBackend implements GamePortalBackend { - private final Identifier gameId; + private final RegistryEntry> game; private CompletableFuture gameFuture; - public SingleGamePortalBackend(Identifier gameId) { - this.gameId = gameId; + public SingleGamePortalBackend(RegistryEntry> game) { + this.game = game; } @Override @@ -71,12 +65,7 @@ public void applyTo(ServerPlayerEntity player) { @Override public Text getName() { - var config = GameConfigs.get(this.gameId); - if (config != null) { - return GameConfig.name(config); - } else { - return Text.literal(this.gameId.toString()).formatted(Formatting.RED); - } + return GameConfig.name(this.game); } @Override @@ -86,22 +75,12 @@ public ActionType getActionType() { @Override public List getDescription() { - var config = GameConfigs.get(this.gameId); - if (config != null) { - return config.description(); - } - - return Collections.emptyList(); + return this.game.value().description(); } @Override public ItemStack getIcon() { - var config = GameConfigs.get(this.gameId); - if (config != null) { - return config.icon(); - } - - return Items.BARRIER.getDefaultStack(); + return this.game.value().icon(); } public CompletableFuture getOrOpen(MinecraftServer server) { @@ -117,18 +96,7 @@ private void onClose() { } private CompletableFuture openGame(MinecraftServer server) { - var config = GameConfigs.get(this.gameId); - if (config == null) { - Plasmid.LOGGER.warn("Missing game config for on-demand game with id '{}'", this.gameId); - - var future = new CompletableFuture(); - var error = Text.translatable("text.plasmid.game_config.game_config_does_not_exist", this.gameId); - future.completeExceptionally(new GameOpenException(error)); - - return future; - } - - return GameSpaceManager.get().open(config).thenApplyAsync(gameSpace -> { + return GameSpaceManager.get().open(this.game).thenApplyAsync(gameSpace -> { var lifecycle = gameSpace.getLifecycle(); lifecycle.addListeners(new LifecycleListeners()); diff --git a/src/main/java/xyz/nucleoid/plasmid/game/portal/game/SingleGamePortalConfig.java b/src/main/java/xyz/nucleoid/plasmid/game/portal/game/SingleGamePortalConfig.java index 01a1329d..a3563d10 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/portal/game/SingleGamePortalConfig.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/portal/game/SingleGamePortalConfig.java @@ -2,23 +2,23 @@ import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; +import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.server.MinecraftServer; import net.minecraft.util.Identifier; import xyz.nucleoid.plasmid.game.config.CustomValuesConfig; +import xyz.nucleoid.plasmid.game.config.GameConfig; import xyz.nucleoid.plasmid.game.portal.GamePortalBackend; import xyz.nucleoid.plasmid.game.portal.GamePortalConfig; -public record SingleGamePortalConfig(Identifier gameId, CustomValuesConfig custom) implements GamePortalConfig { - public static final Codec CODEC = RecordCodecBuilder.create(instance -> { - return instance.group( - Identifier.CODEC.fieldOf("game").forGetter(c -> c.gameId), - CustomValuesConfig.CODEC.optionalFieldOf("custom", CustomValuesConfig.empty()).forGetter(c -> c.custom) - ).apply(instance, SingleGamePortalConfig::new); - }); +public record SingleGamePortalConfig(RegistryEntry> game, CustomValuesConfig custom) implements GamePortalConfig { + public static final Codec CODEC = RecordCodecBuilder.create(i -> i.group( + GameConfig.CODEC.fieldOf("game").forGetter(c -> c.game), + CustomValuesConfig.CODEC.optionalFieldOf("custom", CustomValuesConfig.empty()).forGetter(c -> c.custom) + ).apply(i, SingleGamePortalConfig::new)); @Override public GamePortalBackend createBackend(MinecraftServer server, Identifier id) { - return new SingleGamePortalBackend(this.gameId); + return new SingleGamePortalBackend(this.game); } @Override diff --git a/src/main/java/xyz/nucleoid/plasmid/game/portal/menu/GameMenuEntryConfig.java b/src/main/java/xyz/nucleoid/plasmid/game/portal/menu/GameMenuEntryConfig.java index e216669c..37e98505 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/portal/menu/GameMenuEntryConfig.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/portal/menu/GameMenuEntryConfig.java @@ -1,50 +1,40 @@ package xyz.nucleoid.plasmid.game.portal.menu; -import java.util.List; -import java.util.Optional; - import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; - import net.minecraft.item.ItemStack; +import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.text.Text; -import net.minecraft.util.Identifier; import xyz.nucleoid.codecs.MoreCodecs; import xyz.nucleoid.plasmid.game.config.GameConfig; -import xyz.nucleoid.plasmid.game.config.GameConfigs; import xyz.nucleoid.plasmid.game.portal.game.ConcurrentGamePortalBackend; import xyz.nucleoid.plasmid.util.PlasmidCodecs; -public record GameMenuEntryConfig(Identifier game, +import java.util.List; +import java.util.Optional; + +public record GameMenuEntryConfig( + RegistryEntry> game, Optional name, Optional> description, Optional icon ) implements MenuEntryConfig { - - public static final Codec CODEC = RecordCodecBuilder.create(instance -> { - return instance.group( - Identifier.CODEC.fieldOf("game").forGetter(GameMenuEntryConfig::game), - PlasmidCodecs.TEXT.optionalFieldOf("name").forGetter(GameMenuEntryConfig::name), - MoreCodecs.listOrUnit(PlasmidCodecs.TEXT).optionalFieldOf("description").forGetter(GameMenuEntryConfig::description), - MoreCodecs.ITEM_STACK.optionalFieldOf("icon").forGetter(GameMenuEntryConfig::icon) - ).apply(instance, GameMenuEntryConfig::new); - }); + public static final Codec CODEC = RecordCodecBuilder.create(i -> i.group( + GameConfig.CODEC.fieldOf("game").forGetter(GameMenuEntryConfig::game), + PlasmidCodecs.TEXT.optionalFieldOf("name").forGetter(GameMenuEntryConfig::name), + MoreCodecs.listOrUnit(PlasmidCodecs.TEXT).optionalFieldOf("description").forGetter(GameMenuEntryConfig::description), + MoreCodecs.ITEM_STACK.optionalFieldOf("icon").forGetter(GameMenuEntryConfig::icon) + ).apply(i, GameMenuEntryConfig::new)); @Override public MenuEntry createEntry() { var game = new ConcurrentGamePortalBackend(this.game); - var gameConfig = GameConfigs.get(this.game); - - if (gameConfig == null) { - return new InvalidMenuEntry(game.getName()); - } else { - return new GameMenuEntry( - game, - this.name.orElse(GameConfig.name(gameConfig)), - this.description.orElse(gameConfig.description()), - this.icon.orElse(gameConfig.icon()) - ); - } + return new GameMenuEntry( + game, + this.name.orElse(GameConfig.name(this.game)), + this.description.orElse(this.game.value().description()), + this.icon.orElse(this.game.value().icon()) + ); } @Override diff --git a/src/main/java/xyz/nucleoid/plasmid/game/portal/menu/MenuEntryConfig.java b/src/main/java/xyz/nucleoid/plasmid/game/portal/menu/MenuEntryConfig.java index 44ad0c00..9f83cf77 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/portal/menu/MenuEntryConfig.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/portal/menu/MenuEntryConfig.java @@ -1,20 +1,20 @@ package xyz.nucleoid.plasmid.game.portal.menu; -import java.util.Optional; -import java.util.function.Function; - import com.mojang.datafixers.util.Either; import com.mojang.serialization.Codec; - import net.minecraft.util.Identifier; +import xyz.nucleoid.plasmid.game.config.GameConfig; import xyz.nucleoid.plasmid.registry.TinyRegistry; +import java.util.Optional; +import java.util.function.Function; + public interface MenuEntryConfig { TinyRegistry> REGISTRY = TinyRegistry.create(); Codec CODEC_OBJECT = REGISTRY.dispatchStable(MenuEntryConfig::codec, Function.identity()); - Codec CODEC = Codec.either(Identifier.CODEC, CODEC_OBJECT).xmap(either -> { - return either.map((identifier) -> new GameMenuEntryConfig(identifier, Optional.empty(), Optional.empty(), Optional.empty()), Function.identity()); + Codec CODEC = Codec.either(GameConfig.CODEC, CODEC_OBJECT).xmap(either -> { + return either.map((game) -> new GameMenuEntryConfig(game, Optional.empty(), Optional.empty(), Optional.empty()), Function.identity()); }, Either::right); static void register(Identifier key, Codec codec) { diff --git a/src/main/java/xyz/nucleoid/plasmid/game/portal/menu/MenuPortalBackend.java b/src/main/java/xyz/nucleoid/plasmid/game/portal/menu/MenuPortalBackend.java index 31d6d9b6..45c18770 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/portal/menu/MenuPortalBackend.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/portal/menu/MenuPortalBackend.java @@ -8,7 +8,6 @@ import net.minecraft.util.Util; import xyz.nucleoid.plasmid.game.GameSpace; import xyz.nucleoid.plasmid.game.config.GameConfig; -import xyz.nucleoid.plasmid.game.config.GameConfigs; import xyz.nucleoid.plasmid.game.portal.GamePortal.GuiProvider; import xyz.nucleoid.plasmid.game.portal.GamePortalBackend; import xyz.nucleoid.plasmid.game.portal.game.ConcurrentGamePortalBackend; @@ -85,19 +84,14 @@ public GuiProvider getGuiProvider() { private List buildGames(List configs) { var games = new ArrayList(configs.size()); for (var configEntry : configs) { - var game = new ConcurrentGamePortalBackend(configEntry.game()); - var gameConfig = GameConfigs.get(configEntry.game()); - - if (gameConfig != null) { - games.add(new GameMenuEntry( - game, - configEntry.name().orElse(GameConfig.name(gameConfig)), - configEntry.description().orElse(gameConfig.description()), - configEntry.icon().orElse(gameConfig.icon()) - )); - } else { - games.add(new InvalidMenuEntry(game.getName())); - } + var config = configEntry.game(); + var game = new ConcurrentGamePortalBackend(config); + games.add(new GameMenuEntry( + game, + configEntry.name().orElse(GameConfig.name(config)), + configEntry.description().orElse(config.value().description()), + configEntry.icon().orElse(config.value().icon()) + )); } return games; diff --git a/src/main/java/xyz/nucleoid/plasmid/game/portal/menu/MenuPortalConfig.java b/src/main/java/xyz/nucleoid/plasmid/game/portal/menu/MenuPortalConfig.java index 2378db27..22d9a8c5 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/portal/menu/MenuPortalConfig.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/portal/menu/MenuPortalConfig.java @@ -5,12 +5,14 @@ import com.mojang.serialization.codecs.RecordCodecBuilder; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; +import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.screen.ScreenTexts; import net.minecraft.server.MinecraftServer; import net.minecraft.text.Text; import net.minecraft.util.Identifier; import xyz.nucleoid.codecs.MoreCodecs; import xyz.nucleoid.plasmid.game.config.CustomValuesConfig; +import xyz.nucleoid.plasmid.game.config.GameConfig; import xyz.nucleoid.plasmid.game.portal.GamePortalBackend; import xyz.nucleoid.plasmid.game.portal.GamePortalConfig; import xyz.nucleoid.plasmid.util.PlasmidCodecs; @@ -28,15 +30,13 @@ public record MenuPortalConfig( CustomValuesConfig custom ) implements GamePortalConfig { - public static final Codec CODEC = RecordCodecBuilder.create(instance -> { - return instance.group( - PlasmidCodecs.TEXT.optionalFieldOf("name", ScreenTexts.EMPTY).forGetter(MenuPortalConfig::name), - MoreCodecs.listOrUnit(PlasmidCodecs.TEXT).optionalFieldOf("description", Collections.emptyList()).forGetter(MenuPortalConfig::description), - MoreCodecs.ITEM_STACK.optionalFieldOf("icon", new ItemStack(Items.GRASS_BLOCK)).forGetter(MenuPortalConfig::icon), - Entry.CODEC.listOf().fieldOf("games").forGetter(config -> config.games), - CustomValuesConfig.CODEC.optionalFieldOf("custom", CustomValuesConfig.empty()).forGetter(config -> config.custom) - ).apply(instance, MenuPortalConfig::new); - }); + public static final Codec CODEC = RecordCodecBuilder.create(i -> i.group( + PlasmidCodecs.TEXT.optionalFieldOf("name", ScreenTexts.EMPTY).forGetter(MenuPortalConfig::name), + MoreCodecs.listOrUnit(PlasmidCodecs.TEXT).optionalFieldOf("description", Collections.emptyList()).forGetter(MenuPortalConfig::description), + MoreCodecs.ITEM_STACK.optionalFieldOf("icon", new ItemStack(Items.GRASS_BLOCK)).forGetter(MenuPortalConfig::icon), + Entry.CODEC.listOf().fieldOf("games").forGetter(config -> config.games), + CustomValuesConfig.CODEC.optionalFieldOf("custom", CustomValuesConfig.empty()).forGetter(config -> config.custom) + ).apply(i, MenuPortalConfig::new)); @Override public GamePortalBackend createBackend(MinecraftServer server, Identifier id) { @@ -55,21 +55,19 @@ public Codec codec() { return CODEC; } - public record Entry(Identifier game, + public record Entry(RegistryEntry> game, Optional name, Optional> description, Optional icon) { - static final Codec CODEC_OBJECT = RecordCodecBuilder.create(instance -> { - return instance.group( - Identifier.CODEC.fieldOf("game").forGetter(entry -> entry.game), - PlasmidCodecs.TEXT.optionalFieldOf("name").forGetter(Entry::name), - MoreCodecs.listOrUnit(PlasmidCodecs.TEXT).optionalFieldOf("description").forGetter(Entry::description), - MoreCodecs.ITEM_STACK.optionalFieldOf("icon").forGetter(Entry::icon) - ).apply(instance, Entry::new); - }); + static final Codec CODEC_OBJECT = RecordCodecBuilder.create(i -> i.group( + GameConfig.CODEC.fieldOf("game").forGetter(entry -> entry.game), + PlasmidCodecs.TEXT.optionalFieldOf("name").forGetter(Entry::name), + MoreCodecs.listOrUnit(PlasmidCodecs.TEXT).optionalFieldOf("description").forGetter(Entry::description), + MoreCodecs.ITEM_STACK.optionalFieldOf("icon").forGetter(Entry::icon) + ).apply(i, Entry::new)); - public static final Codec CODEC = Codec.either(Identifier.CODEC, CODEC_OBJECT) - .xmap(either -> either.map((identifier) -> new Entry(identifier, Optional.empty(), Optional.empty(), Optional.empty()), Function.identity()), Either::right); + public static final Codec CODEC = Codec.either(GameConfig.CODEC, CODEC_OBJECT) + .xmap(either -> either.map((game) -> new Entry(game, Optional.empty(), Optional.empty(), Optional.empty()), Function.identity()), Either::right); } }