Skip to content

Commit

Permalink
Lot of code cleanup and few fixes + changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kanzaji committed Mar 28, 2024
1 parent c6fce4b commit a0e217d
Show file tree
Hide file tree
Showing 18 changed files with 256 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,40 @@ public BiFunction<JsonElement, Path, Boolean> getRequiredIntRange(long min, long
(array? NON_EMPTY_ARRAY_REQUIRED.apply(element, path): assertNotArray(element, path)) && getIntRange(min, max).apply(element, path);
}

/**
* Used to get validator of Max Y Level range. Automatically changes the Int Range depending on the value of min field.
* @param minName Name of the min y level field.
* @return BiFunction used as validator.
*/
public BiFunction<JsonElement, Path, Boolean> getMaxYLevelValidation(String minName) {
return (element, path) -> {
if (!assertParentObject(element, path)) return false;
JsonObject parent = element.getAsJsonObject();
JsonElement value = parent.get(name);
JsonElement min = parent.get(minName);
int minValue = -64;

if (!REQUIRED.apply(value, path)) return false;

if (Objects.isNull(min)) {
LOGGER.warn("Field \"%s\" is missing! Can't accurately verify \"%s\" in file \"%s\".".formatted(minName, name, Validator.obfuscatePath(path)));
} else {
try {
if (!min.isJsonPrimitive()) throw new ClassCastException();
minValue = min.getAsInt();
if (minValue > 320) {
LOGGER.error("Field \"%s\" is above max 320! Can't accurately verify \"%s\" in file \"%s\".".formatted(minName, name, Validator.obfuscatePath(path)));
minValue = -64;
}
} catch (ClassCastException e) {
LOGGER.error("Field \"%s\" requires a Numeric value! Can't accurately verify \"%s\" in file \"%s\".".formatted(minName, name, Validator.obfuscatePath(path)));
}
}

return getIntRange(minValue, 320, false).apply(value, path);
};
}

/**
* Used to get validator of another JsonObject.
* @param validators Map with validators for JsonObject to verify.
Expand Down Expand Up @@ -736,14 +770,78 @@ public BiFunction<JsonElement, Path, Boolean> getPassParentToValidators(Map<Stri
if (!assertParentObject(parent, path)) return false;
JsonObject parentObj = parent.getAsJsonObject();
JsonElement child = parentObj.get(name);
if (Objects.nonNull(child) && (array? !assertArray(child, path): !assertNotArray(child, path))) return false;
if (Objects.nonNull(child) && (array? !NON_EMPTY_ARRAY_REQUIRED.apply(child, path) : !assertNotArray(child, path))) return false;
return passTempToValidators(parentObj, child, path, validators, required);
};
}

/**
* Used to execute the specified validator only if the specified fieldName is set. Requires the specified field to be a boolean.
* Prints "Field x should not be present when x field is not set" if fieldName doesn't exist.
* @param fieldName Name of the Field which should be set.
* @param validator Validator to execute if fieldName is present.
* @return BiFunction used as a validator.
* @apiNote Requires use of _rg suffix.
*/
public BiFunction<JsonElement, Path, Boolean> getIfOtherFieldSet(String fieldName, BiFunction<JsonElement, Path, Boolean> validator) {
return (element, path) -> {
if (!assertParentObject(element, path)) return false;
JsonObject parent = element.getAsJsonObject();
JsonElement validatedField = parent.get(name);

if (isOtherFieldSet(fieldName, parent)) return validator.apply(validatedField, path);
if (Objects.nonNull(validatedField)) LOGGER.warn("\"%s\" should not be present when \"%s\" is not set in file \"%s\"".formatted(name, fieldName, Validator.obfuscatePath(path)));
return true;
};
}

