feat: build Fabric biomes, dimensions and timelines from environment attributes

This commit is contained in:
Zoe Gidiere committed 2026-11-09 16:34:50 -07:00
1 parent 4e37c361ea
commit bebcb775fc
59 files changed
+4773 -1264

No files matched your search

@@ -25,6 +25,7 @@ import java.util.stream.Stream;
import com.dfsek.terra.addon.EphemeralAddon;
import com.dfsek.terra.api.addon.BaseAddon;
import com.dfsek.terra.lifecycle.util.LifecycleBiomeUtil;
import com.dfsek.terra.lifecycle.util.LifecycleUtil;
import com.dfsek.terra.mod.CommonPlatform;
import com.dfsek.terra.mod.ModPlatform;
import com.dfsek.terra.mod.generation.MinecraftChunkGeneratorWrapper;
@@ -82,22 +83,32 @@ public abstract class LifecyclePlatform extends ModPlatform {
@Override
public boolean reload() {
getTerraConfig().load(this);
boolean succeed = loadConfigPacks();
// Old packs keep their parents until the reloaded packs are prepared and in the chunk generators.
attributeParents().beginPackReload();
boolean succeed;
try {
succeed = loadConfigPacks();
if(server != null) {
LifecycleBiomeUtil.registerBiomes(server.registryAccess().lookupOrThrow(Registries.BIOME));
server.reloadResources(server.getPackRepository().getSelectedIds()).exceptionally(throwable -> {
LOGGER.warn("Failed to execute reload", throwable);
return null;
}).join();
server.getAllLevels().forEach(world -> {
if(world.getChunkSource().getGenerator() instanceof MinecraftChunkGeneratorWrapper chunkGeneratorWrapper) {
getConfigRegistry().get(chunkGeneratorWrapper.getPack().getRegistryKey()).ifPresent(pack -> {
chunkGeneratorWrapper.setPack(pack);
LOGGER.info("Replaced pack in chunk generator for world {}", world);
});
}
});
if(server != null) {
LifecycleBiomeUtil.registerBiomes(server.registryAccess().lookupOrThrow(Registries.BIOME),
LifecycleUtil.biomeFactory(this));
server.reloadResources(server.getPackRepository().getSelectedIds()).exceptionally(throwable -> {
LOGGER.warn("Failed to execute reload", throwable);
return null;
}).join();
server.getAllLevels().forEach(world -> {
if(world.getChunkSource().getGenerator() instanceof MinecraftChunkGeneratorWrapper chunkGeneratorWrapper) {
getConfigRegistry().get(chunkGeneratorWrapper.getPack().getRegistryKey()).ifPresent(pack -> {
chunkGeneratorWrapper.setPack(pack);
LOGGER.info("Replaced pack in chunk generator for world {}", world);
});
}
});
LOGGER.info("Packs reloaded; dimension types, generation settings, timelines and existing biomes need a restart " +
"or new world.");
}
} finally {
attributeParents().endPackReload();
}
return succeed;
}
@@ -0,0 +1,28 @@
package com.dfsek.terra.lifecycle.mixin;
import net.minecraft.core.HolderLookup;
import net.minecraft.resources.RegistryLoadTask;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import java.util.List;
import com.dfsek.terra.lifecycle.util.LoadContextHolder;
@Mixin(RegistryLoadTask.class)
public abstract class RegistryLoadTaskMixin implements LoadContextHolder {
@Unique
private @Nullable List<HolderLookup.RegistryLookup<?>> terra$contextRegistries;
@Override
public @Nullable List<HolderLookup.RegistryLookup<?>> terra_contextRegistries() {
return terra$contextRegistries;
}
@Override
public void terra_setContextRegistries(List<HolderLookup.RegistryLookup<?>> contextRegistries) {
this.terra$contextRegistries = contextRegistries;
}
}
@@ -1,11 +1,13 @@
package com.dfsek.terra.lifecycle.mixin.lifecycle;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.WritableRegistry;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.RegistryDataLoader;
import net.minecraft.resources.RegistryLoadTask;
import net.minecraft.resources.RegistryOps;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceManagerRegistryLoadTask;
import net.minecraft.world.item.enchantment.Enchantment;
@@ -14,6 +16,7 @@ import net.minecraft.world.level.biome.MultiNoiseBiomeSourceParameterList;
import net.minecraft.world.level.dimension.DimensionType;
import net.minecraft.world.level.levelgen.NoiseGeneratorSettings;
import net.minecraft.world.level.levelgen.presets.WorldPreset;
import net.minecraft.world.timeline.Timeline;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
@@ -22,18 +25,29 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import com.dfsek.terra.lifecycle.LifecyclePlatform;
import com.dfsek.terra.lifecycle.mixin.RegistryLoadTaskAccessor;
import com.dfsek.terra.lifecycle.util.LifecycleUtil;
import com.dfsek.terra.lifecycle.util.LoadContextHolder;
import com.dfsek.terra.lifecycle.util.RegistryHack;
import com.dfsek.terra.mod.CommonPlatform;
import com.dfsek.terra.mod.ModPlatform;
import com.dfsek.terra.mod.attribute.VanillaContext;
import com.dfsek.terra.mod.mixin.access.VillagerTypeAccessor;
@Mixin(RegistryDataLoader.class)
public class RegistryDataLoaderMixin {
// Until the load finishes, the static registries' tags are only in these lookups.
@Inject(method = "createContext", at = @At("HEAD"))
private static void captureContext(List<HolderLookup.RegistryLookup<?>> contextRegistries, List<RegistryLoadTask<?>> loadTasks,
CallbackInfoReturnable<RegistryOps.RegistryInfoLookup> cir) {
for(RegistryLoadTask<?> task : loadTasks) ((LoadContextHolder) task).terra_setContextRegistries(contextRegistries);
}
// The thenApplyAsync callback in load(LoaderFactory, List, List, Executor): every registry populated, none frozen.
@Inject(
method = "lambda$load$2(Ljava/util/List;Ljava/util/Map;Ljava/lang/Void;)Lnet/minecraft/core/RegistryAccess$Frozen;",
@@ -54,10 +68,18 @@ public class RegistryDataLoaderMixin {
WritableRegistry<MultiNoiseBiomeSourceParameterList> multiNoiseBiomeSourceParameterLists = extractRegistry(loadTasks,
Registries.MULTI_NOISE_BIOME_SOURCE_PARAMETER_LIST).orElseThrow();
WritableRegistry<Enchantment> enchantments = extractRegistry(loadTasks, Registries.ENCHANTMENT).orElseThrow();
// Binds the datapack's timelines before the freeze, for the time-marker check.
WritableRegistry<Timeline> timelines = extractRegistry(loadTasks, Registries.TIMELINE).orElseThrow();
LifecyclePlatform.setRegistries(biomes, dimensionTypes, chunkGeneratorSettings, multiNoiseBiomeSourceParameterLists,
enchantments);
LifecycleUtil.initialize(biomes, worldPresets);
// After extractRegistry: its terra_bind gives the parents' holders their values before the freeze.
List<? extends WritableRegistry<?>> loaded = loadTasks.stream().map(RegistryDataLoaderMixin::getRegistry).toList();
List<HolderLookup.RegistryLookup<?>> context = Objects.requireNonNull(
((LoadContextHolder) loadTasks.getFirst()).terra_contextRegistries(), "the load built no context");
platform.attributeParents().beginLoad(
VanillaContext.ofLoad(loaded, context, VillagerTypeAccessor.getBiomeTypeToIdMap()::get));
LifecycleUtil.initialize(biomes, timelines, worldPresets);
}
}
@@ -4,23 +4,25 @@ import net.minecraft.core.Registry;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.Identifier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.entity.npc.villager.VillagerType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;
import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.world.biome.Biome;
import com.dfsek.terra.mod.CommonPlatform;
import com.dfsek.terra.mod.ModPlatform;
import com.dfsek.terra.mod.attribute.AttributeReport;
import com.dfsek.terra.mod.attribute.MinecraftAttributeParents;
import com.dfsek.terra.mod.attribute.MinecraftBiomeFactory;
import com.dfsek.terra.mod.attribute.PackParents;
import com.dfsek.terra.mod.config.PreLoadCompatibilityOptions;
import com.dfsek.terra.mod.config.ProtoPlatformBiome;
import com.dfsek.terra.mod.config.VanillaBiomeProperties;
import com.dfsek.terra.mod.mixin.access.VillagerTypeAccessor;
import com.dfsek.terra.mod.util.BiomeUtil;
import com.dfsek.terra.mod.util.MinecraftUtil;
import com.dfsek.terra.mod.util.TagUtil;
public final class LifecycleBiomeUtil {
@@ -30,78 +32,63 @@ public final class LifecycleBiomeUtil {
}
public static void registerBiomes(Registry<net.minecraft.world.level.biome.Biome> biomeRegistry) {
public static void registerBiomes(Registry<net.minecraft.world.level.biome.Biome> biomeRegistry, MinecraftBiomeFactory factory) {
logger.info("Registering biomes...");
CommonPlatform.get().getConfigRegistry().forEach(pack -> { // Register all Terra biomes.
BiomeUtil.TERRA_BIOME_MAP.clear();
BiomeUtil.TERRA_BIOME_PLATFORM_TAGS.clear();
ModPlatform platform = CommonPlatform.get();
MinecraftAttributeParents parents = platform.attributeParents();
platform.getConfigRegistry().forEach(pack -> { // Register all Terra biomes.
PackParents packParents = parents.prepare(pack, platform.getAttributeHandle().pack(pack),
AttributeReport.logging(pack));
pack.getCheckedRegistry(Biome.class)
.forEach((id, biome) -> registerBiome(biome, pack, id, biomeRegistry));
.forEach((id, biome) -> registerBiome(biome, pack, packParents, id, biomeRegistry, parents, factory));
});
logger.info("Terra biomes registered.");
}
/**
* Clones a Vanilla biome and injects Terra data to create a Terra-vanilla biome delegate.
* Builds a Terra biome's vanilla biome from its vanilla parent and its attribute entries, and registers it.
*
* @param biome The Terra BiomeBuilder.
* @param pack The ConfigPack this biome belongs to.
*/
private static void registerBiome(Biome biome, ConfigPack pack,
private static void registerBiome(Biome biome, ConfigPack pack, PackParents packParents,
com.dfsek.terra.api.registry.key.RegistryKey id,
Registry<net.minecraft.world.level.biome.Biome> registry) {
ResourceKey<net.minecraft.world.level.biome.Biome> vanilla = ((ProtoPlatformBiome) biome.getPlatformBiome()).get(registry);
if(vanilla == null) {
Registry<net.minecraft.world.level.biome.Biome> registry, MinecraftAttributeParents parents,
MinecraftBiomeFactory factory) {
ProtoPlatformBiome proto = (ProtoPlatformBiome) biome.getPlatformBiome();
ResourceKey<net.minecraft.world.level.biome.Biome> vanilla = proto.get(registry);
net.minecraft.world.level.biome.Biome vanillaBiome = vanilla == null ? null : registry.getValue(vanilla);
if(vanillaBiome == null) {
logger.error("""
Failed to get Vanilla Biome Regiestry key!
Failed to get Vanilla Biome!
Terra Biome ID: {}
Vanilla Biome: {}""", biome.getID(), biome.getPlatformBiome());
Vanilla Biome: {}""", biome.getID(), vanilla == null ? biome.getPlatformBiome() : vanilla.identifier());
return;
}
proto.setParentEntries(parents.biomeEntries(packParents, vanilla, vanillaBiome));
if(pack.getContext().get(PreLoadCompatibilityOptions.class).useVanillaBiomes()) {
((ProtoPlatformBiome) biome.getPlatformBiome()).setDelegate(registry.wrapAsHolder(registry.getValue(vanilla)));
} else {
VanillaBiomeProperties vanillaBiomeProperties = biome.getContext().get(VanillaBiomeProperties.class);
net.minecraft.world.level.biome.Biome vanilaBiome = registry.getValue(vanilla);
if(vanilaBiome == null) {
String vanillaBiomeName;
if(vanilla != null) {
vanillaBiomeName = vanilla.identifier().toString();
} else {
vanillaBiomeName = "NULL";
}
logger.error("""
Failed to get Vanilla Biome!
Terra Biome ID: {}
Vanilla Biome: {}""", biome.getID(), vanillaBiomeName);
return;
}
net.minecraft.world.level.biome.Biome minecraftBiome = BiomeUtil.createBiome(Objects.requireNonNull(vanilaBiome),
vanillaBiomeProperties);
Identifier identifier = Identifier.fromNamespaceAndPath("terra", BiomeUtil.createBiomeID(pack, id));
if(registry.containsKey(identifier)) {
((ProtoPlatformBiome) biome.getPlatformBiome()).setDelegate(MinecraftUtil.getEntry(registry, identifier)
.orElseThrow());
} else {
((ProtoPlatformBiome) biome.getPlatformBiome()).setDelegate(Registry.registerForHolder(registry,
MinecraftUtil.registerBiomeKey(identifier)
.identifier(),
minecraftBiome));
}
Map<ResourceKey<net.minecraft.world.level.biome.Biome>, ResourceKey<VillagerType>> villagerMap =
VillagerTypeAccessor.getBiomeTypeToIdMap();
villagerMap.put(ResourceKey.create(Registries.BIOME, identifier),
Objects.requireNonNullElse(vanillaBiomeProperties.getVillagerType(),
villagerMap.getOrDefault(vanilla, VillagerType.PLAINS)));
BiomeUtil.TERRA_BIOME_MAP.computeIfAbsent(vanilla.identifier(), i -> new ArrayList<>()).add(identifier);
proto.setDelegate(registry.wrapAsHolder(vanillaBiome));
return;
}
}
Identifier identifier = Identifier.fromNamespaceAndPath("terra", BiomeUtil.createBiomeID(pack, id));
if(registry.containsKey(identifier)) {
// Registered by an earlier load; edits wait for the next registry load.
proto.setDelegate(MinecraftUtil.getEntry(registry, identifier).orElseThrow());
} else {
MinecraftBiomeFactory.Built built = factory.create(vanilla, vanillaBiome, biome, packParents, packParents.report());
proto.setDelegate(Registry.registerForHolder(registry, MinecraftUtil.registerBiomeKey(identifier).identifier(),
built.biome()));
VillagerTypeAccessor.getBiomeTypeToIdMap().put(ResourceKey.create(Registries.BIOME, identifier), built.villagerType());
}
BiomeUtil.TERRA_BIOME_PLATFORM_TAGS.put(identifier,
new TagUtil.PlatformTagSource(packParents.report(), biome.getID(), biome.getPlatformTags()));
BiomeUtil.TERRA_BIOME_MAP.computeIfAbsent(vanilla.identifier(), i -> new ArrayList<>()).add(identifier);
}
}
@@ -6,16 +6,52 @@ import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.levelgen.presets.WorldPreset;
import net.minecraft.world.timeline.Timeline;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.dfsek.terra.api.event.events.platform.PlatformInitializationEvent;
import com.dfsek.terra.mod.CommonPlatform;
import com.dfsek.terra.mod.ModPlatform;
import com.dfsek.terra.mod.attribute.AttributeReport;
import com.dfsek.terra.mod.attribute.MinecraftAttributeParents;
import com.dfsek.terra.mod.attribute.MinecraftBiomeFactory;
import com.dfsek.terra.mod.attribute.MinecraftTimelines;
import com.dfsek.terra.mod.attribute.PackParents;
import com.dfsek.terra.mod.attribute.VanillaContext;
public final class LifecycleUtil {
public static void initialize(WritableRegistry<Biome> biomeMutableRegistry, WritableRegistry<WorldPreset> worldPresetMutableRegistry) {
CommonPlatform.get().getEventManager().callEvent(new PlatformInitializationEvent());
LifecycleBiomeUtil.registerBiomes(biomeMutableRegistry);
CommonPlatform.get().registerWorldTypes(
private static final Logger LOGGER = LoggerFactory.getLogger(LifecycleUtil.class);
/**
* Timelines go before world types: the dimension types name the packs' timeline tags.
*/
public static void initialize(WritableRegistry<Biome> biomeMutableRegistry, WritableRegistry<Timeline> timelineMutableRegistry,
WritableRegistry<WorldPreset> worldPresetMutableRegistry) {
ModPlatform platform = CommonPlatform.get();
platform.getEventManager().callEvent(new PlatformInitializationEvent());
LifecycleBiomeUtil.registerBiomes(biomeMutableRegistry, biomeFactory(platform));
registerTimelines(platform, timelineMutableRegistry);
platform.registerWorldTypes(
(id, preset) -> Registry.register(worldPresetMutableRegistry, ResourceKey.create(Registries.WORLD_PRESET, id), preset));
MinecraftTimelines.bindPendingTags(timelineMutableRegistry);
}
private static void registerTimelines(ModPlatform platform, WritableRegistry<Timeline> timelines) {
MinecraftAttributeParents parents = platform.attributeParents();
VanillaContext context = parents.context();
if(context == null) throw new IllegalStateException("no registry load has begun");
platform.getConfigRegistry().forEach(pack -> {
PackParents packParents = parents.prepare(pack, platform.getAttributeHandle().pack(pack),
AttributeReport.logging(pack));
MinecraftTimelines.register(pack, packParents, timelines, platform.getAttributeHandle(), context);
});
}
public static MinecraftBiomeFactory biomeFactory(ModPlatform platform) {
VanillaContext context = platform.attributeParents().context();
if(context == null) throw new IllegalStateException("no registry load has begun");
return new MinecraftBiomeFactory(platform.getAttributeHandle(), context);
}
}
@@ -0,0 +1,13 @@
package com.dfsek.terra.lifecycle.util;
import net.minecraft.core.HolderLookup;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public interface LoadContextHolder {
@Nullable List<HolderLookup.RegistryLookup<?>> terra_contextRegistries();
void terra_setContextRegistries(List<HolderLookup.RegistryLookup<?>> contextRegistries);
}
@@ -7,6 +7,7 @@
"RandomStateMixin",
"HolderReferenceInvoker",
"RegistryLoadTaskAccessor",
"RegistryLoadTaskMixin",
"BuiltInRegistriesMixin",
"MappedRegistryMixin",
"lifecycle.MinecraftServerMixin",