summaryrefslogtreecommitdiffstats
path: root/EssentialsUpdate/src/com/earth2me/essentials/update/EssentialsHelp.java
blob: ec62566cacfddb54068a3f9f882e6a75052a6379 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package com.earth2me.essentials.update;

import com.earth2me.essentials.update.chat.Command;
import com.earth2me.essentials.update.chat.ConfigCommand;
import com.earth2me.essentials.update.chat.ErrorsCommand;
import com.earth2me.essentials.update.chat.HelpCommand;
import com.earth2me.essentials.update.chat.IrcBot;
import com.earth2me.essentials.update.chat.ListCommand;
import com.earth2me.essentials.update.chat.StartupCommand;
import com.earth2me.essentials.update.chat.UsernameUtil;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.Event.Type;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;


public class EssentialsHelp extends PlayerListener
{
	private transient Player chatUser;
	private final transient Server server;
	private final transient Plugin plugin;
	private transient IrcBot ircBot;
	private final transient Map<String, Command> commands = new HashMap<String, Command>();

	public EssentialsHelp(final Plugin plugin)
	{
		super();
		this.plugin = plugin;
		this.server = plugin.getServer();
		commands.put("!help", new HelpCommand());
		commands.put("!list", new ListCommand());
		commands.put("!startup", new StartupCommand(plugin));
		commands.put("!errors", new ErrorsCommand(plugin));
		commands.put("!config", new ConfigCommand(plugin));
	}

	public void registerEvents()
	{
		final PluginManager pluginManager = server.getPluginManager();
		pluginManager.registerEvent(Type.PLAYER_QUIT, this, Priority.Low, plugin);
		pluginManager.registerEvent(Type.PLAYER_CHAT, this, Priority.Low, plugin);
	}

	public void onCommand(final CommandSender sender)
	{
		if (sender instanceof Player && sender.hasPermission("essentials.helpchat"))
		{
			if (chatUser == null)
			{
				chatUser = (Player)sender;
				ircBot = null;
				sender.sendMessage("You will be connected to the Essentials Help Chat.");
				sender.sendMessage("All your chat messages will be forwarded to the channel. You can't chat with other players on your server while in help chat, but you can use commands.");
				sender.sendMessage("Please be patient, if noone is available, check back later.");
				sender.sendMessage("Type !help to get a list of all commands.");
				sender.sendMessage("Type !quit to leave the channel.");
				sender.sendMessage("Do you want to join the channel now? (yes/no)");
			}
			if (!chatUser.equals(sender))
			{
				sender.sendMessage("The player " + chatUser.getDisplayName() + " is already using the essentialshelp.");
			}
		}
		else
		{
			sender.sendMessage("Please run the command as op from in game.");
		}
	}

	public void onDisable()
	{
		closeConnection();
	}

	private boolean sendChatMessage(final Player player, final String message)
	{
		final String messageCleaned = message.trim();
		if (messageCleaned.isEmpty())
		{
			return false;
		}
		if (ircBot == null)
		{
			return handleAnswer(messageCleaned, player);
		}
		else
		{
			if (ircBot.isKicked())
			{
				closeConnection();
				return false;
			}
			final String lowMessage = messageCleaned.toLowerCase(Locale.ENGLISH);
			if (lowMessage.startsWith("!quit"))
			{
				closeConnection();
				player.sendMessage("Connection closed.");
				return true;
			}
			if (!ircBot.isConnected() || ircBot.getChannels().length == 0)
			{
				return false;
			}
			if (handleCommands(lowMessage, player))
			{
				return true;
			}
			ircBot.sendMessage(messageCleaned);
			chatUser.sendMessage("§6" + ircBot.getNick() + ": §7" + messageCleaned);
			return true;
		}
	}

	private void closeConnection()
	{
		chatUser = null;
		if (ircBot != null)
		{
			ircBot.quit();
			ircBot = null;
		}
	}

	private boolean handleAnswer(final String message, final Player player)
	{
		if (message.equalsIgnoreCase("yes"))
		{
			player.sendMessage("Connecting...");
			connectToIRC(player);
			return true;
		}
		if (message.equalsIgnoreCase("no") || message.equalsIgnoreCase("!quit"))
		{
			chatUser = null;
			return true;
		}
		return false;
	}

	private boolean handleCommands(final String lowMessage, final Player player)
	{
		final String[] parts = lowMessage.split(" ");
		if (commands.containsKey(parts[0]))
		{
			commands.get(parts[0]).run(ircBot, player);
			return true;
		}
		return false;
	}

	private void connectToIRC(final Player player)
	{
		ircBot = new IrcBot(player, "Ess_" + player.getName(), UsernameUtil.createUsername(player));
	}

	@Override
	public void onPlayerChat(final PlayerChatEvent event)
	{
		if (event.getPlayer() == chatUser)
		{
			final boolean success = sendChatMessage(event.getPlayer(), event.getMessage());
			event.setCancelled(success);
			return;
		}
	}

	@Override
	public void onPlayerQuit(final PlayerQuitEvent event)
	{
		closeConnection();
	}
}