/**
* Used to execute the specified validator only if the specified fieldName is present.
* Prints "Field x should not be present when x field is not present" if fieldName doesn't exist.
* @param fieldName Name of the Field which should be set.
* @param validator Validator to execute if fieldName is present.
* @return BiFunction used as a validator.
* @apiNote Requires use of _rg suffix.
*/
public BiFunction<JsonElement, Path, Boolean> getIfOtherFieldPresent(String fieldName, BiFunction<JsonElement, Path, Boolean> validator) {
return (element, path) -> {
if (!assertParentObject(element, path)) return false;
JsonObject parent = element.getAsJsonObject();
JsonElement validatedField = parent.get(name);

if (isOtherFieldPresent(fieldName, parent)) return validator.apply(validatedField, path);
if (Objects.nonNull(validatedField)) LOGGER.warn("\"%s\" should not be present when \"%s\" is not present in file \"%s\"".formatted(name, fieldName, Validator.obfuscatePath(path)));
return true;
};
}

/**
* Utility method for custom validators. Used to check if specified field is set in the provided object.
* @param fieldName Name of the field to check.
* @param object JsonObject to check if the specified field is set.
* @return Returns false if the value is not a boolean, otherwise returns set value of the field.
*/
public static boolean isOtherFieldSet(String fieldName, JsonObject object) {
JsonElement field = object.get(fieldName);
return Objects.nonNull(field) && field.isJsonPrimitive() && field.getAsJsonPrimitive().isBoolean() && field.getAsJsonPrimitive().getAsBoolean();
}

/**
* Utility method for custom validators. Used to check if specified field is present in the provided object.
* @param fieldName Name of the field to check.
* @param object JsonObject to check if the specified field is set.
* @return True if field exists, otherwise false.
* @apiNote If you need to also check if the boolean value of this field is true/false, use {@link Validator#isOtherFieldSet(String, JsonObject)} instead.
*/
public static boolean isOtherFieldPresent(String fieldName, JsonObject object) {
return Objects.nonNull(object.get(fieldName));
}

