From d83a3b0239e50f2deb60ca035e836ce29cd0cbed Mon Sep 17 00:00:00 2001 From: Sollace Date: Tue, 23 Apr 2024 17:57:50 +0100 Subject: [PATCH] Fix compatibilty with HDSkins --- .../unicopia/datagen/Datagen.java | 7 +-- .../unicopia/entity/damage/UDamageTypes.java | 2 +- .../unicopia/server/world/UWorldGen.java | 59 ++++++++++++++++++- .../util/registry/DynamicRegistry.java | 36 ++++++++--- 4 files changed, 88 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/datagen/Datagen.java b/src/main/java/com/minelittlepony/unicopia/datagen/Datagen.java index 505bc54f..463ddef0 100644 --- a/src/main/java/com/minelittlepony/unicopia/datagen/Datagen.java +++ b/src/main/java/com/minelittlepony/unicopia/datagen/Datagen.java @@ -32,7 +32,6 @@ import net.minecraft.registry.Registry; import net.minecraft.registry.RegistryBuilder; import net.minecraft.registry.RegistryKeys; import net.minecraft.util.Identifier; -import net.minecraft.world.biome.OverworldBiomeCreator; public class Datagen implements DataGeneratorEntrypoint { public static final Logger LOGGER = LogManager.getLogger(); @@ -73,11 +72,7 @@ public class Datagen implements DataGeneratorEntrypoint { @Override public void buildRegistry(RegistryBuilder builder) { - builder.addRegistry(RegistryKeys.BIOME, registerable -> { - final var placedFeatureLookup = registerable.getRegistryLookup(RegistryKeys.PLACED_FEATURE); - final var carverLookup = registerable.getRegistryLookup(RegistryKeys.CONFIGURED_CARVER); - registerable.register(UWorldGen.SWEET_APPLE_ORCHARD, OverworldBiomeCreator.createNormalForest(placedFeatureLookup, carverLookup, false, false, false)); - }); + builder.addRegistry(RegistryKeys.BIOME, UWorldGen.REGISTRY); builder.addRegistry(RegistryKeys.DAMAGE_TYPE, UDamageTypes.REGISTRY); } } diff --git a/src/main/java/com/minelittlepony/unicopia/entity/damage/UDamageTypes.java b/src/main/java/com/minelittlepony/unicopia/entity/damage/UDamageTypes.java index 3bdc4a8f..b91e2331 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/damage/UDamageTypes.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/damage/UDamageTypes.java @@ -8,7 +8,7 @@ import net.minecraft.registry.RegistryKey; import net.minecraft.registry.RegistryKeys; public interface UDamageTypes { - DynamicRegistry REGISTRY = new DynamicRegistry<>(RegistryKeys.DAMAGE_TYPE, key -> new DamageType(key.getValue().getNamespace() + "." + key.getValue().getPath(), 0)); + DynamicRegistry REGISTRY = new DynamicRegistry<>(RegistryKeys.DAMAGE_TYPE, (lookup, key) -> new DamageType(key.getValue().getNamespace() + "." + key.getValue().getPath(), 0)); RegistryKey EXHAUSTION = register("magical_exhaustion"); RegistryKey ALICORN_AMULET = register("alicorn_amulet"); diff --git a/src/main/java/com/minelittlepony/unicopia/server/world/UWorldGen.java b/src/main/java/com/minelittlepony/unicopia/server/world/UWorldGen.java index 69a7991e..c7c781a6 100644 --- a/src/main/java/com/minelittlepony/unicopia/server/world/UWorldGen.java +++ b/src/main/java/com/minelittlepony/unicopia/server/world/UWorldGen.java @@ -1,30 +1,42 @@ package com.minelittlepony.unicopia.server.world; import java.util.List; +import java.util.function.Consumer; import com.minelittlepony.unicopia.Unicopia; import com.minelittlepony.unicopia.block.ShellsBlock; import com.minelittlepony.unicopia.block.UBlocks; import com.minelittlepony.unicopia.server.world.gen.OverworldBiomeSelectionCallback; +import com.minelittlepony.unicopia.util.registry.DynamicRegistry; import net.fabricmc.fabric.api.biome.v1.BiomeModifications; import net.fabricmc.fabric.api.biome.v1.BiomeSelectors; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; +import net.minecraft.client.sound.MusicType; +import net.minecraft.entity.EntityType; +import net.minecraft.entity.SpawnGroup; import net.minecraft.registry.Registries; import net.minecraft.registry.Registry; import net.minecraft.registry.RegistryKey; import net.minecraft.registry.RegistryKeys; import net.minecraft.registry.tag.BiomeTags; +import net.minecraft.sound.BiomeMoodSound; +import net.minecraft.sound.SoundEvents; import net.minecraft.util.collection.DataPool; import net.minecraft.util.math.Direction; import net.minecraft.util.math.Vec3i; import net.minecraft.util.math.intprovider.UniformIntProvider; import net.minecraft.world.biome.Biome; +import net.minecraft.world.biome.BiomeEffects; import net.minecraft.world.biome.BiomeKeys; +import net.minecraft.world.biome.GenerationSettings; +import net.minecraft.world.biome.OverworldBiomeCreator; +import net.minecraft.world.biome.SpawnSettings; import net.minecraft.world.gen.GenerationStep; import net.minecraft.world.gen.blockpredicate.BlockPredicate; import net.minecraft.world.gen.feature.ConfiguredFeatures; +import net.minecraft.world.gen.feature.DefaultBiomeFeatures; import net.minecraft.world.gen.feature.Feature; import net.minecraft.world.gen.feature.PlacedFeature; import net.minecraft.world.gen.feature.PlacedFeatures; @@ -38,6 +50,8 @@ import net.minecraft.world.gen.stateprovider.RandomizedIntBlockStateProvider; import net.minecraft.world.gen.stateprovider.WeightedBlockStateProvider; public interface UWorldGen { + DynamicRegistry REGISTRY = new DynamicRegistry<>(RegistryKeys.BIOME, (lookup, key) -> { throw new RuntimeException("Registerable is required"); }); + PineapplePlantFeature PINEAPPLE_PLANT_FEATURE = Registry.register(Registries.FEATURE, Unicopia.id("pineapple_plant"), new PineapplePlantFeature()); RegistryKey PINEAPPLE_PLANT_PLACED_FEATURE = FeatureRegistry.registerPlaceableFeature(Unicopia.id("pineapple_plant"), PINEAPPLE_PLANT_FEATURE, PineapplePlantFeature.Config.INSTANCE, List.of( RarityFilterPlacementModifier.of(100), @@ -69,7 +83,50 @@ public interface UWorldGen { BiomePlacementModifier.of() )); - RegistryKey SWEET_APPLE_ORCHARD = RegistryKey.of(RegistryKeys.BIOME, Unicopia.id("sweet_apple_orchard")); + RegistryKey SWEET_APPLE_ORCHARD = REGISTRY.register(Unicopia.id("sweet_apple_orchard"), (lookup, key) -> { + return new Biome.Builder() + .precipitation(true) + .temperature(0.8F) + .downfall(0.8F) + .effects(new BiomeEffects.Builder() + .waterColor(4159204) + .waterFogColor(329011) + .fogColor(12638463) + .skyColor(OverworldBiomeCreator.getSkyColor(0.8F)) + .moodSound(BiomeMoodSound.CAVE) + .music(MusicType.createIngameMusic(SoundEvents.MUSIC_OVERWORLD_FOREST)) + .build()) + .spawnSettings(applyAll(new SpawnSettings.Builder(), + DefaultBiomeFeatures::addFarmAnimals, + DefaultBiomeFeatures::addBatsAndMonsters + ).spawn(SpawnGroup.CREATURE, new SpawnSettings.SpawnEntry(EntityType.WOLF, 5, 4, 4)) + .build()) + .generationSettings(applyAll(new GenerationSettings.LookupBackedBuilder(lookup.getRegistryLookup(RegistryKeys.PLACED_FEATURE), lookup.getRegistryLookup(RegistryKeys.CONFIGURED_CARVER)), + DefaultBiomeFeatures::addLandCarvers, + DefaultBiomeFeatures::addAmethystGeodes, + DefaultBiomeFeatures::addDungeons, + DefaultBiomeFeatures::addMineables, + DefaultBiomeFeatures::addSprings, + DefaultBiomeFeatures::addFrozenTopLayer, + DefaultBiomeFeatures::addDefaultOres, + DefaultBiomeFeatures::addDefaultDisks, + DefaultBiomeFeatures::addForestFlowers, + DefaultBiomeFeatures::addDefaultFlowers, + DefaultBiomeFeatures::addForestGrass, + DefaultBiomeFeatures::addDefaultMushrooms, + DefaultBiomeFeatures::addDefaultVegetation + ) + .build()) + .build(); + }); + + @SafeVarargs + static T applyAll(T t, Consumer ...consumers) { + for (Consumer consumer : consumers) { + consumer.accept(t); + } + return t; + } static void bootstrap() { BiomeModifications.addFeature(BiomeSelectors.tag(BiomeTags.IS_JUNGLE), GenerationStep.Feature.VEGETAL_DECORATION, PINEAPPLE_PLANT_PLACED_FEATURE); diff --git a/src/main/java/com/minelittlepony/unicopia/util/registry/DynamicRegistry.java b/src/main/java/com/minelittlepony/unicopia/util/registry/DynamicRegistry.java index 0dd2949a..8cc6336d 100644 --- a/src/main/java/com/minelittlepony/unicopia/util/registry/DynamicRegistry.java +++ b/src/main/java/com/minelittlepony/unicopia/util/registry/DynamicRegistry.java @@ -2,44 +2,64 @@ package com.minelittlepony.unicopia.util.registry; import java.util.HashMap; import java.util.Map; -import java.util.function.Function; +import java.util.concurrent.atomic.AtomicBoolean; import net.fabricmc.fabric.api.event.registry.DynamicRegistrySetupCallback; import net.minecraft.registry.Registerable; import net.minecraft.registry.Registry; import net.minecraft.registry.RegistryBuilder; +import net.minecraft.registry.RegistryEntryLookup; import net.minecraft.registry.RegistryKey; +import net.minecraft.registry.RegistryKeys; import net.minecraft.util.Identifier; public class DynamicRegistry implements RegistryBuilder.BootstrapFunction { private final RegistryKey> registry; private final Map, Entry> keys = new HashMap<>(); - private final Function, T> valueFactory; + private final Registerant valueFactory; - public DynamicRegistry(RegistryKey> registry, Function, T> valueFactory) { + public DynamicRegistry(RegistryKey> registry, Registerant valueFactory) { this.registry = registry; this.valueFactory = valueFactory; - DynamicRegistrySetupCallback.EVENT.register(registries -> { registries.getOptional(registry).ifPresent(r -> { - keys.forEach((key, entry)-> Registry.register(r, key.getValue(), entry.factory().apply(key))); + AtomicBoolean added = new AtomicBoolean(false); + registries.registerEntryAdded(RegistryKeys.MULTI_NOISE_BIOME_SOURCE_PARAMETER_LIST, (raw, id, value) -> { + if (added.getAndSet(true)) { + return; + } + final WrapperLookup lookup = registries.asDynamicRegistryManager()::getWrapperOrThrow; + keys.forEach((key, entry) -> { + if (!r.contains(key)) { + Registry.register(r, key, entry.factory().apply(lookup, key)); + } + }); + }); }); }); } @Override public void run(Registerable registerable) { - keys.forEach((key, entry) -> registerable.register(key, entry.factory().apply(key))); + final WrapperLookup lookup = registerable::getRegistryLookup; + keys.forEach((key, entry) -> registerable.register(key, entry.factory().apply(lookup, key))); } public RegistryKey register(Identifier id) { return register(id, valueFactory); } - public RegistryKey register(Identifier id, Function, T> valueFactory) { + public RegistryKey register(Identifier id, Registerant valueFactory) { return keys.computeIfAbsent(RegistryKey.of(registry, id), k -> new Entry<>(k, valueFactory)).key(); } - record Entry(RegistryKey key, Function, T> factory) {} + record Entry(RegistryKey key, Registerant factory) {} + public interface Registerant { + T apply(WrapperLookup lookup, RegistryKey key); + } + + public interface WrapperLookup { + RegistryEntryLookup getRegistryLookup(RegistryKey> registryRef); + } }