package com.khorn.terraincontrol.bukkit.events;

import com.khorn.terraincontrol.bukkit.TCPlugin;
import com.khorn.terraincontrol.bukkit.commands.MapCommand;
import com.khorn.terraincontrol.configuration.standard.PluginStandardValues;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerRegisterChannelEvent;
import org.bukkit.event.world.StructureGrowEvent;
import org.bukkit.event.world.WorldInitEvent;
import org.bukkit.event.world.WorldUnloadEvent;

import java.util.ArrayList;
import java.util.List;

public class TCListener implements Listener
{
    private final TCPlugin tcPlugin;
    private final TCSender tcSender;
    private final SaplingListener saplingListener;

    public TCListener(TCPlugin plugin)
    {
        this.tcPlugin = plugin;
        this.tcSender = new TCSender(plugin);
        this.saplingListener = new SaplingListener();
        Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
    }

    @EventHandler(priority = EventPriority.HIGH)
    public void onWorldInit(WorldInitEvent event)
    {
        this.tcPlugin.onWorldInit(event.getWorld());
    }

    @EventHandler
    public void onWorldUnload(WorldUnloadEvent event)
    {
        this.tcPlugin.onWorldUnload(event.getWorld());
    }

    @EventHandler(priority = EventPriority.NORMAL)
    public void onStructureGrow(StructureGrowEvent event)
    {
        saplingListener.onStructureGrow(event);
    }


    @EventHandler
    public void onPlayerRegisterChannel(PlayerRegisterChannelEvent event)
    {
        // Sends custom colors on join
        if (event.getChannel().equals(PluginStandardValues.ChannelName))
        {
            tcSender.send(event.getPlayer());
        }
    }

    @EventHandler
    public void onPlayerChangedWorld(PlayerChangedWorldEvent event)
    {
        // Resends the packet so that the biomes are right again
        Player player = event.getPlayer();
        if (player.getListeningPluginChannels().contains(PluginStandardValues.ChannelName))
        {
            tcSender.send(player);
        }
    }
    
    //>>	TESTING CODE, REMOVE WHEN DONE
    /**/    @EventHandler
    /**/    public void onPlayerJoin(PlayerJoinEvent event)
    /**/    {
    /**/        if (event.getPlayer().getDisplayName().equalsIgnoreCase("timethor"))
    /**/        {
    /**/            List<String> args = new ArrayList<String>();
    /**/            args.add("-s");
    /**/            args.add("600");
    /**/            new MapCommand(this.tcPlugin).onCommand(event.getPlayer(), args);
    /**/        }
    /**/    }
    //>>	END TESTING CODE, REMOVE WHEN DONE

}

@@ -95,68 +95,8 @@
 in;%0A
-import com.khorn.terraincontrol.bukkit.commands.MapCommand;%0A
 impo
@@ -168,16 +168,16 @@
 Values;%0A
+
 import o
@@ -398,56 +398,8 @@
 nt;%0A
-import org.bukkit.event.player.PlayerJoinEvent;%0A
 impo
@@ -545,24 +545,24 @@
 dInitEvent;%0A
+
 import org.b
@@ -602,60 +602,8 @@
 t;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 publ
@@ -2145,556 +2145,8 @@
    %7D
-%0A    %0A    //%3E%3E%09TESTING CODE, REMOVE WHEN DONE%0A    /**/    @EventHandler%0A    /**/    public void onPlayerJoin(PlayerJoinEvent event)%0A    /**/    %7B%0A    /**/        if (event.getPlayer().getDisplayName().equalsIgnoreCase(%22timethor%22))%0A    /**/        %7B%0A    /**/            List%3CString%3E args = new ArrayList%3CString%3E();%0A    /**/            args.add(%22-s%22);%0A    /**/            args.add(%22600%22);%0A    /**/            new MapCommand(this.tcPlugin).onCommand(event.getPlayer(), args);%0A    /**/        %7D%0A    /**/    %7D%0A    //%3E%3E%09END TESTING CODE, REMOVE WHEN DONE
 %0A%0A%7D%0A

Remove testing code