/
DataManager.java
433 lines (366 loc) · 15.5 KB
/
DataManager.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
* Skulls
* Copyright 2022 Kiran Hart
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package ca.tweetzy.skulls.database;
import ca.tweetzy.flight.database.Callback;
import ca.tweetzy.flight.database.DataManagerAbstract;
import ca.tweetzy.flight.database.DatabaseConnector;
import ca.tweetzy.flight.database.UpdateCallback;
import ca.tweetzy.flight.utils.Common;
import ca.tweetzy.flight.utils.SerializeUtil;
import ca.tweetzy.skulls.Skulls;
import ca.tweetzy.skulls.api.enums.BaseCategory;
import ca.tweetzy.skulls.api.interfaces.*;
import ca.tweetzy.skulls.impl.*;
import lombok.NonNull;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.UUID;
import java.util.stream.Collectors;
/**
* Date Created: April 01 2022
* Time Created: 11:23 p.m.
*
* @author Kiran Hart
*/
public final class DataManager extends DataManagerAbstract {
public DataManager(DatabaseConnector databaseConnector, Plugin plugin) {
super(databaseConnector, plugin);
}
public void insertSkulls(Collection<Skull> skulls) {
this.runAsync(() -> this.databaseConnector.connect(connection -> {
PreparedStatement statement = connection.prepareStatement("INSERT OR IGNORE INTO " + this.getTablePrefix() + "skull(id, name, category, texture, tags, price, blocked) VALUES(?, ?, ?, ?, ?, ?, ?)");
for (Skull skull : skulls) {
statement.setInt(1, skull.getId());
statement.setString(2, skull.getName());
statement.setString(3, skull.getCategory());
statement.setString(4, skull.getTexture());
statement.setString(5, String.join(",", skull.getTags()));
statement.setDouble(6, skull.getPrice());
statement.setBoolean(7, skull.isBlocked());
statement.addBatch();
}
statement.executeBatch();
Skulls.getInstance().getSkullManager().setDownloading(false);
Common.broadcast("&r&aFinished inserting all heads into the data file!");
}));
}
public void insertHistories(Collection<History> histories) {
this.runAsync(() -> this.databaseConnector.connect(connection -> {
PreparedStatement statement = connection.prepareStatement("INSERT INTO " + this.getTablePrefix() + "history(id, time, skulls) VALUES(?, ?, ?)");
for (History history : histories) {
statement.setInt(1, history.getID());
statement.setLong(2, history.getTime());
statement.setString(3, history.getSkulls().stream().map(String::valueOf).collect(Collectors.joining(",")));
statement.addBatch();
}
statement.executeBatch();
}));
}
public void insertHistory(@NonNull final History history, Callback<History> callback) {
this.runAsync(() -> this.databaseConnector.connect(connection -> {
try (PreparedStatement statement = connection.prepareStatement("INSERT INTO " + this.getTablePrefix() + "history (id, time, skulls) VALUES(?, ?, ?)")) {
PreparedStatement fetch = connection.prepareStatement("SELECT * FROM " + this.getTablePrefix() + "history WHERE id = ?");
fetch.setInt(1, history.getID());
statement.setInt(1, history.getID());
statement.setLong(2, history.getTime());
statement.setString(3, history.getSkulls().stream().map(String::valueOf).collect(Collectors.joining(",")));
statement.executeUpdate();
if (callback != null) {
ResultSet res = fetch.executeQuery();
res.next();
callback.accept(null, extractHistory(res));
}
} catch (Exception e) {
e.printStackTrace();
resolveCallback(callback, e);
}
}));
}
public void insertPlacedSkull(@NonNull final PlacedSkull placedSkull, Callback<PlacedSkull> callback) {
this.runAsync(() -> this.databaseConnector.connect(connection -> {
try (PreparedStatement statement = connection.prepareStatement("INSERT INTO " + this.getTablePrefix() + "placed_skull (id, skull_id, location) VALUES(?, ?, ?)")) {
PreparedStatement fetch = connection.prepareStatement("SELECT * FROM " + this.getTablePrefix() + "placed_skull WHERE id = ?");
fetch.setString(1, placedSkull.getId().toString());
statement.setString(1, placedSkull.getId().toString());
statement.setInt(2, placedSkull.getSkullId());
statement.setString(3, SerializeUtil.serializeLocation(placedSkull.getLocation()));
statement.executeUpdate();
if (callback != null) {
ResultSet res = fetch.executeQuery();
res.next();
callback.accept(null, extractPlacedSkull(res));
}
} catch (Exception e) {
e.printStackTrace();
resolveCallback(callback, e);
}
}));
}
public void getPlacedSkulls(Callback<ArrayList<PlacedSkull>> callback) {
ArrayList<PlacedSkull> placedSkulls = new ArrayList<>();
this.runAsync(() -> this.databaseConnector.connect(connection -> {
try (PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + this.getTablePrefix() + "placed_skull")) {
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
placedSkulls.add(extractPlacedSkull(resultSet));
}
callback.accept(null, placedSkulls);
} catch (Exception e) {
resolveCallback(callback, e);
}
}));
}
public void deletePlacedSkull(final UUID id, Callback<Boolean> callback) {
this.runAsync(() -> this.databaseConnector.connect(connection -> {
try (PreparedStatement statement = connection.prepareStatement("DELETE FROM " + this.getTablePrefix() + "placed_skull WHERE id = ?")) {
statement.setString(1, id.toString());
int result = statement.executeUpdate();
callback.accept(null, result > 0);
} catch (Exception e) {
resolveCallback(callback, e);
}
}));
}
public void getHistories(Callback<ArrayList<History>> callback) {
ArrayList<History> histories = new ArrayList<>();
this.runAsync(() -> this.databaseConnector.connect(connection -> {
try (PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + this.getTablePrefix() + "history")) {
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
histories.add(extractHistory(resultSet));
}
callback.accept(null, histories);
} catch (Exception e) {
resolveCallback(callback, e);
}
}));
}
//id, name, category, tags, price, blocked
public void getSkulls(Callback<ArrayList<Skull>> callback) {
ArrayList<Skull> skulls = new ArrayList<>();
this.runAsync(() -> this.databaseConnector.connect(connection -> {
try (PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + this.getTablePrefix() + "skull")) {
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
skulls.add(extractSkull(resultSet));
}
this.sync(() -> callback.accept(null, skulls));
} catch (Exception e) {
resolveCallback(callback, e);
}
}));
}
public void syncSkullPricesByCategory(Callback<Boolean> callback) {
this.runAsync(() -> this.databaseConnector.connect(connection -> {
final PreparedStatement statement = connection.prepareStatement("UPDATE " + this.getTablePrefix() + "skull SET price = ? WHERE category = ?");
for (BaseCategory value : BaseCategory.values()) {
statement.setDouble(1, value.getDefaultPrice());
statement.setString(2, value.getId());
statement.addBatch();
}
int[] updated = statement.executeBatch();
Common.log("updated" + updated.length);
if (callback != null)
this.sync(() -> callback.accept(null, updated.length > 0));
}));
}
public void getCategories(Callback<ArrayList<Category>> callback) {
ArrayList<Category> categories = new ArrayList<>();
this.runAsync(() -> this.databaseConnector.connect(connection -> {
try (PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + this.getTablePrefix() + "categories")) {
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
categories.add(extractCategory(resultSet));
}
callback.accept(null, categories);
} catch (Exception e) {
resolveCallback(callback, e);
}
}));
}
public void getPlayers(Callback<ArrayList<SkullUser>> callback) {
ArrayList<SkullUser> skulls = new ArrayList<>();
this.runAsync(() -> this.databaseConnector.connect(connection -> {
try (PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + this.getTablePrefix() + "players")) {
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
skulls.add(extractSkullPlayer(resultSet));
}
this.sync(() -> callback.accept(null, skulls));
} catch (Exception e) {
resolveCallback(callback, e);
}
}));
}
public void insertPlayer(@NonNull final SkullUser user, Callback<SkullUser> callback) {
this.runAsync(() -> this.databaseConnector.connect(connection -> {
try (PreparedStatement statement = connection.prepareStatement("INSERT INTO " + this.getTablePrefix() + "players (uuid, favourites) VALUES(?, ?)")) {
PreparedStatement fetch = connection.prepareStatement("SELECT * FROM " + this.getTablePrefix() + "players WHERE uuid = ?");
fetch.setString(1, user.getUUID().toString());
statement.setString(1, user.getUUID().toString());
statement.setString(2, user.getFavourites().stream().map(String::valueOf).collect(Collectors.joining(",")));
statement.executeUpdate();
if (callback != null) {
ResultSet res = fetch.executeQuery();
res.next();
callback.accept(null, extractSkullPlayer(res));
}
} catch (Exception e) {
e.printStackTrace();
resolveCallback(callback, e);
}
}));
}
public void insertCategory(@NonNull final Category category, Callback<Category> callback) {
this.runAsync(() -> this.databaseConnector.connect(connection -> {
try (PreparedStatement statement = connection.prepareStatement("INSERT INTO " + this.getTablePrefix() + "categories (id, name, skulls) VALUES(?, ?, ?)")) {
PreparedStatement fetch = connection.prepareStatement("SELECT * FROM " + this.getTablePrefix() + "categories WHERE id = ?");
fetch.setString(1, category.getId());
statement.setString(1, category.getId());
statement.setString(2, category.getName());
statement.setString(3, category.getSkulls().stream().map(String::valueOf).collect(Collectors.joining(",")));
statement.executeUpdate();
if (callback != null) {
ResultSet res = fetch.executeQuery();
res.next();
callback.accept(null, extractCategory(res));
}
} catch (Exception e) {
e.printStackTrace();
resolveCallback(callback, e);
}
}));
}
public void updateCategory(@NonNull final Category category, Callback<Boolean> callback) {
this.runAsync(() -> this.databaseConnector.connect(connection -> {
try (PreparedStatement statement = connection.prepareStatement("UPDATE " + this.getTablePrefix() + "categories SET name = ?, skulls = ? WHERE id = ?")) {
statement.setString(1, category.getName());
statement.setString(2, category.getSkulls().stream().map(String::valueOf).collect(Collectors.joining(",")));
statement.setString(3, category.getId());
int result = statement.executeUpdate();
if (callback != null)
callback.accept(null, result > 0);
} catch (Exception e) {
resolveCallback(callback, e);
}
}));
}
public void updateSkull(@NonNull final Skull skull, Callback<Boolean> callback) {
this.runAsync(() -> this.databaseConnector.connect(connection -> {
try (PreparedStatement statement = connection.prepareStatement("UPDATE " + this.getTablePrefix() + "skull SET name = ?, price = ?, blocked = ? WHERE id = ?")) {
statement.setString(1, skull.getName());
statement.setDouble(2, skull.getPrice());
statement.setBoolean(3, skull.isBlocked());
statement.setInt(4, skull.getId());
int result = statement.executeUpdate();
if (callback != null)
callback.accept(null, result > 0);
} catch (Exception e) {
resolveCallback(callback, e);
}
}));
}
public void updatePlayer(@NonNull final SkullUser user, Callback<Boolean> callback) {
this.runAsync(() -> this.databaseConnector.connect(connection -> {
try (PreparedStatement statement = connection.prepareStatement("UPDATE " + this.getTablePrefix() + "players SET favourites = ? WHERE uuid = ?")) {
statement.setString(1, user.getFavourites().stream().map(String::valueOf).collect(Collectors.joining(",")));
statement.setString(2, user.getUUID().toString());
int result = statement.executeUpdate();
if (callback != null)
callback.accept(null, result > 0);
} catch (Exception e) {
resolveCallback(callback, e);
}
}));
}
public Skull extractSkull(@NonNull final ResultSet resultSet) throws SQLException {
return new TexturedSkull(
resultSet.getInt("id"),
resultSet.getString("name"),
resultSet.getString("category"),
Arrays.asList(resultSet.getString("tags").split(",")),
resultSet.getString("texture"),
resultSet.getDouble("price"),
resultSet.getBoolean("blocked")
);
}
public Category extractCategory(@NonNull final ResultSet resultSet) throws SQLException {
final String skulls = resultSet.getString("skulls");
final String[] split = skulls.split(",");
return new SkullCategory(
resultSet.getString("id"),
resultSet.getString("name"),
true,
skulls.length() == 0 || split.length == 0 ? new ArrayList<>() : Arrays.stream(split).map(Integer::parseInt).collect(Collectors.toList())
);
}
public History extractHistory(@NonNull final ResultSet resultSet) throws SQLException {
return new InsertHistory(
resultSet.getInt("id"),
resultSet.getLong("time"),
Arrays.stream(resultSet.getString("skulls").split(",")).map((Integer::parseInt)).collect(Collectors.toList())
);
}
public PlacedSkull extractPlacedSkull(@NonNull final ResultSet resultSet) throws SQLException {
return new PlacedSkullLocation(
UUID.fromString(resultSet.getString("id")),
resultSet.getInt("skull_id"),
SerializeUtil.deserializeLocation(resultSet.getString("location"))
);
}
public SkullUser extractSkullPlayer(@NonNull final ResultSet resultSet) throws SQLException {
final String favs = resultSet.getString("favourites");
final String[] split = favs.split(",");
return new SkullPlayer(
UUID.fromString(resultSet.getString("uuid")),
favs.length() == 0 || split.length == 0 ? new ArrayList<>() : Arrays.stream(split).map(Integer::parseInt).collect(Collectors.toList())
);
}
private void resolveUpdateCallback(@Nullable UpdateCallback callback, @Nullable Exception ex) {
if (callback != null) {
callback.accept(ex);
} else if (ex != null) {
ex.printStackTrace();
}
}
private void resolveCallback(@Nullable Callback<?> callback, @NotNull Exception ex) {
if (callback != null) {
callback.accept(ex, null);
} else {
ex.printStackTrace();
}
}
private boolean hasColumn(ResultSet rs, String columnName) throws SQLException {
ResultSetMetaData rsmd = rs.getMetaData();
int columns = rsmd.getColumnCount();
for (int x = 1; x <= columns; x++) {
if (columnName.equals(rsmd.getColumnName(x))) {
return true;
}
}
return false;
}
}