Skip to content

Commit d522480

Browse files
committed
accept ownership change sub-command
This command doesn't appear in the help or tab complete. It's intended to only be used with `<click:run_command:...>` for better UX.
1 parent 8b2dec9 commit d522480

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -639,13 +639,13 @@ private static void deleteExpired() {
639639
/**
640640
* Get account ownership change request
641641
*
642-
* @param account Account
642+
* @param account Account ID
643643
* @param newOwner New owner
644644
*/
645-
public static @NotNull Optional<@NotNull ChangeOwnerRequest> get(final @NotNull Account account, @NotNull OfflinePlayer newOwner) {
645+
public static @NotNull Optional<@NotNull ChangeOwnerRequest> get(final @NotNull String account, @NotNull OfflinePlayer newOwner) {
646646
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
647647
final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `change_owner_requests` WHERE `account` = ? AND `new_owner` = ? LIMIT 1")) {
648-
stmt.setString(1, account.id);
648+
stmt.setString(1, account);
649649
stmt.setString(2, newOwner.getUniqueId().toString());
650650
final @NotNull ResultSet rs = stmt.executeQuery();
651651
return rs.next() ? Optional.of(new ChangeOwnerRequest(rs)) : Optional.empty();

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,16 @@ public int interestInterval(final @NotNull Account.Type type) {
521521
return Objects.requireNonNull(config.getString("messages.errors.already-owns-account"));
522522
}
523523

524+
// messages.errors.change-owner-not-found
525+
public @NotNull String messagesErrorsChangeOwnerNotFound() {
526+
return Objects.requireNonNull(config.getString("messages.errors.change-owner-not-found"));
527+
}
528+
529+
// messages.errors.change-owner-accept-failed
530+
public @NotNull String messagesErrorsChangeOwnerAcceptFailed() {
531+
return Objects.requireNonNull(config.getString("messages.errors.change-owner-accept-failed"));
532+
}
533+
524534
// messages.balance
525535
public @NotNull String messagesBalance() {
526536
return Objects.requireNonNull(config.getString("messages.balance"));

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public final class Permissions {
1717
public static @NotNull String CHANGE_OWNER = "bank.change.owner";
1818
public static @NotNull String CHANGE_OWNER_OTHER = "bank.change.owner.other";
1919
public static @NotNull String CHANGE_OWNER_SKIP_CONFIRMATION = "bank.change.owner.skip-confirmation";
20+
public static @NotNull String CHANGE_OWNER_ACCEPT = "bank.change.owner.accept";
2021
public static @NotNull String BALTOP = "bank.baltop";
2122
public static @NotNull String POS_CREATE = "bank.pos.create";
2223
public static @NotNull String POS_USE = "bank.pos.use";

src/main/java/pro/cloudnode/smp/bankaccounts/commands/BankCommand.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ public static boolean run(final @NotNull CommandSender sender, final @NotNull St
180180
case "unfreeze", "enable", "unblock" -> unfreeze(sender, argsSubset, label);
181181
case "delete" -> delete(sender, argsSubset, label);
182182
case "changeowner", "newowner", "newholder", "changeholder" -> changeOwner(sender, argsSubset, label);
183+
case "acceptchangeowner" -> acceptChangeOwner(sender, argsSubset, label);
183184
case "transfer", "send", "pay" -> transfer(sender, argsSubset, label);
184185
case "transactions", "history" -> transactions(sender, argsSubset, label);
185186
case "instrument", "card" -> instrument(sender, argsSubset, label);
@@ -490,6 +491,33 @@ public static boolean changeOwner(final @NotNull CommandSender sender, final @No
490491
return true;
491492
}
492493

494+
/**
495+
* Accept ownership change request
496+
*/
497+
public static boolean acceptChangeOwner(final @NotNull CommandSender sender, final @NotNull String @NotNull [] args, final @NotNull String label) {
498+
if (!sender.hasPermission(Permissions.CHANGE_OWNER_ACCEPT)) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsNoPermission());
499+
if (args.length < 1) return sendUsage(sender, label, "acceptchangeowner <account>");
500+
final @NotNull Optional<Account.@NotNull ChangeOwnerRequest> request = Account.ChangeOwnerRequest.get(args[0], BankAccounts.getOfflinePlayer(sender));
501+
if (request.isEmpty()) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsChangeOwnerNotFound());
502+
final @NotNull Optional<@NotNull Account> account = request.get().account();
503+
if (account.isEmpty()) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsAccountNotFound());
504+
505+
if (!sender.hasPermission(Permissions.ACCOUNT_CREATE_BYPASS)) {
506+
final @NotNull Account @NotNull [] accounts = Account.get(BankAccounts.getOfflinePlayer(sender), account.get().type);
507+
int limit = BankAccounts.getInstance().config().accountLimits(account.get().type);
508+
if (limit != -1 && accounts.length >= limit)
509+
return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsMaxAccounts(),
510+
Placeholder.unparsed("type", account.get().type.getName()),
511+
Placeholder.unparsed("limit", String.valueOf(limit))
512+
);
513+
}
514+
515+
final boolean success = request.get().confirm();
516+
if (!success) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsChangeOwnerAcceptFailed());
517+
// TODO: send success message to new owner
518+
return true;
519+
}
520+
493521
/**
494522
* Make a transfer to another account
495523
* <p>

src/main/resources/config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,10 @@ messages:
353353
change-owner-no-history: "<red>(!) You must have a transaction history in order to change the owner.</red>"
354354
# The account is already owned by <player> (the new owner's username)
355355
already-owns-account: "<red>(!) This account is already owned by <gray><player></gray>.</red>"
356+
# Change owner request not found
357+
change-owner-not-found: "<red>(!) This account ownership change request was not found.</red>"
358+
# Cannot accept change owner request
359+
change-owner-accept-failed: "<red>(!) Failed to accept ownership change request.</red>"
356360

357361
# Account balance
358362
# Available placeholders:

0 commit comments

Comments
 (0)