mirror of
https://github.com/italicsjenga/valence.git
synced 2024-12-23 14:31:30 +11:00
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 <ryanj00a@gmail.com>
This commit is contained in:
parent
11ba70586e
commit
a68792e605
|
@ -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 {
|
||||
|
|
|
@ -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/
|
||||
|
|
|
@ -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 {
|
|||
}
|
||||
}
|
||||
|
||||
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.");
|
||||
System.exit(0);
|
||||
server.shutdown();
|
||||
});
|
||||
}
|
||||
|
||||
public interface Extractor {
|
||||
|
|
|
@ -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<NbtElement> 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<DynamicRegistryManager> codec = SerializableRegistries.CODEC;
|
||||
//DynamicRegistryManager.get(net.minecraft.registry.RegistryKey<? extends net.minecraft.registry.Registry<? extends E>>) method.
|
||||
|
||||
NbtElement nbtElement = Util.getResult(codec.encodeStart(REGISTRY_OPS, registryManager), (error) -> new EncoderException("Failed to encode: " + error + " " + registryManager));
|
||||
return (NbtCompound) nbtElement;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue