Search in sources :

Example 1 with IAgriFertilizer

use of com.infinityraider.agricraft.api.v1.fertilizer.IAgriFertilizer in project AgriCraft by AgriCraft.

the class BlockCrop method onBlockActivated.

/*
     * Handles right-clicks from the player (a.k.a usage).
     * <br>
     * When the block is right clicked, the behaviour depends on the crop, and
     * what item it was clicked with.
     */
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {
    // Step 0. Abort if remote.
    if (world.isRemote) {
        // I'm not sure if this is right, but oh well.
        return true;
    }
    // Step 1. Fetch the crop.
    TileEntityCrop crop = WorldHelper.getTile(world, pos, TileEntityCrop.class).orElse(null);
    // Step 2. Give up if the crop doesn't exist;
    if (crop == null) {
        // Allow others to use the click event.
        return false;
    }
    // Step 3. If the player is not holding anything, then harvest the crop.
    if (heldItem == null) {
        crop.onHarvest(player);
        return true;
    }
    // Step 4. If the held item is excluded from handling, skip it.
    if (TypeHelper.isAnyType(heldItem.getItem(), ITEM_EXCLUDES)) {
        // Allow the excludes to do their things.
        return false;
    }
    // Step 5. If the held item is a type of fertilizer, apply it.
    if (AgriApi.getFertilizerRegistry().hasAdapter(heldItem)) {
        Optional<IAgriFertilizer> fert = AgriApi.getFertilizerRegistry().valueOf(heldItem);
        return fert.isPresent() && fert.get().applyFertilizer(player, world, pos, crop, heldItem, crop.getRandom());
    }
    // Step 6. If the held item is crops, attempt to make cross-crops.
    if (heldItem.getItem() == AgriItems.getInstance().CROPS) {
        // Attempt to apply crop-sticks to crop.
        if (crop.onApplyCrops(player) == MethodResult.SUCCESS) {
            // If player isn't in creative remove an item from the stack.
            if (!player.isCreative()) {
                heldItem.stackSize--;
            }
            // The application was a success!
            return true;
        }
    }
    // Step 7. Attempt to resolve held item as a seed.
    final Optional<AgriSeed> seed = AgriApi.getSeedRegistry().valueOf(heldItem);
    // Step 8. If held item is a seed, attempt to plant it in the crop.
    if (seed.isPresent()) {
        if (crop.onApplySeeds(player, seed.get()) == MethodResult.SUCCESS) {
            // The planting was a success!
            return true;
        }
    }
    // Step 8. If we can't do anything else, give up and attemp to harvest instead.
    crop.onHarvest(player);
    //Returning true will prevent other things from happening
    return true;
}
Also used : TileEntityCrop(com.infinityraider.agricraft.tiles.TileEntityCrop) IAgriFertilizer(com.infinityraider.agricraft.api.v1.fertilizer.IAgriFertilizer) AgriSeed(com.infinityraider.agricraft.api.v1.seed.AgriSeed)

Example 2 with IAgriFertilizer

use of com.infinityraider.agricraft.api.v1.fertilizer.IAgriFertilizer 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;
}
Also used : IAgriFertilizable(com.infinityraider.agricraft.api.v1.fertilizer.IAgriFertilizable) IAgriFertilizer(com.infinityraider.agricraft.api.v1.fertilizer.IAgriFertilizer)

Aggregations

IAgriFertilizer (com.infinityraider.agricraft.api.v1.fertilizer.IAgriFertilizer)2 IAgriFertilizable (com.infinityraider.agricraft.api.v1.fertilizer.IAgriFertilizable)1 AgriSeed (com.infinityraider.agricraft.api.v1.seed.AgriSeed)1 TileEntityCrop (com.infinityraider.agricraft.tiles.TileEntityCrop)1