Skip to content

Commit

Permalink
Predicates for selecting particular biome selections
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebemish committed Jun 13, 2024
1 parent 77d709d commit 35d852e
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public void onInitialize() {

SurfaceModifierBootstrap.modifiers((s, c) -> Registry.register(BiomeSquisherRegistries.SURFACE_MODIFIER_TYPES, Utils.id(s), c));
SurfaceModifierBootstrap.predicates((s, c) -> Registry.register(BiomeSquisherRegistries.SURFACE_PREDICATE_TYPES, Utils.id(s), c));
SurfaceModifierBootstrap.conditionPredicates((s, c) -> Registry.register(BiomeSquisherRegistries.SURFACE_CONDITION_PREDICATE_TYPES, Utils.id(s), c));
SurfaceModifierBootstrap.finders((s, c) -> Registry.register(BiomeSquisherRegistries.SURFACE_FINDER_TYPES, Utils.id(s), c));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.serialization.MapCodec;
import dev.lukebemish.biomesquisher.impl.Platform;
import dev.lukebemish.biomesquisher.impl.Utils;
import dev.lukebemish.biomesquisher.surface.ConditionPredicate;
import dev.lukebemish.biomesquisher.surface.RuleFinder;
import dev.lukebemish.biomesquisher.surface.RuleModifier;
import dev.lukebemish.biomesquisher.surface.RulePredicate;
Expand All @@ -19,9 +20,11 @@ private BiomeSquisherRegistries() {}

public static final ResourceKey<Registry<MapCodec<? extends RuleModifier>>> SURFACE_MODIFIER_TYPES_KEY = ResourceKey.createRegistryKey(Utils.id("surface_modifier_types"));
public static final ResourceKey<Registry<MapCodec<? extends RulePredicate>>> SURFACE_PREDICATE_TYPES_KEY = ResourceKey.createRegistryKey(Utils.id("surface_predicate_types"));
public static final ResourceKey<Registry<MapCodec<? extends ConditionPredicate>>> SURFACE_CONDITION_PREDICATE_TYPES_KEY = ResourceKey.createRegistryKey(Utils.id("surface_predicate_types"));
public static final ResourceKey<Registry<MapCodec<? extends RuleFinder>>> SURFACE_FINDER_TYPES_KEY = ResourceKey.createRegistryKey(Utils.id("surface_finder_types"));

public static final Registry<MapCodec<? extends RuleModifier>> SURFACE_MODIFIER_TYPES = Platform.INSTANCE.registry(SURFACE_MODIFIER_TYPES_KEY);
public static final Registry<MapCodec<? extends RulePredicate>> SURFACE_PREDICATE_TYPES = Platform.INSTANCE.registry(SURFACE_PREDICATE_TYPES_KEY);
public static final Registry<MapCodec<? extends ConditionPredicate>> SURFACE_CONDITION_PREDICATE_TYPES = Platform.INSTANCE.registry(SURFACE_CONDITION_PREDICATE_TYPES_KEY);
public static final Registry<MapCodec<? extends RuleFinder>> SURFACE_FINDER_TYPES = Platform.INSTANCE.registry(SURFACE_FINDER_TYPES_KEY);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
import com.mojang.serialization.MapCodec;
import dev.lukebemish.biomesquisher.surface.AfterModifier;
import dev.lukebemish.biomesquisher.surface.AllPredicate;
import dev.lukebemish.biomesquisher.surface.AlwaysConditionPredicate;
import dev.lukebemish.biomesquisher.surface.AlwaysPredicate;
import dev.lukebemish.biomesquisher.surface.AndConditionPredicate;
import dev.lukebemish.biomesquisher.surface.BeforeModifier;
import dev.lukebemish.biomesquisher.surface.BiomeConditionPredicate;
import dev.lukebemish.biomesquisher.surface.ConditionPredicate;
import dev.lukebemish.biomesquisher.surface.IfTruePredicate;
import dev.lukebemish.biomesquisher.surface.ManyFinder;
import dev.lukebemish.biomesquisher.surface.AndPredicate;
import dev.lukebemish.biomesquisher.surface.AnyPredicate;
Expand All @@ -13,8 +18,11 @@
import dev.lukebemish.biomesquisher.surface.FindModifier;
import dev.lukebemish.biomesquisher.surface.MatchingFinder;
import dev.lukebemish.biomesquisher.surface.MatchingModifier;
import dev.lukebemish.biomesquisher.surface.NeverConditionPredicate;
import dev.lukebemish.biomesquisher.surface.NeverPredicate;
import dev.lukebemish.biomesquisher.surface.NotConditionPredicate;
import dev.lukebemish.biomesquisher.surface.NotPredicate;
import dev.lukebemish.biomesquisher.surface.OrConditionPredicate;
import dev.lukebemish.biomesquisher.surface.OrPredicate;
import dev.lukebemish.biomesquisher.surface.CheckFinder;
import dev.lukebemish.biomesquisher.surface.PrependModifier;
Expand All @@ -25,6 +33,7 @@
import dev.lukebemish.biomesquisher.surface.RulePredicate;
import dev.lukebemish.biomesquisher.surface.SequenceModifier;
import dev.lukebemish.biomesquisher.surface.ThenRunFinder;
import dev.lukebemish.biomesquisher.surface.TypeConditionPredicate;
import dev.lukebemish.biomesquisher.surface.TypePredicate;

import java.util.function.BiConsumer;
Expand Down Expand Up @@ -53,6 +62,17 @@ public static void predicates(BiConsumer<String, MapCodec<? extends RulePredicat
consumer.accept("any", AnyPredicate.CODEC);
consumer.accept("all", AllPredicate.CODEC);
consumer.accept("type", TypePredicate.CODEC);
consumer.accept("if_true", IfTruePredicate.CODEC);
}

public static void conditionPredicates(BiConsumer<String, MapCodec<? extends ConditionPredicate>> consumer) {
consumer.accept("and", AndConditionPredicate.CODEC);
consumer.accept("or", OrConditionPredicate.CODEC);
consumer.accept("not", NotConditionPredicate.CODEC);
consumer.accept("always", AlwaysConditionPredicate.CODEC);
consumer.accept("never", NeverConditionPredicate.CODEC);
consumer.accept("type", TypeConditionPredicate.CODEC);
consumer.accept("biome", BiomeConditionPredicate.CODEC);
}

public static void finders(BiConsumer<String, MapCodec<? extends RuleFinder>> consumer) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dev.lukebemish.biomesquisher.surface;

import com.mojang.serialization.MapCodec;
import net.minecraft.world.level.levelgen.SurfaceRules;

public final class AlwaysConditionPredicate implements ConditionPredicate {
private AlwaysConditionPredicate() {}

public static final AlwaysConditionPredicate INSTANCE = new AlwaysConditionPredicate();

public static final MapCodec<AlwaysConditionPredicate> CODEC = MapCodec.unit(INSTANCE);

@Override
public MapCodec<? extends ConditionPredicate> codec() {
return CODEC;
}

@Override
public boolean matches(RuleModifier.Context context, SurfaceRules.ConditionSource source) {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.lukebemish.biomesquisher.surface;

import com.mojang.serialization.MapCodec;
import net.minecraft.world.level.levelgen.SurfaceRules;

import java.util.List;

public record AndConditionPredicate(List<ConditionPredicate> predicates) implements ConditionPredicate {
public static final MapCodec<AndConditionPredicate> CODEC = ConditionPredicate.CODEC.listOf().fieldOf("predicates").xmap(AndConditionPredicate::new, AndConditionPredicate::predicates);

@Override
public MapCodec<AndConditionPredicate> codec() {
return CODEC;
}

@Override
public boolean matches(RuleModifier.Context context, SurfaceRules.ConditionSource source) {
return predicates.stream().allMatch(p -> p.matches(context, source));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.lukebemish.biomesquisher.surface;

import com.mojang.serialization.MapCodec;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.levelgen.SurfaceRules;

public record BiomeConditionPredicate(ResourceKey<Biome> key) implements ConditionPredicate {
public static final MapCodec<BiomeConditionPredicate> CODEC = ResourceKey.codec(Registries.BIOME).fieldOf("key").xmap(BiomeConditionPredicate::new, BiomeConditionPredicate::key);

@Override
public MapCodec<BiomeConditionPredicate> codec() {
return CODEC;
}

@Override
public boolean matches(RuleModifier.Context context, SurfaceRules.ConditionSource source) {
if (SurfaceRuleModifierUtils.isBiome(source)) {
var predicate = SurfaceRuleModifierUtils.biomeNameTest(source);
return predicate.test(key);
} else {
SurfaceRuleModifierUtils.warnOnNonBiome(context, source);
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dev.lukebemish.biomesquisher.surface;

import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import dev.lukebemish.biomesquisher.BiomeSquisherRegistries;
import net.minecraft.world.level.levelgen.SurfaceRules;

import java.util.function.Function;

public interface ConditionPredicate {
Codec<ConditionPredicate> CODEC = BiomeSquisherRegistries.SURFACE_CONDITION_PREDICATE_TYPES.byNameCodec()
.dispatch(ConditionPredicate::codec, Function.identity());

MapCodec<? extends ConditionPredicate> codec();

boolean matches(RuleModifier.Context context, SurfaceRules.ConditionSource source);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dev.lukebemish.biomesquisher.surface;

import com.mojang.serialization.MapCodec;
import net.minecraft.world.level.levelgen.SurfaceRules;

public record IfTruePredicate(ConditionPredicate predicate) implements RulePredicate {
public static final MapCodec<IfTruePredicate> CODEC = ConditionPredicate.CODEC.fieldOf("predicate").xmap(IfTruePredicate::new, IfTruePredicate::predicate);

@Override
public MapCodec<IfTruePredicate> codec() {
return CODEC;
}

@Override
public boolean matches(RuleModifier.Context context, SurfaceRules.RuleSource ruleSource) {
if (SurfaceRuleModifierUtils.isTest(ruleSource)) {
var ifTrue = SurfaceRuleModifierUtils.ifTrue(ruleSource);
return predicate.matches(context, ifTrue);
} else {
SurfaceRuleModifierUtils.warnOnNonTest(context, ruleSource);
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dev.lukebemish.biomesquisher.surface;

import com.mojang.serialization.MapCodec;
import net.minecraft.world.level.levelgen.SurfaceRules;

public final class NeverConditionPredicate implements ConditionPredicate {
private NeverConditionPredicate() {}

public static final NeverConditionPredicate INSTANCE = new NeverConditionPredicate();

public static final MapCodec<NeverConditionPredicate> CODEC = MapCodec.unit(INSTANCE);

@Override
public MapCodec<NeverConditionPredicate> codec() {
return CODEC;
}

@Override
public boolean matches(RuleModifier.Context context, SurfaceRules.ConditionSource source) {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.lukebemish.biomesquisher.surface;

import com.mojang.serialization.MapCodec;
import net.minecraft.world.level.levelgen.SurfaceRules;

public record NotConditionPredicate(ConditionPredicate predicate) implements ConditionPredicate {
public static final MapCodec<NotConditionPredicate> CODEC = ConditionPredicate.CODEC.fieldOf("predicate").xmap(NotConditionPredicate::new, NotConditionPredicate::predicate);

@Override
public MapCodec<NotConditionPredicate> codec() {
return CODEC;
}

@Override
public boolean matches(RuleModifier.Context context, SurfaceRules.ConditionSource source) {
return !predicate.matches(context, source);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.lukebemish.biomesquisher.surface;

import com.mojang.serialization.MapCodec;
import net.minecraft.world.level.levelgen.SurfaceRules;

import java.util.List;

public record OrConditionPredicate(List<ConditionPredicate> predicates) implements ConditionPredicate {
public static final MapCodec<OrConditionPredicate> CODEC = ConditionPredicate.CODEC.listOf().fieldOf("predicates").xmap(OrConditionPredicate::new, OrConditionPredicate::predicates);

@Override
public MapCodec<OrConditionPredicate> codec() {
return CODEC;
}

@Override
public boolean matches(RuleModifier.Context context, SurfaceRules.ConditionSource source) {
return predicates.stream().anyMatch(p -> p.matches(context, source));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.levelgen.SurfaceRules;

import java.util.List;
import java.util.function.Predicate;

final class SurfaceRuleModifierUtils {
private SurfaceRuleModifierUtils() {}

static final ResourceKey<MapCodec<? extends SurfaceRules.RuleSource>> SEQUENCE = ResourceKey.create(Registries.MATERIAL_RULE, new ResourceLocation("sequence"));
static final ResourceKey<MapCodec<? extends SurfaceRules.RuleSource>> TEST = ResourceKey.create(Registries.MATERIAL_RULE, new ResourceLocation("condition"));
static final ResourceKey<MapCodec<? extends SurfaceRules.ConditionSource>> BIOME = ResourceKey.create(Registries.MATERIAL_CONDITION, new ResourceLocation("biome"));

static boolean isSequence(SurfaceRules.RuleSource source) {
return BuiltInRegistries.MATERIAL_RULE.getResourceKey(source.codec().codec()).orElse(null) == SEQUENCE;
Expand Down Expand Up @@ -72,4 +75,21 @@ static SurfaceRules.ConditionSource ifTrue(SurfaceRules.RuleSource sequence) {
static void warnOnNonTest(RuleModifier.Context context, SurfaceRules.RuleSource source) {
Utils.LOGGER.warn("In surface rule modifier {}, expected minecraft:condition rule source, got {}", context.modifierKey(), source);
}

public static boolean isBiome(SurfaceRules.ConditionSource source) {
return BuiltInRegistries.MATERIAL_CONDITION.getResourceKey(source.codec().codec()).orElse(null) == BIOME;
}

@Open(
targetName = "net.minecraft.world.level.levelgen.SurfaceRules$BiomeConditionSource",
name = "biomeNameTest",
type = Open.Type.GET_INSTANCE
)
static Predicate<ResourceKey<Biome>> biomeNameTest(SurfaceRules.ConditionSource source) {
throw new UnsupportedOperationException("Replaced by OpenSesame at compile time");
}

static void warnOnNonBiome(RuleModifier.Context context, SurfaceRules.ConditionSource source) {
Utils.LOGGER.warn("In surface rule modifier {}, expected minecraft:biome condition source, got {}", context.modifierKey(), source);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dev.lukebemish.biomesquisher.surface;

import com.mojang.serialization.MapCodec;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.levelgen.SurfaceRules;

public record TypeConditionPredicate(ResourceKey<MapCodec<? extends SurfaceRules.ConditionSource>> key) implements ConditionPredicate {
public static final MapCodec<TypeConditionPredicate> CODEC = ResourceKey.codec(Registries.MATERIAL_CONDITION).fieldOf("key").xmap(TypeConditionPredicate::new, TypeConditionPredicate::key);

@Override
public MapCodec<TypeConditionPredicate> codec() {
return CODEC;
}

@Override
public boolean matches(RuleModifier.Context context, SurfaceRules.ConditionSource source) {
return BuiltInRegistries.MATERIAL_CONDITION.getResourceKey(source.codec().codec()).orElse(null) == key;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class BiomeSquisherMod {
static final List<Registry<?>> NEW_REGISTRIES = List.of(
BiomeSquisherRegistries.SURFACE_MODIFIER_TYPES,
BiomeSquisherRegistries.SURFACE_PREDICATE_TYPES,
BiomeSquisherRegistries.SURFACE_CONDITION_PREDICATE_TYPES,
BiomeSquisherRegistries.SURFACE_FINDER_TYPES
);

Expand Down Expand Up @@ -91,6 +92,9 @@ private void registerCodecs(RegisterEvent event) {
event.register(BiomeSquisherRegistries.SURFACE_PREDICATE_TYPES_KEY, helper ->
SurfaceModifierBootstrap.predicates((s, c) -> helper.register(Utils.id(s), c))
);
event.register(BiomeSquisherRegistries.SURFACE_CONDITION_PREDICATE_TYPES_KEY, helper ->
SurfaceModifierBootstrap.conditionPredicates((s, c) -> helper.register(Utils.id(s), c))
);
event.register(BiomeSquisherRegistries.SURFACE_FINDER_TYPES_KEY, helper ->
SurfaceModifierBootstrap.finders((s, c) -> helper.register(Utils.id(s), c))
);
Expand Down

0 comments on commit 35d852e

Please sign in to comment.