/**
* Used to pass a provided object to the validators by adding it to the TEMP field of the object
* (or for all entries in the array) and validation will be launched as normal.
* @param tempObject Object to pass in the TEMP field.
* @param element JsonObject or JsonArray of JsonObject, to which TEMP field should be added.
* @param validators Map with validators.
* @param required Determines if the value is required or not.
* @return BiFunction used as validator.
Expand All @@ -752,7 +850,7 @@ public BiFunction<JsonElement, Path, Boolean> getPassParentToValidators(Map<Stri
*/
public boolean passTempToValidators(JsonObject tempObject, @Nullable JsonElement element, Path path, Map<String, BiFunction<JsonElement, Path, Boolean>> validators, boolean required) {
if (Objects.isNull(element)) return (required? REQUIRED.apply(null, path): true);
if (element.isJsonPrimitive() || element.isJsonNull()) {
if (!element.isJsonObject() && !element.isJsonArray()) {
LOGGER.error("Expected array or object for \"%s\" in file \"%s\".".formatted(name, obfuscatePath(path)));
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,40 +53,6 @@ public class DepositValidators {
validators.put(EEDeposits.DepositType.TEST.getType(), new LinkedHashMap<>(Map.of("config", new Validator("config").getRequiredObjectValidation(TestDepositConfigModel.validators, false))));
}

/**
* Provider of MaxYLevelValidation. Requires use of _rg suffix to work.
* @apiNote This is meant to be modified and moved to {@link Validator} class!
*/
public static BiFunction<JsonElement, Path, Boolean> getMaxYLevelValidation(Validator validator, String minName) {
//TODO: Add additional configuration option and move it to validator
return (element, path) -> {
if (!validator.assertParentObject(element, path)) return false;
JsonObject parent = element.getAsJsonObject();
JsonElement value = parent.get(validator.getName());
JsonElement min = parent.get(minName);
int minValue = -64;

if (!validator.REQUIRED.apply(value, path)) return false;

if (Objects.isNull(min)) {
LOGGER.warn("Field \"%s\" is missing! Can't accurately verify \"%s\" in file \"%s\".".formatted(minName, validator.getName(), Validator.obfuscatePath(path)));
} else {
try {
if (!min.isJsonPrimitive()) throw new ClassCastException();
minValue = min.getAsInt();
if (minValue > 320) {
LOGGER.error("Field \"%s\" is above max 320! Can't accurately verify \"%s\" in file \"%s\".".formatted(minName, validator.getName(), Validator.obfuscatePath(path)));
minValue = -64;
}
} catch (ClassCastException e) {
LOGGER.error("Field \"%s\" requires a Numeric value! Can't accurately verify \"%s\" in file \"%s\".".formatted(minName, validator.getName(), Validator.obfuscatePath(path)));
}
}

return validator.getIntRange(minValue, 320, false).apply(value, path);
};
}

/**
* Used to get a validator for Material Field, when Block and Tag fields are as an alternatives.
* Requires usage of _rg suffix.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class CommonBlockDefinitionModel {
);
}

return DepositValidators.getMaxYLevelValidation(maxValidator, "min").apply(parent, path);
return maxValidator.getMaxYLevelValidation("min").apply(parent, path);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import com.ridanisaurus.emendatusenigmatica.loader.Validator;
import com.ridanisaurus.emendatusenigmatica.loader.deposit.model.DepositValidators;
import com.ridanisaurus.emendatusenigmatica.loader.deposit.model.common.CommonBlockDefinitionModel;
import com.ridanisaurus.emendatusenigmatica.loader.deposit.model.sample.SampleBlockDefinitionModel;
import com.ridanisaurus.emendatusenigmatica.plugin.DefaultConfigPlugin;
Expand Down Expand Up @@ -77,11 +76,12 @@ public class DenseDepositConfigModel {
validators.put("chance", new Validator("chance").getRequiredIntRange(1, 100, false));
validators.put("size", new Validator("size").getRequiredIntRange(1, 48, false));
validators.put("minYLevel", new Validator("minYLevel").getRequiredIntRange(-64, 320, false));
validators.put("maxYLevel_rg", DepositValidators.getMaxYLevelValidation(new Validator("maxYLevel"), "minYLevel"));
validators.put("maxYLevel_rg", new Validator("maxYLevel").getMaxYLevelValidation("minYLevel"));
validators.put("placement", new Validator("placement").getAcceptsOnlyValidation(List.of("uniform", "triangle"), false));
validators.put("rarity", new Validator("rarity").getAcceptsOnlyValidation(List.of("common", "rare"), false));
validators.put("generateSamples",new Validator("generateSamples").REQUIRES_BOOLEAN);
validators.put("sampleBlocks", new Validator("sampleBlocks").getObjectValidation(SampleBlockDefinitionModel.validators, true));
Validator sampleValidator = new Validator("sampleBlocks");
validators.put("sampleBlocks_rg", sampleValidator.getIfOtherFieldSet("generateSamples", sampleValidator.getRequiredObjectValidation(SampleBlockDefinitionModel.validators, true)));
}

public DenseDepositConfigModel(List<CommonBlockDefinitionModel> blocks, List<String> fillerTypes, int chance, int size, int minYLevel, int maxYLevel, String placement, String rarity, boolean generateSamples, List<SampleBlockDefinitionModel> sampleBlocks) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public String getType() {
}

public int getChance() {
if (config.chance < 1 || config.chance > 100) throw new IllegalArgumentException("Chance for " + name + " is out of Range [1 - 100]");
// if (config.chance < 1 || config.chance > 100) throw new IllegalArgumentException("Chance for " + name + " is out of Range [1 - 100]");
return config.chance;
}

Expand All @@ -62,37 +62,37 @@ public int getPlacementChance() {
}

public int getMaxYLevel() {
if (config.maxYLevel < -64 || config.maxYLevel > 320) throw new IllegalArgumentException("Max Y for " + name + " is out of Range [-64 - 320]");
// if (config.maxYLevel < -64 || config.maxYLevel > 320) throw new IllegalArgumentException("Max Y for " + name + " is out of Range [-64 - 320]");
return config.maxYLevel;
}

public int getMinYLevel() {
if (config.minYLevel < -64 || config.minYLevel > 320) throw new IllegalArgumentException("Min Y for " + name + " is out of Range [-64 - 320]");
// if (config.minYLevel < -64 || config.minYLevel > 320) throw new IllegalArgumentException("Min Y for " + name + " is out of Range [-64 - 320]");
return config.minYLevel;
}

public List<CommonBlockDefinitionModel> getBlocks() {
if (config.blocks.isEmpty()) throw new IllegalArgumentException("Blocks for " + name + " cannot be empty.");
// if (config.blocks.isEmpty()) throw new IllegalArgumentException("Blocks for " + name + " cannot be empty.");
return config.blocks;
}

public List<String> getFillerTypes() {
if (config.fillerTypes.isEmpty()) throw new IllegalArgumentException("Filler Types for " + name + " cannot be empty.");
// if (config.fillerTypes.isEmpty()) throw new IllegalArgumentException("Filler Types for " + name + " cannot be empty.");
return config.fillerTypes;
}

public int getSize() {
if (config.size < 1 || config.size > 48) throw new IllegalArgumentException("Size for " + name + " is out of Range [1 - 48]");
// if (config.size < 1 || config.size > 48) throw new IllegalArgumentException("Size for " + name + " is out of Range [1 - 48]");
return config.size;
}

public String getPlacement() {
if (!config.placement.equals("uniform") && !config.placement.equals("triangle")) throw new IllegalArgumentException("Placement for " + name + " is invalid.");
// if (!config.placement.equals("uniform") && !config.placement.equals("triangle")) throw new IllegalArgumentException("Placement for " + name + " is invalid.");
return config.placement;
}

public String getRarity() {
if (!config.rarity.equals("common") && !config.rarity.equals("rare")) throw new IllegalArgumentException("Rarity for " + name + " is invalid.");
// if (!config.rarity.equals("common") && !config.rarity.equals("rare")) throw new IllegalArgumentException("Rarity for " + name + " is invalid.");
return config.rarity;
}

Expand All @@ -101,7 +101,7 @@ public boolean hasSamples() {
}

public List<SampleBlockDefinitionModel> getSampleBlocks() {
if (hasSamples() && config.sampleBlocks.isEmpty()) throw new IllegalArgumentException("Sample Blocks for " + name + " cannot be empty if generateSamples is set to true.");
// if (hasSamples() && config.sampleBlocks.isEmpty()) throw new IllegalArgumentException("Sample Blocks for " + name + " cannot be empty if generateSamples is set to true.");
return config.sampleBlocks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ public class DikeDepositConfigModel {
//TODO: Verify the real maximum size for dikes.
validators.put("size", new Validator("size").getRequiredIntRange(1, 64, false));
validators.put("minYLevel", new Validator("minYLevel").getRequiredIntRange(-64, 320, false));
validators.put("maxYLevel_rg", DepositValidators.getMaxYLevelValidation(new Validator("maxYLevel"), "minYLevel"));
validators.put("maxYLevel_rg", new Validator("maxYLevel").getMaxYLevelValidation("minYLevel"));
validators.put("placement", new Validator("placement").getAcceptsOnlyValidation(List.of("uniform", "triangle"), false));
validators.put("rarity", new Validator("rarity").getAcceptsOnlyValidation(List.of("common", "rare"), false));
validators.put("generateSamples",new Validator("generateSamples").REQUIRES_BOOLEAN);
validators.put("sampleBlocks", new Validator("sampleBlocks").getObjectValidation(SampleBlockDefinitionModel.validators, true));
Validator sampleValidator = new Validator("sampleBlocks");
validators.put("sampleBlocks_rg", sampleValidator.getIfOtherFieldSet("generateSamples", sampleValidator.getRequiredObjectValidation(SampleBlockDefinitionModel.validators, true)));
}

public DikeDepositConfigModel(List<CommonBlockDefinitionModel> blocks, List<String> fillerTypes, int chance, int size, int minYLevel, int maxYLevel, String placement, String rarity, boolean generateSamples, List<SampleBlockDefinitionModel> sampleBlocks) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public String getType() {
}

public int getChance() {
if (config.chance < 1 || config.chance > 100) throw new IllegalArgumentException("Chance for " + name + " is out of Range [1 - 100]");
// if (config.chance < 1 || config.chance > 100) throw new IllegalArgumentException("Chance for " + name + " is out of Range [1 - 100]");
return config.chance;
}

Expand All @@ -38,38 +38,37 @@ public int getPlacementChance() {
}

public int getMaxYLevel() {
if (config.maxYLevel < -64 || config.maxYLevel > 320) throw new IllegalArgumentException("Max Y for " + name + " is out of Range [-64 - 320]");
// if (config.maxYLevel < -64 || config.maxYLevel > 320) throw new IllegalArgumentException("Max Y for " + name + " is out of Range [-64 - 320]");
return config.maxYLevel;
}

public int getMinYLevel() {
if (config.minYLevel < -64 || config.minYLevel > 320) throw new IllegalArgumentException("Min Y for " + name + " is out of Range [-64 - 320]");
// if (config.minYLevel < -64 || config.minYLevel > 320) throw new IllegalArgumentException("Min Y for " + name + " is out of Range [-64 - 320]");
return config.minYLevel;
}

public List<CommonBlockDefinitionModel> getBlocks() {
if (config.blocks.isEmpty()) throw new IllegalArgumentException("Blocks for " + name + " cannot be empty.");
// if (config.blocks.isEmpty()) throw new IllegalArgumentException("Blocks for " + name + " cannot be empty.");
return config.blocks;
}

public List<String> getFillerTypes() {
if (config.fillerTypes.isEmpty()) throw new IllegalArgumentException("Filler Types for " + name + " cannot be empty.");
// if (config.fillerTypes.isEmpty()) throw new IllegalArgumentException("Filler Types for " + name + " cannot be empty.");
return config.fillerTypes;
}

public int getSize() {
// See loader/deposit/model/dike/DikeDepositConfigModel.javaDikeDepositConfigModel for validation of the size.
// if (config.size < 1 || config.size > 16) throw new IllegalArgumentException("Size for " + name + " is out of Range [1 - 16]");
return config.size;
}

public String getPlacement() {
if (!config.placement.equals("uniform") && !config.placement.equals("triangle")) throw new IllegalArgumentException("Placement for " + name + " is invalid.");
// if (!config.placement.equals("uniform") && !config.placement.equals("triangle")) throw new IllegalArgumentException("Placement for " + name + " is invalid.");
return config.placement;
}

public String getRarity() {
if (!config.rarity.equals("common") && !config.rarity.equals("rare")) throw new IllegalArgumentException("Rarity for " + name + " is invalid.");
// if (!config.rarity.equals("common") && !config.rarity.equals("rare")) throw new IllegalArgumentException("Rarity for " + name + " is invalid.");
return config.rarity;
}

Expand All @@ -78,7 +77,7 @@ public boolean hasSamples() {
}

public List<SampleBlockDefinitionModel> getSampleBlocks() {
if (hasSamples() && config.sampleBlocks.isEmpty()) throw new IllegalArgumentException("Sample Blocks for " + name + " cannot be empty if generateSamples is set to true.");
// if (hasSamples() && config.sampleBlocks.isEmpty()) throw new IllegalArgumentException("Sample Blocks for " + name + " cannot be empty if generateSamples is set to true.");
return config.sampleBlocks;
}
}
Loading

0 comments on commit a0e217d

Please sign in to comment.