From 16a65ac3f31c9944f2ffa2632b467fdc32e88847 Mon Sep 17 00:00:00 2001 From: Patbox <39821509+Patbox@users.noreply.github.com> Date: Sun, 10 Nov 2024 13:05:30 +0100 Subject: [PATCH] Add hacky fallback support for gameconfigs --- gradle.properties | 2 +- .../plasmid/api/game/config/GameConfig.java | 23 +++++++++ .../xyz/nucleoid/plasmid/impl/Plasmid.java | 12 ++++- .../nucleoid/plasmid/impl/PlasmidConfig.java | 3 ++ .../plasmid/mixin/SimpleRegistryMixin.java | 47 +++++++++++++++++++ .../resources/data/plasmid/lang/en_us.json | 1 + src/main/resources/plasmid.mixins.json | 1 + 7 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 src/main/java/xyz/nucleoid/plasmid/mixin/SimpleRegistryMixin.java diff --git a/gradle.properties b/gradle.properties index 2310f2e9..ab339edc 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ org.gradle.jvmargs=-Xmx2G # Fabric Properties minecraft_version=1.21.3 yarn_mappings=1.21.3+build.2 -loader_version=0.16.7 +loader_version=0.16.8 # Dependencies fabric_version=0.107.0+1.21.3 diff --git a/src/main/java/xyz/nucleoid/plasmid/api/game/config/GameConfig.java b/src/main/java/xyz/nucleoid/plasmid/api/game/config/GameConfig.java index f8cd2c8f..47e1e923 100644 --- a/src/main/java/xyz/nucleoid/plasmid/api/game/config/GameConfig.java +++ b/src/main/java/xyz/nucleoid/plasmid/api/game/config/GameConfig.java @@ -1,7 +1,9 @@ package xyz.nucleoid.plasmid.api.game.config; import com.mojang.serialization.Codec; +import com.mojang.serialization.DataResult; import com.mojang.serialization.MapCodec; +import com.mojang.serialization.codecs.KeyDispatchCodec; import com.mojang.serialization.codecs.RecordCodecBuilder; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; @@ -9,6 +11,7 @@ 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; @@ -16,12 +19,15 @@ import xyz.nucleoid.plasmid.api.game.GameOpenProcedure; import xyz.nucleoid.plasmid.api.game.GameType; import xyz.nucleoid.plasmid.api.util.PlasmidCodecs; +import xyz.nucleoid.plasmid.impl.Plasmid; +import xyz.nucleoid.plasmid.impl.PlasmidConfig; import xyz.nucleoid.server.translations.api.language.ServerLanguage; import xyz.nucleoid.server.translations.api.language.ServerLanguageDefinition; import java.util.Collections; import java.util.List; import java.util.Optional; +import java.util.function.Consumer; public record GameConfig( GameType type, @@ -33,6 +39,23 @@ public record GameConfig( C config ) { public static final Codec> DIRECT_CODEC = GameType.REGISTRY.dispatch(GameConfig::type, GameConfig::createTypedCodec); + @Deprecated + public static final Codec> REGISTRY_CODEC = Codec.lazyInitialized(() -> { + if (!PlasmidConfig.get().ignoreInvalidGames()) { + return DIRECT_CODEC; + } + var type = (GameType) GameType.REGISTRY.get(Identifier.of(Plasmid.ID, "invalid")); + + return Codec.withAlternative(DIRECT_CODEC, Codec.unit(() -> new GameConfig<>( + type, + null, + null, + null, + null, + null, + "" + ))); + }); public static final Codec>> CODEC = RegistryElementCodec.of(GameConfigs.REGISTRY_KEY, DIRECT_CODEC); public GameOpenProcedure openProcedure(MinecraftServer server) { diff --git a/src/main/java/xyz/nucleoid/plasmid/impl/Plasmid.java b/src/main/java/xyz/nucleoid/plasmid/impl/Plasmid.java index a0c471cd..754d1065 100644 --- a/src/main/java/xyz/nucleoid/plasmid/impl/Plasmid.java +++ b/src/main/java/xyz/nucleoid/plasmid/impl/Plasmid.java @@ -1,5 +1,7 @@ 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; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; @@ -11,12 +13,16 @@ import net.minecraft.registry.DynamicRegistryManager; import net.minecraft.resource.ResourceManager; 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.GameType; import xyz.nucleoid.plasmid.impl.game.composite.RandomGame; import xyz.nucleoid.plasmid.impl.game.composite.RandomGameConfig; @@ -42,7 +48,7 @@ public final class Plasmid implements ModInitializer { @Override public void onInitialize() { - DynamicRegistries.register(GameConfigs.REGISTRY_KEY, GameConfig.DIRECT_CODEC); + DynamicRegistries.register(GameConfigs.REGISTRY_KEY, GameConfig.REGISTRY_CODEC); GamePortalConfig.register(Identifier.of(ID, "single_game"), SingleGamePortalConfig.CODEC); GamePortalConfig.register(Identifier.of(ID, "new_game"), NewGamePortalConfig.CODEC); @@ -56,6 +62,10 @@ public void onInitialize() { MenuEntryConfig.register(Identifier.of(ID, "portal"), PortalEntryConfig.CODEC); 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()); + throw new GameOpenException(Text.translatable("text.plasmid.map.open.invalid_game", id != null ? id.toString() : context.game())); + }); this.registerCallbacks(); diff --git a/src/main/java/xyz/nucleoid/plasmid/impl/PlasmidConfig.java b/src/main/java/xyz/nucleoid/plasmid/impl/PlasmidConfig.java index 70989af4..bc587049 100644 --- a/src/main/java/xyz/nucleoid/plasmid/impl/PlasmidConfig.java +++ b/src/main/java/xyz/nucleoid/plasmid/impl/PlasmidConfig.java @@ -19,6 +19,7 @@ import java.util.Optional; public record PlasmidConfig( + boolean ignoreInvalidGames, Optional userFacingPackAddress, Optional webServerConfig ) { @@ -28,6 +29,7 @@ public record PlasmidConfig( private static final Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( + Codec.BOOL.optionalFieldOf("ignore_invalid_game_configs", false).forGetter(PlasmidConfig::ignoreInvalidGames), Codec.STRING.optionalFieldOf("resource_pack_address").forGetter(PlasmidConfig::userFacingPackAddress), PlasmidWebServer.Config.CODEC.optionalFieldOf("web_server").forGetter(PlasmidConfig::webServerConfig) ).apply(instance, PlasmidConfig::new) @@ -37,6 +39,7 @@ public record PlasmidConfig( private PlasmidConfig() { this( + false, Optional.of("http://127.0.0.1:25566/" + PlasmidWebServer.RESOURCE_PACKS_ENDPOINT), Optional.of(new PlasmidWebServer.Config(25566)) ); diff --git a/src/main/java/xyz/nucleoid/plasmid/mixin/SimpleRegistryMixin.java b/src/main/java/xyz/nucleoid/plasmid/mixin/SimpleRegistryMixin.java new file mode 100644 index 00000000..eb44be6f --- /dev/null +++ b/src/main/java/xyz/nucleoid/plasmid/mixin/SimpleRegistryMixin.java @@ -0,0 +1,47 @@ +package xyz.nucleoid.plasmid.mixin; + +import net.minecraft.registry.Registry; +import net.minecraft.registry.RegistryKey; +import net.minecraft.registry.SimpleRegistry; +import net.minecraft.registry.entry.RegistryEntry; +import net.minecraft.registry.entry.RegistryEntryInfo; +import net.minecraft.util.Identifier; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import xyz.nucleoid.plasmid.api.game.GameType; +import xyz.nucleoid.plasmid.api.game.config.CustomValuesConfig; +import xyz.nucleoid.plasmid.api.game.config.GameConfig; +import xyz.nucleoid.plasmid.api.game.config.GameConfigs; +import xyz.nucleoid.plasmid.impl.Plasmid; +import xyz.nucleoid.plasmid.impl.PlasmidConfig; + +import java.util.Map; +import java.util.stream.Collectors; + +@Deprecated +@Mixin(SimpleRegistry.class) +public abstract class SimpleRegistryMixin { + @Shadow public abstract RegistryKey> getKey(); + + @Shadow @Final private Map, RegistryEntry.Reference> keyToEntry; + + @Shadow public abstract RegistryEntry.Reference add(RegistryKey key, Object value, RegistryEntryInfo info); + + @Inject(method = "freeze", at = @At("HEAD")) + private void maybeRegisterInvalidConfigs(CallbackInfoReturnable> cir) { + if (!PlasmidConfig.get().ignoreInvalidGames() || !this.getKey().equals(GameConfigs.REGISTRY_KEY)) { + return; + } + var type = (GameType) GameType.REGISTRY.get(Identifier.of(Plasmid.ID, "invalid")); + + var keys = this.keyToEntry.entrySet().stream().filter((entry) -> !entry.getValue().hasKeyAndValue()).toList(); + for (var key : keys) { + Plasmid.LOGGER.error("Something depends on non-existing game config '{}'!", key.getKey().getValue()); + this.add(key.getKey(), new GameConfig<>(type, null, null, null, null, CustomValuesConfig.empty(), key.getKey().getValue().toString()),RegistryEntryInfo.DEFAULT); + } + } +} diff --git a/src/main/resources/data/plasmid/lang/en_us.json b/src/main/resources/data/plasmid/lang/en_us.json index df41fd87..033bfddf 100644 --- a/src/main/resources/data/plasmid/lang/en_us.json +++ b/src/main/resources/data/plasmid/lang/en_us.json @@ -93,6 +93,7 @@ "text.plasmid.map.leave.success": "You have left '%s'!", "text.plasmid.map.map_not_here": "No map found here", "text.plasmid.map.open.error": "An unexpected error occurred while trying to open workspace!", + "text.plasmid.map.open.invalid_game": "Tried to open invalid game '%s'!", "text.plasmid.map.open.invalid_generator_config": "Invalid generator config! %s", "text.plasmid.map.open.join_command": "/map join %s", "text.plasmid.map.open.map_already_exists": "Map with id '%s' already exists!", diff --git a/src/main/resources/plasmid.mixins.json b/src/main/resources/plasmid.mixins.json index d37cb95f..30ec7345 100644 --- a/src/main/resources/plasmid.mixins.json +++ b/src/main/resources/plasmid.mixins.json @@ -4,6 +4,7 @@ "package": "xyz.nucleoid.plasmid.mixin", "compatibilityLevel": "JAVA_17", "mixins": [ + "SimpleRegistryMixin", "chat.PlayerEntityMixin", "chat.PlayerListS2CPacketEntryAccessor", "chat.ServerPlayerEntityMixin",