forked from ChestShop-authors/ChestShop-3
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SignCreate.java
159 lines (132 loc) · 6.29 KB
/
SignCreate.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
package com.Acrobot.ChestShop.Listeners.Block;
import java.util.UUID;
import org.bukkit.Material;
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.block.sign.Side;
import org.bukkit.block.sign.SignSide;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.ItemMeta;
import com.Acrobot.Breeze.Utils.BlockUtil;
import com.Acrobot.Breeze.Utils.MaterialUtil;
import com.Acrobot.Breeze.Utils.PriceUtil;
import com.Acrobot.Breeze.Utils.StringUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import com.Acrobot.ChestShop.Events.ShopCreatedEvent;
import com.Acrobot.ChestShop.Signs.ChestShopMetaData;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import com.Acrobot.ChestShop.Utils.uBlock;
/**
* @author Acrobot
*/
public class SignCreate implements Listener {
public static final String AUTOFILL_CODE = "?";
public static final String AUTOFILL_SHULKER_CONTENT_CODE = "??";
@EventHandler
public static void onSignChange(SignChangeEvent event) {
Block signBlock = event.getBlock();
if (event.getSide() == Side.BACK) {
if (ChestShopSign.isChestShop(signBlock)) {
event.setCancelled(true);
}
return;
}
String[] line = StringUtil.stripColourCodes(event.getLines());
if (!BlockUtil.isSign(signBlock)) {
return;
}
if (!ChestShopSign.isValidPreparedSign(line)) {
return;
}
ItemStack itemStack = getItemStack(event.getLine(4), (Sign) event.getBlock().getState());
PreShopCreationEvent preEvent = new PreShopCreationEvent(event.getPlayer(), (Sign) signBlock.getState(), line, itemStack);
ChestShop.callEvent(preEvent);
if (preEvent.isCancelled()) {
return;
}
for (byte i = 0; i < event.getLines().length; ++i) {
event.setLine(i, preEvent.getSignLine(i));
}
ChestShopMetaData chestShopMetaData = createShopMetaData(preEvent.getSign(), event.getPlayer(), event.getLines(), itemStack);
ShopCreatedEvent postEvent = new ShopCreatedEvent(preEvent.getPlayer(), preEvent.getSign(),
uBlock.findConnectedChest(preEvent.getSign()), preEvent.getSignLines(), chestShopMetaData);
ChestShop.callEvent(postEvent);
// clear back side
Sign sign = (Sign) signBlock.getState();
SignSide signSide = sign.getSide(Side.BACK);
for (byte i = 0; i < 4; ++i) {
signSide.setLine(i, "");
}
ChestShopSign.saveChestShopMetaData(sign, chestShopMetaData);
}
public static ChestShopMetaData createShopMetaData(Sign sign, Player creator, String[] signLines, ItemStack itemStack) {
int quantity = Integer.parseInt(signLines[1].replaceAll("[^0-9]", ""));
String priceLine = signLines[3];
double sellPrice = PriceUtil.getSellPrice(priceLine);
double buyPrice = PriceUtil.getBuyPrice(priceLine);
String ownerLine = signLines[0];
boolean isAdminShop = ChestShopSign.isAdminshopLine(ownerLine);
if (isAdminShop) {
return createAdminChestShop(sign, quantity, sellPrice, buyPrice, itemStack);
} else {
return createChestShop(sign, creator.getUniqueId(), quantity, sellPrice, buyPrice, itemStack);
}
}
private static ChestShopMetaData createChestShop(Sign sign, UUID owner, int quantity, double sellPrice, double buyPrice,
ItemStack itemStack) {
return new ChestShopMetaData(owner, quantity, sellPrice, buyPrice, itemStack);
}
private static ChestShopMetaData createAdminChestShop(Sign sign, int quantity, double sellPrice, double buyPrice, ItemStack itemStack) {
return new ChestShopMetaData(NameManager.getAdminShopUUID(), quantity, sellPrice, buyPrice, itemStack);
}
private static ItemStack getItemStack(String itemLine, Sign sign) {
Material material = MaterialUtil.getMaterial(itemLine);
if (material != null)
return new ItemStack(material);
ItemStack item = null;
if (Properties.ALLOW_AUTO_ITEM_FILL && (itemLine.equals(AUTOFILL_CODE) || itemLine.equals(AUTOFILL_SHULKER_CONTENT_CODE))) {
Container connectedChest = uBlock.findConnectedChest(sign, true);
if (connectedChest != null) {
if (itemLine.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;
break out;
}
}
}
}
}
}
} else {
for (ItemStack stack : connectedChest.getInventory().getContents()) {
if (!MaterialUtil.isEmpty(stack)) {
item = stack;
break;
}
}
}
}
}
return item;
}
}