package net.glowstone.block.blocktype;

import java.util.Arrays;

import org.bukkit.Material;
import org.bukkit.block.BlockFace;

import net.glowstone.GlowWorld;
import net.glowstone.block.GlowBlock;

public class BlockSoil extends BlockType {

    private Material[] possibleCrops = {Material.CROPS, Material.CARROT, Material.POTATO, Material.MELON_STEM, Material.PUMPKIN_STEM};

    @Override
    public boolean canTickRandomly() {
        return true;
    }

    @Override
    public void updateBlock(GlowBlock block) {
        if (isNearWater(block) || block.getWorld().hasStorm()) { // better to ensure that rain is falling on block later
            block.setData((byte) 7); // set this block as fully wet
        } else if (block.getData() > 0) {
            block.setData((byte) (block.getData() - 1)); // if this block is wet, it becomes less wet
        } else if (!Arrays.asList(possibleCrops).contains(block.getRelative(BlockFace.UP).getType())) {
            // turns block back to dirt if nothing is planted on
            block.setType(Material.DIRT);
            block.setData((byte) 0);
        }
    }

    private boolean isNearWater(GlowBlock block) {
        // check around for some water blocks
        final GlowWorld world = block.getWorld();
        for (int x = block.getX() - 4; x <= block.getX() + 4; x++) {
            for (int z = block.getZ() - 4; z <= block.getZ() + 4; z++) {
                for (int y = block.getY(); y <= block.getY() + 1; y++) {
                    if (world.getBlockAt(x, y, z).getType().equals(Material.WATER)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
}

@@ -122,16 +122,97 @@
 ckFace;%0A
+import org.bukkit.event.block.BlockFadeEvent;%0A%0Aimport net.glowstone.EventFactory;
 %0Aimport 
@@ -273,16 +273,59 @@
 owBlock;
+%0Aimport net.glowstone.block.GlowBlockState;
 %0A%0Apublic
@@ -1150,29 +1150,88 @@
             
-block
+final GlowBlockState state = block.getState();%0A            state
 .setType(Mat
@@ -1251,33 +1251,36 @@
             
-block
+state
 .set
+Raw
 Data((byte) 
@@ -1283,16 +1283,230 @@
 te) 0);%0A
+            BlockFadeEvent fadeEvent = new BlockFadeEvent(block, state);%0A            EventFactory.callEvent(fadeEvent);%0A            if (!fadeEvent.isCancelled()) %7B%0A                state.update(true);%0A            %7D%0A
         

Call BlockFadeEvent when soil turns back to dirt