package net.glowstone.block.blocktype;

import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
import net.glowstone.entity.GlowPlayer;
import org.bukkit.DoublePlantSpecies;
import org.bukkit.Material;
import org.bukkit.block.BlockFace;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.DoublePlant;
import org.bukkit.material.MaterialData;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

public class BlockDoublePlant extends BlockNeedsAttached implements IBlockGrowable {

    @Override
    public boolean canPlaceAt(GlowBlock block, BlockFace against) {
        final Material type = block.getRelative(BlockFace.DOWN).getType();
        if ((type == Material.GRASS || type == Material.DIRT || type == Material.SOIL) &&
                block.getRelative(BlockFace.UP).getType() == Material.AIR) {
            return true;
        }
        return false;
    }

    @Override
    public void afterPlace(GlowPlayer player, GlowBlock block, ItemStack holding, GlowBlockState oldState) {
        final GlowBlockState headBlockState = block.getRelative(BlockFace.UP).getState();
        headBlockState.setType(Material.DOUBLE_PLANT);
        headBlockState.setData(new DoublePlant(DoublePlantSpecies.PLANT_APEX));
        headBlockState.update(true);
    }

    @Override
    public Collection<ItemStack> getDrops(GlowBlock block, ItemStack tool) {
        final MaterialData data = block.getState().getData();
        if (data instanceof DoublePlant) {
            final DoublePlantSpecies species = ((DoublePlant) data).getSpecies();
            if (species != DoublePlantSpecies.DOUBLE_TALLGRASS && species != DoublePlantSpecies.LARGE_FERN && species != DoublePlantSpecies.PLANT_APEX) {
                return Collections.unmodifiableList(Arrays.asList(data.toItemStack(1)));
            }
        } else {
            warnMaterialData(DoublePlant.class, data);
        }
        return BlockDropless.EMPTY_STACK;
    }

    @Override
    public void blockDestroy(GlowPlayer player, GlowBlock block, BlockFace face) {
        final MaterialData data = block.getState().getData();
        if (data instanceof DoublePlant) {
            if (((DoublePlant) data).getSpecies() == DoublePlantSpecies.PLANT_APEX) {
                block.setType(Material.AIR);
                block.setData((byte) 0);
                block = block.getRelative(BlockFace.DOWN);
                if (!(block.getState().getData() instanceof DoublePlant)) {
                    return;
                }
            } else {
                block.setType(Material.AIR);
                block.setData((byte) 0);
                block = block.getRelative(BlockFace.UP);
                if (!(block.getState().getData() instanceof DoublePlant)) {
                    return;
                }
            }
            block.setType(Material.AIR);
            block.setData((byte) 0);
        } else {
            warnMaterialData(DoublePlant.class, data);
        }
    }

    @Override
    public boolean isFertilizable(GlowBlock block) {
        MaterialData data = block.getState().getData();
        if (data instanceof DoublePlant) {
            if (((DoublePlant) data).getSpecies() == DoublePlantSpecies.PLANT_APEX) {
                data = block.getRelative(BlockFace.DOWN).getState().getData();
                if (!(data instanceof DoublePlant)) {
                    return false;
                }
            }
            final DoublePlantSpecies species = ((DoublePlant) data).getSpecies(); 
            if (species != DoublePlantSpecies.DOUBLE_TALLGRASS && species != DoublePlantSpecies.LARGE_FERN) {
                return true;
            }
        } else {
            warnMaterialData(DoublePlant.class, data);
        }
        return false;
    }

    @Override
    public boolean canGrowWithChance(GlowBlock block) {
        return true;
    }

    @Override
    public void grow(GlowPlayer player, GlowBlock block) {
        MaterialData data = block.getState().getData();
        if (data instanceof DoublePlant) {
            if (((DoublePlant) data).getSpecies() == DoublePlantSpecies.PLANT_APEX) {
                data = block.getRelative(BlockFace.DOWN).getState().getData();
                if (!(data instanceof DoublePlant)) {
                    return;
                }
            }
            final DoublePlantSpecies species = ((DoublePlant) data).getSpecies();
            switch (species) {
                case SUNFLOWER:
                case LILAC:
                case ROSE_BUSH:
                case PEONY:
                    block.getWorld().dropItemNaturally(block.getLocation(), new ItemStack(data.toItemStack(1)));
                    break;
                default:
                    break;
            }
        } else {
            warnMaterialData(DoublePlant.class, data);
        }
    }
}

@@ -2211,167 +2211,159 @@
     
-if (((DoublePlant) data).getSpecies() == DoublePlantSpecies.PLANT_APEX) %7B%0A                block.setType(Material.AIR);%0A                block.setData((byte) 0);
+block.setType(Material.AIR);%0A            block.setData((byte) 0);%0A            if (((DoublePlant) data).getSpecies() == DoublePlantSpecies.PLANT_APEX) %7B
 %0A   
@@ -2532,32 +2532,32 @@
               %7D%0A
+
             %7D el
@@ -2565,94 +2565,8 @@
 e %7B%0A
-                block.setType(Material.AIR);%0A                block.setData((byte) 0);%0A
     

optimize double plant breaking