package com.hyperfresh.mchyperchat.bukkit;

import com.hyperfresh.mchyperchat.HyperChat;
import com.hyperfresh.mchyperchat.Player;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.server.ServerCommandEvent;

public class BukkitListener implements Listener
{
	private BukkitPlugin plugin;
	private HyperChat hyperChat;

	public BukkitListener(HyperChat hyperChat, BukkitPlugin plugin)
	{
		this.plugin = plugin;
		this.hyperChat = hyperChat;
	}

	@EventHandler
	public void onPlayerJoin(PlayerJoinEvent event)
	{
		plugin.addPlayer(event.getPlayer());
	}

	@EventHandler
	public void onPlayerQuit(PlayerQuitEvent event)
	{
		plugin.removePlayer(event.getPlayer());
	}

	@EventHandler
	public void onPlayerChat(AsyncPlayerChatEvent event)
	{
		event.setCancelled(true);

		Player player = plugin.getPlayer(event.getPlayer());

		hyperChat.getEventPoster().onPlayerChat(player, event.getMessage());
	}

	@EventHandler
	public void onConsoleCommand(ServerCommandEvent event)
	{
		if(event.getSender().equals(Bukkit.getConsoleSender()))
		{
			String[] args = event.getCommand().split(" ");
			if(args[0].equals("say"))
			{
				event.setCommand("");

				//remove the "say " part of the command
				hyperChat.getEventPoster().onConsoleChat(event.getCommand().substring(4));
			}
		}
	}
}

@@ -120,24 +120,67 @@
 hat.Player;%0A
+import org.apache.commons.lang.ArrayUtils;%0A
 import org.b
@@ -1382,35 +1382,8 @@
 %09%09%7B%0A
-%09%09%09%09event.setCommand(%22%22);%0A%0A
 %09%09%09%09
@@ -1399,17 +1399,16 @@
 the %22say
- 
 %22 part o
@@ -1421,16 +1421,118 @@
 command%0A
+%09%09%09%09String said = String.join(%22 %22, (String%5B%5D)ArrayUtils.remove(args, 0));%0A%0A%09%09%09%09event.setCommand(%22%22);%0A%0A
 %09%09%09%09hype
@@ -1572,39 +1572,12 @@
 hat(
-event.getCommand().substring(4)
+said
 );%0A%09

fix onConsoleCommand giving an IndexOutOfBoundsException