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

import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.User;
import static com.earth2me.essentials.chat.EssentialsChatPlayer.logger;
import net.ess3.api.IEssentials;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import net.ess3.api.events.LocalChatSpyEvent;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.AsyncPlayerChatEvent;


public class EssentialsChatPlayerListenerNormal extends EssentialsChatPlayer
{
	public EssentialsChatPlayerListenerNormal(final Server server,
											  final IEssentials ess,
											  final Map<AsyncPlayerChatEvent, ChatStore> chatStorage)
	{
		super(server, ess, chatStorage);
	}

	@EventHandler(priority = EventPriority.NORMAL)
	@Override
	public void onPlayerChat(final AsyncPlayerChatEvent event)
	{
		if (isAborted(event))
		{
			return;
		}

		/**
		 * This file should handle detection of the local chat features... if local chat is enabled, we need to handle
		 * it here
		 */
		long radius = ess.getSettings().getChatRadius();
		if (radius < 1)
		{
			return;
		}
		radius *= radius;

		final ChatStore chatStore = getChatStore(event);
		final User user = chatStore.getUser();
		chatStore.setRadius(radius);

		if (event.getMessage().length() > 1 && chatStore.getType().length() > 0)
		{
			final StringBuilder permission = new StringBuilder();
			permission.append("essentials.chat.").append(chatStore.getType());

			if (user.isAuthorized(permission.toString()))
			{
				final StringBuilder format = new StringBuilder();
				format.append(chatStore.getType()).append("Format");
				event.setMessage(event.getMessage().substring(1));
				event.setFormat(_(format.toString(), event.getFormat()));
				return;
			}

			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;
		}

		final Location loc = user.getLocation();
		final World world = loc.getWorld();

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

		Set<Player> outList = event.getRecipients();
		Set<Player> spyList = new HashSet<Player>();

		try
		{
			outList.add(event.getPlayer());
		}
		catch (UnsupportedOperationException ex)
		{
			if (ess.getSettings().isDebug())
			{
				ess.getLogger().info("Plugin triggered custom chat event, local chat handling aborted.");
			}
			return;
		}
		
		final String format = event.getFormat();
		event.setFormat(_("chatTypeLocal").concat(event.getFormat()));

		logger.info(_("localFormat", user.getName(), event.getMessage()));

		final Iterator<Player> it = outList.iterator();
		while (it.hasNext())
		{
			final Player onlinePlayer = it.next();
			final User onlineUser = ess.getUser(onlinePlayer);
			if (!onlineUser.equals(user))
			{
				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"))
					{
						spyList.add(onlinePlayer);
					}
					it.remove();
				}
			}
		}

		if (outList.size() < 2) {
			user.sendMessage(_("localNoOne"));
		}

		LocalChatSpyEvent spyEvent = new LocalChatSpyEvent(event.isAsynchronous(), event.getPlayer(), format, event.getMessage(), spyList);
		server.getPluginManager().callEvent(spyEvent);

		if (!spyEvent.isCancelled())
		{
			for (Player onlinePlayer : spyEvent.getRecipients())
			{
				onlinePlayer.sendMessage(String.format(spyEvent.getFormat(), user.getDisplayName(), spyEvent.getMessage()));
			}
		}
	}
}