use of com.infinityraider.agricraft.api.v1.requirement in project AgriCraft by AgriCraft.
the class JsonPlant method initGrowthRequirement.
public static IAgriGrowthRequirement initGrowthRequirement(AgriPlant plant) {
// Run checks
if (plant == null) {
return AgriGrowthRequirement.getNone();
}
// Initialize utility objects
IAgriGrowthRequirement.Builder builder = AgriApi.getGrowthRequirementBuilder();
// Define requirement for humidity
String humidityString = plant.getRequirement().getHumiditySoilCondition().getCondition();
IAgriSoil.Humidity humidity = IAgriSoil.Humidity.fromString(humidityString).orElse(IAgriSoil.Humidity.INVALID);
handleSoilCriterion(humidity, builder::defineHumidity, plant.getRequirement().getHumiditySoilCondition().getType(), plant.getRequirement().getHumiditySoilCondition().getToleranceFactor(), () -> AgriCore.getLogger("agricraft").warn("Plant: \"{0}\" has an invalid humidity criterion (\"{1}\")!", plant.getId(), humidityString));
// Define requirement for acidity
String acidityString = plant.getRequirement().getAciditySoilCondition().getCondition();
IAgriSoil.Acidity acidity = IAgriSoil.Acidity.fromString(acidityString).orElse(IAgriSoil.Acidity.INVALID);
handleSoilCriterion(acidity, builder::defineAcidity, plant.getRequirement().getAciditySoilCondition().getType(), plant.getRequirement().getAciditySoilCondition().getToleranceFactor(), () -> AgriCore.getLogger("agricraft").warn("Plant: \"{0}\" has an invalid acidity criterion (\"{1}\")!", plant.getId(), acidityString));
// Define requirement for nutrients
String nutrientString = plant.getRequirement().getNutrientSoilCondition().getCondition();
IAgriSoil.Nutrients nutrients = IAgriSoil.Nutrients.fromString(nutrientString).orElse(IAgriSoil.Nutrients.INVALID);
handleSoilCriterion(nutrients, builder::defineNutrients, plant.getRequirement().getNutrientSoilCondition().getType(), plant.getRequirement().getNutrientSoilCondition().getToleranceFactor(), () -> AgriCore.getLogger("agricraft").warn("Plant: \"{0}\" has an invalid nutrients criterion (\"{1}\")!", plant.getId(), nutrientString));
// Define requirement for light
final double f = plant.getRequirement().getLightToleranceFactor();
final int minLight = plant.getRequirement().getMinLight();
final int maxLight = plant.getRequirement().getMaxLight();
builder.defineLightLevel((strength, light) -> {
int lower = minLight - (int) (f * strength);
int upper = maxLight + (int) (f * strength);
return light >= lower && light <= upper ? IAgriGrowthResponse.FERTILE : IAgriGrowthResponse.INFERTILE;
});
// Define requirement for nearby blocks
plant.getRequirement().getConditions().forEach(obj -> {
BlockPos min = new BlockPos(obj.getMinX(), obj.getMinY(), obj.getMinZ());
BlockPos max = new BlockPos(obj.getMaxX(), obj.getMaxY(), obj.getMaxZ());
builder.addCondition(builder.blockStatesNearby(obj.convertAll(BlockState.class), obj.getAmount(), min, max));
});
// Define requirement for seasons
List<AgriSeason> seasons = plant.getRequirement().getSeasons().stream().map(AgriSeason::fromString).filter(Optional::isPresent).map(Optional::get).distinct().collect(Collectors.toList());
if (seasons.size() > 0) {
builder.defineSeasonality((str, season) -> {
if (str >= AgriApi.getStatRegistry().strengthStat().getMax() || seasons.stream().anyMatch(season::matches)) {
return IAgriGrowthResponse.FERTILE;
} else {
return IAgriGrowthResponse.INFERTILE;
}
});
}
// Define requirement for fluids
List<Fluid> fluids = plant.getRequirement().getFluid().convertAll(FluidState.class).stream().map(FluidState::getFluid).distinct().collect(Collectors.toList());
BiFunction<Integer, Fluid, IAgriGrowthResponse> response = (strength, fluid) -> {
if (fluids.size() > 0) {
if (fluids.contains(fluid)) {
return IAgriGrowthResponse.FERTILE;
}
return fluid.equals(Fluids.LAVA) ? IAgriGrowthResponse.KILL_IT_WITH_FIRE : IAgriGrowthResponse.LETHAL;
} else {
if (fluid.equals(Fluids.LAVA)) {
return IAgriGrowthResponse.KILL_IT_WITH_FIRE;
}
return fluid.equals(Fluids.EMPTY) ? IAgriGrowthResponse.FERTILE : IAgriGrowthResponse.LETHAL;
}
};
builder.defineFluid(response);
// Build the growth requirement
IAgriGrowthRequirement req = builder.build();
// Log warning if no soils exist for this requirement combination
if (noSoilsMatch(req)) {
AgriCore.getLogger("agricraft").warn("Plant: \"{0}\" has no valid soils to plant on for any strength level!", plant.getId());
}
// Return the growth requirements
return req;
}
Aggregations