package net.yyc12345.teyvatcraft.blocks;

import net.minecraft.block.BlockState;
import net.minecraft.block.SweetBerryBushBlock;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemConvertible;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class TeyvatHarvestablePlantBlock extends SweetBerryBushBlock {

    private Item droppedItem;

    public TeyvatHarvestablePlantBlock(Settings settings, Item droppedItem) {
        super(settings);
        this.droppedItem = droppedItem;
    }

    @Override
    public void onEntityCollision(BlockState state, World world, BlockPos pos, Entity entity) {
        // revoke sweet berry bush default operation
        return;
    }

    @Override
    public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
        int i = ((Integer)state.get(AGE)).intValue();
        boolean bl = (i == 3);
        if (!bl && player.getStackInHand(hand).getItem() == Items.BONE_MEAL)
            return ActionResult.PASS;
        if (i > 1) {
            int j = 1 + world.random.nextInt(2);
            dropStack(world, pos, new ItemStack((ItemConvertible)droppedItem, j + (bl ? 1 : 0)));
            world.playSound(null, pos, SoundEvents.ITEM_SWEET_BERRIES_PICK_FROM_BUSH, SoundCategory.BLOCKS, 1.0F, 0.8F + world.random.nextFloat() * 0.4F);
            world.setBlockState(pos, (BlockState)state.with(AGE, Integer.valueOf(1)), 2);
            return ActionResult.success(world.isClient);
        } 
        return super.onUse(state, world, pos, player, hand, hit);
    }
}
