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
|
package com.earth2me.essentials;
import java.util.ArrayList;
import org.bukkit.*;
import org.bukkit.block.*;
import org.bukkit.craftbukkit.block.CraftSign;
import org.bukkit.event.block.*;
public class EssentialsBlockListener extends BlockListener
{
private final Essentials parent;
public final static ArrayList<Material> protectedBlocks = new ArrayList<Material>(4);
static
{
protectedBlocks.add(Material.CHEST);
protectedBlocks.add(Material.BURNING_FURNACE);
protectedBlocks.add(Material.FURNACE);
protectedBlocks.add(Material.DISPENSER);
}
public EssentialsBlockListener(Essentials parent)
{
this.parent = parent;
}
@Override
public void onBlockBreak(BlockBreakEvent event)
{
if (event.isCancelled()) return;
if (Essentials.getSettings().areSignsDisabled()) return;
User user = User.get(event.getPlayer());
if (protectedBlocks.contains(event.getBlock().getType()) && !user.isAuthorized("essentials.signs.protection.override"))
{
if (isBlockProtected(event.getBlock(), user))
{
event.setCancelled(true);
user.sendMessage("§cYou do not have permission to destroy that chest.");
return;
}
}
if (checkProtectionSign(event.getBlock(), user) == NOT_ALLOWED)
{
event.setCancelled(true);
user.sendMessage("§cYou do not have permission to destroy that sign.");
}
}
@Override
public void onSignChange(SignChangeEvent event)
{
if (event.isCancelled()) return;
if (Essentials.getSettings().areSignsDisabled()) return;
User user = User.get(event.getPlayer());
try
{
if (event.getLine(0).equalsIgnoreCase("[Protection]"))
{
Block block = event.getBlock();
if (user.isAuthorized("essentials.signs.protection.create") && hasAdjacentChest(block) && !isBlockProtected(block, user))
event.setLine(0, "§1[Protection]");
else
event.setLine(0, "§4[Protection]");
event.setLine(3, user.getName());
return;
}
if (event.getLine(0).equalsIgnoreCase("[Disposal]"))
{
if (user.isAuthorized("essentials.signs.disposal.create"))
event.setLine(0, "§1[Disposal]");
else
event.setLine(0, "§4[Disposal]");
return;
}
if (event.getLine(0).equalsIgnoreCase("[Heal]"))
{
if (user.isAuthorized("essentials.signs.heal.create"))
event.setLine(0, "§1[Heal]");
else
event.setLine(0, "§4[Heal]");
return;
}
if (event.getLine(0).equalsIgnoreCase("[Free]"))
{
event.setLine(0, "§4[Free]");
ItemDb.get(event.getLine(1));
if (user.isAuthorized("essentials.signs.free.create"))
event.setLine(0, "§1[Free]");
return;
}
if (event.getLine(0).equalsIgnoreCase("[Mail]"))
{
if (user.isAuthorized("essentials.signs.mail.create"))
event.setLine(0, "§1[Mail]");
else
event.setLine(0, "§4[Mail]");
return;
}
}
catch (Throwable ex)
{
user.sendMessage("§cError: " + ex.getMessage());
}
}
@Override
public void onBlockPlace(BlockPlaceEvent event) {
Block signBlock = event.getBlockAgainst();
if (signBlock.getType() == Material.WALL_SIGN || signBlock.getType() == Material.SIGN_POST) {
Sign sign = new CraftSign(signBlock);
if (sign.getLine(0).matches("§1\\[[a-zA-Z]+\\]")) {
event.setCancelled(true);
return;
}
}
}
public boolean hasAdjacentChest(Block block)
{
Block[] faces = getAdjacentBlocks(block);
for (Block b : faces)
{
if (protectedBlocks.contains(b.getType()))
{
return true;
}
}
return false;
}
private static final int NOT_ALLOWED = 0;
private static final int ALLOWED = 1;
private static final int NOSIGN = 2;
private static int checkProtectionSign(Block block, User user)
{
if (block.getType() == Material.SIGN_POST || block.getType() == Material.WALL_SIGN)
{
Sign sign = new CraftSign(block);
if (sign.getLine(0).equalsIgnoreCase("§1[Protection]") && !user.isAuthorized("essentials.signs.protection.override"))
{
if (sign.getLine(1).equalsIgnoreCase(user.getName()))
{
return ALLOWED;
}
if (sign.getLine(2).equalsIgnoreCase(user.getName()))
{
return ALLOWED;
}
if (sign.getLine(3).equalsIgnoreCase(user.getName()))
{
return ALLOWED;
}
return NOT_ALLOWED;
}
}
return NOSIGN;
}
private static Block[] getAdjacentBlocks(Block block)
{
return new Block[]
{
block.getFace(BlockFace.NORTH),
block.getFace(BlockFace.SOUTH),
block.getFace(BlockFace.EAST),
block.getFace(BlockFace.WEST),
block.getFace(BlockFace.DOWN),
block.getFace(BlockFace.UP)
};
}
public static boolean isBlockProtected(Block block, User user)
{
Block[] faces = getAdjacentBlocks(block);
boolean protect = false;
for (Block b : faces)
{
int check = checkProtectionSign(b, user);
if (check == NOT_ALLOWED)
{
protect = true;
}
if (check == ALLOWED)
{
return false;
}
if (protectedBlocks.contains(b.getType()))
{
Block[] faceChest = getAdjacentBlocks(b);
for (Block a : faceChest)
{
check = checkProtectionSign(a, user);
if (check == NOT_ALLOWED)
{
protect = true;
}
if (check == ALLOWED)
{
return false;
}
}
}
}
return protect;
}
}
|