From a68792e605e50443a7318aa4e8d4ad2e879b3532 Mon Sep 17 00:00:00 2001 From: Bafbi Date: Mon, 10 Apr 2023 10:39:03 +0200 Subject: [PATCH] Registry codec extractor (#316) ## Description - #311 . - Update the script to cp all the file of the output dir . ## Change to the gradle project - Adding the fabric API . - The serveur now need to start starting . ## Test plan Do the same as before and compare the 1.9.4 output file and my generated one . --------- Co-authored-by: Ryan Johnson --- extractor/build.gradle | 3 +- extractor/copy_extractor_output.sh | 4 +-- .../main/java/rs/valence/extractor/Main.java | 27 +++++++++++++-- .../valence/extractor/extractors/Codec.java | 34 +++++++++++++++++++ 4 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 extractor/src/main/java/rs/valence/extractor/extractors/Codec.java diff --git a/extractor/build.gradle b/extractor/build.gradle index 83f8bb9..c570c2e 100644 --- a/extractor/build.gradle +++ b/extractor/build.gradle @@ -16,8 +16,7 @@ dependencies { minecraft "com.mojang:minecraft:${project.minecraft_version}" mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" - - // modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" + modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" } processResources { diff --git a/extractor/copy_extractor_output.sh b/extractor/copy_extractor_output.sh index 4c946d5..1ab7351 100755 --- a/extractor/copy_extractor_output.sh +++ b/extractor/copy_extractor_output.sh @@ -2,5 +2,5 @@ cd "$(dirname "$0")" || return -rm ../extracted/*.json -cp run/valence_extractor_output/*.json ../extracted/ +rm ../extracted/* +cp run/valence_extractor_output/* ../extracted/ diff --git a/extractor/src/main/java/rs/valence/extractor/Main.java b/extractor/src/main/java/rs/valence/extractor/Main.java index 96c7fce..b6d5e2b 100644 --- a/extractor/src/main/java/rs/valence/extractor/Main.java +++ b/extractor/src/main/java/rs/valence/extractor/Main.java @@ -2,7 +2,10 @@ package rs.valence.extractor; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; +import io.netty.handler.codec.EncoderException; import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; +import net.minecraft.nbt.NbtIo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import rs.valence.extractor.extractors.*; @@ -70,8 +73,28 @@ public class Main implements ModInitializer { } } - LOGGER.info("Done."); - System.exit(0); + ServerLifecycleEvents.SERVER_STARTING.register(server -> { + LOGGER.info("Server starting, Extracting registry codec..."); + var codecExtractor = new Codec(server); + + try { + var out = outputDirectory.resolve(codecExtractor.fileName()); + var compound = codecExtractor.extract(); + // read the compound byte-wise and write it to the file + try { + NbtIo.write(compound, out.toFile()); + } catch (IOException var3) { + throw new EncoderException(var3); + } + + LOGGER.info("Wrote " + out.toAbsolutePath()); + } catch (Exception e) { + LOGGER.error("Extractor for \"" + codecExtractor.fileName() + "\" failed.", e); + } + + LOGGER.info("Done."); + server.shutdown(); + }); } public interface Extractor { diff --git a/extractor/src/main/java/rs/valence/extractor/extractors/Codec.java b/extractor/src/main/java/rs/valence/extractor/extractors/Codec.java new file mode 100644 index 0000000..521e2c2 --- /dev/null +++ b/extractor/src/main/java/rs/valence/extractor/extractors/Codec.java @@ -0,0 +1,34 @@ +package rs.valence.extractor.extractors; + +import io.netty.handler.codec.EncoderException; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtElement; +import net.minecraft.nbt.NbtOps; +import net.minecraft.registry.DynamicRegistryManager; +import net.minecraft.registry.Registries; +import net.minecraft.registry.RegistryOps; +import net.minecraft.registry.SerializableRegistries; +import net.minecraft.server.MinecraftServer; +import net.minecraft.util.Util; + +public class Codec { + + private static final RegistryOps REGISTRY_OPS= RegistryOps.of(NbtOps.INSTANCE, DynamicRegistryManager.of(Registries.REGISTRIES)); + private final DynamicRegistryManager.Immutable registryManager; + + public Codec(MinecraftServer server) { + this.registryManager = server.getRegistryManager(); + } + + public String fileName() { + return "registry_codec.dat"; + } + + public NbtCompound extract() { + com.mojang.serialization.Codec codec = SerializableRegistries.CODEC; + //DynamicRegistryManager.get(net.minecraft.registry.RegistryKey>) method. + + NbtElement nbtElement = Util.getResult(codec.encodeStart(REGISTRY_OPS, registryManager), (error) -> new EncoderException("Failed to encode: " + error + " " + registryManager)); + return (NbtCompound) nbtElement; + } +}