-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Database.java
452 lines (387 loc) · 15 KB
/
Database.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package com.github.games647.scoreboardstats.pvpstats;
import com.github.games647.scoreboardstats.BackwardsCompatibleUtil;
import com.github.games647.scoreboardstats.ScoreboardStats;
import com.github.games647.scoreboardstats.config.Lang;
import com.github.games647.scoreboardstats.config.Settings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zaxxer.hikari.HikariDataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.CancellationException;
import java.util.concurrent.Future;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.metadata.MetadataValue;
/**
* This represents a handler for saving player stats.
*/
public class Database {
private static final String METAKEY = "player_stats";
private final ScoreboardStats plugin;
private final Map<String, Integer> toplist = Maps.newHashMapWithExpectedSize(Settings.getTopitems());
private final DatabaseConfiguration dbConfig;
private HikariDataSource dataSource;
public Database(ScoreboardStats plugin) {
this.plugin = plugin;
this.dbConfig = new DatabaseConfiguration(plugin);
}
/**
* Get the cache player stats if they exists and the arguments are valid.
*
* @param request the associated player
* @return the stats if they are in the cache
*/
public PlayerStats getCachedStats(Player request) {
if (request != null) {
for (MetadataValue metadata : request.getMetadata(METAKEY)) {
if (metadata.value() instanceof PlayerStats) {
return (PlayerStats) metadata.value();
}
}
}
return null;
}
/**
* Starts loading the stats for a specific player in an external thread.
*
* @param player the associated player
*/
public void loadAccountAsync(Player player) {
if (getCachedStats(player) == null && dataSource != null) {
Runnable statsLoader = new StatsLoader(plugin, dbConfig.isUuidUse(), player, this);
Bukkit.getScheduler().runTaskAsynchronously(plugin, statsLoader);
}
}
/**
* Starts loading the stats for a specific player sync
*
* @param uniqueId the associated playername or uuid
* @return the loaded stats
*/
public PlayerStats loadAccount(Object uniqueId) {
if (uniqueId == null || dataSource == null) {
return null;
} else {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet resultSet = null;
try {
conn = dataSource.getConnection();
stmt = conn.prepareStatement("SELECT * FROM player_stats WHERE "
+ (dbConfig.isUuidUse() ? "uuid" : "playername")
+ "=?");
stmt.setString(1, uniqueId.toString());
resultSet = stmt.executeQuery();
return extractPlayerStats(resultSet);
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, "Error loading player profile", ex);
} finally {
close(resultSet);
close(stmt);
close(conn);
}
return null;
}
}
private PlayerStats extractPlayerStats(ResultSet resultSet) throws SQLException {
if (resultSet.next()) {
int id = resultSet.getInt(1);
String unparsedUUID = resultSet.getString(2);
UUID uuid = null;
if (unparsedUUID != null) {
uuid = UUID.fromString(unparsedUUID);
}
String playerName = resultSet.getString(3);
int kills = resultSet.getInt(4);
int deaths = resultSet.getInt(5);
int mobkills = resultSet.getInt(6);
int killstreak = resultSet.getInt(7);
long lastOnline = resultSet.getLong(8);
return new PlayerStats(id, uuid, playerName, kills, deaths, mobkills, killstreak, lastOnline);
} else {
//If there are no existing stat create a new object with empty stats
return new PlayerStats();
}
}
/**
* Starts loading the stats for a specific player sync
*
* @param player the associated player
* @return the loaded stats
*/
public PlayerStats loadAccount(Player player) {
if (player == null || dataSource == null) {
return null;
} else {
if (dbConfig.isUuidUse()) {
return loadAccount(player.getUniqueId());
} else {
return loadAccount(player.getName());
}
}
}
/**
* Save PlayerStats async.
*
* @param stats PlayerStats data
*/
public void saveAsync(PlayerStats stats) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> save(Lists.newArrayList(stats)));
}
/**
* Save the PlayerStats on the current Thread.
*
* @param stats PlayerStats data
*/
public void save(List<PlayerStats> stats) {
if (stats != null && dataSource != null) {
update(stats.stream()
.filter(PlayerStats::isModified)
.filter(stat -> !stat.isNew())
.collect(Collectors.toList()));
insert(stats.stream()
.filter(PlayerStats::isModified)
.filter(PlayerStats::isNew)
.collect(Collectors.toList()));
}
}
private void update(List<PlayerStats> stats) {
//Save the stats to the database
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = dataSource.getConnection();
conn.setAutoCommit(false);
stmt = conn.prepareStatement("UPDATE player_stats "
+ "SET kills=?, deaths=?, killstreak=?, mobkills=?, last_online=?, playername=? "
+ "WHERE id=?");
for (PlayerStats stat : stats) {
stmt.setInt(1, stat.getKills());
stmt.setInt(2, stat.getDeaths());
stmt.setInt(3, stat.getKillstreak());
stmt.setInt(4, stat.getMobkills());
stmt.setLong(5, stat.getLastOnline());
stmt.setString(6, stat.getPlayername());
stmt.setInt(7, stat.getId());
stmt.addBatch();
}
stmt.executeBatch();
conn.commit();
} catch (Exception ex) {
plugin.getLogger().log(Level.SEVERE, "Error updating profiles", ex);
} finally {
close(stmt);
close(conn);
}
}
private void insert(List<PlayerStats> stats) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet generatedKeys = null;
try {
conn = dataSource.getConnection();
conn.setAutoCommit(false);
stmt = conn.prepareStatement("INSERT INTO player_stats "
+ "(uuid, playername, kills, deaths, killstreak, mobkills, last_online) VALUES "
+ "(?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
for (PlayerStats stat : stats) {
stmt.setString(1, stat.getUuid() == null ? null : stat.getUuid().toString());
stmt.setString(2, stat.getPlayername());
stmt.setInt(3, stat.getKills());
stmt.setInt(4, stat.getDeaths());
stmt.setInt(5, stat.getKillstreak());
stmt.setInt(6, stat.getMobkills());
stmt.setLong(7, stat.getLastOnline());
stmt.addBatch();
}
stmt.executeBatch();
conn.commit();
generatedKeys = stmt.getGeneratedKeys();
for (PlayerStats stat : stats) {
if (!generatedKeys.next()) {
break;
}
stat.setId(generatedKeys.getInt(1));
}
} catch (Exception ex) {
plugin.getLogger().log(Level.SEVERE, "Error inserting profiles", ex);
} finally {
close(generatedKeys);
close(stmt);
close(conn);
}
}
/**
* Starts saving all cache player stats and then clears the cache.
*/
public void saveAll() {
try {
plugin.getLogger().info(Lang.get("savingStats"));
//If pvpstats are enabled save all stats that are in the cache
List<PlayerStats> toSave = BackwardsCompatibleUtil.getOnlinePlayers().stream()
.map(this::getCachedStats)
.filter(PlayerStats::isModified)
.collect(Collectors.toList());
if (!toSave.isEmpty()) {
save(toSave);
}
dataSource.close();
} finally {
//Make rally sure we remove all even on error
BackwardsCompatibleUtil.getOnlinePlayers()
.forEach(player -> player.removeMetadata(METAKEY, plugin));
}
}
/**
* Initialize a components and checking for an existing database
*/
public void setupDatabase() {
//Check if pvpstats should be enabled
dbConfig.loadConfiguration();
dataSource = new HikariDataSource(dbConfig.getServerConfig());
Connection conn = null;
Statement stmt = null;
try {
conn = dataSource.getConnection();
stmt = conn.createStatement();
String createTableQuery = "CREATE TABLE IF NOT EXISTS player_stats ( "
+ "id integer PRIMARY KEY AUTO_INCREMENT, "
+ "uuid varchar(40), "
+ "playername varchar(16) not null, "
+ "kills integer not null, "
+ "deaths integer not null, "
+ "mobkills integer not null, "
+ "killstreak integer not null, "
+ "last_online timestamp not null )";
if (dbConfig.getServerConfig().getDriverClassName().contains("sqlite")) {
createTableQuery = createTableQuery.replace("AUTO_INCREMENT", "");
}
stmt.execute(createTableQuery);
} catch (Exception ex) {
plugin.getLogger().log(Level.SEVERE, "Error creating database ", ex);
} finally {
close(stmt);
close(conn);
}
Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, this::updateTopList, 20 * 60 * 5, 0);
Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, () -> {
if (dataSource == null) {
return;
}
Future<Collection<? extends Player>> syncPlayers = Bukkit.getScheduler()
.callSyncMethod(plugin, BackwardsCompatibleUtil::getOnlinePlayers);
try {
Collection<? extends Player> onlinePlayers = syncPlayers.get();
List<PlayerStats> toSave = onlinePlayers.stream()
.map(this::getCachedStats)
.filter(Objects::nonNull)
.filter(PlayerStats::isModified)
.collect(Collectors.toList());
if (!toSave.isEmpty()) {
save(toSave);
}
} catch (CancellationException cancelEx) {
//ignore it on shutdown
} catch (Exception ex) {
plugin.getLogger().log(Level.SEVERE, null, ex);
}
}, 20 * 60, 0);
registerEvents();
}
/**
* Get the a map of the best players for a specific category.
*
* @return a iterable of the entries
*/
public Collection<Map.Entry<String, Integer>> getTop() {
synchronized (toplist) {
return toplist.entrySet();
}
}
/**
* Updates the toplist
*/
public void updateTopList() {
String type = Settings.getTopType();
Map<String, Integer> newToplist;
switch (type) {
case "killstreak":
newToplist = getTopList("killstreak", PlayerStats::getKillstreak);
break;
case "mob":
newToplist = getTopList("mobkills", PlayerStats::getMobkills);
break;
default:
newToplist = getTopList("kills", PlayerStats::getKills);
break;
}
synchronized (toplist) {
//set it after fetching so it's only blocking for a short time
toplist.clear();
toplist.putAll(newToplist);
}
}
private Map<String, Integer> getTopList(String type, Function<PlayerStats, Integer> valueMapper) {
if (dataSource != null) {
Connection conn = null;
Statement stmt = null;
ResultSet resultSet = null;
try {
conn = dataSource.getConnection();
stmt = conn.createStatement();
resultSet = stmt.executeQuery("SELECT * FROM player_stats "
+ "ORDER BY " + type + " desc "
+ "LIMIT " + Settings.getTopitems());
List<PlayerStats> result = Lists.newArrayList();
for (int i = 0; i < Settings.getTopitems(); i++) {
PlayerStats stats = extractPlayerStats(resultSet);
if (!stats.isNew()) {
result.add(stats);
}
}
return result.stream().collect(Collectors.toMap(PlayerStats::getPlayername, valueMapper));
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, "Error loading top list", ex);
} finally {
close(resultSet);
close(stmt);
close(conn);
}
}
return Collections.emptyMap();
}
private void registerEvents() {
if (Bukkit.getPluginManager().isPluginEnabled("InSigns")) {
//Register this listerner if InSigns is available
new SignListener(plugin, "[Kill]", this);
new SignListener(plugin, "[Death]", this);
new SignListener(plugin, "[KDR]", this);
new SignListener(plugin, "[Streak]", this);
new SignListener(plugin, "[Mob]", this);
}
plugin.getReplaceManager().register(new StatsVariables(plugin, this));
Bukkit.getPluginManager().registerEvents(new StatsListener(plugin, this), plugin);
}
private void close(AutoCloseable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (Exception ex) {
//ignore
}
}
}
}