Interface VariantRule
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Before implementing this interface, check the static factory methods, a suitable implementation may already exist.
To combine multiple variant rules into one, see firstMatch(VariantRule...)
.
-
Method Summary
Modifier and TypeMethodDescriptionstatic VariantRule
Returns a variant rule that always returns the rule with the provided variant ID.apply
(@Unmodifiable Map<String, BoundEntityVariant> variants, EntitySpawnContext context) Picks the variant to apply to an entity upon spawning.static VariantRule
first()
Returns a rule that selects the first variant registered for an entity.static VariantRule
firstMatch
(VariantRule... rules) Combines multiple variant rules into a single rule by evaluating them in order, from first to last.static VariantRule
ifBiome
(BiomeFilter filter, String variantId) Returns a rule that returns the provided variant only if the provided biome filter tests positive on the biome of the entity's spawn location.static VariantRule
ifTemperature
(org.apache.commons.lang3.DoubleRange temperatureRange, String variantId) Returns a rule that returns the provided variant only if the temperature of the entity's spawn location is in the provided temperature range.static VariantRule
Returns a rule that returns the provided variant only if the provided world key matches the one of the entity's spawn location.static VariantRule
Returns a rule that returns the provided variant only if one of the provided world keys matches the one of the entity's spawn location.static VariantRule
none()
Returns a variant rule that always returnsnull
, no variant (model) will be applied.static VariantRule
random()
Returns a rule that randomly selects one of the available variants.static VariantRule
Returns a rule that selects a variant based on weighted chances.
-
Method Details
-
apply
@Nullable BoundEntityVariant apply(@Unmodifiable Map<String, BoundEntityVariant> variants, EntitySpawnContext context) Picks the variant to apply to an entity upon spawning.The returned
BoundEntityVariant
must be one of the values in the providedvariants
map. If a variant is returned that does not exist in this map, an exception will be thrown.The
variants
map may be empty.Only the data exposed by the
context
should be used in this method. Accessing other properties of the entity may be unsafe at this stage.- Parameters:
variants
- an unmodifiable map of variant IDs to bound variantscontext
- a safe snapshot of contextual data from the spawning entity- Returns:
- the variant to apply, or
null
if no variant (model) should be applied
-
first
Returns a rule that selects the first variant registered for an entity. If no variants are available, returnsnull
.This is the default
VariantRule
used when no rule is provided in entity injection.Note: Unless this entity has no variants, this rule will never return
null
. Passing it tofirstMatch(VariantRule...)
will result in all following rules being ignored.- Returns:
- a rule that always selects the first available variant or
null
if none are available
-
random
Returns a rule that randomly selects one of the available variants. If no variants are available, returnsnull
.Note: Unless this entity has no variants, this rule will never return
null
. Passing it tofirstMatch(VariantRule...)
will result in all following rules being ignored.- Returns:
- a rule that selects a random variant
-
weighted
Returns a rule that selects a variant based on weighted chances.For example:
In this case, theVariantRule.weighted(Map.of( "normal", 10, "rare", 1 ));
"normal"
variant is 10× more likely to be selected than"rare"
. Only the variants explicitly listed in the chance map may be selected. Unlisted variants and variants with weight less than or equal to0
are silently ignored.Note: This rule will never return
null
, passing it tofirstMatch(VariantRule...)
will result in all following rules being ignored.Note: If the rule attempts to select a non-existent variant ID from the provided map, an
IllegalArgumentException
is thrown.- Parameters:
weights
- a map of variant IDs to their selection weight (positive integers)- Returns:
- a chance based rule
- Throws:
IllegalArgumentException
- if theweights
map is empty
-
none
Returns a variant rule that always returnsnull
, no variant (model) will be applied.- Returns:
- a variant rule always returning
null
-
always
Returns a variant rule that always returns the rule with the provided variant ID.Note: This rule will never return
null
, passing it tofirstMatch(VariantRule...)
will result in all following rules being ignored.Note: If the variant ID does not exist and this rule is applied, an
IllegalArgumentException
is thrown.- Parameters:
variantId
- the ID of the variant to apply- Returns:
- a variant rule always returning a variant matching the provided ID
-
ifBiome
Returns a rule that returns the provided variant only if the provided biome filter tests positive on the biome of the entity's spawn location. If the biome does not match,null
is returned.For example:
VariantRule.ifBiome(BiomeFilter.key(Key.key("minecraft:plains")), "normal");
You may omit the
"minecraft:"
namespace for vanilla biomes.This rule is designed to be chained using
firstMatch(VariantRule...)
.Note: If the variant ID does not exist and this rule is applied, an
IllegalArgumentException
is thrown.- Parameters:
filter
- the biome filter to testvariantId
- the ID of the variant to apply- Returns:
- a biome based rule
-
ifWorld
Returns a rule that returns the provided variant only if the provided world key matches the one of the entity's spawn location. If the world does not match,null
is returned.For example:
VariantRule.ifWorld(Key.key("minecraft:overworld"), "normal");
You may omit the
"minecraft:"
namespace for vanilla worlds.This rule is designed to be chained using
firstMatch(VariantRule...)
.Note: If the variant ID does not exist and this rule is applied, an
IllegalArgumentException
is thrown.- Parameters:
worldKey
- the world key to matchvariantId
- the ID of the variant to apply- Returns:
- a world based rule
- See Also:
-
ifWorlds
Returns a rule that returns the provided variant only if one of the provided world keys matches the one of the entity's spawn location. If the world does not match,null
is returned.For example:
VariantRule.ifWorlds(Set.of( Key.key("minecraft:the_end"), Key.key("minecraft:the_nether") ), "normal");
You may omit the
"minecraft:"
namespace for vanilla worlds.This rule is designed to be chained using
firstMatch(VariantRule...)
.Note: If the variant ID does not exist and this rule is applied, an
IllegalArgumentException
is thrown.- Parameters:
worldKeys
- the world keys to matchvariantId
- the ID of the variant to apply- Returns:
- a world based rule
- See Also:
-
ifTemperature
static VariantRule ifTemperature(org.apache.commons.lang3.DoubleRange temperatureRange, @Namespace String variantId) Returns a rule that returns the provided variant only if the temperature of the entity's spawn location is in the provided temperature range. If not,null
is returned.For example:
VariantRule.ifTemperature(DoubleRange.of(Double.MIN_VALUE, 0.5), "cold");
You may also use the predefined ranges in
BiomeTemperature
.This rule is designed to be chained using
firstMatch(VariantRule...)
.Note: If the variant ID does not exist and this rule is applied, an
IllegalArgumentException
is thrown.- Parameters:
temperatureRange
- the temperature range when the variant is appliedvariantId
- the ID of the variant to apply- Returns:
- a temperature based rule
- See Also:
-
firstMatch
Combines multiple variant rules into a single rule by evaluating them in order, from first to last.The first rule that returns a non-null
BoundEntityVariant
will be used. All subsequent rules are ignored. This creates layered fallback logic where more specific rules are prioritized.Variant rules can be categorized into two types:
- Nullable rules (allow fallthrough, e.g,
ifBiome(BiomeFilter, String)
,ifWorld(Key, String)
,ifTemperature(DoubleRange, String)
) - Non-null rules (always return a rule and stop evaluation, e.g.,
always(String)
,random()
,weighted(Map)
)
null
(likealways(String)
) should only be used as the final rule in the chain, acting as a fallback. Using a non-null rule earlier in the sequence will prevent any following rules from being evaluated.Example (complex) usage:
VariantRule.firstMatch( // complicated custom rule: // if the entity is named BOB, apply "bob" variant (variants, ctx) -> { Component name = ctx.getEntity().customName(); if (name == null) return null; String nameStr = PlainTextComponentSerializer.plainText().serialize(name); return nameStr.equalsIgnoreCase("bob") ? variants.get("bob") : null; }, // apply "desert" variant in deserts VariantRule.ifBiome(Key.key("minecraft:desert"), "desert"), // apply "jungle" variant in jungles VariantRule.ifBiomes(Set.of( Key.key("minecraft:jungle"), Key.key("minecraft:bamboo_jungle"), Key.key("minecraft:sparse_jungle"), ), "jungle"), // apply "warm" variant warm biomes other than desert and jungle VariantRule.ifTemperature(BiomeTemperature.WARM, "warm"), // apply "cold" variant in cold biomes VariantRule.ifTemperature(BiomeTemperature.COLD, "cold"), // fallback: apply "normal" variant in all other biomes VariantRule.always("normal") )
- Parameters:
rules
- the variant rules to evaluate in order- Returns:
- a
VariantRule
returning the first non-null variant
- Nullable rules (allow fallthrough, e.g,
-