Skip to content

Commit 776cff7

Browse files
authored
Merge branch 'master' into runtime-lib-loading
2 parents a0690e5 + 46240fa commit 776cff7

File tree

12 files changed

+246
-106
lines changed

12 files changed

+246
-106
lines changed

src/main/java/pro/cloudnode/smp/bankaccounts/Account.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.bukkit.inventory.ItemStack;
1111
import org.bukkit.inventory.meta.ItemMeta;
1212
import org.bukkit.persistence.PersistentDataType;
13+
import org.jetbrains.annotations.ApiStatus;
1314
import org.jetbrains.annotations.NotNull;
1415

1516
import javax.annotation.Nullable;
@@ -194,8 +195,19 @@ public static boolean isInstrument(final @NotNull ItemStack item) {
194195
/**
195196
* Get account by ID
196197
* @param id Account ID
198+
* @deprecated Use {@link #get(Account.Tag)}
197199
*/
200+
@Deprecated
198201
public static @NotNull Optional<@NotNull Account> get(final @NotNull String id) {
202+
return getByID(id);
203+
}
204+
205+
/**
206+
* Get account by ID
207+
* @param id Account ID
208+
*/
209+
@ApiStatus.Internal
210+
private static @NotNull Optional<@NotNull Account> getByID(final @NotNull String id) {
199211
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
200212
final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `bank_accounts` WHERE `id` = ? LIMIT 1")) {
201213
stmt.setString(1, id);
@@ -208,6 +220,19 @@ public static boolean isInstrument(final @NotNull ItemStack item) {
208220
}
209221
}
210222

223+
/**
224+
* Get account by tag
225+
*/
226+
public static @NotNull Optional<@NotNull Account> get(final @NotNull Account.Tag tag) {
227+
return switch (tag.type) {
228+
case ID -> get(tag.value);
229+
case USERNAME -> {
230+
final @NotNull OfflinePlayer player = BankAccounts.getInstance().getServer().getOfflinePlayer(tag.value);
231+
yield getVaultAccount(player);
232+
}
233+
};
234+
}
235+
211236
/**
212237
* Get account from instrument
213238
* @param instrument Instrument item
@@ -267,6 +292,9 @@ public static boolean isInstrument(final @NotNull ItemStack item) {
267292
}
268293
}
269294

295+
/**
296+
* Get the Vault account of a player
297+
*/
270298
public static @NotNull Optional<@NotNull Account> getVaultAccount(final @NotNull OfflinePlayer player) {
271299
final @NotNull Account @NotNull [] accounts = get(player, Type.VAULT);
272300
if (accounts.length == 0) return Optional.empty();
@@ -437,4 +465,55 @@ public void update() {}
437465
@Override
438466
public void delete() {}
439467
}
468+
469+
/**
470+
* An account tag is a unique pointer to a specific account.
471+
*/
472+
public record Tag(@NotNull Account.Tag.Type type, @NotNull String value) {
473+
/**
474+
* Create a new account ID tag
475+
* @param id Account ID
476+
*/
477+
public static @NotNull Tag id(final @NotNull String id) {
478+
return new Tag(Account.Tag.Type.ID, id);
479+
}
480+
481+
/**
482+
* Create a new Vault account tag by username
483+
* @param username Username of Vault account holder
484+
*/
485+
public static @NotNull Tag username(final @NotNull String username) {
486+
return new Tag(Account.Tag.Type.USERNAME, username);
487+
}
488+
489+
/**
490+
* Create account tag from string
491+
* @param string Use {@code @} prefix for Vault account owner username.
492+
*/
493+
public static @NotNull Tag from(final @NotNull String string) {
494+
if (string.startsWith("@")) return username(string.substring(1));
495+
return id(string);
496+
}
497+
498+
/**
499+
* Get the account that this tag points to
500+
*/
501+
public @NotNull Optional<@NotNull Account> get() {
502+
return Account.get(this);
503+
}
504+
505+
/**
506+
* Account tag type
507+
*/
508+
public enum Type {
509+
/**
510+
* Account by ID
511+
*/
512+
ID,
513+
/**
514+
* Vault account by username
515+
*/
516+
USERNAME
517+
}
518+
}
440519
}

src/main/java/pro/cloudnode/smp/bankaccounts/Command.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.util.Optional;
1515
import java.util.Set;
1616
import java.util.regex.Matcher;
17-
import java.util.regex.Pattern;
1817
import java.util.stream.Collectors;
1918

