Search in sources :

Example 1 with TileEntityCrop

use of com.infinityraider.agricraft.tiles.TileEntityCrop 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)

Aggregations

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