forked from ChestShop-authors/ChestShop-3
-
Notifications
You must be signed in to change notification settings - Fork 2
/
RestrictedSign.java
165 lines (131 loc) · 5.24 KB
/
RestrictedSign.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
package com.Acrobot.ChestShop.Signs;
import com.Acrobot.Breeze.Utils.BlockUtil;
import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Events.PreTransactionEvent;
import static com.Acrobot.ChestShop.Events.PreTransactionEvent.TransactionOutcome.SHOP_IS_RESTRICTED;
import com.Acrobot.ChestShop.Permission;
import static com.Acrobot.ChestShop.Permission.ADMIN;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Sign;
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.BlockBreakEvent;
import org.bukkit.event.block.SignChangeEvent;
/**
* @author Acrobot
*/
public class RestrictedSign implements Listener {
private static final BlockFace[] SIGN_CONNECTION_FACES = { BlockFace.SELF, BlockFace.UP, BlockFace.EAST, BlockFace.WEST, BlockFace.NORTH, BlockFace.SOUTH };
@EventHandler(ignoreCancelled = true)
public static void onBlockDestroy(BlockBreakEvent event) {
Block destroyed = event.getBlock();
Sign attachedRestrictedSign = getRestrictedSign(destroyed.getLocation());
if (attachedRestrictedSign == null) {
return;
}
if (!canDestroy(event.getPlayer(), attachedRestrictedSign)) {
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public static void onSignChange(SignChangeEvent event) {
String[] lines = event.getLines();
Player player = event.getPlayer();
if (isRestricted(lines)) {
if (!hasPermission(player, lines)) {
player.sendMessage(Messages.prefix(Messages.ACCESS_DENIED));
dropSignAndCancelEvent(event);
return;
}
Block connectedSign = event.getBlock().getRelative(BlockFace.DOWN);
if (!Permission.has(player, ADMIN) || !ChestShopSign.isChestShop(connectedSign)) {
dropSignAndCancelEvent(event);
return;
}
Sign sign = (Sign) connectedSign.getState();
if (!ChestShopSign.canAccess(player, sign) && !Permission.has(player, ADMIN)) {
dropSignAndCancelEvent(event);
}
player.sendMessage(Messages.prefix(Messages.RESTRICTED_SIGN_CREATED));
}
}
@EventHandler(priority = EventPriority.LOW)
public static void onPreTransaction(PreTransactionEvent event) {
if (event.isCancelled()) {
return;
}
Sign sign = event.getSign();
if (isRestrictedShop(sign) && !canAccess(sign, event.getClient())) {
event.setCancelled(SHOP_IS_RESTRICTED);
}
}
public static Sign getRestrictedSign(Location location) {
Block currentBlock = location.getBlock();
if (BlockUtil.isSign(currentBlock)) {
Sign sign = (Sign) currentBlock.getState();
if (isRestricted(sign)) {
return sign;
} else {
return null;
}
}
for (BlockFace face : SIGN_CONNECTION_FACES) {
Block relative = currentBlock.getRelative(face);
if (!BlockUtil.isSign(relative)) {
continue;
}
Sign sign = (Sign) relative.getState();
if (!BlockUtil.getAttachedBlock(sign).equals(currentBlock)) {
continue;
}
if (isRestricted(sign)) {
return sign;
}
}
return null; // No sign found
}
public static boolean isRestrictedShop(Sign sign) {
Block blockUp = sign.getBlock().getRelative(BlockFace.UP);
return BlockUtil.isSign(blockUp) && isRestricted(((Sign) blockUp.getState()).getLines());
}
public static boolean isRestricted(String[] lines) {
return lines[0].equalsIgnoreCase("[restricted]");
}
public static boolean isRestricted(Sign sign) {
return isRestricted(sign.getLines());
}
public static boolean canAccess(Sign sign, Player player) {
Block blockUp = sign.getBlock().getRelative(BlockFace.UP);
return !BlockUtil.isSign(blockUp) || hasPermission(player, ((Sign) blockUp.getState()).getLines());
}
public static boolean canDestroy(Player player, Sign sign) {
if (Permission.has(player, ADMIN)) {
return true;
}
Sign shopSign = getAssociatedSign(sign);
return ChestShopSign.canAccess(player, shopSign);
}
public static Sign getAssociatedSign(Sign restricted) {
Block down = restricted.getBlock().getRelative(BlockFace.DOWN);
return BlockUtil.isSign(down) ? (Sign) down.getState() : null;
}
public static boolean hasPermission(Player p, String[] lines) {
if (Permission.has(p, ADMIN)) {
return true;
}
for (String line : lines) {
if (p.hasPermission(Permission.GROUP.toString() + line)) {
return true;
}
}
return false;
}
private static void dropSignAndCancelEvent(SignChangeEvent event) {
event.getBlock().breakNaturally();
event.setCancelled(true);
}
}