Skip to content

Commit 46e4ba1

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 46e4ba1

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import org.bukkit.inventory.ItemStack;
1414
import org.bukkit.inventory.meta.ItemMeta;
1515
import org.bukkit.persistence.PersistentDataType;
16-
import org.bukkit.scheduler.BukkitTask;
1716
import org.jetbrains.annotations.NotNull;
1817

1918
import javax.annotation.Nullable;
@@ -32,7 +31,6 @@
3231
import java.util.Objects;
3332
import java.util.Optional;
3433
import java.util.UUID;
35-
import java.util.function.Supplier;
3634
import java.util.logging.Level;
3735

3836
/**
@@ -639,19 +637,19 @@ private static void deleteExpired() {
639637
/**
640638
* Get account ownership change request
641639
*
642-
* @param account Account
640+
* @param account Account ID
643641
* @param newOwner New owner
644642
*/
645-
public static @NotNull Optional<@NotNull ChangeOwnerRequest> get(final @NotNull Account account, @NotNull OfflinePlayer newOwner) {
643+
public static @NotNull Optional<@NotNull ChangeOwnerRequest> get(final @NotNull String account, @NotNull OfflinePlayer newOwner) {
646644
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
647645
final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `change_owner_requests` WHERE `account` = ? AND `new_owner` = ? LIMIT 1")) {
648-
stmt.setString(1, account.id);
646+
stmt.setString(1, account);
649647
stmt.setString(2, newOwner.getUniqueId().toString());
650648
final @NotNull ResultSet rs = stmt.executeQuery();
651649
return rs.next() ? Optional.of(new ChangeOwnerRequest(rs)) : Optional.empty();
652650
}
653651
catch (final @NotNull Exception e) {
654-
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not get account ownership change request. account: " + account.id + ", newOwner: " + newOwner.getUniqueId(), e);
652+
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not get account ownership change request. account: " + account + ", newOwner: " + newOwner.getUniqueId(), e);
655653
return Optional.empty();
656654
}
657655
}

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)