forked from ChestShop-authors/ChestShop-3
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ChestShopSign.java
210 lines (163 loc) · 7.01 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
package com.Acrobot.ChestShop.Signs;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import com.Acrobot.ChestShop.Utils.ItemNamingUtils;
import java.util.UUID;
import java.util.logging.Level;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType;
/**
* @author Acrobot
*/
public class ChestShopSign {
public static final byte PRICE_LINE = 2;
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§? #:-]+$") };
private static NamespacedKey METADATA_NAMESPACED_KEY;
public static void createNamespacedKeys(ChestShop chestShop) {
METADATA_NAMESPACED_KEY = new NamespacedKey(chestShop, "metadata");
}
public static boolean isAdminShop(Sign sign) {
if (!isChestShop(sign)) {
return false;
}
return getChestShopMetaData(sign).isAdminshop();
}
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) {
return getChestShopMetaData(sign).isOwner(player.getUniqueId());
}
public static UUID getOwner(Sign sign) {
if (!isChestShop(sign)) {
return null;
}
ChestShopMetaData chestShopMetaData = getChestShopMetaData(sign);
return chestShopMetaData.getOwner();
}
public static ItemStack getItemStack(Sign sign) {
if (!isChestShop(sign)) {
return null;
}
ChestShopMetaData chestShopMetaData = getChestShopMetaData(sign);
return chestShopMetaData.getItemStack();
}
public static void addAccessor(UUID player, Sign sign) {
ChestShopMetaData chestShopMetaData = getChestShopMetaData(sign);
chestShopMetaData.addAccessor(player);
saveChestShopMetaData(sign, chestShopMetaData);
}
public static boolean isAccessor(OfflinePlayer player, Sign sign) {
return isAccessor(player.getUniqueId(), sign);
}
public static boolean isAccessor(UUID player, Sign sign) {
ChestShopMetaData chestShopMetaData = getChestShopMetaData(sign);
return chestShopMetaData.isAccessor(player);
}
public static void removeAccessor(UUID accessor, Sign sign) {
ChestShopMetaData chestShopMetaData = getChestShopMetaData(sign);
chestShopMetaData.removeAccessor(accessor);
saveChestShopMetaData(sign, chestShopMetaData);
}
private static void updateLegacyChestShop(Sign sign) {
UUID ownerUUID = LegacyChestShopSign.getOwnerUUID(sign);
int quantity = LegacyChestShopSign.getQuantity(sign);
double buyPrice = LegacyChestShopSign.getBuyPrice(sign);
double sellPrice = LegacyChestShopSign.getSellPrice(sign);
ItemStack itemStack = LegacyChestShopSign.getItemStack(sign);
ChestShopMetaData chestShopMetaData = new ChestShopMetaData(ownerUUID, quantity, sellPrice, buyPrice, itemStack);
saveChestShopMetaData(sign, chestShopMetaData);
}
private static boolean isLegacyChestShop(Sign sign) {
if (sign.getPersistentDataContainer().has(METADATA_NAMESPACED_KEY, PersistentDataType.STRING)) {
return false;
}
if (!LegacyChestShopSign.isValid(sign)) {
return false;
}
if (LegacyChestShopSign.getOwnerUUID(sign) == null) {
return false;
}
return LegacyChestShopSign.getItemStack(sign) != null;
}
public static boolean isChestShop(Block block) {
BlockState state = block.getState();
if (!(state instanceof Sign sign))
return false;
return isChestShop(sign);
}
public static boolean isChestShop(Sign sign) {
if (isLegacyChestShop(sign)) {
updateLegacyChestShop(sign);
return true;
}
boolean isChestshop = sign.getPersistentDataContainer().has(METADATA_NAMESPACED_KEY, PersistentDataType.STRING);
if (isChestshop) {
updateSignDisplay(sign);
}
return isChestshop;
}
private static void updateSignDisplay(Sign sign) {
ChestShopMetaData chestShopMetaData = getChestShopMetaData(sign);
UUID owner = chestShopMetaData.getOwner();
String fullOwnerName = NameManager.getFullNameFor(owner);
sign.setLine(0, fullOwnerName);
sign.setLine(3, ItemNamingUtils.getSignItemName(chestShopMetaData.getItemStack()));
sign.update();
}
public static ChestShopMetaData getChestShopMetaData(Sign sign) {
try {
String string = sign.getPersistentDataContainer().get(METADATA_NAMESPACED_KEY, PersistentDataType.STRING);
YamlConfiguration yamlConfiguration = new YamlConfiguration();
yamlConfiguration.loadFromString(string);
return (ChestShopMetaData) yamlConfiguration.get("metadata");
} catch (Exception e) {
Bukkit.getLogger().log(Level.WARNING,
"Exception loading Chestshop Metadata (" + sign.getX() + " " + sign.getY() + " " + sign.getZ() + ").", e);
return null;
}
}
public static void saveChestShopMetaData(Sign sign, ChestShopMetaData chestShopMetaData) {
Bukkit.getScheduler().scheduleSyncDelayedTask(ChestShop.getPlugin(), () -> {
try {
YamlConfiguration yamlConfiguration = new YamlConfiguration();
yamlConfiguration.set("metadata", chestShopMetaData);
String string = yamlConfiguration.saveToString();
sign.getPersistentDataContainer().set(METADATA_NAMESPACED_KEY, PersistentDataType.STRING, string);
sign.update();
} catch (Exception e) {
Bukkit.getLogger().log(Level.WARNING,
"Exception saving Chestshop Metadata (" + sign.getX() + " " + sign.getY() + " " + sign.getZ() + ").", e);
}
}, 1L);
}
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(':');
}
public static boolean isAdminshopLine(String ownerLine) {
return ownerLine.replace(" ", "").equalsIgnoreCase(Properties.ADMIN_SHOP_NAME.replace(" ", ""));
}
}