forked from ChestShop-authors/ChestShop-3
-
Notifications
You must be signed in to change notification settings - Fork 2
/
LightweightChestProtection.java
131 lines (106 loc) · 4.08 KB
/
LightweightChestProtection.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
package com.Acrobot.ChestShop.Plugins;
import com.Acrobot.ChestShop.Security;
import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Events.ShopCreatedEvent;
import com.Acrobot.ChestShop.Events.ShopDestroyedEvent;
import com.Acrobot.ChestShop.Events.Protection.ProtectBlockEvent;
import com.Acrobot.ChestShop.Events.Protection.ProtectionCheckEvent;
import com.griefcraft.lwc.LWC;
import com.griefcraft.model.Protection;
import com.griefcraft.scripting.event.LWCProtectionRegisterEvent;
import org.bukkit.block.Block;
import org.bukkit.block.Container;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
/**
* @author Acrobot
*/
public class LightweightChestProtection implements Listener {
private LWC lwc;
public LightweightChestProtection() {
this.lwc = LWC.getInstance();
}
@EventHandler
public static void onShopCreation(ShopCreatedEvent event) {
Player player = event.getPlayer();
Sign sign = event.getSign();
Container connectedChest = event.getChest();
if (Properties.PROTECT_SIGN_WITH_LWC) {
if (!Security.protect(player, sign.getBlock())) {
player.sendMessage(Messages.prefix(Messages.NOT_ENOUGH_PROTECTIONS));
}
}
if (Properties.PROTECT_CHEST_WITH_LWC && connectedChest != null && Security.protect(player, connectedChest.getBlock())) {
player.sendMessage(Messages.prefix(Messages.PROTECTED_SHOP));
}
}
@EventHandler
public void onProtectionCheck(ProtectionCheckEvent event) {
if (event.getResult() == Event.Result.DENY) {
return;
}
Block block = event.getBlock();
Player player = event.getPlayer();
Protection protection = lwc.findProtection(block);
if (protection == null) {
return;
}
try {
if (!lwc.canAccessProtectionContents(player, protection)) {
event.setResult(Event.Result.DENY);
}
} catch (NoSuchMethodError e) {
// use old method as fallback
if (protection.getType() == Protection.Type.DONATION ? !lwc.canAdminProtection(player, protection) : !lwc.canAccessProtection(player, protection)) {
event.setResult(Event.Result.DENY);
}
}
}
@EventHandler
public void onBlockProtect(ProtectBlockEvent event) {
if (event.isProtected()) {
return;
}
Block block = event.getBlock();
Player player = event.getPlayer();
if (player == null) {
return;
}
Protection existingProtection = lwc.findProtection(block);
if (existingProtection != null) {
event.setProtected(true);
return;
}
LWCProtectionRegisterEvent protectionEvent = new LWCProtectionRegisterEvent(player, block);
lwc.getModuleLoader().dispatchEvent(protectionEvent);
if (protectionEvent.isCancelled()) {
return;
}
int x = block.getX();
int y = block.getY();
int z = block.getZ();
String worldName = block.getWorld().getName();
Protection protection = lwc.getPhysicalDatabase().registerProtection(block.getType(), Protection.Type.PRIVATE, worldName, player.getUniqueId().toString(), "", x, y, z);
if (protection != null) {
event.setProtected(true);
}
}
@EventHandler
public void onShopRemove(ShopDestroyedEvent event) {
Protection signProtection = lwc.findProtection(event.getSign().getBlock());
if (signProtection != null) {
signProtection.remove();
}
if (event.getChest() == null || !Properties.REMOVE_LWC_PROTECTION_AUTOMATICALLY) {
return;
}
Protection chestProtection = lwc.findProtection(event.getChest().getBlock());
if (chestProtection != null) {
chestProtection.remove();
}
}
}