summaryrefslogtreecommitdiffstats
path: root/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayer.java
blob: acf08a5ddf7c038799b9f910653a4b38d5bf6897 (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
package com.earth2me.essentials.chat;

import com.earth2me.essentials.api.ChargeException;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.economy.Trade;
import com.earth2me.essentials.utils.Util;
import com.earth2me.essentials.api.IEssentials;
import com.earth2me.essentials.api.IRanks;
import com.earth2me.essentials.api.ISettings;
import com.earth2me.essentials.api.IUser;
import com.earth2me.essentials.permissions.Permissions;
import com.earth2me.essentials.storage.Location;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Logger;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChatEvent;

//TODO: Translate the local/spy tags
public abstract class EssentialsChatPlayer implements Listener
{
	protected transient IEssentials ess;
	protected final static Logger LOGGER = Logger.getLogger("Minecraft");
	protected final transient Server server;
	protected final transient Map<PlayerChatEvent, ChatStore> chatStorage;

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

	public void onPlayerChat(final PlayerChatEvent event)
	{
	}

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

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

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

	protected void chargeChat(final PlayerChatEvent event, final ChatStore chatStore)
	{
		try
		{
			charge(chatStore.getUser(), chatStore.getCharge());
		}
		catch (ChargeException e)
		{
			ess.getCommandHandler().showCommandError(chatStore.getUser(), chatStore.getLongType(), e);
			event.setCancelled(true);
		}
	}

	protected void charge(final CommandSender sender, final Trade charge) throws ChargeException
	{
		if (sender instanceof Player)
		{
			charge.charge(ess.getUser((Player)sender));
		}
	}

	protected void formatChat(final PlayerChatEvent event, final ChatStore chatStore)
	{
		final IUser user = chatStore.getUser();
		if (Permissions.CHAT_COLOR.isAuthorized(user))
		{
			event.setMessage(Util.stripColor(event.getMessage()));
		}
		String group = ess.getRanks().getMainGroup(user);
		String world = user.getWorld().getName();

		IRanks groupSettings = ess.getRanks();
		event.setFormat(groupSettings.getChatFormat(user).format(new Object[]
				{
					group, world, world.substring(0, 1).toUpperCase(Locale.ENGLISH)
				}));

	}

	//TODO: Flesh this out - '?' trigger is too easily accidentally triggered
	protected String getChatType(final String message)
	{
		switch (message.charAt(0))
		{
		case '!':
			return "shout";
		//case '?':
		//return "question";
		//case '@':
		//	return "admin";			
		default:
			return "";
		}
	}

	protected void handleLocalChat(final PlayerChatEvent event, final ChatStore chatStore)
	{
		long radius = 0;
		ISettings settings = ess.getSettings();
		settings.acquireReadLock();
		try
		{
			radius = settings.getData().getChat().getLocalRadius();
		}
		finally
		{
			settings.unlock();
		}

		if (radius < 1)
		{
			return;
		}

		radius *= radius;

		final IUser user = chatStore.getUser();

		if (event.getMessage().length() > 1 && chatStore.getType().length() > 0)
		{
			if (ChatPermissions.getPermission(chatStore.getType()).isAuthorized(user))
			{
				final StringBuilder format = new StringBuilder();
				format.append(chatStore.getType()).append("Format");
				event.setMessage(event.getMessage().substring(1));
				event.setFormat(_(format.toString(), event.getFormat()));
				return;
			}
			if (!onlineUser.equals(sender))
			{
				if (onlineUser.isAuthorized("essentials.chat.spy"))
				{
					type = type.concat(_("chatTypeSpy"));
				}
				else
				{
					final Location playerLoc = onlineUser.getLocation();
					if (playerLoc.getWorld() != world)
					{
						continue;
					}
					final double delta = playerLoc.distanceSquared(loc);
					if (delta > chatStore.getRadius())
					{
						continue;
					}
				}
			}

			final StringBuilder errorMsg = new StringBuilder();
			errorMsg.append("notAllowedTo").append(chatStore.getType().substring(0, 1).toUpperCase(Locale.ENGLISH)).append(chatStore.getType().substring(1));

			user.sendMessage(_(errorMsg.toString()));
			event.setCancelled(true);
			return;
		}

		event.setCancelled(true);
		final EssentialsLocalChatEvent localChat = new EssentialsLocalChatEvent(event, radius);
		ess.getServer().getPluginManager().callEvent(localChat);
	}
}