summaryrefslogtreecommitdiffstats
path: root/EssentialsSigns/src/com/earth2me/essentials/signs/SignProtection.java
blob: 3921ffc5e5515c0069c210dd2d161a3dbcea4994 (plain)
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
package com.earth2me.essentials.signs;

import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.api.ChargeException;
import com.earth2me.essentials.api.IEssentials;
import com.earth2me.essentials.api.IUser;
import com.earth2me.essentials.economy.Trade;
import com.earth2me.essentials.utils.Util;
import java.util.*;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Sign;
import org.bukkit.inventory.ItemStack;


public class SignProtection extends EssentialsSign
{
	private final transient Set<Material> protectedBlocks = EnumSet.noneOf(Material.class);

	public SignProtection()
	{
		super("Protection");
		protectedBlocks.add(Material.CHEST);
		protectedBlocks.add(Material.BURNING_FURNACE);
		protectedBlocks.add(Material.FURNACE);
		protectedBlocks.add(Material.DISPENSER);
	}

	@Override
	protected boolean onSignCreate(final ISign sign, final IUser player, final String username, final IEssentials ess) throws SignException, ChargeException
	{
		sign.setLine(3, "§4" + username);
		if (hasAdjacentBlock(sign.getBlock()))
		{
			final SignProtectionState state = isBlockProtected(sign.getBlock(), player, username, true, ess);
			if (state == SignProtectionState.NOSIGN || state == SignProtectionState.OWNER
				|| SignsPermissions.PROTECTION_OVERRIDE.isAuthorized(player))
			{
				sign.setLine(3, "§1" + username);
				return true;
			}
		}
		player.sendMessage(_("signProtectInvalidLocation"));
		return false;
	}

	@Override
	protected boolean onSignBreak(final ISign sign, final IUser player, final String username, final IEssentials ess) throws SignException
	{
		final SignProtectionState state = checkProtectionSign(sign, player, username, ess);
		return state == SignProtectionState.OWNER;
	}

	public boolean hasAdjacentBlock(final Block block, final Block... ignoredBlocks)
	{
		final Block[] faces = getAdjacentBlocks(block);
		for (Block b : faces)
		{
			for (Block ignoredBlock : ignoredBlocks)
			{
				if (b.getLocation().equals(ignoredBlock.getLocation()))
				{
					continue;
				}
			}
			if (protectedBlocks.contains(b.getType()))
			{
				return true;
			}
		}
		return false;
	}

	private void checkIfSignsAreBroken(final Block block, final IUser player, final String username, final IEssentials ess)
	{
		final Map<Location, SignProtectionState> signs = getConnectedSigns(block, player, username, false, ess);
		for (Map.Entry<Location, SignProtectionState> entry : signs.entrySet())
		{
			if (entry.getValue() != SignProtectionState.NOSIGN)
			{
				final Block sign = entry.getKey().getBlock();
				if (!hasAdjacentBlock(sign, block))
				{
					block.setType(Material.AIR);
					final Trade trade = new Trade(new ItemStack(Material.SIGN, 1), ess);
					trade.pay(player);
				}
			}
		}
	}

	private Map<Location, SignProtectionState> getConnectedSigns(final Block block, final IUser user, final String username, boolean secure, final IEssentials ess)
	{
		final Map<Location, SignProtectionState> signs = new HashMap<Location, SignProtectionState>();
		getConnectedSigns(block, signs, user, username, secure ? 4 : 2, ess);
		return signs;
	}

	private void getConnectedSigns(final Block block, final Map<Location, SignProtectionState> signs, final IUser user, final String username, final int depth, final IEssentials ess)
	{
		final Block[] faces = getAdjacentBlocks(block);
		for (Block b : faces)
		{
			final Location loc = b.getLocation();
			if (signs.containsKey(loc))
			{
				continue;
			}
			final SignProtectionState check = checkProtectionSign(b, user, username, ess);
			signs.put(loc, check);

			if (protectedBlocks.contains(b.getType()) && depth > 0)
			{
				getConnectedSigns(b, signs, user, username, depth - 1, ess);
			}
		}
	}


	public enum SignProtectionState
	{
		NOT_ALLOWED, ALLOWED, NOSIGN, OWNER
	}

	private SignProtectionState checkProtectionSign(final Block block, final IUser user, final String username, final IEssentials ess)
	{
		if (block.getType() == Material.SIGN_POST || block.getType() == Material.WALL_SIGN)
		{
			final BlockSign sign = new BlockSign(block);
			if (sign.getLine(0).equals(this.getSuccessName()))
			{
				return checkProtectionSign(sign, user, username, ess);
			}
		}
		return SignProtectionState.NOSIGN;
	}

	private SignProtectionState checkProtectionSign(final ISign sign, final IUser user, final String username, final IEssentials ess)
	{
		if (user == null || username == null)
		{
			return SignProtectionState.NOT_ALLOWED;
		}
		if (SignsPermissions.PROTECTION_OVERRIDE.isAuthorized(user))
		{
			return SignProtectionState.OWNER;
		}
		if (Util.stripFormat(sign.getLine(3)).equalsIgnoreCase(username))
		{
			return SignProtectionState.OWNER;
		}
		for (int i = 1; i <= 2; i++)
		{
			final String line = sign.getLine(i);
			if (line.startsWith("(") && line.endsWith(")") && ess.getRanks().inGroup(user, line.substring(1, line.length() - 1)))
			{
				return SignProtectionState.ALLOWED;
			}
			else if (line.equalsIgnoreCase(username))
			{
				return SignProtectionState.ALLOWED;
			}
		}
		return SignProtectionState.NOT_ALLOWED;
	}

