-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAccount.java
More file actions
729 lines (658 loc) · 29 KB
/
Account.java
File metadata and controls
729 lines (658 loc) · 29 KB
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
package pro.cloudnode.smp.bankaccounts;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.OfflinePlayer;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nullable;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.logging.Level;
import java.util.stream.Collectors;
/**
* Bank account
*/
public class Account {
/**
* Unique account ID
*/
public final @NotNull String id;
/**
* Account owner
*/
public @NotNull OfflinePlayer owner;
/**
* Account type
*/
public final @NotNull Type type;
/**
* Account display name
* <p>
* Mostly intended for business accounts, i.e. showing the company name
*/
public @Nullable String name;
/**
* Account balance
*/
public @Nullable BigDecimal balance;
/**
* Whether the account is frozen
* <p>
* Frozen accounts cannot be used for transactions
*/
public boolean frozen;
/**
* Create a new account instance
* @param id Unique account ID
* @param owner Account owner
* @param type Account type
* @param name Account display name
* @param balance Account balance
* @param frozen Whether the account is frozen
*/
public Account(final @NotNull String id, final @NotNull OfflinePlayer owner, final @NotNull Type type, final @Nullable String name, final @Nullable BigDecimal balance, final boolean frozen) {
this.id = id;
this.owner = owner;
this.type = type;
this.name = name;
this.balance = balance;
this.frozen = frozen;
}
/**
* Create a new account
* @param owner Account owner
* @param type Account type
* @param name Account display name
* @param balance Account balance
* @param frozen Whether the account is frozen
*/
public Account(final @NotNull OfflinePlayer owner, final @NotNull Type type, final @Nullable String name, final @Nullable BigDecimal balance, final boolean frozen) {
this(StringGenerator.generate(16), owner, type, name, balance, frozen);
}
/**
* Create bank account instance from database result set
* @param rs Database result set
*/
public Account(final @NotNull ResultSet rs) throws @NotNull SQLException {
this(
rs.getString("id"),
BankAccounts.getInstance().getServer().getOfflinePlayer(UUID.fromString(rs.getString("owner"))),
Type.getType(rs.getInt("type")),
rs.getString("name"),
rs.getBigDecimal("balance"),
rs.getBoolean("frozen")
);
}
public final @NotNull String name() {
return this.name == null ? (this.type == Type.VAULT && this.owner.getName() != null ? this.owner.getName() : this.id) : this.name;
}
public final @NotNull Component ownerName() {
return this.owner.getUniqueId().equals(BankAccounts.getConsoleOfflinePlayer().getUniqueId()) ? MiniMessage.miniMessage().deserialize("<i>the server</i>") : this.owner.getName() == null ? MiniMessage.miniMessage().deserialize("<i>unknown player</i>") : Component.text(this.owner.getName());
}
public final @NotNull String ownerNameUnparsed() {
return this.owner.getUniqueId().equals(BankAccounts.getConsoleOfflinePlayer().getUniqueId()) ? "<i>the server</i>" : this.owner.getName() == null ? "<i>unknown player</i>" : this.owner.getName();
}
/**
* Update account balance
* @param diff Balance difference (positive or negative)
*/
public final void updateBalance(final @NotNull BigDecimal diff) {
if (balance == null) return;
this.balance = balance.add(diff);
this.update();
}
/**
* Create a new transaction/transfer
* @param to Recipient account
* @param amount Transaction amount Must be greater than zero.
* @param description Transaction description
* @param instrument Payment instrument used to facilitate the transaction
* @throws IllegalStateException If the sender or recipient account is frozen or the sender has insufficient funds
* @throws IllegalArgumentException If the amount is less than or equal to zero
*/
public final @NotNull Transaction transfer(final @NotNull Account to, final @NotNull BigDecimal amount, final @Nullable String description, final @Nullable String instrument) {
if (frozen) throw new IllegalStateException("Your account is frozen");
if (to.frozen) throw new IllegalStateException("Recipient account is frozen");
if (amount.compareTo(BigDecimal.ZERO) <= 0) throw new IllegalArgumentException("Amount must be greater than zero");
if (!hasFunds(amount)) throw new IllegalStateException("Insufficient funds");
final @NotNull Transaction transaction = new Transaction(this, to, amount, description, instrument);
transaction.save();
this.updateBalance(amount.negate());
to.updateBalance(amount);
return transaction;
}
/**
* Check if account has sufficient funds
* @param amount Amount to check
*/
public final boolean hasFunds(final @NotNull BigDecimal amount) {
return balance == null || balance.compareTo(amount) >= 0;
}
/**
* Create payment instrument
*/
public final @NotNull ItemStack createInstrument() {
final @NotNull Material material = BankAccounts.getInstance().config().instrumentsMaterial();
final @NotNull ItemStack instrument = new ItemStack(material);
final @NotNull ItemMeta meta = instrument.getItemMeta();
meta.displayName(BankAccounts.getInstance().config().instrumentsName(this, LocalDateTime.now(ZoneOffset.UTC)));
meta.lore(BankAccounts.getInstance().config().instrumentsLore(this, LocalDateTime.now(ZoneOffset.UTC)));
if (BankAccounts.getInstance().config().instrumentsGlintEnabled()) {
final @NotNull Enchantment enchantment = BankAccounts.getInstance().config().instrumentsGlintEnchantment();
meta.addEnchant(enchantment, 1, true);
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
}
final @NotNull NamespacedKey id = BankAccounts.Key.INSTRUMENT_ACCOUNT;
meta.getPersistentDataContainer().set(id, PersistentDataType.STRING, this.id);
instrument.setItemMeta(meta);
return instrument;
}
/**
* Check if an item is an instrument
*
* @param item Item to check
*/
public static boolean isInstrument(final @NotNull ItemStack item) {
return item.getItemMeta().getPersistentDataContainer().has(BankAccounts.Key.INSTRUMENT_ACCOUNT, PersistentDataType.STRING);
}
/**
* Get account by ID
* @param id Account ID
* @deprecated Use {@link #get(Account.Tag)}
*/
@Deprecated
public static @NotNull Optional<@NotNull Account> get(final @NotNull String id) {
return getByID(id);
}
/**
* Get account by ID
* @param id Account ID
*/
@ApiStatus.Internal
private static @NotNull Optional<@NotNull Account> getByID(final @NotNull String id) {
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `bank_accounts` WHERE `id` = ? LIMIT 1")) {
stmt.setString(1, id);
final @NotNull ResultSet rs = stmt.executeQuery();
return rs.next() ? Optional.of(new Account(rs)) : Optional.empty();
}
catch (final @NotNull Exception e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not get account: " + id, e);
return Optional.empty();
}
}
/**
* Get account by tag
*/
public static @NotNull Optional<@NotNull Account> get(final @NotNull Account.Tag tag) {
return switch (tag.type) {
case ID -> get(tag.value);
case USERNAME -> {
final @NotNull OfflinePlayer player = BankAccounts.getInstance().getServer().getOfflinePlayer(tag.value);
yield getVaultAccount(player);
}
};
}
/**
* Get account from instrument
* @param instrument Instrument item
*/
public static @NotNull Optional<@NotNull Account> get(final @NotNull ItemStack instrument) {
if (!isInstrument(instrument)) return Optional.empty();
final @NotNull NamespacedKey id = BankAccounts.Key.INSTRUMENT_ACCOUNT;
final @NotNull ItemMeta meta = instrument.getItemMeta();
final @NotNull String accountId = Objects.requireNonNull(meta.getPersistentDataContainer().get(id, PersistentDataType.STRING));
return get(accountId);
}
/**
* Get accounts by owner
* @param owner Account owner
* @param type Account type
*/
public static @NotNull Account[] get(final @NotNull OfflinePlayer owner, final @Nullable Type type) {
final @NotNull List<@NotNull Account> accounts = new ArrayList<>();
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement(type == null ? "SELECT * FROM `bank_accounts` WHERE `owner` = ?" : "SELECT * FROM `bank_accounts` WHERE `owner` = ? AND `type` = ?")) {
stmt.setString(1, owner.getUniqueId().toString());
if (type != null) stmt.setInt(2, Type.getType(type));
final @NotNull ResultSet rs = stmt.executeQuery();
while (rs.next()) accounts.add(new Account(rs));
return accounts.toArray(new Account[0]);
}
catch (final @NotNull Exception e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not get accounts for: " + owner.getUniqueId() + " (" + owner.getName() + "), type = " + (type == null ? "all" : type.name()), e);
return new Account[0];
}
}
/**
* Get accounts by owner
* @param owner Account owner
*/
public static @NotNull Account[] get(final @NotNull OfflinePlayer owner) {
return get(owner, null);
}
/**
* Get all accounts
*/
public static @NotNull Account[] get() {
final @NotNull List<@NotNull Account> accounts = new ArrayList<>();
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `bank_accounts`")) {
final @NotNull ResultSet rs = stmt.executeQuery();
while (rs.next()) accounts.add(new Account(rs));
return accounts.toArray(new Account[0]);
} catch (final @NotNull Exception e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not get accounts", e);
return new Account[0];
}
}
/**
* Get the Vault account of a player
*/
public static @NotNull Optional<@NotNull Account> getVaultAccount(final @NotNull OfflinePlayer player) {
final @NotNull Account @NotNull [] accounts = get(player, Type.VAULT);
if (accounts.length == 0) return Optional.empty();
return Optional.of(accounts[0]);
}
/**
* Get accounts sorted by balance
*
* @param limit Max number of accounts to return. If not set, all accounts are returned
* @param page Page number starting from 1. Defaults to 1.
* @param type If set, only accounts of this type are returned
*/
public static @NotNull Account @NotNull [] getTopBalance(final @Nullable Integer limit, final @Nullable Integer page, final @Nullable Type type) {
final @NotNull List<@NotNull Account> accounts = new ArrayList<>();
final @NotNull String query;
final int offset = (page != null ? page - 1 : 0) * (limit != null ? limit : 0);
if (type == null) query = "SELECT * FROM `bank_accounts` WHERE `balance` IS NOT NULL AND `balance` > 0 ORDER BY `balance` DESC" + (limit != null ? " LIMIT ? OFFSET ?" : "");
else query = "SELECT * FROM `bank_accounts` WHERE `balance` IS NOT NULL AND `balance` > 0 AND `type` = ? ORDER BY `balance` DESC" + (limit != null ? " LIMIT ? OFFSET ?" : "");
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement(query)) {
if (type != null) {
stmt.setInt(1, Type.getType(type));
if (limit != null) {
stmt.setInt(2, limit);
stmt.setInt(3, offset);
}
}
else if (limit != null) {
stmt.setInt(1, limit);
stmt.setInt(2, offset);
}
final @NotNull ResultSet rs = stmt.executeQuery();
while (rs.next()) accounts.add(new Account(rs));
return accounts.toArray(new Account[0]);
}
catch (final @NotNull Exception e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not get top balance accounts", e);
return new Account[0];
}
}
/*
* Get the server account
*/
public static @NotNull Optional<@NotNull Account> getServerAccount() {
if (!BankAccounts.getInstance().config().serverAccountEnabled()) return Optional.empty();
final @NotNull Optional<@NotNull Account> account = Arrays.stream(get(BankAccounts.getConsoleOfflinePlayer())).filter(a -> a.type != Type.VAULT).findFirst();
return account;
}
/**
* Get the server Vault account
*/
public static @NotNull Optional<@NotNull Account> getServerVaultAccount() {
if (!BankAccounts.getInstance().config().integrationsVaultEnabled()) return Optional.empty();
return getVaultAccount(BankAccounts.getConsoleOfflinePlayer());
}
/**
* Insert into database
*/
public void insert() {
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement("INSERT INTO `bank_accounts` (`id`, `owner`, `type`, `name`, `balance`, `frozen`) VALUES (?, ?, ?, ?, ?, ?)")) {
stmt.setString(1, id);
stmt.setString(2, owner.getUniqueId().toString());
stmt.setInt(3, Type.getType(type));
if (name == null) stmt.setNull(4, java.sql.Types.VARCHAR);
else stmt.setString(4, name);
if (balance == null) stmt.setNull(5, java.sql.Types.DECIMAL);
else stmt.setBigDecimal(5, balance);
stmt.setBoolean(6, frozen);
stmt.executeUpdate();
} catch (final @NotNull Exception e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not save account: " + id, e);
}
}
/**
* Update in database
*/
public void update() {
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement("UPDATE `bank_accounts` SET `name` = ?, `balance` = ?, `frozen` = ? WHERE `id` = ?")) {
if (name == null) stmt.setNull(1, java.sql.Types.VARCHAR);
else stmt.setString(1, name);
if (balance == null) stmt.setNull(2, java.sql.Types.DECIMAL);
else stmt.setBigDecimal(2, balance);
stmt.setBoolean(3, frozen);
stmt.setString(4, id);
stmt.executeUpdate();
} catch (final @NotNull Exception e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not save account: " + id, e);
}
}
/**
* Delete account from database
*/
public void delete() {
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement("DELETE FROM `bank_accounts` WHERE `id` = ?")) {
stmt.setString(1, id);
stmt.executeUpdate();
} catch (final @NotNull Exception e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not delete account: " + id, e);
}
}
/**
* Bank account type
*/
public enum Type {
/**
* Personal, individual, private account
*/
PERSONAL,
/**
* Account owned by a company or other corporate entity
*/
BUSINESS,
/**
* Vault integration account
*/
VAULT;
/**
* Get type name (as set in config)
*/
public @NotNull String getName() {
return BankAccounts.getInstance().config().messagesTypes(this);
}
/**
* Convert account type to integer
* @param type Account type
* @return Account type as integer
*/
public static int getType(final @NotNull Type type) {
return type.ordinal();
}
/**
* Convert integer to account type
* @param type Account type as integer
* @return Account type
*/
public static @NotNull Type getType(final int type) {
return Type.values()[type];
}
public static @NotNull Optional<@NotNull Type> fromString(final @NotNull String name) {
for (final @NotNull Type type : Type.values())
if (type.name().equalsIgnoreCase(name)) return Optional.of(type);
return Optional.empty();
}
}
/**
* A dummy class representing a missing account (e.g. deleted).
*/
public static final class ClosedAccount extends Account {
public ClosedAccount() {
super("closed account", BankAccounts.getConsoleOfflinePlayer(), Type.PERSONAL, null, BigDecimal.ZERO, true);
}
@Override
public void insert() {}
@Override
public void update() {}
@Override
public void delete() {}
}
/**
* An account tag is a unique pointer to a specific account.
*/
public record Tag(@NotNull Account.Tag.Type type, @NotNull String value) {
/**
* Create a new account ID tag
* @param id Account ID
*/
public static @NotNull Tag id(final @NotNull String id) {
return new Tag(Account.Tag.Type.ID, id);
}
/**
* Create a new Vault account tag by username
* @param username Username of Vault account holder
*/
public static @NotNull Tag username(final @NotNull String username) {
return new Tag(Account.Tag.Type.USERNAME, username);
}
/**
* Create account tag from string
* @param string Use {@code @} prefix for Vault account owner username.
*/
public static @NotNull Tag from(final @NotNull String string) {
if (string.startsWith("@")) return username(string.substring(1));
return id(string);
}
/**
* Get the account that this tag points to
*/
public @NotNull Optional<@NotNull Account> get() {
return Account.get(this);
}
/**
* Account tag type
*/
public enum Type {
/**
* Account by ID
*/
ID,
/**
* Vault account by username
*/
USERNAME
}
}
/**
* A request to change the owner of a bank account (sent to the new owner)
*/
public final static class ChangeOwnerRequest {
/**
* Account id
*/
private final @NotNull String account;
/**
* New owner UUID
*/
public final @NotNull OfflinePlayer newOwner;
/**
* Request creation timestamp
*/
public final @NotNull Date created;
/**
* Create a new account ownership transfer request instance
*
* @param account Account to transfer ownership of
* @param newOwner The new account owner
*/
public ChangeOwnerRequest(final @NotNull Account account, final @NotNull OfflinePlayer newOwner) {
this.account = account.id;
this.newOwner = newOwner;
this.created = new Date();
}
private ChangeOwnerRequest(final @NotNull ResultSet rs) throws SQLException {
this.account = rs.getString("id");
this.newOwner = BankAccounts.getInstance().getServer().getOfflinePlayer(UUID.fromString(rs.getString("new_owner")));
this.created = new Date(rs.getDate("created").getTime());
}
/**
* Get account
*/
public @NotNull Optional<@NotNull Account> account() {
return Account.get(account);
}
/**
* Check if request has expired
*/
public boolean expired() {
return System.currentTimeMillis() - created.getTime() > BankAccounts.getInstance().config().changeOwnerTimeout() * 6e4;
}
/**
* Confirm/accept the request
*
* @return Whether the change was successful
*/
public boolean confirm() {
final @NotNull Optional<@NotNull Account> account = this.account();
if (account.isEmpty()) return false;
if (account.get().frozen) return false;
account.get().owner = newOwner;
account.get().update();
this.delete();
return true;
}
/**
* Insert into database
*/
public void insert() {
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement("INSERT INTO `change_owner_requests` (`account`, `new_owner`, `created`) VALUES (?, ?, ?)")) {
stmt.setString(1, account);
stmt.setString(2, newOwner.toString());
stmt.setDate(3, new java.sql.Date(created.getTime()));
stmt.executeUpdate();
}
catch (final @NotNull Exception e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not save account ownership change request. account: " + account + ", newOwner: " + newOwner, e);
}
}
/**
* Delete from database
*/
public void delete() {
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement("DELETE FROM `change_owner_requests` WHERE `account` = ? AND `new_owner` = ?")) {
stmt.setString(1, account);
stmt.setString(2, newOwner.toString());
stmt.executeUpdate();
}
catch (final @NotNull Exception e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not delete account ownership change request. account: " + account + ", newOwner: " + newOwner, e);
}
}
/**
* Delete all request to transfer a certain account
*
* @param account Account ID
*/
public static void delete(final @NotNull UUID account) {
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement("DELETE FROM `change_owner_requests` WHERE `account` = ?")) {
stmt.setString(1, account.toString());
stmt.executeUpdate();
}
catch (final @NotNull Exception e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not delete account ownership change request. account: " + account, e);
}
}
/**
* Delete expired requests
*/
private static void deleteExpired() {
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement("DELETE FROM `change_owner_requests` WHERE `created` = ?")) {
stmt.setDate(1, new java.sql.Date(System.currentTimeMillis() - BankAccounts.getInstance().config().changeOwnerTimeout() * 60_000L));
stmt.executeUpdate();
}
catch (final @NotNull Exception e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not delete account ownership change request. account: ", e);
}
}
/**
* Asynchronously delete expired requests
*/
public final static @NotNull Runnable deleteExpiredLater = () -> BankAccounts.getInstance().getServer().getScheduler().runTaskAsynchronously(BankAccounts.getInstance(), ChangeOwnerRequest::deleteExpired);
/**
* Get account ownership change request
*
* @param account Account ID
* @param newOwner New owner
*/
public static @NotNull Optional<@NotNull ChangeOwnerRequest> get(final @NotNull String account, @NotNull OfflinePlayer newOwner) {
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `change_owner_requests` WHERE `account` = ? AND `new_owner` = ? LIMIT 1")) {
stmt.setString(1, account);
stmt.setString(2, newOwner.getUniqueId().toString());
final @NotNull ResultSet rs = stmt.executeQuery();
return rs.next() ? Optional.of(new ChangeOwnerRequest(rs)) : Optional.empty();
}
catch (final @NotNull Exception e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not get account ownership change request. account: " + account + ", newOwner: " + newOwner.getUniqueId(), e);
return Optional.empty();
}
}
/**
* List a player's incoming (received) account ownership change requests.
*
* @param player The player whose requests to list
*/
public static @NotNull Account @NotNull [] incoming(final @NotNull OfflinePlayer player) {
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `change_owner_requests` WHERE `new_owner` = ?")) {
stmt.setString(1, player.getUniqueId().toString());
final @NotNull ResultSet rs = stmt.executeQuery();
final @NotNull List<@NotNull Account> accounts = new ArrayList<>();
while (rs.next()) accounts.add(new Account(rs));
return accounts.toArray(new Account[0]);
}
catch (final @NotNull Exception e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not get incoming account ownership change requests. player: " + player.getUniqueId(), e);
return new Account[0];
}
}
/**
* List a player’s outgoing (sent) account ownership change requests.
*
* @param player The player whose requests to list
*/
public static @NotNull Account @NotNull [] outgoing(final @NotNull OfflinePlayer player) {
final @NotNull String @NotNull [] ids = Arrays.stream(Account.get(player)).map(a -> a.id).toArray(String[]::new);
final @NotNull String placeholders = Arrays.stream(ids).map(id -> "?").collect(Collectors.joining(", "));
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `change_owner_requests` WHERE `account` in (" + placeholders + ")")) {
for (int i = ids.length; i > 0;)
stmt.setString(i, ids[--i]);
final @NotNull ResultSet rs = stmt.executeQuery();
final @NotNull List<@NotNull Account> accounts = new ArrayList<>();
while (rs.next()) accounts.add(new Account(rs));
return accounts.toArray(new Account[0]);
}
catch (final @NotNull Exception e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not get outgoing account ownership change requests. player: " + player.getUniqueId(), e);
return new Account[0];
}
}
}
}