Skip to content

Commit 4bd8660

Browse files
committed
fix transfer ownership send & accept
1 parent 5580d06 commit 4bd8660

File tree

5 files changed

+121
-111
lines changed

5 files changed

+121
-111
lines changed

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

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Optional;
3030
import java.util.UUID;
3131
import java.util.logging.Level;
32+
import java.util.stream.Collectors;
3233

3334
/**
3435
* Bank account
@@ -534,12 +535,12 @@ public final static class ChangeOwnerRequest {
534535
/**
535536
* Account id
536537
*/
537-
public final @NotNull String account;
538+
private final @NotNull String account;
538539

539540
/**
540541
* New owner UUID
541542
*/
542-
public final @NotNull UUID newOwner;
543+
public final @NotNull OfflinePlayer newOwner;
543544

544545
/**
545546
* Request creation timestamp
@@ -552,15 +553,15 @@ public final static class ChangeOwnerRequest {
552553
* @param account Account to transfer ownership of
553554
* @param newOwner The new account owner
554555
*/
555-
public ChangeOwnerRequest(final @NotNull Account account, final @NotNull UUID newOwner) {
556+
public ChangeOwnerRequest(final @NotNull Account account, final @NotNull OfflinePlayer newOwner) {
556557
this.account = account.id;
557558
this.newOwner = newOwner;
558559
this.created = new Date();
559560
}
560561

561562
private ChangeOwnerRequest(final @NotNull ResultSet rs) throws SQLException {
562563
this.account = rs.getString("id");
563-
this.newOwner = UUID.fromString(rs.getString("new_owner"));
564+
this.newOwner = BankAccounts.getInstance().getServer().getOfflinePlayer(UUID.fromString(rs.getString("new_owner")));
564565
this.created = new Date(rs.getDate("created").getTime());
565566
}
566567

@@ -571,13 +572,6 @@ private ChangeOwnerRequest(final @NotNull ResultSet rs) throws SQLException {
571572
return Account.get(account);
572573
}
573574

574-
/**
575-
* Get new owner
576-
*/
577-
public @NotNull OfflinePlayer newOwner() {
578-
return BankAccounts.getInstance().getServer().getOfflinePlayer(newOwner);
579-
}
580-
581575
/**
582576
* Check if request has expired
583577
*/
@@ -595,7 +589,7 @@ public boolean confirm() {
595589
final @NotNull Optional<@NotNull Account> account = this.account();
596590
if (account.isEmpty()) return false;
597591
if (account.get().frozen) return false;
598-
account.get().owner = newOwner();
592+
account.get().owner = newOwner;
599593
account.get().update();
600594
this.delete();
601595
return true;
@@ -689,22 +683,46 @@ private static void deleteExpired() {
689683
}
690684

691685
/**
692-
* Get account ownership change requests
686+
* List a player's incoming (received) account ownership change requests.
693687
*
694-
* @param newOwner New owner
688+
* @param player The player whose requests to list
695689
*/
696-
public static @NotNull Account @NotNull [] get(final @NotNull OfflinePlayer newOwner) {
690+
public static @NotNull Account @NotNull [] incoming(final @NotNull OfflinePlayer player) {
697691
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
698692
final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `change_owner_requests` WHERE `new_owner` = ?")) {
699-
stmt.setString(1, newOwner.getUniqueId().toString());
693+
stmt.setString(1, player.getUniqueId().toString());
694+
final @NotNull ResultSet rs = stmt.executeQuery();
695+
696+
final @NotNull List<@NotNull Account> accounts = new ArrayList<>();
697+
while (rs.next()) accounts.add(new Account(rs));
698+
return accounts.toArray(new Account[0]);
699+
}
700+
catch (final @NotNull Exception e) {
701+
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not get incoming account ownership change requests. player: " + player.getUniqueId(), e);
702+
return new Account[0];
703+
}
704+
}
705+
706+
/**
707+
* List a player’s outgoing (sent) account ownership change requests.
708+
*
709+
* @param player The player whose requests to list
710+
*/
711+
public static @NotNull Account @NotNull [] outgoing(final @NotNull OfflinePlayer player) {
712+
final @NotNull String @NotNull [] ids = Arrays.stream(Account.get(player)).map(a -> a.id).toArray(String[]::new);
713+
final @NotNull String placeholders = Arrays.stream(ids).map(id -> "?").collect(Collectors.joining(", "));
714+
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
715+
final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `change_owner_requests` WHERE `account` in (" + placeholders + ")")) {
716+
for (int i = ids.length; i > 0;)
717+
stmt.setString(i, ids[--i]);
700718
final @NotNull ResultSet rs = stmt.executeQuery();
701719

702720
final @NotNull List<@NotNull Account> accounts = new ArrayList<>();
703721
while (rs.next()) accounts.add(new Account(rs));
704722
return accounts.toArray(new Account[0]);
705723
}
706724
catch (final @NotNull Exception e) {
707-
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not get account ownership change requests. newOwner: " + newOwner.getUniqueId(), e);
725+
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not get outgoing account ownership change requests. player: " + player.getUniqueId(), e);
708726
return new Account[0];
709727
}
710728
}

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

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
88
import org.bukkit.Material;
99
import org.bukkit.NamespacedKey;
10-
import org.bukkit.OfflinePlayer;
1110
import org.bukkit.Registry;
1211
import org.bukkit.configuration.file.FileConfiguration;
1312
import org.bukkit.enchantments.Enchantment;
@@ -239,16 +238,6 @@ public boolean instrumentsGlintEnabled() {
239238
return Objects.requireNonNull(Registry.ENCHANTMENT.get(NamespacedKey.minecraft(Objects.requireNonNull(config.getString("instruments.glint.enchantment")))));
240239
}
241240

242-
// change-owner.min-balance
243-
public double changeOwnerMinBalance() {
244-
return config.getDouble("change-owner.min-balance");
245-
}
246-
247-
// change-owner.require-history
248-
public boolean changeOwnerRequireHistory() {
249-
return config.getBoolean("change-owner.require-history");
250-
}
251-
252241
// change-owner.confirm
253242
public boolean changeOwnerConfirm() {
254243
return config.getBoolean("change-owner.confirm");
@@ -259,6 +248,11 @@ public int changeOwnerTimeout() {
259248
return config.getInt("change-owner.timeout");
260249
}
261250

251+
// change-owner.limit-send
252+
public int changeOwnerLimitSend() {
253+
return config.getInt("change-owner.limit-send");
254+
}
255+
262256
// pos.allow-personal
263257
public boolean posAllowPersonal() {
264258
return config.getBoolean("pos.allow-personal");
@@ -786,42 +780,31 @@ public int invoiceNotifyInterval() {
786780
public @NotNull Component messagesErrorsPlayerNeverJoined() {
787781
return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(config.getString("messages.errors.player-never-joined")));
788782
}
789-
790-
// messages.errors.change-owner-balance
791-
public @NotNull Component messagesErrorsChangeOwnerBalance(final @NotNull Account account, final @NotNull BigDecimal requiredBalance) {
783+
784+
// messages.errors.change-owner-same
785+
public @NotNull Component messagesErrorsAlreadyOwnsAccount(final @NotNull Account account) {
792786
return MiniMessage.miniMessage().deserialize(
793-
Objects.requireNonNull(config.getString("messages.errors.change-owner-balance"))
794-
.replace("<account>", account.name())
795-
.replace("<account-id>", account.id)
796-
.replace("<account-type>", account.type.getName())
797-
.replace("<account-owner>", account.ownerNameUnparsed())
798-
.replace("<balance>", account.balance == null ? "∞" : account.balance.toPlainString())
799-
.replace("<balance-formatted>", BankAccounts.formatCurrency(account.balance))
800-
.replace("<balance-short>", BankAccounts.formatCurrencyShort(account.balance))
801-
.replace("<required-balance>", requiredBalance.toPlainString())
802-
.replace("<required-balance-formatted>", BankAccounts.formatCurrency(requiredBalance))
803-
.replace("<required-balance-short>", BankAccounts.formatCurrencyShort(requiredBalance))
787+
Objects.requireNonNull(config.getString("messages.errors.change-owner-same")),
788+
Placeholder.parsed("player", account.ownerNameUnparsed())
804789
);
805790
}
806791

807-
// messages.errors.change-owner-no-history
808-
public @NotNull Component messagesErrorsChangeOwnerNoHistory() {
809-
return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(config.getString("messages.errors.change-owner-no-history")));
810-
}
811-
812-
// messages.errors.already-owns-account
813-
public @NotNull String messagesErrorsAlreadyOwnsAccount() {
814-
return Objects.requireNonNull(config.getString("messages.errors.already-owns-account"));
792+
// messages.errors.change-owner-limit-send
793+
public @NotNull Component messagesErrorsChangeOwnerLimitSend() {
794+
return MiniMessage.miniMessage().deserialize(
795+
Objects.requireNonNull(config.getString("messages.errors.change-owner-limit-send")),
796+
Placeholder.unparsed("limit", String.valueOf(this.changeOwnerLimitSend()))
797+
);
815798
}
816799

817800
// messages.errors.change-owner-not-found
818-
public @NotNull String messagesErrorsChangeOwnerNotFound() {
819-
return Objects.requireNonNull(config.getString("messages.errors.change-owner-not-found"));
801+
public @NotNull Component messagesErrorsChangeOwnerNotFound() {
802+
return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(config.getString("messages.errors.change-owner-not-found")));
820803
}
821804

822805
// messages.errors.change-owner-accept-failed
823-
public @NotNull String messagesErrorsChangeOwnerAcceptFailed() {
824-
return Objects.requireNonNull(config.getString("messages.errors.change-owner-accept-failed"));
806+
public @NotNull Component messagesErrorsChangeOwnerAcceptFailed() {
807+
return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(config.getString("messages.errors.change-owner-accept-failed")));
825808
}
826809

827810
// messages.errors.async-failed
@@ -1511,11 +1494,10 @@ public int invoiceNotifyInterval() {
15111494
// messages.change-owner.request
15121495
public @NotNull Component messagesChangeOwnerRequest(final @NotNull Account.ChangeOwnerRequest request, final @NotNull String acceptCommand) {
15131496
final @NotNull Account account = request.account().orElse(new Account.ClosedAccount());
1514-
final @NotNull OfflinePlayer newOwner = request.newOwner();
15151497
return MiniMessage.miniMessage().deserialize(
15161498
Objects.requireNonNull(config.getString("messages.change-owner.request"))
1517-
.replace("<new-owner-uuid>", newOwner.getUniqueId().toString())
1518-
.replace("<new-owner>", newOwner.getName() == null ? "<i>unknown player</i>" : newOwner.getName())
1499+
.replace("<new-owner-uuid>", request.newOwner.getUniqueId().toString())
1500+
.replace("<new-owner>", request.newOwner.getName() == null ? "<i>unknown player</i>" : request.newOwner.getName())
15191501
.replace("<accept-command>", acceptCommand)
15201502
.replace("<account>", account.name())
15211503
.replace("<account-id>", account.id)
@@ -1531,11 +1513,10 @@ public int invoiceNotifyInterval() {
15311513
// messages.change-owner.sent
15321514
public @NotNull Component messagesChangeOwnerSent(final @NotNull Account.ChangeOwnerRequest request) {
15331515
final @NotNull Account account = request.account().orElse(new Account.ClosedAccount());
1534-
final @NotNull OfflinePlayer newOwner = request.newOwner();
15351516
return MiniMessage.miniMessage().deserialize(
15361517
Objects.requireNonNull(config.getString("messages.change-owner.sent"))
1537-
.replace("<new-owner-uuid>", newOwner.getUniqueId().toString())
1538-
.replace("<new-owner>", newOwner.getName() == null ? "<i>unknown player</i>" : newOwner.getName())
1518+
.replace("<new-owner-uuid>", request.newOwner.getUniqueId().toString())
1519+
.replace("<new-owner>", request.newOwner.getName() == null ? "<i>unknown player</i>" : request.newOwner.getName())
15391520
.replace("<account>", account.name())
15401521
.replace("<account-id>", account.id)
15411522
.replace("<account-type>", account.type.getName())
@@ -1550,11 +1531,10 @@ public int invoiceNotifyInterval() {
15501531
// messages.change-owner.accepted
15511532
public @NotNull Component messagesChangeOwnerAccepted(final @NotNull Account.ChangeOwnerRequest request) {
15521533
final @NotNull Account account = request.account().orElse(new Account.ClosedAccount());
1553-
final @NotNull OfflinePlayer newOwner = request.newOwner();
15541534
return MiniMessage.miniMessage().deserialize(
15551535
Objects.requireNonNull(config.getString("messages.change-owner.accepted"))
1556-
.replace("<new-owner-uuid>", newOwner.getUniqueId().toString())
1557-
.replace("<new-owner>", newOwner.getName() == null ? "<i>unknown player</i>" : newOwner.getName())
1536+
.replace("<new-owner-uuid>", request.newOwner.getUniqueId().toString())
1537+
.replace("<new-owner>", request.newOwner.getName() == null ? "<i>unknown player</i>" : request.newOwner.getName())
15581538
.replace("<account>", account.name())
15591539
.replace("<account-id>", account.id)
15601540
.replace("<account-type>", account.type.getName())
@@ -1587,6 +1567,7 @@ public enum HelpCommandsBank {
15871567
FREEZE("freeze"),
15881568
UNFREEZE("unfreeze"),
15891569
DELETE("delete"),
1570+
CHANGE_OWNER("change-owner"),
15901571
INSTRUMENT("instrument"),
15911572
WHOIS("whois"),
15921573
BALTOP("baltop"),

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public final class Permissions {
1616
public static final @NotNull String DELETE = "bank.delete";
1717
public static final @NotNull String CHANGE_OWNER = "bank.change.owner";
1818
public static final @NotNull String CHANGE_OWNER_OTHER = "bank.change.owner.other";
19-
public static final @NotNull String CHANGE_OWNER_SKIP_CONFIRMATION = "bank.change.owner.skip-confirmation";
19+
public static final @NotNull String CHANGE_OWNER_BYPASS_CONFIRM = "bank.change.owner.bypass.confirm";
20+
public static final @NotNull String CHANGE_OWNER_BYPASS_LIMIT = "bank.change.owner.bypass.limit";
2021
public static final @NotNull String CHANGE_OWNER_ACCEPT = "bank.change.owner.accept";
2122
public static final @NotNull String BALTOP = "bank.baltop";
2223
public static final @NotNull String POS_CREATE = "bank.pos.create";

0 commit comments

Comments
 (0)