Skip to content

Commit

Permalink
Add hacky fallback support for gameconfigs
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Nov 10, 2024
1 parent 51f38aa commit 16a65ac
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/xyz/nucleoid/plasmid/api/game/config/GameConfig.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
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;
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;
import xyz.nucleoid.plasmid.api.game.GameOpenContext;
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<C>(
GameType<C> type,
Expand All @@ -33,6 +39,23 @@ public record GameConfig<C>(
C config
) {
public static final Codec<GameConfig<?>> DIRECT_CODEC = GameType.REGISTRY.dispatch(GameConfig::type, GameConfig::createTypedCodec);
@Deprecated
public static final Codec<GameConfig<?>> REGISTRY_CODEC = Codec.lazyInitialized(() -> {
if (!PlasmidConfig.get().ignoreInvalidGames()) {
return DIRECT_CODEC;
}
var type = (GameType<Object>) 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<RegistryEntry<GameConfig<?>>> CODEC = RegistryElementCodec.of(GameConfigs.REGISTRY_KEY, DIRECT_CODEC);

public GameOpenProcedure openProcedure(MinecraftServer server) {
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/xyz/nucleoid/plasmid/impl/Plasmid.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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();

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/xyz/nucleoid/plasmid/impl/PlasmidConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Optional;

public record PlasmidConfig(
boolean ignoreInvalidGames,
Optional<String> userFacingPackAddress,
Optional<PlasmidWebServer.Config> webServerConfig
) {
Expand All @@ -28,6 +29,7 @@ public record PlasmidConfig(

private static final Codec<PlasmidConfig> 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)
Expand All @@ -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))
);
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/xyz/nucleoid/plasmid/mixin/SimpleRegistryMixin.java
Original file line number Diff line number Diff line change
@@ -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<? extends Registry<Object>> getKey();

@Shadow @Final private Map<RegistryKey<Object>, RegistryEntry.Reference<Object>> keyToEntry;

@Shadow public abstract RegistryEntry.Reference<Object> add(RegistryKey<Object> key, Object value, RegistryEntryInfo info);

@Inject(method = "freeze", at = @At("HEAD"))
private void maybeRegisterInvalidConfigs(CallbackInfoReturnable<Registry<Object>> cir) {
if (!PlasmidConfig.get().ignoreInvalidGames() || !this.getKey().equals(GameConfigs.REGISTRY_KEY)) {
return;
}
var type = (GameType<Object>) 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);
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/data/plasmid/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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!",
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/plasmid.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"package": "xyz.nucleoid.plasmid.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"SimpleRegistryMixin",
"chat.PlayerEntityMixin",
"chat.PlayerListS2CPacketEntryAccessor",
"chat.ServerPlayerEntityMixin",
Expand Down

0 comments on commit 16a65ac

Please sign in to comment.