-
Notifications
You must be signed in to change notification settings - Fork 8
/
ShopHelper.java
276 lines (238 loc) · 11.4 KB
/
ShopHelper.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package ua.leonidius.trdinterface;
import cn.nukkit.item.Item;
import cn.nukkit.item.enchantment.Enchantment;
import cn.nukkit.nbt.NBTIO;
import cn.nukkit.nbt.tag.CompoundTag;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;
import static ua.leonidius.trdinterface.Trading.settings;
public abstract class ShopHelper {
public static Map<Item, Double> getItemAndPrice(int itemRecordId) throws SQLException, IOException {
String query = "SELECT id, price, nbt FROM buy_items WHERE record_id = ?";
PreparedStatement statement = Trading.getDbConnection().prepareStatement(query);
statement.setInt(1, itemRecordId);
ResultSet results = statement.executeQuery();
results.next();
Item item = Item.fromString(results.getString("id"));
byte[] nbtBytes = results.getBytes("nbt");
if (nbtBytes != null && nbtBytes.length != 0) {
CompoundTag nbt = NBTIO.read(nbtBytes);
item.setCompoundTag(nbt);
}
double price = results.getDouble("price");
results.close();
statement.close();
LinkedHashMap<Item, Double> map = new LinkedHashMap<>();
map.put(item, price);
return map;
}
public static Item getItem(int itemRecordId) throws SQLException, IOException {
String query = "SELECT id, nbt FROM buy_items WHERE record_id = ?";
PreparedStatement statement = Trading.getDbConnection().prepareStatement(query);
statement.setInt(1, itemRecordId);
ResultSet results = statement.executeQuery();
results.next();
Item item = Item.fromString(results.getString("id"));
byte[] nbtBytes = results.getBytes("nbt");
results.close();
statement.close();
if (nbtBytes != null && nbtBytes.length != 0) {
CompoundTag nbt = NBTIO.read(nbtBytes);
item.setCompoundTag(nbt);
}
return item;
}
public static String buildDescription(int itemRecordId) throws SQLException, IOException {
StringBuilder contentBuilder = new StringBuilder();
Map<Item, Double> itemAndPrice = ShopHelper.getItemAndPrice(itemRecordId);
Item item = itemAndPrice.keySet().iterator().next();
double priceWithoutDiscount = itemAndPrice.get(item);
double price = priceWithoutDiscount; // TODO: apply discount
contentBuilder.append(Message.WDW_BUY_NAME.getText(item.getName()));
if (!item.getCustomName().equals("")) {
contentBuilder.append("\n")
.append(Message.WDW_BUY_ORIGINAL_NAME.getText(item.getName(), item.getId(), item.getDamage()));
}
contentBuilder.append("\n").append(Message.WDW_BUY_PRICE.getText(price, settings.currency));
/*if (shopItem.discount != 0) {
contentBuilder.append("\n")
.append(Message.WDW_BUY_DISCOUNT.getText(shopItem.discount, shopItem.calculatePrice(), settings.currency));
}*/
if (item.getLore().length != 0) {
StringBuilder loreBuilder = new StringBuilder();
int index = -1;
for (String line : item.getLore()) {
index++;
if (index != item.getLore().length - 1) loreBuilder.append(line);
}
contentBuilder.append("\n")
.append(Message.WDW_BUY_CUSTOM_LORE.getText(loreBuilder.toString()));
}
Enchantment[] enchantments = item.getEnchantments();
if (enchantments != null && enchantments.length != 0) {
contentBuilder.append("\n");
StringBuilder enchListBuilder = new StringBuilder();
for (int i = 0; i < enchantments.length; i++) {
Enchantment enchantment = enchantments[i];
enchListBuilder.append(enchantment.getName()).append(" ").append(enchantment.getLevel());
if (i != enchantments.length - 1) enchListBuilder.append(", ");
}
contentBuilder.append(Message.WDW_BUY_ENCHANTMENTS.getText(enchListBuilder.toString()));
}
return contentBuilder.toString();
}
public static String buildItemButtonText(Item item, double price) {
StringBuilder sb = new StringBuilder();
sb.append(item.getName());
for (Enchantment enchantment : item.getEnchantments()) {
sb.append(getEnchanmentSymbol(enchantment));
}
sb.append("(").append(price).append(settings.currency).append(")");
return sb.toString();
}
private static String getEnchanmentSymbol(Enchantment enchantment) {
switch (enchantment.getId()) { // TODO: complete the list
case Enchantment.ID_PROTECTION_ALL: // 0
return "PA";
case Enchantment.ID_PROTECTION_FIRE: // 1
case Enchantment.ID_PROTECTION_FALL: // 2
case Enchantment.ID_PROTECTION_EXPLOSION:
case Enchantment.ID_PROTECTION_PROJECTILE:
case Enchantment.ID_THORNS:
case Enchantment.ID_WATER_BREATHING: // 6
case Enchantment.ID_WATER_WALKER:
case Enchantment.ID_WATER_WORKER: // 8
case Enchantment.ID_DAMAGE_ALL:
case Enchantment.ID_DAMAGE_SMITE:
case Enchantment.ID_DAMAGE_ARTHROPODS: // 11
case Enchantment.ID_KNOCKBACK:
return "KB";
case Enchantment.ID_FIRE_ASPECT:
return "FA";
case Enchantment.ID_LOOTING:
case Enchantment.ID_EFFICIENCY: // 15
case Enchantment.ID_SILK_TOUCH: // 16
case Enchantment.ID_DURABILITY:
case Enchantment.ID_FORTUNE_DIGGING:
case Enchantment.ID_BOW_POWER:
case Enchantment.ID_BOW_KNOCKBACK:
case Enchantment.ID_BOW_FLAME:
case Enchantment.ID_BOW_INFINITY:
case Enchantment.ID_FORTUNE_FISHING:
case Enchantment.ID_LURE: // 24
return "LU";
case Enchantment.ID_FROST_WALKER:
return "FW";
case Enchantment.ID_MENDING:
case Enchantment.ID_BINDING_CURSE:
case Enchantment.ID_VANISHING_CURSE:
case Enchantment.ID_TRIDENT_IMPALING:
case Enchantment.ID_TRIDENT_RIPTIDE: // 30
case Enchantment.ID_TRIDENT_LOYALTY:
case Enchantment.ID_TRIDENT_CHANNELING:
default:
return "UE"; // Unknown Enchantment
}
}
public static String getBuyCategoryName(int categoryId) throws SQLException {
String nameQuery = "SELECT name FROM categories WHERE record_id = ?";
PreparedStatement nameStatement = Trading.getDbConnection().prepareStatement(nameQuery);
nameStatement.setInt(1, categoryId);
ResultSet nameResults = nameStatement.executeQuery();
nameResults.next();
String name = nameResults.getString("name");
nameResults.close();
nameStatement.close();
return name;
}
public static void addBuyItem(int shopId, int categoryId, Item item, double price) throws SQLException, IOException {
String query = "INSERT INTO buy_items(shop_id, category_id, id, price, nbt) VALUES(?, ?, ?, ?, ?)";
PreparedStatement statement = Trading.getDbConnection().prepareStatement(query);
String id = item.getId() + ":" + item.getDamage();
byte[] nbtBytes = item.getNamedTag() == null ? null : NBTIO.write(item.getNamedTag());
statement.setInt(1, shopId);
statement.setInt(2, categoryId);
statement.setString(3, id);
statement.setDouble(4, price);
statement.setBytes(5, nbtBytes);
statement.executeUpdate();
statement.close();
}
public static void deleteBuyItem(int itemId) throws SQLException {
String query = "DELETE FROM buy_items WHERE record_id = ?";
PreparedStatement statement = Trading.getDbConnection().prepareStatement(query);
statement.setInt(1, itemId);
statement.executeUpdate();
statement.close();
}
public static void addBuyEnchantment(int itemId, int id, int level) throws SQLException, IOException {
String query1 = "SELECT id, nbt FROM buy_items WHERE record_id = ?";
PreparedStatement statement1 = Trading.getDbConnection().prepareStatement(query1);
statement1.setInt(1, itemId);
ResultSet result = statement1.executeQuery();
result.next();
Item item = Item.fromString(result.getString("id"));
byte[] nbtBytes = result.getBytes("nbt");
statement1.close();
if (nbtBytes != null && nbtBytes.length != 0) {
CompoundTag nbt1 = NBTIO.read(nbtBytes);
item.setCompoundTag(nbt1);
}
Enchantment enchantment = Enchantment.get(id).setLevel(level);
item.addEnchantment(enchantment);
byte[] newNbtBytes = NBTIO.write(item.getNamedTag()); // certainly not null
String query2 = "UPDATE buy_items SET nbt = ? WHERE record_id = ?";
PreparedStatement statement2 = Trading.getDbConnection().prepareStatement(query2);
statement2.setBytes(1, newNbtBytes);
statement2.setInt(2, itemId);
statement2.executeUpdate();
statement2.close();
}
public static void removeBuyEnchantment(int itemId, Enchantment enchantment) throws SQLException, IOException {
Item item = getItem(itemId);
Item newItem = Item.fromString(item.getId() + ":" + item.getDamage());
if (!item.getCustomName().equals("")) newItem.setCustomName(item.getCustomName());
if (item.getLore() != null && item.getLore().length != 0) {
newItem.setLore(item.getLore());
}
for (Enchantment enchantment1 : item.getEnchantments()) {
if (enchantment1.getId() != enchantment.getId()) {
newItem.addEnchantment(enchantment1);
}
}
byte[] newNbtBytes = newItem.getNamedTag() == null ? null : NBTIO.write(newItem.getNamedTag());
String query = "UPDATE buy_items SET nbt = ? WHERE record_id = ?";
PreparedStatement statement = Trading.getDbConnection().prepareStatement(query);
statement.setBytes(1, newNbtBytes);
statement.setInt(2, itemId);
statement.executeUpdate();
statement.close();
}
public static void renameCategory(int categoryId, String newName) throws SQLException {
String query = "UPDATE categories SET name = ? WHERE record_id = ?";
PreparedStatement statement = Trading.getDbConnection().prepareStatement(query);
statement.setString(1, newName);
statement.setInt(2, categoryId);
statement.executeUpdate();
statement.close();
}
public static void deleteCategory(int categoryId) throws SQLException {
String query = "DELETE FROM categories WHERE record_id = ?";
PreparedStatement statement = Trading.getDbConnection().prepareStatement(query);
statement.setInt(1, categoryId);
statement.executeUpdate();
statement.close();
}
public static void addCategory(int shopId, String categoryName) throws SQLException {
String query = "INSERT INTO categories(shop_id, name) VALUES(?, ?)";
PreparedStatement statement = Trading.getDbConnection().prepareStatement(query);
statement.setInt(1, shopId);
statement.setString(2, categoryName);
statement.executeUpdate();
statement.close();
}
}