-
Notifications
You must be signed in to change notification settings - Fork 17
/
LWCPlayerListener.java
517 lines (426 loc) · 18.8 KB
/
LWCPlayerListener.java
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
/*
* Copyright 2011 Tyler Blair. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and contributors and should not be interpreted as representing official policies,
* either expressed or implied, of anybody else.
*/
package com.griefcraft.listeners;
import com.griefcraft.lwc.LWC;
import com.griefcraft.lwc.LWCPlugin;
import com.griefcraft.model.Flag;
import com.griefcraft.model.LWCPlayer;
import com.griefcraft.model.Protection;
import com.griefcraft.scripting.Module;
import com.griefcraft.scripting.event.LWCBlockInteractEvent;
import com.griefcraft.scripting.event.LWCDropItemEvent;
import com.griefcraft.scripting.event.LWCProtectionInteractEvent;
import com.griefcraft.util.UUIDRegistry;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.DoubleChest;
import org.bukkit.block.Hopper;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import java.util.Map;
import java.util.Set;
public class LWCPlayerListener implements Listener {
/**
* The plugin instance
*/
private LWCPlugin plugin;
public LWCPlayerListener(LWCPlugin plugin) {
this.plugin = plugin;
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
UUIDRegistry.updateCache(player.getUniqueId(), player.getName());
}
@EventHandler(ignoreCancelled = true)
public void onMoveItem(InventoryMoveItemEvent event) {
boolean result;
// if the initiator is the same as the source it is a dropper i.e. depositing items
if (event.getInitiator() == event.getSource()) {
result = handleMoveItemEvent(event.getInitiator(), event.getDestination());
} else {
result = handleMoveItemEvent(event.getInitiator(), event.getSource());
}
if (result) {
event.setCancelled(true);
}
}
/**
* Handle the item move event
*
* @param inventory
*/
private boolean handleMoveItemEvent(Inventory initiator, Inventory inventory) {
LWC lwc = LWC.getInstance();
if (inventory == null) {
return false;
}
Location location;
InventoryHolder holder;
Location hopperLocation = null;
InventoryHolder hopperHolder;
try {
holder = inventory.getHolder();
hopperHolder = initiator.getHolder();
} catch (AbstractMethodError e) {
return false;
}
try {
if (holder instanceof BlockState) {
location = ((BlockState) holder).getLocation();
} else if (holder instanceof DoubleChest) {
location = ((DoubleChest) holder).getLocation();
} else {
return false;
}
if (hopperHolder instanceof Hopper) {
hopperLocation = ((Hopper) hopperHolder).getLocation();
}
} catch (Exception e) {
return false;
}
// High-intensity zone: increase protection cache if it's full, otherwise
// the database will be getting rammed
lwc.getProtectionCache().increaseIfNecessary();
// Attempt to load the protection at that location
Protection protection = lwc.findProtection(location);
// If no protection was found we can safely ignore it
if (protection == null) {
return false;
}
if (hopperLocation != null && Boolean.parseBoolean(lwc.resolveProtectionConfiguration(Material.HOPPER, "enabled"))) {
Protection hopperProtection = lwc.findProtection(hopperLocation);
if (hopperProtection != null) {
// if they're owned by the same person then we can allow the move
if (protection.getOwner().equals(hopperProtection.getOwner())) {
return false;
}
}
}
boolean denyHoppers = Boolean.parseBoolean(lwc.resolveProtectionConfiguration(Material.getMaterial(protection.getBlockId()), "denyHoppers"));
// xor = (a && !b) || (!a && b)
if (denyHoppers ^ protection.hasFlag(Flag.Type.HOPPER)) {
return true;
}
return false;
}
@EventHandler(priority = EventPriority.HIGH)
public void onPlayerDropItem(PlayerDropItemEvent event) {
if (event.isCancelled() || !LWC.ENABLED) {
return;
}
Player player = event.getPlayer();
LWCDropItemEvent evt = new LWCDropItemEvent(player, event);
plugin.getLWC().getModuleLoader().dispatchEvent(evt);
if (evt.isCancelled()) {
event.setCancelled(true);
}
}
/*
@EventHandler
public void onPlayerChat(PlayerChatEvent event) {
if (event.isCancelled() || !LWC.ENABLED) {
return;
}
LWC lwc = plugin.getLWC();
if (!lwc.getConfiguration().getBoolean("core.filterunlock", true)) {
return;
}
// We want to block messages starting with cunlock incase someone screws up /cunlock password.
String message = event.getMessage();
if (message.startsWith("cunlock") || message.startsWith("lcunlock") || message.startsWith(".cunlock")) {
event.setCancelled(true);
lwc.sendLocale(event.getPlayer(), "lwc.blockedmessage");
}
}
*/
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
if (!LWC.ENABLED) {
return;
}
LWC lwc = plugin.getLWC();
Player player = event.getPlayer();
LWCPlayer lwcPlayer = lwc.wrapPlayer(player);
if (event.getAction() != Action.LEFT_CLICK_BLOCK && event.getAction() != Action.RIGHT_CLICK_BLOCK) {
return;
}
Block block = event.getClickedBlock();
BlockState state;
try {
state = block.getState();
} catch (NullPointerException e) {
//
lwc.log("Invalid Tile Entity detected at " + block.getLocation());
lwc.log("This is either an issue with your world or a bug in Bukkit");
return;
}
// Prevent players with lwc.deny from interacting with blocks that have an inventory
if (state instanceof InventoryHolder && lwc.isProtectable(block)) {
if (!lwc.hasPermission(player, "lwc.protect") && lwc.hasPermission(player, "lwc.deny") && !lwc.isAdmin(player) && !lwc.isMod(player)) {
lwc.sendLocale(player, "protection.interact.error.blocked");
event.setCancelled(true);
return;
}
}
try {
Set<String> actions = lwcPlayer.getActionNames();
Protection protection = lwc.findProtection(block.getLocation());
Module.Result result;
boolean canAccess = lwc.canAccessProtection(player, protection);
// Calculate if the player has a pending action (i.e any action besides 'interacted')
int actionCount = actions.size();
boolean hasInteracted = actions.contains("interacted");
boolean hasPendingAction = (hasInteracted && actionCount > 1) || (!hasInteracted && actionCount > 0);
if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
boolean ignoreLeftClick = Boolean.parseBoolean(lwc.resolveProtectionConfiguration(block, "ignoreLeftClick"));
if (ignoreLeftClick) {
return;
}
} else if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
boolean ignoreRightClick = Boolean.parseBoolean(lwc.resolveProtectionConfiguration(block, "ignoreRightClick"));
if (ignoreRightClick) {
return;
}
}
// If the event was cancelled and they have an action, warn them
if (event.isCancelled()) {
// only send it if a non-"interacted" action is set which is always set on the player
if (hasPendingAction) {
lwc.sendLocale(player, "lwc.pendingaction");
}
// it's cancelled, do not continue !
return;
}
// register in an action what protection they interacted with (if applicable.)
if (protection != null) {
com.griefcraft.model.Action action = new com.griefcraft.model.Action();
action.setName("interacted");
action.setPlayer(lwcPlayer);
action.setProtection(protection);
lwcPlayer.addAction(action);
}
// events are only used when they already have an action pending
boolean canAdmin = lwc.canAdminProtection(player, protection);
if (protection != null) {
LWCProtectionInteractEvent evt = new LWCProtectionInteractEvent(event, protection, actions, canAccess, canAdmin);
lwc.getModuleLoader().dispatchEvent(evt);
result = evt.getResult();
} else {
LWCBlockInteractEvent evt = new LWCBlockInteractEvent(event, block, actions);
lwc.getModuleLoader().dispatchEvent(evt);
result = evt.getResult();
}
if (result == Module.Result.ALLOW) {
return;
}
// optional.onlyProtectIfOwnerIsOnline
if (protection != null && !canAccess && lwc.getConfiguration().getBoolean("optional.onlyProtectWhenOwnerIsOnline", false)) {
Player owner = protection.getBukkitOwner();
// If they aren't online, allow them in :P
if (owner == null || !owner.isOnline()) {
return;
}
}
// optional.onlyProtectIfOwnerIsOffline
if (protection != null && !canAccess && lwc.getConfiguration().getBoolean("optional.onlyProtectWhenOwnerIsOffline", false)) {
Player owner = protection.getBukkitOwner();
// If they aren't online, allow them in :P
if (owner != null && owner.isOnline()) {
return;
}
}
if (result == Module.Result.DEFAULT) {
canAccess = lwc.enforceAccess(player, protection, block, canAccess);
}
if (!canAccess || result == Module.Result.CANCEL) {
event.setCancelled(true);
event.setUseInteractedBlock(org.bukkit.event.Event.Result.DENY);
}
} catch (Exception e) {
event.setCancelled(true);
event.setUseInteractedBlock(org.bukkit.event.Event.Result.DENY);
lwc.sendLocale(player, "protection.internalerror", "id", "PLAYER_INTERACT");
e.printStackTrace();
}
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerQuit(PlayerQuitEvent event) {
if (!LWC.ENABLED) {
return;
}
// remove the place from the player cache and reset anything they can access
LWCPlayer.removePlayer(event.getPlayer());
}
@EventHandler(ignoreCancelled = true)
public void onInventoryClick(InventoryClickEvent event) {
LWC lwc = LWC.getInstance();
if (!(event.getWhoClicked() instanceof Player)) {
return;
}
// Player interacting with the inventory
Player player = (Player) event.getWhoClicked();
// The inventory they are using
Inventory inventory = event.getInventory();
if (inventory == null || event.getSlot() < 0) {
return;
}
// Location of the container
Location location;
InventoryHolder holder = null;
try {
holder = event.getInventory().getHolder();
} catch (AbstractMethodError e) {
lwc.log("Caught issue with Bukkit's Inventory.getHolder() method! This is occuring NEAR the player: " + player.getName());
lwc.log("This player is located at: " + player.getLocation().toString());
lwc.log("This should be reported to the Bukkit developers.");
e.printStackTrace();
return;
}
try {
if (holder instanceof BlockState) {
location = ((BlockState) holder).getLocation();
} else if (holder instanceof DoubleChest) {
location = ((DoubleChest) holder).getLocation();
} else {
return;
}
} catch (Exception e) {
Location ploc = player.getLocation();
String holderName = holder != null ? holder.getClass().getSimpleName() : "Unknown Block";
lwc.log("Exception with getting the location of a " + holderName + " has occurred NEAR the player: " + player.getName() + " [" + ploc.getBlockX() + " " + ploc.getBlockY() + " " + ploc.getBlockZ() + "]");
lwc.log("The exact location of the block is not possible to obtain. This is caused by a Minecraft or Bukkit exception normally.");
e.printStackTrace();
return;
}
if (event.getAction() != InventoryAction.COLLECT_TO_CURSOR) {
// If it's not a container, we don't want it
if (event.getSlotType() != InventoryType.SlotType.CONTAINER) {
return;
}
// Nifty trick: these will different IFF they are interacting with the player's inventory or hotbar instead of the block's inventory
if (event.getSlot() != event.getRawSlot()) {
return;
}
// The item they are taking/swapping with
ItemStack item;
try {
item = event.getCurrentItem();
} catch (ArrayIndexOutOfBoundsException e) {
return;
}
// Item their cursor has
ItemStack cursor = event.getCursor();
if (item == null || item.getType() == null || item.getType() == Material.AIR) {
return;
}
// if it's not a right click or a shift click it should be a left click (no shift)
// this is for when players are INSERTing items (i.e. item in hand and left clicking)
if (player.getItemInHand() == null && (!event.isRightClick() && !event.isShiftClick())) {
return;
}
// Are they inserting a stack?
if (cursor != null && item.getType() == cursor.getType()) {
boolean enchantmentsEqual = areEnchantmentsEqual(item, cursor);
// If they are clicking an item of the stack type, they are inserting it into the inventory,
// not switching it
// As long as the item isn't a degradable item, we can explicitly allow it if they have the same durability
if (item.getDurability() == cursor.getDurability() && item.getAmount() == cursor.getAmount() && enchantmentsEqual) {
return;
}
}
}
// Attempt to load the protection at that location
Protection protection = lwc.findProtection(location);
// If no protection was found we can safely ignore it
if (protection == null) {
return;
}
// If it's not a donation chest, ignore if
if (protection.getType() != Protection.Type.DONATION) {
return;
}
// Can they admin it? (remove items/etc)
boolean canAdmin = lwc.canAdminProtection(player, protection);
// nope.avi
if (!canAdmin) {
event.setCancelled(true);
}
}
/**
* Compares the enchantments on two item stacks and checks that they are equal (identical)
*
* @param stack1
* @param stack2
* @return
*/
private boolean areEnchantmentsEqual(ItemStack stack1, ItemStack stack2) {
if (stack1 == null || stack2 == null) {
return false;
}
Map<Enchantment, Integer> enchantments1 = stack1.getEnchantments();
Map<Enchantment, Integer> enchantments2 = stack2.getEnchantments();
if (enchantments1.size() != enchantments2.size()) {
return false;
}
// Enchanted Books use ItemMeta
if (stack1.getItemMeta() != null && stack2.getItemMeta() != null) {
if (!stack1.getItemMeta().equals(stack2.getItemMeta())) {
return false;
}
}
for (Enchantment enchantment : enchantments1.keySet()) {
if (!enchantments2.containsKey(enchantment)) {
return false;
}
int level1 = enchantments1.get(enchantment);
int level2 = enchantments2.get(enchantment);
if (level1 != level2) {
return false;
}
}
return true;
}
}