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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
package com.earth2me.essentials;
import com.earth2me.essentials.craftbukkit.BedLocationFix;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.textreader.IText;
import com.earth2me.essentials.textreader.KeywordReplacer;
import com.earth2me.essentials.textreader.TextInput;
import com.earth2me.essentials.textreader.TextPager;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerLoginEvent.Result;
import org.bukkit.event.player.*;
import org.bukkit.inventory.ItemStack;
public class EssentialsPlayerListener extends PlayerListener
{
private static final Logger LOGGER = Logger.getLogger("Minecraft");
private final transient Server server;
private final transient IEssentials ess;
public EssentialsPlayerListener(final IEssentials parent)
{
this.ess = parent;
this.server = parent.getServer();
}
@Override
public void onPlayerRespawn(final PlayerRespawnEvent event)
{
final User user = ess.getUser(event.getPlayer());
updateCompass(user);
if (ess.getSettings().changeDisplayName())
{
user.setDisplayNick();
}
}
@Override
public void onPlayerChat(final PlayerChatEvent event)
{
final User user = ess.getUser(event.getPlayer());
if (user.isMuted())
{
event.setCancelled(true);
user.sendMessage(_("playerMuted"));
LOGGER.info(_("mutedUserSpeaks", user.getName()));
}
final Iterator<Player> it = event.getRecipients().iterator();
while (it.hasNext())
{
final User u = ess.getUser(it.next());
if (u.isIgnoredPlayer(user.getName()))
{
it.remove();
}
}
user.updateActivity(true);
if (ess.getSettings().changeDisplayName())
{
user.setDisplayNick();
}
}
@Override
public void onPlayerMove(final PlayerMoveEvent event)
{
if (event.isCancelled())
{
return;
}
final User user = ess.getUser(event.getPlayer());
if (user.isAfk() && ess.getSettings().getFreezeAfkPlayers())
{
final Location from = event.getFrom();
final Location to = event.getTo().clone();
to.setX(from.getX());
to.setY(from.getY());
to.setZ(from.getZ());
try
{
event.setTo(Util.getSafeDestination(to));
}
catch (Exception ex)
{
event.setTo(to);
}
return;
}
final Location afk = user.getAfkPosition();
if (afk == null || !event.getTo().getWorld().equals(afk.getWorld()) || afk.distanceSquared(event.getTo()) > 9)
{
user.updateActivity(true);
}
}
@Override
public void onPlayerQuit(final PlayerQuitEvent event)
{
final User user = ess.getUser(event.getPlayer());
if (ess.getSettings().removeGodOnDisconnect() && user.isGodModeEnabled())
{
user.toggleGodModeEnabled();
}
if (user.getSavedInventory() != null)
{
user.getInventory().setContents(user.getSavedInventory());
user.setSavedInventory(null);
}
user.updateActivity(false);
user.dispose();
if (!ess.getSettings().getReclaimSetting())
{
return;
}
final Thread thread = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
Thread.sleep(1000);
Runtime rt = Runtime.getRuntime();
double mem = rt.freeMemory();
rt.runFinalization();
rt.gc();
mem = rt.freeMemory() - mem;
mem /= 1024 * 1024;
LOGGER.log(Level.INFO, _("freedMemory", mem));
}
catch (InterruptedException ex)
{
}
}
});
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
@Override
public void onPlayerJoin(final PlayerJoinEvent event)
{
ess.getBackup().onPlayerJoin();
final User user = ess.getUser(event.getPlayer());
if (ess.getSettings().changeDisplayName())
{
user.setDisplayNick();
}
user.setLastLoginAddress(user.getAddress().getAddress().getHostAddress());
user.updateActivity(false);
if (user.isAuthorized("essentials.sleepingignored"))
{
user.setSleepingIgnored(true);
}
if (!ess.getSettings().isCommandDisabled("motd") && user.isAuthorized("essentials.motd"))
{
try
{
final IText input = new TextInput(user, "motd", true, ess);
final IText output = new KeywordReplacer(input, user, ess);
final TextPager pager = new TextPager(output, true);
pager.showPage("1", null, user);
}
catch (IOException ex)
{
LOGGER.log(Level.WARNING, ex.getMessage(), ex);
}
}
if (!ess.getSettings().isCommandDisabled("mail") && user.isAuthorized("essentials.mail"))
{
final List<String> mail = user.getMails();
if (mail.isEmpty())
{
user.sendMessage(_("noNewMail"));
}
else
{
user.sendMessage(_("youHaveNewMail", mail.size()));
}
}
}
@Override
public void onPlayerLogin(final PlayerLoginEvent event)
{
if (event.getResult() != Result.ALLOWED && event.getResult() != Result.KICK_FULL && event.getResult() != Result.KICK_BANNED)
{
LOGGER.log(Level.INFO, "Disconnecting user " + event.getPlayer().toString() + " due to " + event.getResult().toString());
return;
}
User user = ess.getUser(event.getPlayer());
user.setNPC(false);
final long currentTime = System.currentTimeMillis();
final boolean banExpired = user.checkBanTimeout(currentTime);
user.checkMuteTimeout(currentTime);
user.checkJailTimeout(currentTime);
if (banExpired == false && (user.isBanned() || event.getResult() == Result.KICK_BANNED))
{
final String banReason = user.getBanReason();
event.disallow(Result.KICK_BANNED, banReason != null && !banReason.isEmpty() && !banReason.equalsIgnoreCase("ban") ? banReason : _("defaultBanReason"));
return;
}
if (server.getOnlinePlayers().length >= server.getMaxPlayers() && !user.isAuthorized("essentials.joinfullserver"))
{
event.disallow(Result.KICK_FULL, _("serverFull"));
return;
}
event.allow();
user.setLastLogin(System.currentTimeMillis());
updateCompass(user);
}
private void updateCompass(final User user)
{
Location loc = user.getHome(user.getLocation());
if (loc == null) {
loc = BedLocationFix.getBedSpawnLocation(user);
}
if (loc != null) {
user.setCompassTarget(loc);
}
}
@Override
public void onPlayerTeleport(PlayerTeleportEvent event)
{
if (event.isCancelled())
{
return;
}
final User user = ess.getUser(event.getPlayer());
if (ess.getSettings().changeDisplayName())
{
user.setDisplayNick();
}
updateCompass(user);
}
@Override
public void onPlayerEggThrow(final PlayerEggThrowEvent event)
{
final User user = ess.getUser(event.getPlayer());
final ItemStack is = new ItemStack(Material.EGG, 1);
if (user.hasUnlimited(is))
{
user.getInventory().addItem(is);
user.updateInventory();
}
}
@Override
public void onPlayerBucketEmpty(final PlayerBucketEmptyEvent event)
{
final User user = ess.getUser(event.getPlayer());
if (user.hasUnlimited(new ItemStack(event.getBucket())))
{
event.getItemStack().setType(event.getBucket());
ess.scheduleSyncDelayedTask(new Runnable()
{
@Override
public void run()
{
user.updateInventory();
}
});
}
}
@Override
public void onPlayerAnimation(final PlayerAnimationEvent event)
{
final User user = ess.getUser(event.getPlayer());
user.updateActivity(true);
usePowertools(event);
}
private void usePowertools(final PlayerAnimationEvent event)
{
if (event.getAnimationType() != PlayerAnimationType.ARM_SWING)
{
return;
}
final User user = ess.getUser(event.getPlayer());
final ItemStack is = user.getItemInHand();
if (is == null || is.getType() == Material.AIR || !user.arePowerToolsEnabled())
{
return;
}
final List<String> commandList = user.getPowertool(is);
if (commandList == null || commandList.isEmpty())
{
return;
}
// We need to loop through each command and execute
for (String command : commandList)
{
if (command.matches(".*\\{player\\}.*"))
{
//user.sendMessage("Click a player to use this command");
continue;
}
else if (command.startsWith("c:"))
{
for (Player p : server.getOnlinePlayers())
{
p.sendMessage(user.getDisplayName() + ":" + command.substring(2));
}
}
else
{
user.getServer().dispatchCommand(event.getPlayer(), command);
}
}
}
@Override
public void onPlayerCommandPreprocess(final PlayerCommandPreprocessEvent event)
{
if (event.isCancelled())
{
return;
}
final User user = ess.getUser(event.getPlayer());
final String cmd = event.getMessage().toLowerCase(Locale.ENGLISH).split(" ")[0].replace("/", "").toLowerCase(Locale.ENGLISH);
final List<String> commands = Arrays.asList("msg", "r", "mail", "m", "t", "emsg", "tell", "er", "reply", "ereply", "email");
if (commands.contains(cmd))
{
for (Player player : ess.getServer().getOnlinePlayers())
{
User spyer = ess.getUser(player);
if (spyer.isSocialSpyEnabled() && !user.equals(spyer))
{
player.sendMessage(user.getDisplayName() + " : " + event.getMessage());
}
}
}
if (!cmd.equalsIgnoreCase("afk"))
{
user.updateActivity(true);
}
}
@Override
public void onPlayerChangedWorld(final PlayerChangedWorldEvent event)
{
if (ess.getSettings().getNoGodWorlds().contains(event.getPlayer().getLocation().getWorld().getName())) {
User user = ess.getUser(event.getPlayer());
if (user.isGodModeEnabledRaw()) {
user.sendMessage(_("noGodWorldWarning"));
}
}
}
}
|