package tw.davy.minecraft.treelogging.bukkit.listener;

import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;

import java.util.ArrayList;

import tw.davy.minecraft.treelogging.bukkit.TreeLoggingPlugin;
import tw.davy.minecraft.treelogging.bukkit.helper.TreeDestroyer;
import tw.davy.minecraft.treelogging.bukkit.helper.TreeDetector;
import tw.davy.minecraft.treelogging.bukkit.helper.TreeRecorder;

/**
 * Listener for logging tree blocks.
 *
 * @author Davy
 */
public final class BlockListener implements Listener {
    private final TreeLoggingPlugin mPlugin;

    public BlockListener(final TreeLoggingPlugin plugin) {
        mPlugin = plugin;
    }

    @EventHandler
    public void onBlockBreak(final BlockBreakEvent event) {
        if (event.isCancelled()) {
            return;
        }

        Block block = event.getBlock();
        TreeRecorder.remove(mPlugin, block);
        ArrayList<Block> targetBlocks = TreeDetector.detect(mPlugin, block);
        if (targetBlocks != null) {
            TreeDestroyer.destroy(mPlugin, targetBlocks);
        }
    }

    @EventHandler
    public void onBlockPlace(final BlockPlaceEvent event) {
        if (event.isCancelled()) {
            return;
        }

        if (event.getPlayer() != null) {
            TreeRecorder.record(mPlugin, event.getBlockPlaced());
        }
    }
}

@@ -67,27 +67,88 @@
 .bukkit.
-block.Block
+GameMode;%0Aimport org.bukkit.block.Block;%0Aimport org.bukkit.entity.Player
 ;%0Aimport
@@ -306,16 +306,64 @@
 ceEvent;
+%0Aimport org.bukkit.event.block.LeavesDecayEvent;
 %0A%0Aimport
@@ -911,32 +911,56 @@
    @EventHandler
+(ignoreCancelled = true)
 %0A    public void
@@ -1016,85 +1016,120 @@
     
-if (event.isCancelled()) %7B%0A            return;%0A        %7D%0A%0A        Block block
+final Block block = event.getBlock();%0A        TreeRecorder.remove(mPlugin, block);%0A%0A        final Player crafter
  = e
@@ -1128,37 +1128,38 @@
 fter = event.get
-Block
+Player
 ();%0A        Tree
@@ -1158,53 +1158,128 @@
     
-TreeRecorder.remove(mPlugin, block)
+if (crafter != null && crafter.getGameMode() == GameMode.CREATIVE && crafter.isSneaking())%0A            return
 ;%0A
+%0A
         
 Arra
@@ -1274,16 +1274,22 @@
         
+final 
 ArrayLis
@@ -1374,34 +1374,32 @@
 tBlocks != null)
- %7B
 %0A            Tre
@@ -1441,26 +1441,16 @@
 locks);%0A
-        %7D%0A
     %7D%0A%0A 
@@ -1465,16 +1465,40 @@
 tHandler
+(ignoreCancelled = true)
 %0A    pub
@@ -1572,24 +1572,28 @@
 ent.
-isCancelled()) %7B
+getPlayer() != null)
 %0A   
@@ -1605,64 +1605,169 @@
     
+T
 re
-turn
+eRecorder.record(mPlugin, event.getBlockPlaced())
 ;%0A    
+%7D%0A%0A
     
-%7D%0A%0A        if (event.getPlayer() != null
+@EventHandler(ignoreCancelled = true)%0A    public void onLeavesDecay(final LeavesDecayEvent event
 ) %7B%0A
@@ -1766,36 +1766,32 @@
 vent) %7B%0A        
- 
-   
 TreeRecorder.rec
@@ -1781,36 +1781,36 @@
  TreeRecorder.re
-cord
+move
 (mPlugin, event.
@@ -1821,28 +1821,12 @@
 lock
-Placed());%0A        %7D
+());
 %0A   

Remove record when leaves decayed; Sneaking with creative won't craft tree.