use of com.infinityraider.agricraft.api.v1.fertilizer.IAgriFertilizable in project AgriCraft by AgriCraft.
the class BlockCrop method checkOrUseBonemeal.
/**
* Helper method for the IGrowable interface. Checks if bonemeal can be applied at the requested
* position. Can also then apply the bonemeal fertilizer if the check passes and the last
* parameter is set to true. Uses AgriCraft's Fertilizer system, specifically whatever adapter
* is registered for bonemeal/ItemDye.
*
* Will throw runtime exceptions if either: - world is null, - pos is null, - rand is null while
* tryToApplyBonemeal is true, - there is no IAgriFertilizable object at the BlockPos, -OR- -
* bonemeal (i.e. ItemDye with meta 15) is not registered as a fertilizer.
*
* @param world The world to check.
* @param rand A source of randomness. Only necessary when tryToApplyBonemeal is true, can be
* null otherwise.
* @param pos The location of crop to check.
* @param tryToApplyBonemeal When true, will also apply the bonemeal if it's accepted by the
* crop.
* @return true if both the IGrowable interface is enabled, and the crop accepts bonemeal
* currently. false if either the IGrowable interface is disabled, or the crop does not accept
* bonemeal. The return value is not dependent on the result of applying the bonemeal.
*/
private boolean checkOrUseBonemeal(@Nonnull World world, @Nullable Random rand, @Nonnull BlockPos pos, boolean tryToApplyBonemeal) {
// Sanity check the parameters.
Objects.requireNonNull(world, "IGrowable on BlockCrop can't function with a null world parameter.");
Objects.requireNonNull(pos, "IGrowable on BlockCrop can't function with a null pos parameter.");
if (tryToApplyBonemeal) {
Objects.requireNonNull(rand, "IGrowable#grow on BlockCrop can't function with a null rand parameter.");
}
// Get the crop that is being targeted.
IAgriFertilizable crop = WorldHelper.getTile(world, pos, IAgriFertilizable.class).orElseThrow(() -> new RuntimeException("There is no IAgriFertilizable at: " + pos));
// Get the AgriCraft fertilizer representation of bonemeal.
IAgriFertilizer meal = AgriApi.getFertilizerRegistry().valueOf(BONEMEAL).orElseThrow(() -> new RuntimeException("Bonemeal is not registered as a fertilizer."));
// Use those two references to perform the check.
boolean canApplyBonemeal = crop.acceptsFertilizer(meal);
// If the caller has requested it, and the crop allows it, then also apply the bonemeal now.
if (tryToApplyBonemeal && canApplyBonemeal) {
crop.onApplyFertilizer(meal, rand);
}
// Regardless of the outcome of applying the fertilizer, return the result of the check itself.
return canApplyBonemeal;
}
use of com.infinityraider.agricraft.api.v1.fertilizer.IAgriFertilizable in project AgriCraft by AgriCraft.
the class JsonFertilizer method applyFertilizer.
@Override
public ActionResultType applyFertilizer(World world, BlockPos pos, IAgriFertilizable target, ItemStack stack, Random random, @Nullable LivingEntity entity) {
if (target instanceof IAgriPlantProvider) {
IAgriCrop crop = ((IAgriCrop) target);
String type = "neutral";
for (int i = 0; i < this.potency; i++) {
if (fertilizerEffect.isNegativelyAffected(crop.getPlant().getId())) {
if (fertilizerEffect.canReduceGrowth() && random.nextBoolean()) {
type = "negative";
IAgriGrowthStage current = crop.getGrowthStage();
IAgriGrowthStage previous = current.getPreviousStage(crop, random);
if (current.equals(previous)) {
if (fertilizerEffect.canKillPlant()) {
crop.removeGenome();
}
} else {
crop.setGrowthStage(previous);
}
}
} else {
if (crop.hasPlant() && this.fertilizerEffect.canFertilize(crop.getPlant().getId())) {
target.applyGrowthTick();
type = "positive";
} else if (crop.isCrossCrop() && this.canTriggerMutation()) {
target.applyGrowthTick();
type = "positive";
} else if (this.canTriggerWeeds()) {
target.applyGrowthTick();
type = "positive";
}
}
}
this.spawnParticles(world, pos, type, random);
if ((entity instanceof PlayerEntity) && !(((PlayerEntity) entity).isCreative())) {
stack.shrink(1);
}
return ActionResultType.SUCCESS;
}
return ActionResultType.FAIL;
}
Aggregations