package net.glowstone.block.blocktype;

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

import org.bukkit.Material;
import org.bukkit.block.BlockFace;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.FlowerPot;
import org.bukkit.material.MaterialData;
import org.bukkit.util.Vector;

import net.glowstone.GlowChunk;
import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
import net.glowstone.block.entity.TEFlowerPot;
import net.glowstone.block.entity.TileEntity;
import net.glowstone.block.state.GlowFlowerPot;
import net.glowstone.entity.GlowPlayer;

public class BlockFlowerPot extends BlockType {

    @Override
    public Collection<ItemStack> getDrops(GlowBlock block) {
        List<ItemStack> drops = Arrays.asList(new ItemStack(Material.FLOWER_POT));
        GlowBlockState state = block.getState();

        if (state instanceof GlowFlowerPot) {
            MaterialData contents = ((GlowFlowerPot) state).getContents();

            if (contents != null) {
                drops.add(new ItemStack(contents.getItemType(), 1, contents.getData()));
            }
        }
        return Collections.unmodifiableList(drops);
    }

    @Override
    public TileEntity createTileEntity(GlowChunk chunk, int cx, int cy, int cz) {
        return new TEFlowerPot(chunk.getBlock(cx, cy, cz));
    }

    @Override
    public boolean blockInteract(GlowPlayer player, GlowBlock block, BlockFace face, Vector clickedLoc) {
        GlowBlockState state = block.getState();
        MaterialData data = state.getData();

        if (!(data instanceof FlowerPot)) {
            warnMaterialData(FlowerPot.class, data);
            return false;
        }
        if (state instanceof GlowFlowerPot) {
            GlowFlowerPot pot = (GlowFlowerPot) state;
            // Only change contents if there is none.
            if (pot.getContents() == null) {
                // Todo: check if the item is valid (null-check too) as flower pot contents.
                pot.setContents(player.getItemInHand().getData().clone());
                pot.update();
                return true;
            }
        }
        return false;
    }
}

@@ -141,16 +141,48 @@
 .List;%0A%0A
+import org.bukkit.GrassSpecies;%0A
 import o
@@ -309,24 +309,62 @@
 .FlowerPot;%0A
+import org.bukkit.material.LongGrass;%0A
 import org.b
@@ -1934,187 +1934,579 @@
     
-// Only change contents if there is none.%0A            if (pot.getContents() == null) %7B%0A                // Todo: check if the item is valid (null-check too) as flower pot contents.
+ItemStack heldItem = player.getItemInHand();%0A            // Only change contents if there is none and if the held item is valid pot contents.%0A            if (pot.getContents() == null && heldItem != null && isValidContents(heldItem.getData())) %7B%0A                pot.setContents(heldItem.getData().clone()); // Null-check in isValidContents.%0A                pot.update();%0A                return true;%0A            %7D%0A        %7D%0A        return false;%0A    %7D%0A%0A    private boolean isValidContents(MaterialData contents) %7B%0A        if (contents == null) %7B%0A            return false;
 %0A   
@@ -2506,32 +2506,35 @@
 se;%0A        
+%7D%0A%0A
         
 pot.setConte
@@ -2525,104 +2525,216 @@
     
-pot.setContents(player.getItemInHand().getData().clone());%0A                pot.update();%0A       
+switch (contents.getItemType()) %7B%0A        case DEAD_BUSH:%0A        case RED_ROSE:%0A        case YELLOW_FLOWER:%0A        case RED_MUSHROOM:%0A        case BROWN_MUSHROOM:%0A        case CACTUS:%0A        case SAPLING:%0A
     
@@ -2730,32 +2730,35 @@
 PLING:%0A         
+   
 return true;%0A   
@@ -2766,32 +2766,194 @@
     
-    %7D%0A        %7D%0A
+case LONG_GRASS:%0A            // The only allowed tall grass type is the fern.%0A            return ((LongGrass) contents).getSpecies() == GrassSpecies.FERN_LIKE;%0A        default:%0A 
         
 retu
@@ -2940,32 +2940,35 @@
 fault:%0A         
+   
 return false;%0A  
@@ -2961,20 +2961,30 @@
 turn false;%0A
+        %7D%0A
     %7D%0A%7D%0A

Validate held item for pot contents on interact