[Changes] Cleanup of old SurfaceRule code

This commit is contained in:
Frank 2022-06-22 00:00:38 +02:00
parent da1d4c6b40
commit cc36c090a1
19 changed files with 305 additions and 147 deletions

View file

@ -1,10 +1,7 @@
package org.betterx.bclib.api.v2.generator;
import org.betterx.bclib.BCLib;
import org.betterx.bclib.api.v2.levelgen.biomes.InternalBiomeAPI;
import org.betterx.bclib.api.v2.levelgen.surface.SurfaceRuleUtil;
import org.betterx.bclib.interfaces.NoiseGeneratorSettingsProvider;
import org.betterx.bclib.interfaces.SurfaceRuleProvider;
import org.betterx.bclib.mixin.common.ChunkGeneratorAccessor;
import org.betterx.worlds.together.WorldsTogether;
import org.betterx.worlds.together.world.WorldGenUtil;
@ -32,9 +29,7 @@ import net.minecraft.world.level.levelgen.synth.NormalNoise;
import com.google.common.base.Suppliers;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
public class BCLChunkGenerator extends NoiseBasedChunkGenerator {
@ -101,7 +96,7 @@ public class BCLChunkGenerator extends NoiseBasedChunkGenerator {
if (initialBiomeSource != getBiomeSource()) {
if (this instanceof ChunkGeneratorAccessor acc) {
BiomeSource bs = WorldGenUtil.getWorldSettings()
.fixBiomeSource(initialBiomeSource, getBiomeSource().possibleBiomes());
.addDatapackBiomes(initialBiomeSource, getBiomeSource().possibleBiomes());
acc.bcl_setBiomeSource(bs);
rebuildFeaturesPerStep(getBiomeSource());
}
@ -109,43 +104,6 @@ public class BCLChunkGenerator extends NoiseBasedChunkGenerator {
}
public static void injectNoiseSettings(
ResourceKey<LevelStem> dimensionKey,
ChunkGenerator loadedChunkGenerator
) {
BCLib.LOGGER.debug("Checking Noise Settings for " + dimensionKey.location().toString());
final BiomeSource loadedBiomeSource = loadedChunkGenerator.getBiomeSource();
InternalBiomeAPI.applyModifications(loadedBiomeSource, dimensionKey);
if (loadedChunkGenerator instanceof NoiseBasedChunkGenerator nbc) {
if (((Object) nbc.generatorSettings().value()) instanceof SurfaceRuleProvider srp) {
srp.bclib_overwrite(SurfaceRuleUtil.addRulesForBiomeSource(nbc
.generatorSettings()
.value()
.surfaceRule(), loadedBiomeSource));
}
}
}
public static final Predicate<ResourceKey<LevelStem>> NON_MANAGED_DIMENSIONS = dim -> dim != LevelStem.NETHER && dim != LevelStem.END;
public static void injectNoiseSettings(WorldGenSettings settings, Predicate<ResourceKey<LevelStem>> filter) {
List<ResourceKey<LevelStem>> otherDimensions = settings
.dimensions()
.entrySet()
.stream()
.map(e -> e.getKey())
.filter(filter)
.toList();
for (ResourceKey<LevelStem> key : otherDimensions) {
Optional<Holder<LevelStem>> stem = settings.dimensions().getHolder(key);
if (stem.isPresent()) {
injectNoiseSettings(key, stem.get().value().generator());
}
}
}
@Override
protected Codec<? extends ChunkGenerator> codec() {
return CODEC;

View file

@ -9,6 +9,7 @@ import org.betterx.bclib.util.CollectionsUtil;
import org.betterx.bclib.util.ColorUtil;
import org.betterx.bclib.util.Pair;
import org.betterx.bclib.util.TriFunction;
import org.betterx.worlds.together.surfaceRules.SurfaceRuleRegistry;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderSet;
@ -737,6 +738,7 @@ public class BCLBiomeBuilder {
*
* @param biomeConstructor {@link BiFunction} biome constructor.
* @return created {@link BCLBiome} instance.
* @deprecated Replaced with {@link #build(BiomeSupplier)}
*/
@Deprecated(forRemoval = true)
public <T extends BCLBiome> T build(BiFunction<ResourceLocation, Biome, T> biomeConstructor) {
@ -787,9 +789,11 @@ public class BCLBiomeBuilder {
final Biome biome = builder.build();
final T res = biomeConstructor.apply(biomeID, biome, settings);
res.addBiomeTags(tags);
res.setSurface(surfaceRule);
//res.setSurface(surfaceRule);
SurfaceRuleRegistry.registerRule(biomeID, surfaceRule, biomeID);
res.addClimateParameters(parameters);
//carvers.forEach(cfg -> BiomeAPI.addBiomeCarver(biome, cfg.second, cfg.first));
return res;
}

View file

@ -3,8 +3,8 @@ package org.betterx.bclib.api.v2.levelgen.biomes;
import org.betterx.bclib.BCLib;
import org.betterx.bclib.interfaces.BiomeSourceAccessor;
import org.betterx.bclib.interfaces.NoiseGeneratorSettingsProvider;
import org.betterx.bclib.interfaces.SurfaceRuleProvider;
import org.betterx.bclib.mixin.common.BiomeGenerationSettingsAccessor;
import org.betterx.worlds.together.surfaceRules.SurfaceRuleProvider;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
@ -205,7 +205,8 @@ public class InternalBiomeAPI {
// This happens for example when using the MiningDimensions, which reuses the generator for the
// Nethering Dimension
//MODIFIED_SURFACE_PROVIDERS.add(provider);
provider.bclib_addBiomeSource(source);
//provider.bclib_addBiomeSource(source);
} else {
BCLib.LOGGER.warning("No generator for " + source);
}

View file

@ -2,14 +2,13 @@ package org.betterx.bclib.api.v2.levelgen.surface;
import org.betterx.bclib.api.v2.levelgen.biomes.BiomeAPI;
import org.betterx.bclib.api.v2.levelgen.biomes.InternalBiomeAPI;
import org.betterx.bclib.mixin.common.NoiseGeneratorSettingsMixin;
import org.betterx.worlds.together.mixin.common.NoiseGeneratorSettingsMixin;
import net.minecraft.core.Holder;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.BiomeSource;
import net.minecraft.world.level.levelgen.SurfaceRules;
import net.minecraft.world.level.levelgen.SurfaceRules.RuleSource;
import com.google.common.collect.Lists;
@ -77,22 +76,12 @@ public class SurfaceRuleUtil {
SURFACE_RULES.put(biomeID, source);
}
public static RuleSource addRulesForBiomeSource(RuleSource org, BiomeSource biomeSource) {
public static RuleSource addRulesForBiomeSource(RuleSource orgiginalSource, BiomeSource biomeSource) {
List<RuleSource> additionalRules = getRuleSources(biomeSource);
if (org instanceof SurfaceRules.SequenceRuleSource sequenceRule) {
List<RuleSource> existingSequence = sequenceRule.sequence();
additionalRules = additionalRules
.stream()
.filter(r -> existingSequence.indexOf(r) < 0)
.collect(Collectors.toList());
if (additionalRules.size() == 0) return org;
additionalRules.addAll(sequenceRule.sequence());
} else {
if (additionalRules.size() == 0) return org;
if (!additionalRules.contains(org))
additionalRules.add(org);
return org.betterx.worlds.together.surfaceRules.SurfaceRuleUtil.mergeSurfaceRules(
orgiginalSource,
additionalRules
);
}
return SurfaceRules.sequence(additionalRules.toArray(new RuleSource[additionalRules.size()]));
}
}

View file

@ -12,10 +12,14 @@ import net.minecraft.commands.CommandSourceStack;
import net.minecraft.core.Holder;
import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.RegistryOps;
import net.minecraft.tags.TagEntry;
import net.minecraft.tags.TagFile;
import net.minecraft.util.KeyDispatchDataCodec;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.level.levelgen.structure.Structure;
import net.minecraft.world.level.levelgen.structure.templatesystem.PosRuleTestType;
import net.minecraft.world.level.levelgen.structure.templatesystem.RuleTestType;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureProcessorList;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureProcessorType;
import net.minecraft.world.level.material.Fluid;
@ -55,20 +59,37 @@ public class DumpDatapack {
File base = new File(System.getProperty("user.dir"), "bclib_datapack_dump");
BCLib.LOGGER.info(registry.key().toString());
// Tag Output
// registry.value()
// .getTagNames()
// .map(tagKey -> registry.value().getTag(tagKey))
// .filter(tag -> tag.isPresent())
// .map(tag -> tag.get())
// .forEach(tag -> {
// tag.stream()
// .map(holder -> holder.unwrapKey())
// .filter(k -> k.isPresent())
// .map(k -> k.get())
// .forEach(key -> {
//
// });
// });
registry.value()
.getTagNames()
.map(tagKey -> registry.value().getTag(tagKey))
.filter(tag -> tag.isPresent())
.map(tag -> tag.get())
.forEach(tag -> {
File f1 = new File(base, tag.key().location().getNamespace());
f1 = new File(f1, "tags");
f1 = new File(f1, registry.key().location().getPath());
f1.mkdirs();
f1 = new File(f1, tag.key().location().getPath() + ".json");
TagFile tf = new TagFile(
tag.stream()
.map(holder -> holder.unwrapKey())
.filter(k -> k.isPresent())
.map(k -> TagEntry.element(k.get().location()))
.toList(),
true
);
var o = TagFile.CODEC
.encodeStart(registryOps, tf)
.result()
.orElse(new JsonObject());
String content = gson.toJson(o);
try {
Files.writeString(f1.toPath(), content, StandardCharsets.UTF_8);
} catch (IOException e) {
BCLib.LOGGER.error(" ->> Unable to WRITE: " + e.getMessage());
}
});
registry
.value()
@ -102,6 +123,10 @@ public class DumpDatapack {
return;
} else if (obj instanceof BaseStairsBlock) {
return;
} else if (obj instanceof RuleTestType<?>) {
codec[0] = registry.value().byNameCodec();
} else if (obj instanceof PosRuleTestType<?>) {
codec[0] = registry.value().byNameCodec();
}
if (codec[0] == null) {
@ -113,6 +138,7 @@ public class DumpDatapack {
try {
codec[0] = (Codec) m.invoke(obj);
BCLib.LOGGER.info(" Got Codec from " + m);
break;
} catch (Exception e) {
BCLib.LOGGER.error(" !!! Unable to get Codec from " + m);
}
@ -120,6 +146,7 @@ public class DumpDatapack {
try {
codec[0] = ((KeyDispatchCodec) m.invoke(obj)).codec();
BCLib.LOGGER.info(" Got Codec from " + m);
break;
} catch (Exception e) {
BCLib.LOGGER.error(" !!! Unable to get Codec from " + m);
}
@ -127,6 +154,7 @@ public class DumpDatapack {
try {
codec[0] = ((KeyDispatchDataCodec) m.invoke(obj)).codec();
BCLib.LOGGER.info(" Got Codec from " + m);
break;
} catch (Exception e) {
BCLib.LOGGER.error(" !!! Unable to get Codec from " + m);
}
@ -138,7 +166,7 @@ public class DumpDatapack {
if (codec[0] == null) {
//Try to find DIRECT_CODEC field
for (Field f : obj.getClass().getDeclaredFields()) {
for (Field f : obj.getClass().getFields()) {
if (Modifier.isStatic(f.getModifiers())) {
if ("DIRECT_CODEC".equals(f.getName())) {
f.setAccessible(true);
@ -155,7 +183,7 @@ public class DumpDatapack {
//Try to find CODEC field
if (codec[0] == null) {
for (Field f : obj.getClass().getDeclaredFields()) {
for (Field f : obj.getClass().getFields()) {
if (Modifier.isStatic(f.getModifiers())) {
if ("CODEC".equals(f.getName())) {
try {
@ -172,7 +200,7 @@ public class DumpDatapack {
//Try to find any Codec field
if (codec[0] == null) {
for (Field f : obj.getClass().getDeclaredFields()) {
for (Field f : obj.getClass().getFields()) {
if (Modifier.isStatic(f.getModifiers())) {
if (Codec.class.isAssignableFrom(f.getType())) {
f.setAccessible(true);
@ -187,6 +215,10 @@ public class DumpDatapack {
}
}
if (codec[0] == null) {
codec[0] = registry.value().byNameCodec();
}
if (codec[0] != null) {
try {

View file

@ -6,9 +6,10 @@ import org.betterx.bclib.api.v2.generator.BCLChunkGenerator;
import org.betterx.bclib.api.v2.generator.BCLibEndBiomeSource;
import org.betterx.bclib.api.v2.levelgen.LevelGenUtil;
import org.betterx.bclib.api.v2.levelgen.biomes.InternalBiomeAPI;
import org.betterx.bclib.api.v2.levelgen.surface.SurfaceRuleUtil;
import org.betterx.bclib.interfaces.ChunkGeneratorAccessor;
import org.betterx.bclib.interfaces.NoiseGeneratorSettingsProvider;
import org.betterx.worlds.together.surfaceRules.SurfaceRuleRegistry;
import org.betterx.worlds.together.surfaceRules.SurfaceRuleUtil;
import org.betterx.worlds.together.world.WorldGenUtil;
import org.betterx.worlds.together.worldPreset.TogetherWorldPreset;
import org.betterx.worlds.together.worldPreset.settings.WorldPresetSettings;
@ -125,35 +126,38 @@ public class BCLWorldPresetSettings extends WorldPresetSettings {
return LevelGenUtil.getBCLEndLevelStem(ctx, Optional.of(endVersion));
}
public BiomeSource fixBiomeSource(BiomeSource biomeSource, Set<Holder<Biome>> datapackBiomes) {
public BiomeSource addDatapackBiomes(BiomeSource biomeSource, Set<Holder<Biome>> datapackBiomes) {
if (biomeSource instanceof BCLBiomeSource bs) {
return bs.createCopyForDatapack(datapackBiomes);
}
return biomeSource;
}
private Holder<NoiseGeneratorSettings> fixNoiseSettings(
private static Holder<NoiseGeneratorSettings> injectSurfaceRules(
Holder<NoiseGeneratorSettings> reference,
Holder<NoiseGeneratorSettings> settings,
BiomeSource biomeSource
) {
NoiseGeneratorSettings old = settings.value();
NoiseGeneratorSettings noise = new NoiseGeneratorSettings(
old.noiseSettings(),
old.defaultBlock(),
old.defaultFluid(),
old.noiseRouter(),
SurfaceRuleUtil.addRulesForBiomeSource(old.surfaceRule(), biomeSource),
old.spawnTarget(),
old.seaLevel(),
old.disableMobGeneration(),
old.aquifersEnabled(),
old.oreVeinsEnabled(),
old.useLegacyRandomSource()
);
return Holder.direct(noise);
SurfaceRuleUtil.injectSurfaceRules(settings.value(), biomeSource);
return settings;
// NoiseGeneratorSettings old = settings.value();
// NoiseGeneratorSettings noise = new NoiseGeneratorSettings(
// old.noiseSettings(),
// old.defaultBlock(),
// old.defaultFluid(),
// old.noiseRouter(),
// SurfaceRuleRegistry.mergeSurfaceRulesFromBiomes(old.surfaceRule(), biomeSource),
// //SurfaceRuleUtil.addRulesForBiomeSource(old.surfaceRule(), biomeSource),
// old.spawnTarget(),
// old.seaLevel(),
// old.disableMobGeneration(),
// old.aquifersEnabled(),
// old.oreVeinsEnabled(),
// old.useLegacyRandomSource()
// );
//
//
// return Holder.direct(noise);
}
@ -200,13 +204,13 @@ public class BCLWorldPresetSettings extends WorldPresetSettings {
if (loadedChunkGenerator instanceof NoiseGeneratorSettingsProvider noiseProvider) {
if (referenceGenerator instanceof NoiseGeneratorSettingsProvider referenceProvider) {
final Set<Holder<Biome>> biomes = loadedChunkGenerator.getBiomeSource().possibleBiomes();
final BiomeSource bs = fixBiomeSource(referenceGenerator.getBiomeSource(), biomes);
final BiomeSource bs = addDatapackBiomes(referenceGenerator.getBiomeSource(), biomes);
InternalBiomeAPI.applyModifications(bs, dimensionKey);
referenceGenerator = new BCLChunkGenerator(
generator.bclib_getStructureSetsRegistry(),
noiseProvider.bclib_getNoises(),
bs,
fixNoiseSettings(
injectSurfaceRules(
referenceProvider.bclib_getNoiseGeneratorSettingHolders(),
noiseProvider.bclib_getNoiseGeneratorSettingHolders(),
bs
@ -224,7 +228,7 @@ public class BCLWorldPresetSettings extends WorldPresetSettings {
referenceGenerator
);
} else {
BCLChunkGenerator.injectNoiseSettings(dimensionKey, loadedChunkGenerator);
SurfaceRuleUtil.injectSurfaceRules(dimensionKey, loadedChunkGenerator);
}
return settings;
}
@ -232,7 +236,7 @@ public class BCLWorldPresetSettings extends WorldPresetSettings {
public WorldGenSettings repairSettingsOnLoad(RegistryAccess registryAccess, WorldGenSettings settings) {
settings = fixSettingsInCurrentWorld(registryAccess, LevelStem.NETHER, BuiltinDimensionTypes.NETHER, settings);
settings = fixSettingsInCurrentWorld(registryAccess, LevelStem.END, BuiltinDimensionTypes.END, settings);
BCLChunkGenerator.injectNoiseSettings(settings, BCLChunkGenerator.NON_MANAGED_DIMENSIONS);
SurfaceRuleUtil.injectSurfaceRules(settings, SurfaceRuleRegistry.NON_MANAGED_DIMENSIONS);
return settings;
}

View file

@ -5,7 +5,6 @@ import org.betterx.worlds.together.surfaceRules.SurfaceRuleRegistry;
import net.minecraft.core.Registry;
import net.minecraft.data.BuiltinRegistries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.levelgen.SurfaceRules;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
@ -18,7 +17,7 @@ public class BuiltinRegistriesMixin {
@Inject(method = "<clinit>", at = @At(value = "INVOKE", target = "Ljava/util/Map;forEach(Ljava/util/function/BiConsumer;)V"))
private static void together_registerSurface(CallbackInfo ci) {
SurfaceRuleRegistry.SURFACE_RULES = (Registry<SurfaceRules.RuleSource>) registerSimple(
SurfaceRuleRegistry.BUILTIN_SURFACE_RULES = registerSimple(
SurfaceRuleRegistry.SURFACE_RULES_REGISTRY,
SurfaceRuleRegistry::bootstrap
);

View file

@ -1,7 +1,7 @@
package org.betterx.bclib.mixin.common;
package org.betterx.worlds.together.mixin.common;
import org.betterx.bclib.api.v2.levelgen.surface.SurfaceRuleUtil;
import org.betterx.bclib.interfaces.SurfaceRuleProvider;
import org.betterx.worlds.together.surfaceRules.SurfaceRuleProvider;
import net.minecraft.world.level.biome.BiomeSource;
import net.minecraft.world.level.levelgen.NoiseGeneratorSettings;
@ -16,7 +16,6 @@ import org.spongepowered.asm.mixin.Shadow;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@Mixin(NoiseGeneratorSettings.class)
public class NoiseGeneratorSettingsMixin implements SurfaceRuleProvider {
@ -34,39 +33,39 @@ public class NoiseGeneratorSettingsMixin implements SurfaceRuleProvider {
@Override
public void bclib_addBiomeSource(BiomeSource source) {
bclib_biomeSources.add(source);
bclib_updateCustomRules();
// bclib_biomeSources.add(source);
// bclib_updateCustomRules();
}
@Override
public void bclib_clearBiomeSources() {
bclib_biomeSources.clear();
bclib_clearCustomRules();
// bclib_biomeSources.clear();
// bclib_clearCustomRules();
}
private void bclib_clearCustomRules() {
if (bclib_originalSurfaceRule != null) {
this.surfaceRule = bclib_originalSurfaceRule;
bclib_originalSurfaceRule = null;
}
// if (bclib_originalSurfaceRule != null) {
// this.surfaceRule = bclib_originalSurfaceRule;
// bclib_originalSurfaceRule = null;
// }
}
private void bclib_setCustomRules(List<RuleSource> rules) {
if (rules.size() == 0) {
bclib_clearCustomRules();
return;
}
RuleSource org = bclib_getOriginalSurfaceRule();
if (org instanceof SurfaceRules.SequenceRuleSource sequenceRule) {
List<RuleSource> currentSequence = sequenceRule.sequence();
rules = rules.stream().filter(r -> currentSequence.indexOf(r) < 0).collect(Collectors.toList());
rules.addAll(sequenceRule.sequence());
} else {
rules.add(org);
}
bclib_setSurfaceRule(SurfaceRules.sequence(rules.toArray(new RuleSource[rules.size()])));
// if (rules.size() == 0) {
// bclib_clearCustomRules();
// return;
// }
//
// RuleSource org = bclib_getOriginalSurfaceRule();
// if (org instanceof SurfaceRules.SequenceRuleSource sequenceRule) {
// List<RuleSource> currentSequence = sequenceRule.sequence();
// rules = rules.stream().filter(r -> currentSequence.indexOf(r) < 0).collect(Collectors.toList());
// rules.addAll(sequenceRule.sequence());
// } else {
// rules.add(org);
// }
//
// bclib_setSurfaceRule(SurfaceRules.sequence(rules.toArray(new RuleSource[rules.size()])));
}
public void bclib_overwrite(SurfaceRules.RuleSource surfaceRule) {

View file

@ -1,12 +1,12 @@
package org.betterx.worlds.together.mixin.common;
import org.betterx.worlds.together.surfaceRules.AssignedSurfaceRule;
import org.betterx.worlds.together.surfaceRules.SurfaceRuleRegistry;
import com.mojang.serialization.Codec;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.levelgen.SurfaceRules;
import com.google.common.collect.ImmutableMap;
import org.spongepowered.asm.mixin.Mixin;
@ -29,7 +29,7 @@ public interface RegistryAccessMixin {
ImmutableMap.Builder<ResourceKey<Registry<?>>, RegistryAccess.RegistryData<?>> builder = ImmutableMap.builder();
builder.putAll(res);
put(builder, SurfaceRuleRegistry.SURFACE_RULES_REGISTRY, SurfaceRules.RuleSource.CODEC);
put(builder, SurfaceRuleRegistry.SURFACE_RULES_REGISTRY, AssignedSurfaceRule.CODEC);
return builder.build();
};
}

View file

@ -1,9 +1,19 @@
package org.betterx.worlds.together.surfaceRules;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.levelgen.SurfaceRules;
public class AssignedSurfaceRule {
public static final Codec<AssignedSurfaceRule> CODEC = RecordCodecBuilder.create(instance -> instance
.group(
SurfaceRules.RuleSource.CODEC.fieldOf("ruleSource").forGetter(o -> o.ruleSource),
ResourceLocation.CODEC.fieldOf("biome").forGetter(o -> o.biomeID)
)
.apply(instance, AssignedSurfaceRule::new)
);
public final SurfaceRules.RuleSource ruleSource;
public final ResourceLocation biomeID;
@ -12,4 +22,5 @@ public class AssignedSurfaceRule {
this.biomeID = biomeID;
}
}

View file

@ -1,4 +1,4 @@
package org.betterx.bclib.interfaces;
package org.betterx.worlds.together.surfaceRules;
import net.minecraft.world.level.biome.BiomeSource;
import net.minecraft.world.level.levelgen.SurfaceRules;

View file

@ -8,26 +8,55 @@ import net.minecraft.data.BuiltinRegistries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.dimension.LevelStem;
import net.minecraft.world.level.levelgen.SurfaceRules;
public class SurfaceRuleRegistry {
public static final ResourceKey<Registry<SurfaceRules.RuleSource>> SURFACE_RULES_REGISTRY =
createRegistryKey(WorldsTogether.makeID("worldgen/surface_rules"));
import java.util.function.Predicate;
import org.jetbrains.annotations.ApiStatus;
public static Registry<SurfaceRules.RuleSource> SURFACE_RULES;
public class SurfaceRuleRegistry {
public static final ResourceKey<Registry<AssignedSurfaceRule>> SURFACE_RULES_REGISTRY =
createRegistryKey(WorldsTogether.makeID("worldgen/surface_rules"));
public static final Predicate<ResourceKey<LevelStem>> NON_MANAGED_DIMENSIONS = dim -> dim != LevelStem.NETHER && dim != LevelStem.END;
public static final Predicate<ResourceKey<LevelStem>> ALL_DIMENSIONS = dim -> true;
public static Registry<AssignedSurfaceRule> BUILTIN_SURFACE_RULES;
private static <T> ResourceKey<Registry<T>> createRegistryKey(ResourceLocation location) {
return ResourceKey.createRegistryKey(location);
}
public static Holder<SurfaceRules.RuleSource> bootstrap(Registry<SurfaceRules.RuleSource> registry) {
@ApiStatus.Internal
public static Holder<AssignedSurfaceRule> bootstrap(Registry<AssignedSurfaceRule> registry) {
return BuiltinRegistries.register(
registry,
WorldsTogether.makeID("test"),
SurfaceRules.state(Blocks.SCULK.defaultBlockState())
WorldsTogether.makeID("dummy"),
new AssignedSurfaceRule(
SurfaceRules.state(Blocks.YELLOW_CONCRETE.defaultBlockState()),
WorldsTogether.makeID("none")
)
);
}
public static ResourceKey<AssignedSurfaceRule> registerRule(
ResourceLocation ruleID,
SurfaceRules.RuleSource rules,
ResourceLocation biomeID
) {
ResourceKey<AssignedSurfaceRule> key = ResourceKey.create(
SurfaceRuleRegistry.SURFACE_RULES_REGISTRY,
ruleID
);
Registry.register(SurfaceRuleRegistry.BUILTIN_SURFACE_RULES, key, new AssignedSurfaceRule(
SurfaceRules.ifTrue(
SurfaceRules.isBiome(ResourceKey.create(Registry.BIOME_REGISTRY, biomeID)),
rules
), biomeID
)
);
return key;
}
public static void ensureStaticallyLoaded() {
}

View file

@ -0,0 +1,122 @@
package org.betterx.worlds.together.surfaceRules;
import org.betterx.bclib.api.v2.levelgen.biomes.InternalBiomeAPI;
import org.betterx.worlds.together.WorldsTogether;
import org.betterx.worlds.together.world.event.WorldBootstrap;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.BiomeSource;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.dimension.LevelStem;
import net.minecraft.world.level.levelgen.NoiseBasedChunkGenerator;
import net.minecraft.world.level.levelgen.NoiseGeneratorSettings;
import net.minecraft.world.level.levelgen.SurfaceRules;
import net.minecraft.world.level.levelgen.WorldGenSettings;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class SurfaceRuleUtil {
public static List<SurfaceRules.RuleSource> getRulesForBiome(ResourceLocation biomeID) {
Registry<AssignedSurfaceRule> registry = SurfaceRuleRegistry.BUILTIN_SURFACE_RULES;
if (WorldBootstrap.getLastRegistryAccess() != null)
registry = WorldBootstrap.getLastRegistryAccess()
.registryOrThrow(SurfaceRuleRegistry.SURFACE_RULES_REGISTRY);
return registry.stream()
.filter(a -> a != null && a.biomeID != null && a.biomeID.equals(
biomeID))
.map(a -> a.ruleSource)
.toList();
}
public static List<SurfaceRules.RuleSource> getRulesForBiomes(List<Biome> biomes) {
Registry<Biome> biomeRegistry = WorldBootstrap.getLastRegistryAccess().registryOrThrow(Registry.BIOME_REGISTRY);
List<ResourceLocation> biomeIDs = biomes.stream()
.map(b -> biomeRegistry.getKey(b))
.filter(id -> id != null)
.toList();
return biomeIDs.stream().map(biomeID -> getRulesForBiome(biomeID)).flatMap(List::stream).toList();
}
public static SurfaceRules.RuleSource mergeSurfaceRulesFromBiomes(
SurfaceRules.RuleSource org,
BiomeSource source
) {
return mergeSurfaceRules(org, getRulesForBiomes(source.possibleBiomes().stream().map(h -> h.value()).toList()));
}
public static SurfaceRules.RuleSource mergeSurfaceRulesFromBiomes(
SurfaceRules.RuleSource org,
List<Biome> biomes
) {
return mergeSurfaceRules(org, getRulesForBiomes(biomes));
}
public static SurfaceRules.RuleSource mergeSurfaceRules(
SurfaceRules.RuleSource org,
List<SurfaceRules.RuleSource> additionalRules
) {
if (additionalRules == null || additionalRules.isEmpty()) return org;
if (org instanceof SurfaceRules.SequenceRuleSource sequenceRule) {
List<SurfaceRules.RuleSource> existingSequence = sequenceRule.sequence();
additionalRules = additionalRules
.stream()
.filter(r -> existingSequence.indexOf(r) < 0)
.collect(Collectors.toList());
if (additionalRules.size() == 0) return org;
additionalRules.addAll(existingSequence);
} else {
if (!additionalRules.contains(org))
additionalRules.add(org);
}
return new SurfaceRules.SequenceRuleSource(additionalRules);
}
public static void injectSurfaceRules(
ResourceKey<LevelStem> dimensionKey,
ChunkGenerator loadedChunkGenerator
) {
WorldsTogether.LOGGER.debug("Checking Surface Rules for " + dimensionKey.location().toString());
final BiomeSource loadedBiomeSource = loadedChunkGenerator.getBiomeSource();
InternalBiomeAPI.applyModifications(loadedBiomeSource, dimensionKey);
if (loadedChunkGenerator instanceof NoiseBasedChunkGenerator nbc) {
injectSurfaceRules(nbc.generatorSettings().value(), loadedBiomeSource);
}
}
public static void injectSurfaceRules(NoiseGeneratorSettings noiseSettings, BiomeSource loadedBiomeSource) {
if (((Object) noiseSettings) instanceof SurfaceRuleProvider srp) {
SurfaceRules.RuleSource originalRules = noiseSettings.surfaceRule();
srp.bclib_overwrite(mergeSurfaceRulesFromBiomes(originalRules, loadedBiomeSource));
}
}
public static void injectSurfaceRules(WorldGenSettings settings, Predicate<ResourceKey<LevelStem>> filter) {
List<ResourceKey<LevelStem>> otherDimensions = settings
.dimensions()
.entrySet()
.stream()
.map(e -> e.getKey())
.filter(filter)
.toList();
for (ResourceKey<LevelStem> key : otherDimensions) {
Optional<Holder<LevelStem>> stem = settings.dimensions().getHolder(key);
if (stem.isPresent()) {
injectSurfaceRules(key, stem.get().value().generator());
}
}
}
}

View file

@ -30,6 +30,10 @@ import org.jetbrains.annotations.ApiStatus;
public class WorldBootstrap {
private static RegistryAccess LAST_REGISTRY_ACCESS = null;
public static RegistryAccess getLastRegistryAccess() {
return LAST_REGISTRY_ACCESS;
}
public static class Helpers {
private static void initializeWorldDataAPI(
LevelStorageSource.LevelStorageAccess levelStorageAccess,

View file

@ -1,5 +1,8 @@
package org.betterx.worlds.together.worldPreset.settings;
import org.betterx.worlds.together.surfaceRules.SurfaceRuleRegistry;
import org.betterx.worlds.together.surfaceRules.SurfaceRuleUtil;
import com.mojang.serialization.Codec;
import net.minecraft.core.Holder;
import net.minecraft.core.RegistryAccess;
@ -20,11 +23,12 @@ public class VanillaWorldPresetSettings extends WorldPresetSettings {
@Override
public WorldGenSettings repairSettingsOnLoad(RegistryAccess registryAccess, WorldGenSettings settings) {
SurfaceRuleUtil.injectSurfaceRules(settings, SurfaceRuleRegistry.ALL_DIMENSIONS);
return settings;
}
@Override
public BiomeSource fixBiomeSource(BiomeSource biomeSource, Set<Holder<Biome>> datapackBiomes) {
public BiomeSource addDatapackBiomes(BiomeSource biomeSource, Set<Holder<Biome>> datapackBiomes) {
return biomeSource;
}
}

View file

@ -51,5 +51,5 @@ public abstract class WorldPresetSettings {
public abstract Codec<? extends WorldPresetSettings> codec();
public abstract WorldGenSettings repairSettingsOnLoad(RegistryAccess registryAccess, WorldGenSettings settings);
public abstract BiomeSource fixBiomeSource(BiomeSource biomeSource, Set<Holder<Biome>> datapackBiomes);
public abstract BiomeSource addDatapackBiomes(BiomeSource biomeSource, Set<Holder<Biome>> datapackBiomes);
}

View file

@ -11,8 +11,10 @@ accessible class net/minecraft/world/level/levelgen/SurfaceRules$SequenceRuleSou
accessible class net/minecraft/world/level/levelgen/presets/WorldPresets$Bootstrap
extendable class net/minecraft/world/level/levelgen/NoiseBasedChunkGenerator
accessible class net/minecraft/data/BuiltinRegistries$RegistryBootstrap
accessible class net/minecraft/world/level/levelgen/SurfaceRules$SequenceRuleSource
#Methods
accessible method net/minecraft/client/gui/screens/worldselection/WorldGenSettingsComponent updateSettings (Lnet/minecraft/client/gui/screens/worldselection/WorldCreationContext$Updater;)V
accessible method net/minecraft/world/level/storage/loot/LootPool <init> ([Lnet/minecraft/world/level/storage/loot/entries/LootPoolEntryContainer;[Lnet/minecraft/world/level/storage/loot/predicates/LootItemCondition;[Lnet/minecraft/world/level/storage/loot/functions/LootItemFunction;Lnet/minecraft/world/level/storage/loot/providers/number/NumberProvider;Lnet/minecraft/world/level/storage/loot/providers/number/NumberProvider;)V
accessible method net/minecraft/world/entity/ai/village/poi/PoiTypes register (Lnet/minecraft/core/Registry;Lnet/minecraft/resources/ResourceKey;Ljava/util/Set;II)Lnet/minecraft/world/entity/ai/village/poi/PoiType;
accessible method net/minecraft/world/level/levelgen/SurfaceRules$SequenceRuleSource <init> (Ljava/util/List;)V

View file

@ -26,7 +26,6 @@
"MinecraftServerMixinLate",
"MobSpawnSettingsAccessor",
"NoiseBasedChunkGeneratorMixin",
"NoiseGeneratorSettingsMixin",
"PistonBaseBlockMixin",
"PortalShapeMixin",
"PotionBrewingAccessor",

View file

@ -7,6 +7,7 @@
"BuiltinRegistriesMixin",
"DiggerItemAccessor",
"MainMixin",
"NoiseGeneratorSettingsMixin",
"PrimaryLevelDataMixin",
"RegistryAccessMixin",
"RegistryOpsAccessor",