forked from ChestShop-authors/ChestShop-3
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ChestShopSign.java
147 lines (130 loc) · 6.08 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
package com.Acrobot.ChestShop.Signs;
import com.Acrobot.Breeze.Utils.BlockUtil;
import com.Acrobot.Breeze.Utils.MaterialUtil;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Containers.AdminInventory;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import com.Acrobot.ChestShop.Utils.uBlock;
import java.util.regex.Pattern;
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;
/**
* @author Acrobot
*/
public class ChestShopSign {
public 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 = "??";
public static boolean isAdminShop(Inventory ownerInventory) {
return ownerInventory instanceof AdminInventory;
}
public static boolean isAdminShop(String owner) {
return owner.replace(" ", "").equalsIgnoreCase(Properties.ADMIN_SHOP_NAME.replace(" ", ""));
}
public static boolean isAdminShop(Sign sign) {
return isAdminShop(sign.getLine(NAME_LINE));
}
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;
}
return NameManager.canUseName(player, sign.getLine(NAME_LINE));
}
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;
}
}