2019
public abstract class Command implements CommandExecutor, TabCompleter {

src/main/java/pro/cloudnode/smp/bankaccounts/Invoice.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public Invoice(final @NotNull Account seller, final @NotNull BigDecimal amount,
5858
public Invoice(final @NotNull ResultSet rs) throws @NotNull SQLException {
5959
this(
6060
rs.getString("id"),
61-
Account.get(rs.getString("seller")).orElse(new Account.ClosedAccount()),
61+
Account.get(Account.Tag.id(rs.getString("seller"))).orElse(new Account.ClosedAccount()),
6262
rs.getBigDecimal("amount"),
6363
rs.getString("description"),
6464
rs.getString("buyer") == null ? null : BankAccounts.getInstance().getServer().getOfflinePlayer(UUID.fromString(rs.getString("buyer"))),

src/main/java/pro/cloudnode/smp/bankaccounts/POS.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public POS(final @NotNull ResultSet rs) throws @NotNull SQLException, @NotNull I
100100
this.world = world.get();
101101
this.price = rs.getBigDecimal("price");
102102
this.description = rs.getString("description");
103-
this.seller = Account.get(rs.getString("seller")).orElse(new Account.ClosedAccount());
103+
this.seller = Account.get(Account.Tag.from(rs.getString("seller"))).orElse(new Account.ClosedAccount());
104104
this.created = rs.getDate("created");
105105
}
106106

src/main/java/pro/cloudnode/smp/bankaccounts/Permissions.java

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,45 @@
33
import org.jetbrains.annotations.NotNull;
44

55
public final class Permissions {
6-
public static @NotNull String COMMAND = "bank.command";
7-
public static @NotNull String BALANCE_SELF = "bank.balance.self";
8-
public static @NotNull String TRANSFER_SELF = "bank.transfer.self";
9-
public static @NotNull String TRANSFER_OTHER = "bank.transfer.other";
10-
public static @NotNull String HISTORY = "bank.history";
11-
public static @NotNull String ACCOUNT_CREATE = "bank.account.create";
12-
public static @NotNull String INSTRUMENT_CREATE = "bank.instrument.create";
13-
public static @NotNull String WHOIS = "bank.whois";
14-
public static @NotNull String SET_NAME = "bank.set.name";
15-
public static @NotNull String FREEZE = "bank.freeze";
16-
public static @NotNull String DELETE = "bank.delete";
17-
public static @NotNull String BALTOP = "bank.baltop";
18-
public static @NotNull String POS_CREATE = "bank.pos.create";
19-
public static @NotNull String POS_USE = "bank.pos.use";
20-
public static @NotNull String INVOICE_CREATE = "bank.invoice.create";
21-
public static @NotNull String INVOICE_CREATE_OTHER = "bank.invoice.create.other";
22-
public static @NotNull String INVOICE_VIEW = "bank.invoice.view";
23-
public static @NotNull String INVOICE_VIEW_OTHER = "bank.invoice.view.other";
24-
public static @NotNull String INVOICE_PAY_OTHER = "bank.invoice.pay.other";
25-
public static @NotNull String INVOICE_PAY_ACCOUNT_OTHER = "bank.invoice.pay.account-other";
26-
public static @NotNull String INVOICE_SEND = "bank.invoice.send";
27-
public static @NotNull String INVOICE_SEND_OTHER = "bank.invoice.send.other";
28-
public static @NotNull String RELOAD = "bank.reload";
29-
public static @NotNull String BALANCE_OTHER = "bank.balance.other";
30-
public static @NotNull String HISTORY_OTHER = "bank.history.other";
31-
public static @NotNull String ACCOUNT_CREATE_OTHER = "bank.account.create.other";
32-
public static @NotNull String ACCOUNT_CREATE_BYPASS = "bank.account.create.bypass";
33-
public static @NotNull String ACCOUNT_CREATE_VAULT = "bank.account.create.vault";
34-
public static @NotNull String INSTRUMENT_CREATE_OTHER = "bank.instrument.create.other";
35-
public static @NotNull String INSTRUMENT_CREATE_BYPASS = "bank.instrument.create.bypass";
36-
public static @NotNull String SET_BALANCE = "bank.set.balance";
37-
public static @NotNull String SET_NAME_OTHER = "bank.set.name.other";
38-
public static @NotNull String SET_NAME_VAULT = "bank.set.name.vault";
39-
public static @NotNull String FREEZE_OTHER = "bank.freeze.other";
40-
public static @NotNull String DELETE_OTHER = "bank.delete.other";
41-
public static @NotNull String DELETE_VAULT = "bank.delete.vault";
42-
public static @NotNull String POS_CREATE_OTHER = "bank.pos.create.other";
43-
public static @NotNull String POS_CREATE_PERSONAL = "bank.pos.create.personal";
44-
public static @NotNull String POS_USE_OTHER = "bank.pos.use.other";
45-
public static @NotNull String NOTIFY_UPDATE = "bank.notify-update";
6+
public static final @NotNull String COMMAND = "bank.command";
7+
public static final @NotNull String BALANCE_SELF = "bank.balance.self";
8+
public static final @NotNull String TRANSFER_SELF = "bank.transfer.self";
9+
public static final @NotNull String TRANSFER_OTHER = "bank.transfer.other";
10+
public static final @NotNull String HISTORY = "bank.history";
11+
public static final @NotNull String ACCOUNT_CREATE = "bank.account.create";
12+
public static final @NotNull String INSTRUMENT_CREATE = "bank.instrument.create";
13+
public static final @NotNull String WHOIS = "bank.whois";
14+
public static final @NotNull String SET_NAME = "bank.set.name";
15+
public static final @NotNull String FREEZE = "bank.freeze";
16+
public static final @NotNull String DELETE = "bank.delete";
17+
public static final @NotNull String BALTOP = "bank.baltop";
18+
public static final @NotNull String POS_CREATE = "bank.pos.create";
19+
public static final @NotNull String POS_USE = "bank.pos.use";
20+
public static final @NotNull String INVOICE_CREATE = "bank.invoice.create";
21+
public static final @NotNull String INVOICE_CREATE_OTHER = "bank.invoice.create.other";
22+
public static final @NotNull String INVOICE_VIEW = "bank.invoice.view";
23+
public static final @NotNull String INVOICE_VIEW_OTHER = "bank.invoice.view.other";
24+
public static final @NotNull String INVOICE_PAY_OTHER = "bank.invoice.pay.other";
25+
public static final @NotNull String INVOICE_PAY_ACCOUNT_OTHER = "bank.invoice.pay.account-other";
26+
public static final @NotNull String INVOICE_SEND = "bank.invoice.send";
27+
public static final @NotNull String INVOICE_SEND_OTHER = "bank.invoice.send.other";
28+
public static final @NotNull String RELOAD = "bank.reload";
29+
public static final @NotNull String BALANCE_OTHER = "bank.balance.other";
30+
public static final @NotNull String TRANSFER_FROM_OTHER = "bank.transfer.from-other";
31+
public static final @NotNull String HISTORY_OTHER = "bank.history.other";
32+
public static final @NotNull String ACCOUNT_CREATE_OTHER = "bank.account.create.other";
33+
public static final @NotNull String ACCOUNT_CREATE_BYPASS = "bank.account.create.bypass";
34+
public static final @NotNull String ACCOUNT_CREATE_VAULT = "bank.account.create.vault";
35+
public static final @NotNull String INSTRUMENT_CREATE_OTHER = "bank.instrument.create.other";
36+
public static final @NotNull String INSTRUMENT_CREATE_BYPASS = "bank.instrument.create.bypass";
37+
public static final @NotNull String SET_BALANCE = "bank.set.balance";
38+
public static final @NotNull String SET_NAME_OTHER = "bank.set.name.other";
39+
public static final @NotNull String SET_NAME_VAULT = "bank.set.name.vault";
40+
public static final @NotNull String FREEZE_OTHER = "bank.freeze.other";
41+
public static final @NotNull String DELETE_OTHER = "bank.delete.other";
42+
public static final @NotNull String DELETE_VAULT = "bank.delete.vault";
43+
public static final @NotNull String POS_CREATE_OTHER = "bank.pos.create.other";
44+
public static final @NotNull String POS_CREATE_PERSONAL = "bank.pos.create.personal";
45+
public static final @NotNull String POS_USE_OTHER = "bank.pos.use.other";
46+
public static final @NotNull String NOTIFY_UPDATE = "bank.notify-update";
4647
}

src/main/java/pro/cloudnode/smp/bankaccounts/Transaction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public Transaction(Account from, Account to, BigDecimal amount, @Nullable String
9696
* @param rs Result set
9797
*/
9898
public Transaction(ResultSet rs) throws SQLException {
99-
this(rs.getInt("id"), Account.get(rs.getString("from")).orElse(new Account.ClosedAccount()), Account.get(rs.getString("to")).orElse(new Account.ClosedAccount()), rs.getBigDecimal("amount"), rs.getTimestamp("time"), rs.getString("description"), rs.getString("instrument"));
99+
this(rs.getInt("id"), Account.get(Account.Tag.from(rs.getString("from"))).orElse(new Account.ClosedAccount()), Account.get(Account.Tag.from(rs.getString("to"))).orElse(new Account.ClosedAccount()), rs.getBigDecimal("amount"), rs.getTimestamp("time"), rs.getString("description"), rs.getString("instrument"));
100100
}
101101

102102
/**

0 commit comments

Comments
 (0)