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
|
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.Logger;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerListener;
//TODO: Translate the local/spy tags
public abstract class EssentialsChatPlayer extends PlayerListener
{
protected transient IEssentials ess;
protected final static Logger logger = Logger.getLogger("Minecraft");
protected final transient Map<String, IEssentialsChatListener> listeners;
protected final transient Server server;
public EssentialsChatPlayer(Server server, IEssentials ess, Map<String, IEssentialsChatListener> listeners)
{
this.ess = ess;
this.listeners = listeners;
this.server = server;
}
public void onPlayerChat(final PlayerChatEvent event)
{
}
public boolean isAborted(final PlayerChatEvent event)
{
return isAborted(event, "chat");
}
public boolean isAborted(final PlayerChatEvent event, final String command)
{
if (event.isCancelled())
{
return true;
}
for (IEssentialsChatListener listener : listeners.values())
{
if (listener.shouldHandleThisChat(event))
{
return true;
}
}
final User user = ess.getUser(event.getPlayer());
if (!isAffordableFor(user, command))
{
event.setCancelled(true);
return true;
}
return false;
}
public String getChatType(final String message)
{
switch (message.charAt(0))
{
case '!':
return "shout";
case '?':
return "question";
default:
return "";
}
}
protected void charge(final CommandSender sender, final String command) throws ChargeException
{
if (sender instanceof Player)
{
final Trade charge = new Trade(command, ess);
charge.charge(ess.getUser((Player)sender));
}
}
protected boolean isAffordableFor(final CommandSender sender, final String command)
{
if (sender instanceof Player)
{
try
{
final Trade charge = new Trade(command, ess);
charge.isAffordableFor(ess.getUser((Player)sender));
}
catch (ChargeException e)
{
return false;
}
}
else
{
return false;
}
return true;
}
protected void sendLocalChat(final User sender, final long radius, final PlayerChatEvent event)
{
event.setCancelled(true);
logger.info(_("localFormat", sender.getName(), event.getMessage()));
final Location loc = sender.getLocation();
final World world = loc.getWorld();
for (Player onlinePlayer : server.getOnlinePlayers())
{
String type = "[L]";
final User user = ess.getUser(onlinePlayer);
//TODO: remove reference to op
if (user.isIgnoredPlayer(sender.getName()) && !sender.isOp())
{
continue;
}
if (!user.equals(sender))
{
final Location playerLoc = user.getLocation();
if (playerLoc.getWorld() != world)
{
continue;
}
final double delta = playerLoc.distanceSquared(loc);
if (delta > radius)
{
if (user.isAuthorized("essentials.chat.spy"))
{
type = type.concat("[Spy]");
}
else
{
continue;
}
}
}
String message = String.format(event.getFormat(), type.concat(sender.getDisplayName()), event.getMessage());
for (IEssentialsChatListener listener : listeners.values())
{
message = listener.modifyMessage(event, onlinePlayer, message);
}
user.sendMessage(message);
}
}
}
|