package net.tropicraft.core.common.block;

import net.minecraft.block.*;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.state.IntegerProperty;
import net.minecraft.state.StateContainer;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.state.properties.DoubleBlockHalf;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import net.minecraftforge.common.IPlantable;

import java.util.Random;

public class PineappleBlock extends TallFlowerBlock implements IGrowable, IPlantable {

    /** Number of total random ticks it takes for this pineapple to grow */
    public static final int TOTAL_GROW_TICKS = 7;

    public static final IntegerProperty STAGE = BlockStateProperties.AGE_0_7;

    public PineappleBlock(final Properties properties) {
        super(properties);
        this.setDefaultState(super.getDefaultState().with(STAGE, 0));
    }

    @Override
    protected void fillStateContainer(final StateContainer.Builder<Block, BlockState> builder) {
        builder.add(HALF, STAGE);
    }

    @Override
    public boolean canGrow(IBlockReader iBlockReader, BlockPos blockPos, BlockState blockState, boolean b) {
        return false;
    }

    @Override
    public boolean canUseBonemeal(World world, Random random, BlockPos blockPos, BlockState blockState) {
        return false;
    }

    @Override
    public void grow(final World world, final Random random, final BlockPos pos, final BlockState state) {
        final int currentStage = state.get(STAGE);
        if (currentStage < TOTAL_GROW_TICKS) {
            final BlockState growthState = state.with(STAGE, currentStage + 1);
            world.setBlockState(pos, growthState, 4);
        } else {
            final BlockState above = world.getBlockState(pos.up());

            // Don't bother placing if it's already there
            if (above.getBlock() == this) return;
            if (state.get(HALF) == DoubleBlockHalf.UPPER) return;

            // Place actual pineapple plant above stem
            final BlockState fullGrowth = state.with(HALF, DoubleBlockHalf.UPPER);
            world.setBlockState(pos.up(), fullGrowth, 3);
        }
    }

    @Override
    public void tick(final BlockState state, final World world, final BlockPos pos, final Random random) {
        checkFlowerChange(world, pos, state);
        if (pos.getY() > world.getHeight() - 2) {
            return;
        }

        // Current metadata
        int growth = state.get(STAGE);

        if (state.getBlock() == this && growth <= TOTAL_GROW_TICKS && world.isAirBlock(pos.up()) && state.get(HALF) == DoubleBlockHalf.LOWER) {
            if (growth >= TOTAL_GROW_TICKS - 1) {
                // Set current state
                BlockState growthState = state.with(STAGE, TOTAL_GROW_TICKS);
                world.setBlockState(pos, growthState, 4);

                // Place actual pineapple plant above stem
                BlockState fullGrowth = state.with(HALF, DoubleBlockHalf.UPPER);
                world.setBlockState(pos.up(), fullGrowth, 3);
            } else {
                BlockState growthState = state.with(STAGE, growth + 1);
                world.setBlockState(pos, growthState, 4);
            }
        }
    }

    private boolean isFullyGrown(BlockState state) {
        return state.get(STAGE) == TOTAL_GROW_TICKS || state.get(HALF) == DoubleBlockHalf.UPPER;
    }

    @Deprecated
    public void neighborChanged(BlockState state, World world, BlockPos pos1, Block block, BlockPos pos2, boolean b) {
        super.neighborChanged(state, world, pos1, block, pos2, b);

        checkForDrop(world, pos1, state);
    }

    protected final boolean checkForDrop(World world, BlockPos pos, BlockState state) {
        if (canBlockStay(world, pos, state)) {
            return true;
        } else {
            spawnDrops(state, world, pos);
            world.setBlockState(pos, Blocks.AIR.getDefaultState(), 4);
            return false;
        }
    }

    protected void checkFlowerChange(World world, BlockPos pos, BlockState state) {
        if (!world.isRemote && !canBlockStay(world, pos, state)) {
            if (isFullyGrown(state)) {
                spawnDrops(state, world, pos);
            }
            world.setBlockState(pos, Blocks.AIR.getDefaultState(), 4);
        }
    }

    public boolean canBlockStay(World worldIn, BlockPos pos, BlockState state) {
        if (state.get(HALF) == DoubleBlockHalf.UPPER) {
            return worldIn.getBlockState(pos.down()).getBlock() == this;
        } else {
            BlockState iblockstate = worldIn.getBlockState(pos.up());
            if (iblockstate.getBlock() == this) {
                return false;
            } else { // If just the stem
                return canPlaceBlockAt(worldIn, pos);
            }
        }
    }

    public boolean canPlaceBlockAt(World worldIn, BlockPos pos) {
        BlockState state = worldIn.getBlockState(pos.down());
        Block block = state.getBlock();

        if (block.canSustainPlant(state, worldIn, pos.down(), Direction.UP, this)) {
            return true;
        }

        return false;
    }

    @Override
    public void onBlockPlacedBy(World p_180633_1_, BlockPos p_180633_2_, BlockState p_180633_3_, LivingEntity p_180633_4_, ItemStack p_180633_5_) {
        // override super behavior of placing top half of double flower by default
    }
}

@@ -544,16 +544,65 @@
 antable;
+%0Aimport net.minecraftforge.common.util.Constants;
 %0A%0Aimport
@@ -2481,54 +2481,8 @@
 ) %7B%0A
-        checkFlowerChange(world, pos, state);%0A
     
@@ -2989,17 +2989,79 @@
 hState, 
-4
+Constants.BlockFlags.DEFAULT %7C Constants.BlockFlags.NO_RERENDER
 );%0A%0A    
@@ -3147,33 +3147,39 @@
 te fullGrowth = 
-s
+growthS
 tate.with(HALF, 
@@ -3252,33 +3252,60 @@
 (), fullGrowth, 
-3
+Constants.BlockFlags.DEFAULT
 );%0A            %7D
@@ -3442,1107 +3442,87 @@
 te, 
-4);%0A            %7D%0A        %7D%0A    %7D%0A%0A    private boolean isFullyGrown(BlockState state) %7B%0A        return state.get(STAGE) == TOTAL_GROW_TICKS %7C%7C state.get(HALF) == DoubleBlockHalf.UPPER;%0A    %7D%0A%0A    @Deprecated%0A    public void neighborChanged(BlockState state, World world, BlockPos pos1, Block block, BlockPos pos2, boolean b) %7B%0A        super.neighborChanged(state, world, pos1, block, pos2, b);%0A%0A        checkForDrop(world, pos1, state);%0A    %7D%0A%0A    protected final boolean checkForDrop(World world, BlockPos pos, BlockState state) %7B%0A        if (canBlockStay(world, pos, state)) %7B%0A            return true;%0A        %7D else %7B%0A            spawnDrops(state, world, pos);%0A            world.setBlockState(pos, Blocks.AIR.getDefaultState(), 4);%0A            return false;%0A        %7D%0A    %7D%0A%0A    protected void checkFlowerChange(World world, BlockPos pos, BlockState state) %7B%0A        if (!world.isRemote && !canBlockStay(world, pos, state)) %7B%0A            if (isFullyGrown(state)) %7B%0A                spawnDrops(state, world, pos);%0A            %7D%0A            world.setBlockState(pos, Blocks.AIR.getDefaultState(), 4);
+Constants.BlockFlags.DEFAULT %7C Constants.BlockFlags.NO_RERENDER);%0A            %7D
 %0A   

Fix pineapple block behavior