package com.gmail.nossr50.listeners;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.bukkit.TreeType;
import org.bukkit.block.BlockState;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.event.world.ChunkUnloadEvent;
import org.bukkit.event.world.StructureGrowEvent;
import org.bukkit.event.world.WorldInitEvent;
import org.bukkit.event.world.WorldUnloadEvent;

import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.runnables.blockstoreconversion.BlockStoreConversionMain;

public class WorldListener implements Listener {
    ArrayList<BlockStoreConversionMain> converters = new ArrayList<BlockStoreConversionMain>();

    @EventHandler (priority = EventPriority.MONITOR, ignoreCancelled = true)
    public void onStructureGrow(StructureGrowEvent event) {
        TreeType species = event.getSpecies();

        if (species == TreeType.BROWN_MUSHROOM || species == TreeType.RED_MUSHROOM) {
            return;
        }

        List<BlockState> blocks = event.getBlocks();

        for (BlockState block : blocks) {
            mcMMO.placeStore.setFalse(block.getBlock());
        }
    }

    @EventHandler
    public void onWorldInit(WorldInitEvent event) {
        File dataDir = new File(event.getWorld().getWorldFolder(), "mcmmo_data");
        if (!dataDir.exists()) {
            return;
        }

        if (mcMMO.p == null)
            return;

        mcMMO.p.getLogger().info("Converting block storage for " + event.getWorld().getName() + " to a new format.");
        BlockStoreConversionMain converter = new BlockStoreConversionMain(event.getWorld());
        converter.run();
        converters.add(converter);
    }

    @EventHandler
    public void onWorldUnload(WorldUnloadEvent event) {
        mcMMO.placeStore.unloadWorld(event.getWorld());
    }

    // This gets called every 45 seconds, by default.
    // The call can and does result in excessive lag, especially on larger servers.
    //@EventHandler
    //public void onWorldSave(WorldSaveEvent event) {
    //    mcMMO.placeStore.saveWorld(event.getWorld());
    //}

    @EventHandler
    public void onChunkUnload(ChunkUnloadEvent event) {
        mcMMO.placeStore.chunkUnloaded(event.getChunk().getX(), event.getChunk().getZ(), event.getWorld());
    }

    @EventHandler
    public void onChunkLoad(ChunkLoadEvent event) {
        if (event.getChunk().getEntities().length > 0)
            mcMMO.placeStore.loadChunk(event.getChunk().getX(), event.getChunk().getZ(), event.getWorld());
    }
}

@@ -83,31 +83,8 @@
 ist;
-%0Aimport java.util.List;
 %0A%0Aim
@@ -1070,39 +1070,55 @@
     
-List%3CBlockState%3E blocks = event
+if (mcMMO.placeStore.isTrue(event.getLocation()
 .get
@@ -1122,22 +1122,27 @@
 getBlock
-s
 ()
-;%0A%0A
+)) %7B%0A    
         
@@ -1169,18 +1169,33 @@
 k : 
-b
+event.getB
 locks
+()
 ) %7B%0A
+    
     
@@ -1243,24 +1243,41 @@
 etBlock());%0A
+            %7D   %0A
         %7D%0A  

Make our StructureGrowEvent only care about player-planted trees.