package de.st_ddt.crazylogin;

import java.nio.charset.Charset;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener;

import de.st_ddt.crazylogin.crypt.CrazyCrypt1;
import de.st_ddt.crazyplugin.exceptions.CrazyException;

public class CrazyLoginMessageListener implements PluginMessageListener
{

	protected final CrazyLogin plugin;
	protected final Charset charset = Charset.forName("UTF-8");
	protected final CrazyCrypt1 encryptor = new CrazyCrypt1();

	public CrazyLoginMessageListener(final CrazyLogin plugin)
	{
		super();
		this.plugin = plugin;
	}

	@Override
	public void onPluginMessageReceived(final String channel, final Player player, final byte[] bytes)
	{
		if (!channel.equals("CrazyLogin"))
			return;
		String message = new String(bytes, charset);
		if (message.equals("Q_sAuth"))
		{
			sendPluginMessage(player, "A_sAuth " + encryptID(player));
			return;
		}
		if (message.equals("Q_PlrIP"))
		{
			sendPluginMessage(player, "A_PlrIP " + player.getAddress().getAddress().getHostAddress());
			return;
		}
		if (message.equals("Q_State"))
		{
			sendPluginMessage(player, "A_State " + (plugin.hasAccount(player) ? "1" : "0") + " " + (plugin.isLoggedIn(player) ? "1" : "0"));
			return;
		}
		if (message.startsWith("Q_Login "))
		{
			message = message.substring(8);
			try
			{
				plugin.command(player, "login", message.split(" "));
			}
			catch (CrazyException e)
			{
				e.print(player, plugin.getChatHeader());
			}
			return;
		}
		if (message.startsWith("Q_ChgPW "))
		{
			message = message.substring(8);
			try
			{
				plugin.commandMain(player, "password", message.split(" "));
			}
			catch (CrazyException e)
			{
				e.print(player, plugin.getChatHeader());
			}
			return;
		}
		if (message.equals("Q_Logout"))
		{
			try
			{
				plugin.command(player, "logout", null);
			}
			catch (CrazyException e)
			{
				e.print(player, plugin.getChatHeader());
			}
			return;
		}
	}

	protected void sendPluginMessage(final Player player, final String message)
	{
		player.sendPluginMessage(plugin, "CrazyLogin", message.getBytes(charset));
	}

	protected String encryptID(final Player player)
	{
		try
		{
			return encryptor.encrypt(player.getName() + " " + player.getAddress().getAddress().getHostAddress(), null, plugin.getUniqueIDKey());
		}
		catch (Exception e)
		{
			return "";
		}
	}
}

@@ -37,128 +37,81 @@
 ort 
-java.nio.charset.Charset;%0D%0Aimport org.bukkit.entity.Player;%0D%0Aimport org.bukkit.plugin.messaging.PluginMessageListener;%0D%0A
+org.bukkit.entity.Player;%0D%0Aimport de.st_ddt.crazylogin.crypt.CrazyCrypt1;
 %0D%0Aim
@@ -130,39 +130,49 @@
 dt.crazy
-lo
+plu
 gin.
-crypt.CrazyCrypt1
+CrazyPluginMessageListener
 ;%0D%0Aimpor
@@ -268,19 +268,21 @@
 ner 
-implement
+extend
 s 
+Crazy
 Plug
@@ -346,70 +346,8 @@
 n;%0D%0A
-%09protected final Charset charset = Charset.forName(%22UTF-8%22);%0D%0A
 %09pro
@@ -477,16 +477,22 @@
 %09%09super(
+plugin
 );%0D%0A%09%09th
@@ -547,11 +547,9 @@
 oid 
-onP
+p
 lugi
@@ -563,20 +563,14 @@
 eRec
-e
 i
+e
 ved(
-final 
 Stri
@@ -580,22 +580,16 @@
 channel,
- final
  Player 
@@ -600,129 +600,27 @@
 er, 
-final byte%5B%5D bytes)%0D%0A%09%7B%0D%0A%09%09if (!channel.equals(%22CrazyLogin%22))%0D%0A%09%09%09return;%0D%0A%09%09String message = new String(bytes, charset);
+String message)%0D%0A%09%7B
 %0D%0A%09%09
@@ -1826,174 +1826,8 @@
 %0D%0A%0D%0A
-%09protected void sendPluginMessage(final Player player, final String message)%0D%0A%09%7B%0D%0A%09%09player.sendPluginMessage(plugin, %22CrazyLogin%22, message.getBytes(charset));%0D%0A%09%7D%0D%0A%0D%0A
 %09pro

use default CrazyPluginMessageListener