mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-16 13:23:07 +00:00
implement namespaced registries
This commit is contained in:
@@ -24,7 +24,7 @@ import com.dfsek.terra.api.entity.CommandSender;
|
||||
|
||||
import com.dfsek.terra.api.event.events.platform.CommandRegistrationEvent;
|
||||
|
||||
import com.dfsek.terra.api.util.generic.Construct;
|
||||
import com.dfsek.terra.fabric.data.Codecs;
|
||||
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.minecraft.server.command.ServerCommandSource;
|
||||
@@ -33,9 +33,6 @@ import net.minecraft.util.registry.Registry;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.dfsek.terra.fabric.generation.FabricChunkGeneratorWrapper;
|
||||
import com.dfsek.terra.fabric.generation.TerraBiomeSource;
|
||||
|
||||
|
||||
public class FabricEntryPoint implements ModInitializer {
|
||||
private static final Logger logger = LoggerFactory.getLogger(FabricEntryPoint.class);
|
||||
@@ -51,8 +48,8 @@ public class FabricEntryPoint implements ModInitializer {
|
||||
public void onInitialize() {
|
||||
logger.info("Initializing Terra Fabric mod...");
|
||||
// register the things
|
||||
Registry.register(Registry.CHUNK_GENERATOR, new Identifier("terra:terra"), FabricChunkGeneratorWrapper.CODEC);
|
||||
Registry.register(Registry.BIOME_SOURCE, new Identifier("terra:terra"), TerraBiomeSource.CODEC);
|
||||
Registry.register(Registry.CHUNK_GENERATOR, new Identifier("terra:terra"), Codecs.CODEC);
|
||||
Registry.register(Registry.BIOME_SOURCE, new Identifier("terra:terra"), Codecs.TERRA_BIOME_SOURCE);
|
||||
|
||||
FabricServerCommandManager<CommandSender> manager = new FabricServerCommandManager<>(
|
||||
CommandExecutionCoordinator.simpleCoordinator(),
|
||||
|
||||
@@ -74,7 +74,7 @@ public class PlatformImpl extends AbstractPlatform {
|
||||
|
||||
worlds.forEach(world -> {
|
||||
FabricChunkGeneratorWrapper chunkGeneratorWrapper = ((FabricChunkGeneratorWrapper) world.getChunkManager().getChunkGenerator());
|
||||
chunkGeneratorWrapper.setPack(getConfigRegistry().get(chunkGeneratorWrapper.getPack().getID()).orElseThrow());
|
||||
chunkGeneratorWrapper.setPack(getConfigRegistry().get(chunkGeneratorWrapper.getPack().getRegistryKey()).orElseThrow());
|
||||
});
|
||||
|
||||
return succeed;
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.dfsek.terra.fabric.data;
|
||||
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.registry.key.RegistryKey;
|
||||
|
||||
import com.dfsek.terra.fabric.FabricEntryPoint;
|
||||
|
||||
import com.dfsek.terra.fabric.generation.FabricChunkGeneratorWrapper;
|
||||
import com.dfsek.terra.fabric.generation.TerraBiomeSource;
|
||||
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||
import net.minecraft.util.dynamic.RegistryLookupCodec;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
import net.minecraft.world.gen.chunk.ChunkGeneratorSettings;
|
||||
|
||||
|
||||
public final class Codecs {
|
||||
public static final Codec<RegistryKey> REGISTRY_KEY = RecordCodecBuilder
|
||||
.create(registryKey -> registryKey.group(Codec.STRING.fieldOf("namespace")
|
||||
.forGetter(RegistryKey::getNamespace),
|
||||
Codec.STRING.fieldOf("id")
|
||||
.forGetter(RegistryKey::getID))
|
||||
.apply(registryKey, registryKey.stable(RegistryKey::of)));
|
||||
|
||||
public static final Codec<ConfigPack> CONFIG_PACK = RecordCodecBuilder
|
||||
.create(config -> config.group(REGISTRY_KEY.fieldOf("pack")
|
||||
.forGetter(ConfigPack::getRegistryKey))
|
||||
.apply(config, config.stable(id -> FabricEntryPoint.getPlatform()
|
||||
.getConfigRegistry()
|
||||
.get(id)
|
||||
.orElseThrow(() -> new IllegalArgumentException(
|
||||
"No such config pack " +
|
||||
id)))));
|
||||
|
||||
public static final Codec<TerraBiomeSource> TERRA_BIOME_SOURCE = RecordCodecBuilder
|
||||
.create(instance -> instance.group(RegistryLookupCodec.of(Registry.BIOME_KEY)
|
||||
.forGetter(TerraBiomeSource::getBiomeRegistry),
|
||||
Codec.LONG.fieldOf("seed").stable()
|
||||
.forGetter(TerraBiomeSource::getSeed),
|
||||
CONFIG_PACK.fieldOf("pack").stable()
|
||||
.forGetter(TerraBiomeSource::getPack))
|
||||
.apply(instance, instance.stable(
|
||||
TerraBiomeSource::new)));
|
||||
|
||||
public static final Codec<FabricChunkGeneratorWrapper> CODEC = RecordCodecBuilder.create(
|
||||
instance -> instance.group(
|
||||
TERRA_BIOME_SOURCE.fieldOf("biome_source")
|
||||
.forGetter(FabricChunkGeneratorWrapper::getBiomeSource),
|
||||
Codec.LONG.fieldOf("seed").stable()
|
||||
.forGetter(FabricChunkGeneratorWrapper::getSeed),
|
||||
CONFIG_PACK.fieldOf("pack").stable()
|
||||
.forGetter(FabricChunkGeneratorWrapper::getPack),
|
||||
ChunkGeneratorSettings.REGISTRY_CODEC.fieldOf("settings")
|
||||
.forGetter(FabricChunkGeneratorWrapper::getSettingsSupplier)
|
||||
).apply(instance, instance.stable(FabricChunkGeneratorWrapper::new))
|
||||
);
|
||||
}
|
||||
@@ -18,19 +18,17 @@
|
||||
package com.dfsek.terra.fabric.generation;
|
||||
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.world.ServerWorld;
|
||||
import com.dfsek.terra.api.world.chunk.generation.ChunkGenerator;
|
||||
import com.dfsek.terra.api.world.chunk.generation.ProtoChunk;
|
||||
import com.dfsek.terra.api.world.chunk.generation.ProtoWorld;
|
||||
import com.dfsek.terra.api.world.chunk.generation.stage.Chunkified;
|
||||
import com.dfsek.terra.api.world.chunk.generation.util.GeneratorWrapper;
|
||||
import com.dfsek.terra.fabric.FabricEntryPoint;
|
||||
import com.dfsek.terra.fabric.data.Codecs;
|
||||
import com.dfsek.terra.fabric.mixin.access.StructureAccessorAccessor;
|
||||
|
||||
import com.dfsek.terra.fabric.util.FabricAdapter;
|
||||
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.entity.SpawnGroup;
|
||||
import net.minecraft.util.collection.Pool;
|
||||
@@ -61,30 +59,7 @@ import java.util.function.Supplier;
|
||||
|
||||
|
||||
public class FabricChunkGeneratorWrapper extends net.minecraft.world.gen.chunk.ChunkGenerator implements GeneratorWrapper {
|
||||
public static final Codec<ConfigPack> PACK_CODEC = RecordCodecBuilder.create(
|
||||
config -> config.group(
|
||||
Codec.STRING.fieldOf("pack")
|
||||
.forGetter(ConfigPack::getID)
|
||||
).apply(config, config.stable(id -> FabricEntryPoint.getPlatform()
|
||||
.getConfigRegistry()
|
||||
.get(id)
|
||||
.orElseThrow(
|
||||
() -> new IllegalArgumentException(
|
||||
"No such config pack " +
|
||||
id)))));
|
||||
private static final Logger logger = LoggerFactory.getLogger(FabricChunkGeneratorWrapper.class);
|
||||
public static final Codec<FabricChunkGeneratorWrapper> CODEC = RecordCodecBuilder.create(
|
||||
instance -> instance.group(
|
||||
TerraBiomeSource.CODEC.fieldOf("biome_source")
|
||||
.forGetter(generator -> generator.biomeSource),
|
||||
Codec.LONG.fieldOf("seed").stable()
|
||||
.forGetter(generator -> generator.seed),
|
||||
PACK_CODEC.fieldOf("pack").stable()
|
||||
.forGetter(generator -> generator.pack),
|
||||
ChunkGeneratorSettings.REGISTRY_CODEC.fieldOf("settings")
|
||||
.forGetter(generator -> generator.settingsSupplier)
|
||||
).apply(instance, instance.stable(FabricChunkGeneratorWrapper::new))
|
||||
);
|
||||
|
||||
private final long seed;
|
||||
private final TerraBiomeSource biomeSource;
|
||||
@@ -107,7 +82,7 @@ public class FabricChunkGeneratorWrapper extends net.minecraft.world.gen.chunk.C
|
||||
|
||||
@Override
|
||||
protected Codec<? extends net.minecraft.world.gen.chunk.ChunkGenerator> getCodec() {
|
||||
return CODEC;
|
||||
return Codecs.CODEC;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -251,4 +226,17 @@ public class FabricChunkGeneratorWrapper extends net.minecraft.world.gen.chunk.C
|
||||
public ChunkGenerator getHandle() {
|
||||
return delegate;
|
||||
}
|
||||
|
||||
public long getSeed() {
|
||||
return seed;
|
||||
}
|
||||
|
||||
public Supplier<ChunkGeneratorSettings> getSettingsSupplier() {
|
||||
return settingsSupplier;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TerraBiomeSource getBiomeSource() {
|
||||
return biomeSource;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,47 +17,33 @@
|
||||
|
||||
package com.dfsek.terra.fabric.generation;
|
||||
|
||||
import com.dfsek.terra.fabric.data.Codecs;
|
||||
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.dynamic.RegistryLookupCodec;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
import net.minecraft.world.biome.source.BiomeSource;
|
||||
import net.minecraft.world.biome.source.util.MultiNoiseUtil.MultiNoiseSampler;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.world.biome.Biome;
|
||||
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
|
||||
import com.dfsek.terra.fabric.FabricEntryPoint;
|
||||
import com.dfsek.terra.fabric.util.FabricUtil;
|
||||
|
||||
|
||||
public class TerraBiomeSource extends BiomeSource {
|
||||
public static final Codec<ConfigPack> PACK_CODEC = (RecordCodecBuilder.create(config -> config.group(
|
||||
Codec.STRING.fieldOf("pack").forGetter(ConfigPack::getID)
|
||||
)
|
||||
.apply(config, config.stable(
|
||||
id -> FabricEntryPoint.getPlatform()
|
||||
.getConfigRegistry()
|
||||
.get(id)
|
||||
.orElseThrow(
|
||||
() -> new IllegalArgumentException(
|
||||
"No such config pack " +
|
||||
id))))));
|
||||
public static final Codec<TerraBiomeSource> CODEC = RecordCodecBuilder.create(instance -> instance.group(
|
||||
RegistryLookupCodec.of(Registry.BIOME_KEY).forGetter(source -> source.biomeRegistry),
|
||||
Codec.LONG.fieldOf("seed").stable().forGetter(source -> source.seed),
|
||||
PACK_CODEC.fieldOf("pack").stable().forGetter(source -> source.pack))
|
||||
.apply(instance, instance.stable(
|
||||
TerraBiomeSource::new)));
|
||||
|
||||
private final Registry<net.minecraft.world.biome.Biome> biomeRegistry;
|
||||
private final long seed;
|
||||
private ConfigPack pack;
|
||||
|
||||
private final Map<Biome, net.minecraft.world.biome.Biome> terraToMinecraft = new HashMap<>();
|
||||
|
||||
public TerraBiomeSource(Registry<net.minecraft.world.biome.Biome> biomes, long seed, ConfigPack pack) {
|
||||
super(biomes.stream()
|
||||
.filter(biome -> Objects.requireNonNull(biomes.getId(biome))
|
||||
@@ -75,7 +61,7 @@ public class TerraBiomeSource extends BiomeSource {
|
||||
|
||||
@Override
|
||||
protected Codec<? extends BiomeSource> getCodec() {
|
||||
return CODEC;
|
||||
return Codecs.TERRA_BIOME_SOURCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -86,10 +72,23 @@ public class TerraBiomeSource extends BiomeSource {
|
||||
@Override
|
||||
public net.minecraft.world.biome.Biome getBiome(int biomeX, int biomeY, int biomeZ, MultiNoiseSampler noiseSampler) {
|
||||
Biome biome = pack.getBiomeProvider().getBiome(biomeX << 2, biomeZ << 2, seed);
|
||||
return biomeRegistry.get(new Identifier("terra", FabricUtil.createBiomeID(pack, biome.getID())));
|
||||
return terraToMinecraft.computeIfAbsent(biome, b -> biomeRegistry
|
||||
.get(new Identifier("terra", FabricUtil.createBiomeID(pack, pack.getKey(b.getID())))));
|
||||
}
|
||||
|
||||
public BiomeProvider getProvider() {
|
||||
return pack.getBiomeProvider();
|
||||
}
|
||||
|
||||
public Registry<net.minecraft.world.biome.Biome> getBiomeRegistry() {
|
||||
return biomeRegistry;
|
||||
}
|
||||
|
||||
public ConfigPack getPack() {
|
||||
return pack;
|
||||
}
|
||||
|
||||
public long getSeed() {
|
||||
return seed;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ public abstract class GeneratorOptionsMixin {
|
||||
prop = prop.substring(prop.indexOf(":") + 1);
|
||||
|
||||
String finalProp = prop;
|
||||
ConfigPack config = main.getConfigRegistry().get(prop).orElseThrow(() -> new IllegalArgumentException(
|
||||
ConfigPack config = main.getConfigRegistry().tryGet(prop).orElseThrow(() -> new IllegalArgumentException(
|
||||
"No such pack " + finalProp));
|
||||
|
||||
cir.setReturnValue(
|
||||
|
||||
@@ -54,8 +54,9 @@ public final class FabricUtil {
|
||||
private static final Map<RegistryKey<net.minecraft.world.biome.Biome>, List<RegistryKey<net.minecraft.world.biome.Biome>>>
|
||||
terraVanillaBiomes = new HashMap<>();
|
||||
|
||||
public static String createBiomeID(ConfigPack pack, String biomeID) {
|
||||
return pack.getID().toLowerCase() + "/" + biomeID.toLowerCase(Locale.ROOT);
|
||||
public static String createBiomeID(ConfigPack pack, com.dfsek.terra.api.registry.key.RegistryKey biomeID) {
|
||||
return pack.getID()
|
||||
.toLowerCase() + "/" + biomeID.getNamespace().toLowerCase(Locale.ROOT) + "/" + biomeID.getID().toLowerCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,7 +65,8 @@ public final class FabricUtil {
|
||||
* @param biome The Terra BiomeBuilder.
|
||||
* @param pack The ConfigPack this biome belongs to.
|
||||
*/
|
||||
public static void registerBiome(Biome biome, ConfigPack pack, DynamicRegistryManager registryManager, String id) {
|
||||
public static void registerBiome(Biome biome, ConfigPack pack, DynamicRegistryManager registryManager,
|
||||
com.dfsek.terra.api.registry.key.RegistryKey id) {
|
||||
// BiomeTemplate template = biome.getTemplate();
|
||||
Map<String, Integer> colors = new HashMap<>(); // template.getColors();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user