-
Notifications
You must be signed in to change notification settings - Fork 17
/
LWCPlayerListener.java
828 lines (716 loc) · 31.2 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
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
/*
* 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.google.common.base.Objects;
import com.google.common.cache.RemovalCause;
import com.griefcraft.bukkit.EntityBlock;
import com.griefcraft.lwc.BlockMap;
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.entity.Entity;
import org.bukkit.entity.ItemFrame;
import org.bukkit.entity.Player;
import org.bukkit.entity.minecart.HopperMinecart;
import org.bukkit.entity.minecart.StorageMinecart;
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.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.hanging.HangingBreakByEntityEvent;
import org.bukkit.event.hanging.HangingBreakEvent;
import org.bukkit.event.hanging.HangingBreakEvent.RemoveCause;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerArmorStandManipulateEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.vehicle.VehicleDestroyEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.Set;
public class LWCPlayerListener implements Listener {
/**
* The plugin instance
*/
private LWCPlugin plugin;
public LWCPlayerListener(LWCPlugin plugin) {
this.plugin = plugin;
new BukkitRunnable() {
@Override
public void run() {
lastHopper = null;
}
}.runTaskTimer(plugin, 1, 1);
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onHangingBreakByEntity(HangingBreakByEntityEvent e) {
Entity entity = e.getEntity();
LWC lwc = LWC.getInstance();
if (!lwc.isProtectable(entity.getType())) {
return;
}
Protection protection = lwc.findProtection(entity);
if (protection == null) {
return;
}
if (e.getCause() != RemoveCause.ENTITY || !(e.getRemover() instanceof Player)) {
e.setCancelled(true); // never allow non-players to damage protected entities
return;
}
Player p = (Player) e.getRemover();
// owner permission is required for removing hangings
if (!protection.isOwner(p)) {
e.setCancelled(true);
return;
}
protection.remove();
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onMinecartBreak(VehicleDestroyEvent e) {
Entity entity = e.getVehicle();
LWC lwc = LWC.getInstance();
if (!lwc.isProtectable(entity.getType())) {
return;
}
Protection protection = lwc.findProtection(entity);
if (protection == null) {
return;
}
if (!(e.getAttacker() instanceof Player)) {
e.setCancelled(true); // never allow non-players to damage protected entities
return;
}
Player p = (Player) e.getAttacker();
// owner permission is required for minecarts
if (!protection.isOwner(p)) {
e.setCancelled(true);
return;
}
protection.remove();
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onHangingBreak(HangingBreakEvent event) {
if (event instanceof HangingBreakByEntityEvent) {
return;
}
Entity entity = event.getEntity();
LWC lwc = LWC.getInstance();
if (!lwc.isProtectable(entity.getType())) {
return;
}
Protection protection = lwc.findProtection(entity);
if (protection != null) {
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onProtectedEntityDamage(EntityDamageEvent e) {
Entity entity = e.getEntity();
LWC lwc = LWC.getInstance();
if (entity instanceof Player || e instanceof EntityDamageByEntityEvent || !lwc.isProtectable(entity.getType())) {
return;
}
Protection protection = lwc.findProtection(entity);
if (protection != null) {
e.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onProtectedEntityDamageByEntity(EntityDamageByEntityEvent e) {
Entity entity = e.getEntity();
LWC lwc = LWC.getInstance();
if (entity instanceof Player || !lwc.isProtectable(entity.getType())) {
return;
}
Protection protection = lwc.findProtection(entity);
if (protection == null) {
return;
}
if (e.getCause() == DamageCause.BLOCK_EXPLOSION || e.getCause() == DamageCause.ENTITY_EXPLOSION || !(e.getDamager() instanceof Player)) {
e.setCancelled(true); // never allow explosions or non-players to damage protected entities
return;
}
Player p = (Player) e.getDamager();
if (onPlayerEntityInteract(p, entity, e.isCancelled())) {
e.setCancelled(true);
return;
}
// owner permission is required for damaging entities
if (!protection.isOwner(p)) {
if (e.getCause() == DamageCause.ENTITY_ATTACK && entity instanceof ItemFrame) {
ItemStack item = ((ItemFrame) entity).getItem();
if (item != null && item.getType() != Material.AIR) {
return; // special case for ItemFrames: they call a damageevent when the contained item is removed.
// this is allowed for protection members.
}
}
e.setCancelled(true);
}
}
@EventHandler
public void onDeath(EntityDeathEvent e) {
Entity entity = e.getEntity();
LWC lwc = LWC.getInstance();
if (!lwc.isProtectable(entity.getType())) {
return;
}
Protection protection = lwc.findProtection(entity);
if (protection != null) {
protection.remove();
}
}
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerArmorStandManipulate(PlayerArmorStandManipulateEvent e) {
Entity entity = e.getRightClicked();
LWC lwc = LWC.getInstance();
if (!lwc.isProtectable(entity.getType())) {
return;
}
Player p = e.getPlayer();
if (onPlayerEntityInteract(p, entity, e.isCancelled())) {
e.setCancelled(true);
}
Protection protection = lwc.findProtection(entity);
if (protection == null) {
return;
}
if (!e.isCancelled() && !lwc.canAccessProtection(p, protection)) {
e.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onEntityInteract(PlayerInteractEntityEvent e) {
Entity entity = e.getRightClicked();
LWC lwc = LWC.getInstance();
if (entity instanceof Player || !lwc.isProtectable(entity.getType())) {
return;
}
Player p = e.getPlayer();
if (onPlayerEntityInteract(p, entity, e.isCancelled())) {
e.setCancelled(true);
}
Protection protection = lwc.findProtection(entity);
if (protection == null) {
return;
}
if (!lwc.canAccessProtection(p, protection)) {
e.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onAtEntityInteract(PlayerInteractAtEntityEvent e) {
Entity entity = e.getRightClicked();
LWC lwc = LWC.getInstance();
if (entity instanceof Player || !lwc.isProtectable(entity.getType())) {
return;
}
Player p = e.getPlayer();
if (onPlayerEntityInteract(p, entity, e.isCancelled())) {
e.setCancelled(true);
}
Protection protection = lwc.findProtection(entity);
if (protection == null) {
return;
}
if (!lwc.canAccessProtection(p, protection)) {
e.setCancelled(true);
}
}
@EventHandler
public void storageMinecraftInventoryOpen(InventoryOpenEvent event) {
InventoryHolder holder = event.getInventory().getHolder();
if ((!(holder instanceof StorageMinecart)) && (!(holder instanceof HopperMinecart))) {
return;
}
Entity entity = (Entity) holder;
LWC lwc = LWC.getInstance();
if (!lwc.isProtectable(entity.getType())) {
return;
}
if (onPlayerEntityInteract((Player) event.getPlayer(), entity, event.isCancelled())) {
event.setCancelled(true);
}
}
private boolean onPlayerEntityInteract(Player player, Entity entity, boolean cancelled) {
// attempt to load the protection for this cart
LWC lwc = LWC.getInstance();
Protection protection = lwc.findProtection(entity);
LWCPlayer lwcPlayer = lwc.wrapPlayer(player);
try {
Set<String> actions = lwcPlayer.getActionNames();
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 the event was cancelled and they have an action, warn them
if (cancelled) {
// 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 false;
}
// 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);
Block fakeBlock = EntityBlock.getEntityBlock(entity);
PlayerInteractEvent fakeEvent = new PlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, null, fakeBlock, null);
if (protection != null) {
LWCProtectionInteractEvent evt = new LWCProtectionInteractEvent(fakeEvent, protection, actions, canAccess, canAdmin);
lwc.getModuleLoader().dispatchEvent(evt);
result = evt.getResult();
} else {
LWCBlockInteractEvent evt = new LWCBlockInteractEvent(fakeEvent, fakeBlock, actions);
lwc.getModuleLoader().dispatchEvent(evt);
result = evt.getResult();
}
if (result == Module.Result.ALLOW) {
return false;
}
// 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 false;
}
}
// 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 false;
}
}
if (result == Module.Result.DEFAULT) {
canAccess = lwc.enforceAccess(player, protection, fakeBlock, canAccess);
}
if (!canAccess || result == Module.Result.CANCEL) {
return true;
}
} catch (Exception e) {
lwc.sendLocale(player, "protection.internalerror", "id", "PLAYER_INTERACT");
e.printStackTrace();
return true;
}
return false;
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
UUIDRegistry.updateCache(player.getUniqueId(), player.getName());
}
private Inventory lastHopper;
private boolean lastHopperWasSource;
private boolean lastHopperResult;
@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()) {
if (Objects.equal(lastHopper, event.getInitiator()) && lastHopperWasSource == true) {
result = lastHopperResult;
// plugin.getLogger().info("Hopper == lasthopper");
} else {
result = handleMoveItemEvent(event.getInitiator(), event.getDestination());
lastHopper = event.getInitiator();
lastHopperWasSource = true;
lastHopperResult = result;
// plugin.getLogger().info("Hopper != lasthopper");
}
} else {
if (Objects.equal(lastHopper, event.getInitiator()) && lastHopperWasSource == false) {
result = lastHopperResult;
// plugin.getLogger().info("Hopper == lasthopper");
} else {
result = handleMoveItemEvent(event.getInitiator(), event.getSource());
lastHopper = event.getInitiator();
lastHopperWasSource = false;
lastHopperResult = result;
// plugin.getLogger().info("Hopper != lasthopper");
}
}
if (result) {
event.setCancelled(true);
}
}
/**
* Handle the item move event
*
* @param inventory
* @return true if the move should be cancelled
*/
private boolean handleMoveItemEvent(Inventory initiator, Inventory inventory) {
LWC lwc = LWC.getInstance();
if (inventory == null) {
return false;
}
Location location = null;
Entity entityHolder = null;
InventoryHolder holder;
Location initiatorLocation = null;
Entity initiatorEntity = null;
InventoryHolder initiatorHolder;
try {
holder = inventory.getHolder();
initiatorHolder = 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 if (holder instanceof Entity && lwc.isProtectable(((Entity)holder).getType())) {
entityHolder = (Entity) holder;
} else {
return false;
}
if (initiatorHolder instanceof Hopper) {
initiatorLocation = ((Hopper) initiatorHolder).getLocation();
} else if(initiatorHolder instanceof Entity && lwc.isProtectable(((Entity)initiatorHolder).getType())) {
initiatorEntity = (Entity) initiatorHolder;
}
} 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/entity
Protection protection = entityHolder == null ? lwc.findProtection(location) : lwc.findProtection(entityHolder);
// If no protection was found we can safely ignore it
if (protection == null) {
return false;
}
if ((initiatorLocation != null || initiatorEntity != null) && Boolean.parseBoolean(lwc.resolveProtectionConfiguration(Material.HOPPER, "enabled"))) {
Protection initiatorProtection = initiatorEntity == null ? lwc.findProtection(initiatorLocation) : lwc.findProtection(initiatorEntity);
if (initiatorProtection != null) {
// if they're owned by the same person then we can allow the
// move
if (protection.getOwner().equals(initiatorProtection.getOwner())) {
return false;
}
}
}
boolean denyHoppers = Boolean.parseBoolean(lwc.resolveProtectionConfiguration(BlockMap.instance().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.getInventory().getItemInMainHand() == null && (!event.isRightClick() && !event.isShiftClick())) {
return;
}
// Are they inserting a stack?
if (cursor != null && item.isSimilar(cursor)) {
// If they are clicking an item of the stack type, they are
// inserting it into the inventory, not switching it
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);
}
}
}