diff --git a/common/src/main/java/com/dtteam/dynamictrees/deserialization/JsonMath.java b/common/src/main/java/com/dtteam/dynamictrees/deserialization/JsonMath.java index 5adcdcb53..d2d70d824 100644 --- a/common/src/main/java/com/dtteam/dynamictrees/deserialization/JsonMath.java +++ b/common/src/main/java/com/dtteam/dynamictrees/deserialization/JsonMath.java @@ -1,6 +1,7 @@ package com.dtteam.dynamictrees.deserialization; import com.dtteam.dynamictrees.tree.species.Species; +import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import net.minecraft.util.RandomSource; @@ -8,6 +9,7 @@ import org.jetbrains.annotations.Nullable; import java.util.ArrayList; +import java.util.List; import java.util.Locale; import java.util.Map.Entry; @@ -41,6 +43,24 @@ private MathOperator getVariable(String name) { return NULL_OPERATOR; } + /** + * Attempt to parse each entry in a JsonArray, assuming each is intended to be a Species. + * Because a species entry is assumed to be a String, non-string entries are skipped. + * @param speciesArray JsonArray containing string entries to parse into Species instances. + * @return An array of valid Species, or an empty array if none are valid. + */ + private Species[] tryGetSpeciesList(JsonArray speciesArray) { + List validSpecies = new ArrayList<>(); + for (JsonElement listElement : speciesArray) { + if (listElement.isJsonPrimitive() && listElement.getAsJsonPrimitive().isString()) { + if (Species.findSpeciesSloppy(listElement.getAsJsonPrimitive().getAsString()) != Species.NULL_SPECIES) { + validSpecies.add(Species.findSpeciesSloppy(listElement.getAsJsonPrimitive().getAsString())); + } + } + } + return validSpecies.toArray(new Species[0]); + } + private MathOperator processElement(String key, JsonElement value) { MathFunction op = MathFunction.getFunction(key); @@ -50,13 +70,16 @@ private MathOperator processElement(String key, JsonElement value) { } ArrayList paramList = new ArrayList<>(); + Species[] speciesList = null; Species speciesArg = Species.NULL_SPECIES; //If the value is an array then these are the parameters for this operation if (value.isJsonArray()) { for (JsonElement parameter : value.getAsJsonArray()) { MathOperator m = NULL_OPERATOR; - if (parameter.isJsonObject()) { + if (parameter.isJsonArray()) { + speciesList = tryGetSpeciesList(parameter.getAsJsonArray()); + } else if (parameter.isJsonObject()) { Entry entry = parameter.getAsJsonObject().entrySet().iterator().next(); m = processElement(entry.getKey(), entry.getValue()); } else if (parameter.isJsonPrimitive()) { @@ -93,7 +116,14 @@ private MathOperator processElement(String key, JsonElement value) { case MAX -> new Maximum(paramArray); case MIN -> new Minimum(paramArray); case IFGT -> new IfGreaterThan(paramArray); - case SPECIES -> speciesArg != Species.NULL_SPECIES ? new IfSpecies(speciesArg, paramArray) : null; + case SPECIES -> { + if (speciesArg != Species.NULL_SPECIES) { + yield new IfSpecies(speciesArg, paramArray); + } else if (speciesList != null && speciesList.length > 0) { + yield new IfSpecies(speciesList, paramArray); + } + yield NULL_OPERATOR; + } case DEBUG -> new Debug(paramArray); default -> NULL_OPERATOR; }; @@ -386,10 +416,15 @@ public float apply(MathContext mc) { public static class IfSpecies implements MathOperator { private final MathOperator[] functions; - private final Species species; + private final Species[] speciesList; public IfSpecies(Species species, MathOperator[] functionArray) { - this.species = species; + this.speciesList = new Species[] {species}; + this.functions = functionArray; + } + + public IfSpecies(Species[] speciesList, MathOperator[] functionArray) { + this.speciesList = speciesList; this.functions = functionArray; } @@ -397,7 +432,12 @@ public IfSpecies(Species species, MathOperator[] functionArray) { public float apply(MathContext mc) { if (mc instanceof MathSpeciesContext && functions.length == 2) { - return ((MathSpeciesContext) mc).species == species ? functions[0].apply(mc) : functions[1].apply(mc); + for (Species species : speciesList) { + if (((MathSpeciesContext) mc).species == species) { + return functions[0].apply(mc); + } + } + return functions[1].apply(mc); } return 0.0f; diff --git a/gradle.properties b/gradle.properties index 52b10b672..eb8d0007d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,7 +9,7 @@ credits=Current developer: MaxHyper. Development of treepacks: Harley O'Connor. license=MIT description=Trees that grow, forests that spread... group=com.dtteam.dynamictrees -mod_version=1.7.1 +mod_version=1.7.2 fabricVersionAppend=-BETA neoVersionAppend= versionType=stable