-
Notifications
You must be signed in to change notification settings - Fork 17
/
LWCBlockListener.java
375 lines (304 loc) · 12.9 KB
/
LWCBlockListener.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
/*
* 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.cache.ProtectionCache;
import com.griefcraft.lwc.LWC;
import com.griefcraft.lwc.LWCPlugin;
import com.griefcraft.model.Protection;
import com.griefcraft.scripting.event.LWCProtectionDestroyEvent;
import com.griefcraft.scripting.event.LWCProtectionRegisterEvent;
import com.griefcraft.scripting.event.LWCProtectionRegistrationPostEvent;
import com.griefcraft.scripting.event.LWCRedstoneEvent;
import com.griefcraft.util.Colors;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPistonExtendEvent;
import org.bukkit.event.block.BlockPistonRetractEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.block.BlockRedstoneEvent;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.material.MaterialData;
import org.bukkit.material.PistonBaseMaterial;
import java.util.List;
public class LWCBlockListener implements Listener {
/**
* The plugin instance
*/
private LWCPlugin plugin;
public LWCBlockListener(LWCPlugin plugin) {
this.plugin = plugin;
}
@EventHandler
public void onBlockRedstoneChange(BlockRedstoneEvent event) {
if (!LWC.ENABLED) {
return;
}
LWC lwc = plugin.getLWC();
Block block = event.getBlock();
if (block == null) {
return;
}
Protection protection = lwc.findProtection(block);
if (protection == null) {
return;
}
LWCRedstoneEvent evt = new LWCRedstoneEvent(event, protection);
lwc.getModuleLoader().dispatchEvent(evt);
if (evt.isCancelled()) {
event.setNewCurrent(event.getOldCurrent());
}
}
@EventHandler
public void onSignChange(SignChangeEvent event) {
if (!LWC.ENABLED || event.isCancelled()) {
return;
}
LWC lwc = plugin.getLWC();
Block block = event.getBlock();
Player player = event.getPlayer();
if (block == null) {
return;
}
Protection protection = lwc.findProtection(block);
if (protection == null) {
return;
}
boolean canAccess = lwc.canAccessProtection(player, protection);
if (!canAccess) {
event.setCancelled(true);
}
}
@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
if (!LWC.ENABLED || event.isCancelled()) {
return;
}
LWC lwc = plugin.getLWC();
Player player = event.getPlayer();
Block block = event.getBlock();
Material material = block.getType();
boolean ignoreBlockDestruction = Boolean.parseBoolean(lwc.resolveProtectionConfiguration(material, "ignoreBlockDestruction"));
if (ignoreBlockDestruction) {
return;
}
Protection protection = lwc.findProtection(block);
if (protection == null) {
return;
}
// when destroying a chest, it's possible they are also destroying a double chest
// in the event they're trying to destroy a double chest, we should just move
// the protection to the chest that is not destroyed, if it is not that one already.
if (block.getType() == Material.CHEST) {
// Get the list of protectable blocks that were matched for the protection
List<Block> protectables = protection.getProtectionFinder().getProtectables();
// >= 1 = double chest!
if (protectables.size() > 1) {
// if they destroyed the protected block we want to move it aye?
if (lwc.blockEquals(protection.getBlock(), block)) {
for (Block protectable : protectables) {
// we want a different block
if (lwc.blockEquals(protectable, block)) {
continue;
}
// We need to manually fix the cache
ProtectionCache cache = lwc.getProtectionCache();
cache.remove(protection);
// correct the block
protection.setBlockId(protectable.getTypeId());
protection.setX(protectable.getX());
protection.setY(protectable.getY());
protection.setZ(protectable.getZ());
protection.save();
// put it into the cache again with the updated block location
cache.add(protection);
return;
}
}
// they did not click the protected block, however we still want to prevent removal
return;
}
}
boolean canAccess = lwc.canAccessProtection(player, protection);
boolean canAdmin = lwc.canAdminProtection(player, protection);
try {
LWCProtectionDestroyEvent evt = new LWCProtectionDestroyEvent(player, protection, LWCProtectionDestroyEvent.Method.BLOCK_DESTRUCTION, canAccess, canAdmin);
lwc.getModuleLoader().dispatchEvent(evt);
if (evt.isCancelled()) {
event.setCancelled(true);
}
} catch (Exception e) {
event.setCancelled(true);
lwc.sendLocale(player, "protection.internalerror", "id", "BLOCK_BREAK");
e.printStackTrace();
}
}
@EventHandler
public void onBlockPistonRetract(BlockPistonRetractEvent event) {
if (!LWC.ENABLED || event.isCancelled()) {
return;
}
LWC lwc = plugin.getLWC();
Block piston = event.getBlock();
BlockState state = piston.getState();
MaterialData data = state.getData();
BlockFace direction = null;
// Check the block it pushed directly
if (data instanceof PistonBaseMaterial) {
direction = ((PistonBaseMaterial) data).getFacing();
}
if (direction == null) {
return;
}
// the block that the piston moved
Block moved = piston.getRelative(direction, 2);
// TODO remove this when spout fixes their shit
if (moved.getType() == Material.WOODEN_DOOR || moved.getType() == Material.IRON_DOOR_BLOCK) {
Block below = moved.getRelative(BlockFace.DOWN).getRelative(direction.getOppositeFace());
if (lwc.findProtection(below) != null) {
event.setCancelled(true);
return;
}
}
if (lwc.findProtection(moved) != null) {
event.setCancelled(true);
}
}
@EventHandler
public void onBlockPistonExtend(BlockPistonExtendEvent event) {
if (!LWC.ENABLED || event.isCancelled()) {
return;
}
LWC lwc = plugin.getLWC();
Block piston = event.getBlock();
BlockState state = piston.getState();
MaterialData data = state.getData();
BlockFace direction = null;
// Check the block it pushed directly
if (data instanceof PistonBaseMaterial) {
direction = ((PistonBaseMaterial) data).getFacing();
Block block = event.getBlock().getRelative(direction);
Protection protection = lwc.findProtection(block);
if (protection != null) {
event.setCancelled(true);
return;
}
}
// if no direction was found, no point in going on
if (direction == null) {
return;
}
// Check the affected blocks
for (int i = 0; i < event.getLength() + 2; i++) {
Block block = piston.getRelative(direction, i);
Protection protection = lwc.findProtection(block);
// We don't want that!
if (block.getType() == Material.AIR) {
break;
}
if (protection != null) {
event.setCancelled(true);
break;
}
}
}
/**
* Used for auto registering placed protections
*/
@EventHandler
public void onBlockPlace(BlockPlaceEvent event) {
if (!LWC.ENABLED || event.isCancelled()) {
return;
}
LWC lwc = plugin.getLWC();
Player player = event.getPlayer();
Block block = event.getBlockPlaced();
// The placable block must be protectable
if (!lwc.isProtectable(block)) {
return;
}
String autoRegisterType = plugin.getLWC().resolveProtectionConfiguration(block.getType(), "autoRegister");
// is it auto protectable?
if (!autoRegisterType.equalsIgnoreCase("private") && !autoRegisterType.equalsIgnoreCase("public")) {
return;
}
if (!lwc.hasPermission(player, "lwc.create." + autoRegisterType, "lwc.create", "lwc.protect")) {
return;
}
// Parse the type
Protection.Type type;
try {
type = Protection.Type.valueOf(autoRegisterType.toUpperCase());
} catch (IllegalArgumentException e) {
// No auto protect type found
return;
}
// Is it okay?
if (type == null) {
player.sendMessage(Colors.Red + "LWC_INVALID_CONFIG_autoRegister");
return;
}
// If it's a chest, make sure they aren't placing it beside an already registered chest
if (block.getType() == Material.CHEST) {
BlockFace[] faces = new BlockFace[]{BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST};
for (BlockFace blockFace : faces) {
Block face = block.getRelative(blockFace);
//They're placing it beside a chest, check if it's already protected
if (face.getType() == Material.CHEST) {
if (lwc.getPhysicalDatabase().loadProtection(face.getWorld().getName(), face.getX(), face.getY(), face.getZ()) != null) {
return;
}
}
}
}
try {
LWCProtectionRegisterEvent evt = new LWCProtectionRegisterEvent(player, block);
lwc.getModuleLoader().dispatchEvent(evt);
// something cancelled registration
if (evt.isCancelled()) {
return;
}
// All good!
Protection protection = lwc.getPhysicalDatabase().registerProtection(block.getTypeId(), type, block.getWorld().getName(), player.getName(), "", block.getX(), block.getY(), block.getZ());
if (!Boolean.parseBoolean(lwc.resolveProtectionConfiguration(block.getType(), "quiet"))) {
lwc.sendLocale(player, "protection.onplace.create.finalize", "type", lwc.getPlugin().getMessageParser().parseMessage(autoRegisterType.toLowerCase()), "block", LWC.materialToString(block));
}
if (protection != null) {
lwc.getModuleLoader().dispatchEvent(new LWCProtectionRegistrationPostEvent(protection));
}
} catch (Exception e) {
lwc.sendLocale(player, "protection.internalerror", "id", "PLAYER_INTERACT");
e.printStackTrace();
}
}
}