package com.gmail.nossr50.listeners;

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

import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.event.world.ChunkUnloadEvent;
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
    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());
    }
}

@@ -84,45 +84,171 @@
 st;%0A
-%0Aimport org.bukkit.event.EventHandler
+import java.util.List;%0A%0Aimport org.bukkit.TreeType;%0Aimport org.bukkit.block.BlockState;%0Aimport org.bukkit.event.EventHandler;%0Aimport org.bukkit.event.EventPriority
 ;%0Aim
@@ -365,32 +365,82 @@
 unkUnloadEvent;%0A
+import org.bukkit.event.world.StructureGrowEvent;%0A
 import org.bukki
@@ -779,16 +779,488 @@
 in%3E();%0A%0A
+    @EventHandler (priority = EventPriority.MONITOR, ignoreCancelled = true)%0A    public void onStructureGrow(StructureGrowEvent event) %7B%0A        TreeType species = event.getSpecies();%0A%0A        if (species == TreeType.BROWN_MUSHROOM %7C%7C species == TreeType.RED_MUSHROOM) %7B%0A            return;%0A        %7D%0A%0A        List%3CBlockState%3E blocks = event.getBlocks();%0A%0A        for (BlockState block : blocks) %7B%0A            mcMMO.placeStore.setFalse(block.getBlock());%0A        %7D%0A    %7D%0A%0A
     @Eve

Fix issue where Tree Feller wouldn't work on the base of a player-planted tree. Fixes #524