Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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;
import org.apache.logging.log4j.LogManager;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map.Entry;

Expand Down Expand Up @@ -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<Species> 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);
Expand All @@ -50,13 +70,16 @@ private MathOperator processElement(String key, JsonElement value) {
}

ArrayList<MathOperator> 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<String, JsonElement> entry = parameter.getAsJsonObject().entrySet().iterator().next();
m = processElement(entry.getKey(), entry.getValue());
} else if (parameter.isJsonPrimitive()) {
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -386,18 +416,28 @@ 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;
}

@Override
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;
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down