	private Block[] getAdjacentBlocks(final Block block)
	{
		return new Block[]
				{
					block.getRelative(BlockFace.NORTH),
					block.getRelative(BlockFace.SOUTH),
					block.getRelative(BlockFace.EAST),
					block.getRelative(BlockFace.WEST),
					block.getRelative(BlockFace.DOWN),
					block.getRelative(BlockFace.UP)
				};
	}

	public SignProtectionState isBlockProtected(final Block block, final IUser user, final String username, boolean secure, final IEssentials ess)
	{
		final Map<Location, SignProtectionState> signs = getConnectedSigns(block, user, username, secure, ess);
		SignProtectionState retstate = SignProtectionState.NOSIGN;
		for (SignProtectionState state : signs.values())
		{
			if (state == SignProtectionState.ALLOWED)
			{
				retstate = state;
			}
			else if (state == SignProtectionState.NOT_ALLOWED && retstate != SignProtectionState.ALLOWED)
			{
				retstate = state;
			}
		}
		if (!secure || retstate == SignProtectionState.NOSIGN)
		{
			for (SignProtectionState state : signs.values())
			{
				if (state == SignProtectionState.OWNER)
				{
					return state;
				}
			}
		}
		return retstate;
	}

	public boolean isBlockProtected(final Block block)
	{
		final Block[] faces = getAdjacentBlocks(block);
		for (Block b : faces)
		{
			if (b.getType() == Material.SIGN_POST || b.getType() == Material.WALL_SIGN)
			{
				final Sign sign = (Sign)b.getState();
				if (sign.getLine(0).equalsIgnoreCase("§1[Protection]"))
				{
					return true;
				}
			}
			if (protectedBlocks.contains(b.getType()))
			{
				final Block[] faceChest = getAdjacentBlocks(b);

				for (Block a : faceChest)
				{
					if (a.getType() == Material.SIGN_POST || a.getType() == Material.WALL_SIGN)
					{
						final Sign sign = (Sign)a.getState();
						if (sign.getLine(0).equalsIgnoreCase("§1[Protection]"))
						{
							return true;
						}
					}
				}
			}
		}
		return false;
	}

	@Override
	public Set<Material> getBlocks()
	{
		return protectedBlocks;
	}

	@Override
	protected boolean onBlockPlace(final Block block, final IUser player, final String username, final IEssentials ess) throws SignException
	{
		for (Block adjBlock : getAdjacentBlocks(block))
		{
			final SignProtectionState state = isBlockProtected(adjBlock, player, username, true, ess);

			if ((state == SignProtectionState.ALLOWED || state == SignProtectionState.NOT_ALLOWED)
				&& !SignsPermissions.PROTECTION_OVERRIDE.isAuthorized(player))
			{
				player.sendMessage(_("noPlacePermission", block.getType().toString().toLowerCase(Locale.ENGLISH)));
				return false;
			}
		}
		return true;

	}

	@Override
	protected boolean onBlockInteract(final Block block, final IUser player, final String username, final IEssentials ess) throws SignException
	{
		final SignProtectionState state = isBlockProtected(block, player, username, false, ess);

		if (state == SignProtectionState.OWNER || state == SignProtectionState.NOSIGN || state == SignProtectionState.ALLOWED)
		{
			return true;
		}

		if (state == SignProtectionState.NOT_ALLOWED
			&& SignsPermissions.PROTECTION_OVERRIDE.isAuthorized(player))
		{
			return true;
		}


		player.sendMessage(_("noAccessPermission", block.getType().toString().toLowerCase(Locale.ENGLISH)));
		return false;
	}

	@Override
	protected boolean onBlockBreak(final Block block, final IUser player, final String username, final IEssentials ess) throws SignException
	{
		final SignProtectionState state = isBlockProtected(block, player, username, false, ess);

		if (state == SignProtectionState.OWNER || state == SignProtectionState.NOSIGN)
		{
			checkIfSignsAreBroken(block, player, username, ess);
			return true;
		}

		if ((state == SignProtectionState.ALLOWED || state == SignProtectionState.NOT_ALLOWED)
			&& SignsPermissions.PROTECTION_OVERRIDE.isAuthorized(player))
		{
			checkIfSignsAreBroken(block, player, username, ess);
			return true;
		}


		player.sendMessage(_("noDestroyPermission", block.getType().toString().toLowerCase(Locale.ENGLISH)));
		return false;
	}

	@Override
	public boolean onBlockBreak(final Block block, final IEssentials ess)
	{
		final SignProtectionState state = isBlockProtected(block, null, null, false, ess);

		return state == SignProtectionState.NOSIGN;
	}

	@Override
	public boolean onBlockExplode(final Block block, final IEssentials ess)
	{
		final SignProtectionState state = isBlockProtected(block, null, null, false, ess);

		return state == SignProtectionState.NOSIGN;
	}

	@Override
	public boolean onBlockBurn(final Block block, final IEssentials ess)
	{
		final SignProtectionState state = isBlockProtected(block, null, null, false, ess);

		return state == SignProtectionState.NOSIGN;
	}

	@Override
	public boolean onBlockIgnite(final Block block, final IEssentials ess)
	{
		final SignProtectionState state = isBlockProtected(block, null, null, false, ess);

		return state == SignProtectionState.NOSIGN;
	}

	@Override
	public boolean onBlockPush(final Block block, final IEssentials ess)
	{
		final SignProtectionState state = isBlockProtected(block, null, null, false, ess);

		return state == SignProtectionState.NOSIGN;
	}
}