forked from ChestShop-authors/ChestShop-3
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ChestShopSign.java
250 lines (206 loc) · 9.92 KB
/
ChestShopSign.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
package com.Acrobot.ChestShop.Signs;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.UUID;
import java.util.regex.Pattern;
import org.bukkit.NamespacedKey;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Container;
import org.bukkit.block.ShulkerBox;
import org.bukkit.block.Sign;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import com.Acrobot.Breeze.Utils.BlockUtil;
import com.Acrobot.Breeze.Utils.MaterialUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Containers.AdminInventory;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import com.Acrobot.ChestShop.Utils.uBlock;
/**
* @author Acrobot
*/
public class ChestShopSign {
private static final byte NAME_LINE = 0;
public static final byte QUANTITY_LINE = 1;
public static final byte PRICE_LINE = 2;
public static final byte ITEM_LINE = 3;
public static final Pattern[] SHOP_SIGN_PATTERN = { Pattern.compile("^?[\\w -.]*$"), Pattern.compile("^[1-9][0-9]{0,4}$"), Pattern.compile("(?i)^[\\d.bs(free) :]+$"), Pattern.compile("^[\\w? #:-]+$") };
public static final String AUTOFILL_CODE = "?";
public static final String AUTOFILL_SHULKER_CONTENT_CODE = "??";
private static NamespacedKey OWNER_NAMESPACED_KEY;
private static NamespacedKey ADMINSHOP_NAMESPACED_KEY;
private static NamespacedKey ACCESSORS_NAMESPACED_KEY;
public static void createNamespacedKeys(ChestShop chestShop) {
OWNER_NAMESPACED_KEY = new NamespacedKey(chestShop, "owner-key");
ADMINSHOP_NAMESPACED_KEY = new NamespacedKey(chestShop, "adminshop-key");
ACCESSORS_NAMESPACED_KEY = new NamespacedKey(chestShop, "accessors-key");
}
public static boolean isAdminShop(Inventory ownerInventory) {
return ownerInventory instanceof AdminInventory;
}
public static boolean isAdminShopNameString(String string) {
return string.replace(" ", "").equalsIgnoreCase(Properties.ADMIN_SHOP_NAME.replace(" ", ""));
}
public static boolean isAdminShop(Sign sign) {
PersistentDataContainer persistentDataContainer = sign.getPersistentDataContainer();
if (!persistentDataContainer.has(ADMINSHOP_NAMESPACED_KEY, PersistentDataType.BOOLEAN)) {
if (!isAdminShopNameString(sign.getLine(NAME_LINE))) {
return false;
}
persistentDataContainer.set(ADMINSHOP_NAMESPACED_KEY, PersistentDataType.BOOLEAN, Boolean.TRUE);
}
return persistentDataContainer.get(ADMINSHOP_NAMESPACED_KEY, PersistentDataType.BOOLEAN).booleanValue();
}
public static boolean isValid(Sign sign) {
return isValid(sign.getLines());
}
public static boolean isValid(String[] line) {
return isValidPreparedSign(line) && (line[PRICE_LINE].toUpperCase().contains("B") || line[PRICE_LINE].toUpperCase().contains("S")) && !line[NAME_LINE].isEmpty();
}
public static boolean isValid(Block sign) {
return BlockUtil.isSign(sign) && isValid((Sign) sign.getState());
}
public static boolean canAccess(OfflinePlayer player, Sign sign) {
if (player == null) {
return false;
}
if (sign == null) {
return true;
}
if (isOwner(player, sign))
return true;
return isAccessor(player, sign);
}
public static boolean isOwner(OfflinePlayer player, Sign sign) {
PersistentDataContainer persistentDataContainer = sign.getPersistentDataContainer();
String playerUUIDAsString = player.getUniqueId().toString();
if (!persistentDataContainer.has(OWNER_NAMESPACED_KEY, PersistentDataType.STRING)) {
if (!NameManager.canUseName(player, sign.getLine(NAME_LINE))) {
return false; // Player isn't Owner
}
// Player is Owner, but it's only saved on the sign. Update to new way of saving the shop owner.
persistentDataContainer.set(OWNER_NAMESPACED_KEY, PersistentDataType.STRING, playerUUIDAsString);
}
String owner = getOwner(sign);
return playerUUIDAsString.equals(owner);
}
public static String getOwner(Sign sign) {
return sign.getPersistentDataContainer().get(OWNER_NAMESPACED_KEY, PersistentDataType.STRING);
}
public static UUID getOwnerUUID(Sign sign) {
if (isAdminShop(sign)) {
return NameManager.getAdminShopUUID();
}
String owner = getOwner(sign);
if (owner == null)
return null;
return UUID.fromString(owner);
}
public static boolean isAccessor(OfflinePlayer player, Sign sign) {
Collection<String> accessors = getAccessors(sign);
return accessors.contains(player.getUniqueId().toString());
}
public static Collection<String> getAccessors(Sign sign) {
PersistentDataContainer persistentDataContainer = sign.getPersistentDataContainer();
if (!persistentDataContainer.has(ACCESSORS_NAMESPACED_KEY, PersistentDataType.STRING))
return new HashSet<>();
String data = persistentDataContainer.get(ACCESSORS_NAMESPACED_KEY, PersistentDataType.STRING);
String[] split = data.split(";");
return Arrays.asList(split);
}
public static void addAccessor(OfflinePlayer player, Sign sign) {
Collection<String> accessors = getAccessors(sign);
accessors.add(player.getUniqueId().toString());
saveAccessors(accessors, sign);
}
public static void removeAccessor(OfflinePlayer player, Sign sign) {
Collection<String> accessors = getAccessors(sign);
accessors.remove(player.getUniqueId().toString());
saveAccessors(accessors, sign);
}
private static void saveAccessors(Collection<String> accessors, Sign sign) {
String joined = String.join(";", accessors);
sign.getPersistentDataContainer().set(ACCESSORS_NAMESPACED_KEY, PersistentDataType.STRING, joined);
}
public static boolean isValidPreparedSign(String[] lines) {
for (int i = 0; i < 4; i++) {
if (!SHOP_SIGN_PATTERN[i].matcher(lines[i]).matches()) {
return false;
}
}
return lines[PRICE_LINE].indexOf(':') == lines[PRICE_LINE].lastIndexOf(':');
}
/**
* Returns the itemCode as it should display on a sign or null, if the itemCode is invalid.
*
* @param itemCode
* the user entered itemCode
* @param sign
* the sign to set the item, required for autofill
* @return the corrected itemCode or null if it was invalid
*/
public static String getCorrectedItemCode(String itemCode, Sign sign) {
ItemStack item = MaterialUtil.getItem(itemCode);
if (item == null) {
if (Properties.ALLOW_AUTO_ITEM_FILL && (itemCode.equals(AUTOFILL_CODE) || itemCode.equals(AUTOFILL_SHULKER_CONTENT_CODE))) {
Container connectedChest = uBlock.findConnectedChest(sign, true);
if (connectedChest != null) {
if (itemCode.equals(AUTOFILL_SHULKER_CONTENT_CODE)) {
out: for (ItemStack stack : connectedChest.getInventory().getContents()) {
if (!MaterialUtil.isEmpty(stack) && BlockUtil.isShulkerBox(stack.getType())) {
ItemMeta meta = stack.getItemMeta();
if (meta instanceof BlockStateMeta) {
BlockStateMeta bsm = (BlockStateMeta) meta;
BlockState blockState = bsm.getBlockState();
if (blockState instanceof ShulkerBox) {
ShulkerBox shulkerBox = (ShulkerBox) blockState;
for (ItemStack shulkerContent : shulkerBox.getSnapshotInventory().getStorageContents()) {
if (!MaterialUtil.isEmpty(shulkerContent)) {
item = shulkerContent;
itemCode = MaterialUtil.getSignName(shulkerContent);
if (itemCode == null) {
item = null;
}
break out;
}
}
}
}
}
}
} else {
for (ItemStack stack : connectedChest.getInventory().getContents()) {
if (!MaterialUtil.isEmpty(stack)) {
item = stack;
itemCode = MaterialUtil.getSignName(stack);
if (itemCode == null) {
item = null;
}
break;
}
}
}
}
}
if (item == null) {
return null;
}
}
String metaCode = MaterialUtil.Metadata.getMetaCodeFromItemCode(itemCode);
metaCode = metaCode == null ? "" : "#" + metaCode;
String itemName = MaterialUtil.getSignMaterialName(item.getType(), metaCode);
ItemStack newItem = MaterialUtil.getItem(itemCode);
if (newItem == null || !newItem.isSimilar(item)) {
return null;
}
return itemName + metaCode;
}
}