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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
|
package com.earth2me.essentials;
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.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.*;
import org.bukkit.event.player.PlayerLoginEvent.Result;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
public class EssentialsPlayerListener implements Listener
{
private static final Logger LOGGER = Logger.getLogger("Minecraft");
private final transient IEssentials ess;
private static final int AIR = Material.AIR.getId();
private static final int BED = Material.BED_BLOCK.getId();
public EssentialsPlayerListener(final IEssentials parent)
{
this.ess = parent;
}
@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerRespawn(final PlayerRespawnEvent event)
{
final User user = ess.getUser(event.getPlayer());
updateCompass(user);
user.setDisplayNick();
}
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerChat(final AsyncPlayerChatEvent 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))
{
it.remove();
}
}
user.updateActivity(true);
user.setDisplayNick();
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onPlayerMove(final PlayerMoveEvent event)
{
if ((!ess.getSettings().cancelAfkOnMove() && !ess.getSettings().getFreezeAfkPlayers())
|| event.getFrom().getBlockX() == event.getTo().getBlockX()
&& event.getFrom().getBlockZ() == event.getTo().getBlockZ()
&& event.getFrom().getBlockY() == event.getTo().getBlockY())
{
return;
}
final User user = ess.getUser(event.getPlayer());
if (user.isAfk() && ess.getSettings().getFreezeAfkPlayers())
{
final Location from = event.getFrom();
final Location origTo = event.getTo();
final Location to = origTo.clone();
if (ess.getSettings().cancelAfkOnMove() && origTo.getY() >= from.getBlockY() + 1)
{
user.updateActivity(true);
return;
}
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);
}
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerQuit(final PlayerQuitEvent event)
{
final User user = ess.getUser(event.getPlayer());
if (ess.getSettings().removeGodOnDisconnect() && user.isGodModeEnabled())
{
user.setGodModeEnabled(false);
}
if (user.isVanished())
{
user.toggleVanished();
}
if (!user.isJailed())
{
user.setLastLocation();
}
user.updateActivity(false);
user.dispose();
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerJoin(final PlayerJoinEvent event)
{
ess.scheduleAsyncDelayedTask(new Runnable()
{
@Override
public void run()
{
delayedJoin(event.getPlayer());
}
});
}
public void delayedJoin(final Player player)
{
if (!player.isOnline())
{
return;
}
ess.getBackup().onPlayerJoin();
final User user = ess.getUser(player);
user.setDisplayNick();
updateCompass(user);
user.setLastLogin(System.currentTimeMillis());
user.updateActivity(false);
if (!ess.getVanishedPlayers().isEmpty() && !user.isAuthorized("essentials.vanish.see"))
{
for (String p : ess.getVanishedPlayers())
{
Player toVanish = ess.getUser(p).getBase();
if (toVanish.isOnline())
{
user.hidePlayer(toVanish);
}
}
}
if (user.isAuthorized("essentials.sleepingignored"))
{
ess.scheduleSyncDelayedTask(new Runnable()
{
@Override
public void run()
{
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, "motd", user);
}
catch (IOException ex)
{
if (ess.getSettings().isDebug())
{
LOGGER.log(Level.WARNING, ex.getMessage(), ex);
}
else
{
LOGGER.log(Level.WARNING, ex.getMessage());
}
}
}
if (!ess.getSettings().isCommandDisabled("mail") && user.isAuthorized("essentials.mail"))
{
final List<String> mail = user.getMails();
if (mail.isEmpty())
{
final String msg = _("noNewMail");
if (!msg.isEmpty())
{
user.sendMessage(msg);
}
}
else
{
user.sendMessage(_("youHaveNewMail", mail.size()));
}
}
}
// Makes the compass item ingame always point to the first essentials home. #EasterEgg
private void updateCompass(final User user)
{
Location loc = user.getHome(user.getLocation());
if (loc == null)
{
loc = user.getBedSpawnLocation();
}
if (loc != null)
{
final Location updateLoc = loc;
ess.scheduleSyncDelayedTask(new Runnable()
{
@Override
public void run()
{
user.setCompassTarget(updateLoc);
}
});
}
}
@EventHandler(priority = EventPriority.HIGH)
public void onPlayerLogin(final PlayerLoginEvent event)
{
switch (event.getResult())
{
case ALLOWED:
case KICK_FULL:
case KICK_BANNED:
break;
default:
return;
}
final User user = ess.getUser(event.getPlayer());
if (user.isNPC())
{
user.setNPC(false);
}
final long currentTime = System.currentTimeMillis();
final boolean banExpired = user.checkBanTimeout(currentTime);
user.checkMuteTimeout(currentTime);
user.checkJailTimeout(currentTime);
if (!banExpired && (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 (event.getResult() == Result.KICK_FULL && !user.isAuthorized("essentials.joinfullserver"))
{
event.disallow(Result.KICK_FULL, _("serverFull"));
return;
}
event.allow();
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onPlayerTeleport(final PlayerTeleportEvent event)
{
final boolean backListener = ess.getSettings().registerBackInListener();
final boolean teleportInvulnerability = ess.getSettings().isTeleportInvulnerability();
if (backListener || teleportInvulnerability)
{
final User user = ess.getUser(event.getPlayer());
//There is TeleportCause.COMMMAND but plugins have to actively pass the cause in on their teleports.
if (backListener && (event.getCause() == TeleportCause.PLUGIN || event.getCause() == TeleportCause.COMMAND))
{
user.setLastLocation();
}
if (teleportInvulnerability)
{
user.enableInvulnerabilityAfterTeleport();
}
}
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onPlayerEggThrow(final PlayerEggThrowEvent event)
{
final User user = ess.getUser(event.getPlayer());
final ItemStack stack = new ItemStack(Material.EGG, 1);
if (user.hasUnlimited(stack))
{
user.getInventory().addItem(stack);
user.updateInventory();
}
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
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();
}
});
}
}
private final static List<String> COMMANDS = Arrays.asList("msg", "w", "r", "mail", "m", "t", "whisper", "emsg", "tell", "er", "reply", "ereply", "email", "action", "describe", "eme", "eaction", "edescribe", "etell", "ewhisper", "pm");
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerCommandPreprocess(final PlayerCommandPreprocessEvent event)
{
final Player player = event.getPlayer();
final String cmd = event.getMessage().toLowerCase(Locale.ENGLISH).split(" ")[0].replace("/", "").toLowerCase(Locale.ENGLISH);
if (COMMANDS.contains(cmd))
{
for (Player onlinePlayer : ess.getServer().getOnlinePlayers())
{
final User spyer = ess.getUser(onlinePlayer);
if (spyer.isSocialSpyEnabled() && !player.equals(onlinePlayer))
{
onlinePlayer.sendMessage(player.getDisplayName() + " : " + event.getMessage());
}
}
}
else if (!cmd.equalsIgnoreCase("afk"))
{
final User user = ess.getUser(player);
user.updateActivity(true);
}
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerChangedWorldHack(final PlayerChangedWorldEvent event)
{
final Player user = event.getPlayer();
if (user.getGameMode() != GameMode.CREATIVE)
{
user.setAllowFlight(false);
}
user.setFlySpeed(0.1f);
user.setWalkSpeed(0.2f);
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerChangedWorld(final PlayerChangedWorldEvent event)
{
final User user = ess.getUser(event.getPlayer());
final String newWorld = event.getPlayer().getLocation().getWorld().getName();
user.setDisplayNick();
updateCompass(user);
if (ess.getSettings().getNoGodWorlds().contains(newWorld) && user.isGodModeEnabledRaw())
{
user.sendMessage(_("noGodWorldWarning"));
}
if (!event.getPlayer().getWorld().getName().equals(newWorld))
{
user.sendMessage(_("currentWorld", newWorld));
}
}
@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerInteract(final PlayerInteractEvent event)
{
switch (event.getAction())
{
case RIGHT_CLICK_BLOCK:
if (!event.isCancelled() && event.getClickedBlock().getTypeId() == BED && ess.getSettings().getUpdateBedAtDaytime())
{
Player player = event.getPlayer();
player.setBedSpawnLocation(event.getClickedBlock().getLocation());
player.sendMessage(_("homeSet", player.getLocation().getWorld().getName(), player.getLocation().getBlockX(), player.getLocation().getBlockY(), player.getLocation().getBlockZ()));
}
break;
case LEFT_CLICK_AIR:
if (event.getPlayer().isFlying())
{
final User user = ess.getUser(event.getPlayer());
if (user.isFlyClickJump())
{
useFlyClickJump(user);
return;
}
}
case LEFT_CLICK_BLOCK:
if (event.getItem() != null && event.getItem().getTypeId() != AIR)
{
final User user = ess.getUser(event.getPlayer());
user.updateActivity(true);
if (user.hasPowerTools() && user.arePowerToolsEnabled() && usePowertools(user, event.getItem().getTypeId()))
{
event.setCancelled(true);
}
}
break;
default:
break;
}
}
// This method allows the /jump lock feature to work, allows teleporting while flying #EasterEgg
private void useFlyClickJump(final User user)
{
try
{
final Location otarget = Util.getTarget(user);
ess.scheduleSyncDelayedTask(
new Runnable()
{
@Override
public void run()
{
Location loc = user.getLocation();
loc.setX(otarget.getX());
loc.setZ(otarget.getZ());
while (Util.isBlockDamaging(loc.getWorld(), loc.getBlockX(), loc.getBlockY() - 1, loc.getBlockZ()))
{
loc.setY(loc.getY() + 1d);
}
user.getBase().teleport(loc, TeleportCause.PLUGIN);
}
});
}
catch (Exception ex)
{
if (ess.getSettings().isDebug())
{
LOGGER.log(Level.WARNING, ex.getMessage(), ex);
}
}
}
private boolean usePowertools(final User user, final int id)
{
final List<String> commandList = user.getPowertool(id);
if (commandList == null || commandList.isEmpty())
{
return false;
}
boolean used = false;
// We need to loop through each command and execute
for (final String command : commandList)
{
if (command.contains("{player}"))
{
continue;
}
else if (command.startsWith("c:"))
{
used = true;
user.chat(command.substring(2));
}
else
{
used = true;
ess.scheduleSyncDelayedTask(
new Runnable()
{
@Override
public void run()
{
user.getServer().dispatchCommand(user.getBase(), command);
LOGGER.log(Level.INFO, String.format("[PT] %s issued server command: /%s", user.getName(), command));
}
});
}
}
return used;
}
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onPlayerPickupItem(final PlayerPickupItemEvent event)
{
if (ess.getSettings().getDisableItemPickupWhileAfk())
{
if (ess.getUser(event.getPlayer()).isAfk())
{
event.setCancelled(true);
}
}
}
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onInventoryClickEvent(final InventoryClickEvent event)
{
if (event.getView().getTopInventory().getType() == InventoryType.PLAYER)
{
final User user = ess.getUser(event.getWhoClicked());
final InventoryHolder invHolder = event.getView().getTopInventory().getHolder();
if (invHolder != null && invHolder instanceof HumanEntity)
{
final User invOwner = ess.getUser((HumanEntity)invHolder);
if (user.isInvSee() && (!user.isAuthorized("essentials.invsee.modify")
|| invOwner.isAuthorized("essentials.invsee.preventmodify")
|| !invOwner.isOnline()))
{
event.setCancelled(true);
}
}
}
else if (event.getView().getTopInventory().getType() == InventoryType.ENDER_CHEST)
{
final User user = ess.getUser(event.getWhoClicked());
if (user.isEnderSee() && (!user.isAuthorized("essentials.enderchest.modify")))
{
event.setCancelled(true);
}
}
}
@EventHandler(priority = EventPriority.MONITOR)
public void onInventoryCloseEvent(final InventoryCloseEvent event)
{
if (event.getView().getTopInventory().getType() == InventoryType.PLAYER)
{
final User user = ess.getUser(event.getPlayer());
user.setInvSee(false);
}
else if (event.getView().getTopInventory().getType() == InventoryType.ENDER_CHEST)
{
final User user = ess.getUser(event.getPlayer());
user.setEnderSee(false);
}
}
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onPlayerFishEvent(final PlayerFishEvent event)
{
final User user = ess.getUser(event.getPlayer());
user.updateActivity(true);
}
}
|