Skip to content

Instantly share code, notes, and snippets.

@misode
Created August 10, 2021 23:44
Show Gist options
  • Save misode/fbf59de0068e2c02152e717cf0bd7ae7 to your computer and use it in GitHub Desktop.
Save misode/fbf59de0068e2c02152e717cf0bd7ae7 to your computer and use it in GitHub Desktop.
Fabric mod using Mojang mappings to export worldgen settings, only tested on the 1.18 experimental snapshots.
package net.misode.worldgenexporter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DynamicOps;
import com.mojang.serialization.JsonOps;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.minecraft.core.Registry;
import net.minecraft.resources.RegistryWriteOps;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.dimension.DimensionType;
import net.minecraft.world.level.dimension.LevelStem;
import net.minecraft.world.level.levelgen.NoiseGeneratorSettings;
import net.minecraft.world.level.levelgen.carver.ConfiguredWorldCarver;
import net.minecraft.world.level.levelgen.feature.ConfiguredFeature;
import net.minecraft.world.level.levelgen.feature.ConfiguredStructureFeature;
import net.minecraft.world.level.levelgen.feature.structures.StructureTemplatePool;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureProcessorType;
import net.minecraft.world.level.levelgen.surfacebuilders.ConfiguredSurfaceBuilder;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
public class WorldgenExporter implements ModInitializer {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private MinecraftServer server;
DynamicOps<JsonElement> ops;
@Override
public void onInitialize() {
ServerLifecycleEvents.SERVER_STARTING.register(server -> {
this.server = server;
this.ops = RegistryWriteOps.create(JsonOps.INSTANCE, server.registryAccess());
Registry<LevelStem> dimensions = server.getWorldData().worldGenSettings().dimensions();
exportRegistry("dimension", dimensions, LevelStem.CODEC);
exportRegistry("dimension_type", Registry.DIMENSION_TYPE_REGISTRY, DimensionType.DIRECT_CODEC);
exportRegistry("worldgen/biome", Registry.BIOME_REGISTRY, Biome.DIRECT_CODEC);
exportRegistry("worldgen/configured_carver", Registry.CONFIGURED_CARVER_REGISTRY, ConfiguredWorldCarver.DIRECT_CODEC);
exportRegistry("worldgen/configured_feature", Registry.CONFIGURED_FEATURE_REGISTRY, ConfiguredFeature.DIRECT_CODEC);
exportRegistry("worldgen/configured_structure_feature", Registry.CONFIGURED_STRUCTURE_FEATURE_REGISTRY, ConfiguredStructureFeature.DIRECT_CODEC);
exportRegistry("worldgen/configured_surface_builder", Registry.CONFIGURED_SURFACE_BUILDER_REGISTRY, ConfiguredSurfaceBuilder.DIRECT_CODEC);
exportRegistry("worldgen/noise_settings", Registry.NOISE_GENERATOR_SETTINGS_REGISTRY, NoiseGeneratorSettings.DIRECT_CODEC);
exportRegistry("worldgen/processor_list", Registry.PROCESSOR_LIST_REGISTRY, StructureProcessorType.DIRECT_CODEC);
exportRegistry("worldgen/template_pool", Registry.TEMPLATE_POOL_REGISTRY, StructureTemplatePool.DIRECT_CODEC);
this.server.stopServer();
});
}
private <T> void exportRegistry(String type, ResourceKey<Registry<T>> resource, Codec<T> codec) {
Registry<T> registry = server.registryAccess().registryOrThrow(resource);
exportRegistry(type, registry, codec);
}
private <T> void exportRegistry(String type, Registry<T> registry, Codec<T> codec) {
Path path = Path.of("./out").resolve(type);
registry.entrySet().forEach(entry -> {
Path file = path.resolve(entry.getKey().location().getPath() + ".json");
try {
JsonElement element = codec.encode(entry.getValue(), ops, new JsonObject()).result().orElseThrow();
Files.createDirectories(file.getParent());
try (BufferedWriter writer = Files.newBufferedWriter(file, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
writer.write(GSON.toJson(element));
}
} catch (IOException e) {
System.err.println("Couldn't save " + file + ": " + e);
}
});
System.out.println("Exported " + type + "!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment