-
Notifications
You must be signed in to change notification settings - Fork 17
/
LWCPlayerListener.java
243 lines (200 loc) · 9.3 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
/*
* 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.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 org.bukkit.Material;
import org.bukkit.block.Block;
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.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import java.util.EnumSet;
import java.util.Set;
public class LWCPlayerListener implements Listener {
/**
* The plugin instance
*/
private LWCPlugin plugin;
/**
* Blocks that can contain an inventory
*/
private static final EnumSet<Material> INVENTORY_BLOCKS = EnumSet.of(Material.CHEST, Material.FURNACE, Material.DISPENSER);
public LWCPlayerListener(LWCPlugin plugin) {
this.plugin = plugin;
}
@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();
Material material = block.getType();
// Prevent players with lwc.deny from interacting
if (INVENTORY_BLOCKS.contains(material)) {
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);
Module.Result result = Module.Result.DEFAULT;
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(material, "ignoreLeftClick"));
if (ignoreLeftClick) {
return;
}
} else if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
boolean ignoreRightClick = Boolean.parseBoolean(lwc.resolveProtectionConfiguration(material, "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());
}
}