summaryrefslogtreecommitdiffstats
path: root/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayer.java
blob: 8ca1e072639426ae683fbba58ed57266ae8dfe26 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package com.earth2me.essentials.chat;

import com.earth2me.essentials.ChargeException;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.Trade;
import com.earth2me.essentials.User;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;

public abstract class EssentialsChatPlayer implements Listener
{
	protected transient IEssentials ess;
	protected final static Logger logger = Logger.getLogger("Minecraft");
	protected final transient Map<String, IEssentialsChatListener> listeners;
	protected final transient Server server;
	protected final transient Map<AsyncPlayerChatEvent, ChatStore> chatStorage;

	public EssentialsChatPlayer(final Server server,
								final IEssentials ess,
								final Map<String, IEssentialsChatListener> listeners,
								final Map<AsyncPlayerChatEvent, ChatStore> chatStorage)
	{
		this.ess = ess;
		this.listeners = listeners;
		this.server = server;
		this.chatStorage = chatStorage;
	}

	public void onPlayerChat(final AsyncPlayerChatEvent event)
	{
	}

	public boolean isAborted(final AsyncPlayerChatEvent event)
	{
		if (event.isCancelled())
		{
			return true;
		}
		synchronized (listeners)
		{
			for (Map.Entry<String, IEssentialsChatListener> listener : listeners.entrySet())
			{
				try
				{
					if (listener.getValue().shouldHandleThisChat(event))
					{
						return true;
					}
				}
				catch (Throwable t)
				{
					if (ess.getSettings().isDebug())
					{
						logger.log(Level.WARNING, "Error with EssentialsChat listener of " + listener.getKey() + ": " + t.getMessage(), t);
					}
					else
					{
						logger.log(Level.WARNING, "Error with EssentialsChat listener of " + listener.getKey() + ": " + t.getMessage());
					}
				}
			}
		}
		return false;
	}

	public String getChatType(final String message)
	{
		switch (message.charAt(0))
		{
		case '!':
			return "shout";
		case '?':
			return "question";
		//case '@':
		//return "admin";
		default:
			return "";
		}
	}

	public ChatStore getChatStore(final AsyncPlayerChatEvent event)
	{
		return chatStorage.get(event);
	}

	public void setChatStore(final AsyncPlayerChatEvent event, final ChatStore chatStore)
	{
		chatStorage.put(event, chatStore);
	}

	public ChatStore delChatStore(final AsyncPlayerChatEvent event)
	{
		return chatStorage.remove(event);
	}

	protected void charge(final User user, final Trade charge) throws ChargeException
	{
		charge.charge(user);
	}

	protected boolean charge(final AsyncPlayerChatEvent event, final ChatStore chatStore)
	{
		try
		{
			charge(chatStore.getUser(), chatStore.getCharge());
		}
		catch (ChargeException e)
		{
			ess.showError(chatStore.getUser(), e, chatStore.getLongType());
			event.setCancelled(true);
			return false;
		}
		return true;
	}

	protected void sendLocalChat(final AsyncPlayerChatEvent event, final ChatStore chatStore)
	{
		event.setCancelled(true);
		final User sender = chatStore.getUser();
		logger.info(_("localFormat", sender.getName(), event.getMessage()));
		final Location loc = sender.getLocation();
		final World world = loc.getWorld();

		if (charge(event, chatStore) == false)
		{
			return;
		}

		for (Player onlinePlayer : event.getRecipients())
		{
			String type = _("chatTypeLocal");
			final User onlineUser = ess.getUser(onlinePlayer);
			if (!onlineUser.equals(sender))
			{
				boolean abort = false;
				final Location playerLoc = onlineUser.getLocation();
				if (playerLoc.getWorld() != world)
				{
					abort = true;
				}
				else
				{
					final double delta = playerLoc.distanceSquared(loc);
					if (delta > chatStore.getRadius())
					{
						abort = true;
					}
				}
				if (abort)
				{
					if (onlineUser.isAuthorized("essentials.chat.spy"))
					{
						type = type.concat(_("chatTypeSpy"));
					}
					else
					{
						continue;
					}
				}
			}

			String message = type.concat(String.format(event.getFormat(), sender.getDisplayName(), event.getMessage()));
			synchronized (listeners)
			{
				for (Map.Entry<String, IEssentialsChatListener> listener : listeners.entrySet())
				{
					try
					{
						message = listener.getValue().modifyMessage(event, onlinePlayer, message);
					}
					catch (Throwable t)
					{
						if (ess.getSettings().isDebug())
						{
							logger.log(Level.WARNING, "Error with EssentialsChat listener of " + listener.getKey() + ": " + t.getMessage(), t);
						}
						else
						{
							logger.log(Level.WARNING, "Error with EssentialsChat listener of " + listener.getKey() + ": " + t.getMessage());
						}
					}
				}
			}
			onlineUser.sendMessage(message);
		}
	}
}