Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
28a8f58
fix bad formatting examples
zefir-git Dec 30, 2023
7c57033
Merge branch 'master' into 65-add-the-ability-to-transfer-ownership-o…
zefir-git Jan 2, 2024
a3b4ef6
Merge branch 'master' into 65-add-the-ability-to-transfer-ownership-o…
zefir-git Jan 5, 2024
6046378
db tables for owner change requests
zefir-git Jan 5, 2024
53e0418
change account owner config
zefir-git Jan 5, 2024
b9907a4
account ownership change permissions
zefir-git Jan 5, 2024
6f9db50
ChangeOwnerRequest class
zefir-git Jan 5, 2024
492475a
tasks that run every minute
zefir-git Jan 5, 2024
d5818f2
remove unused import
zefir-git Jan 5, 2024
64f93ee
use Runnable instead of Supplier for minute interval sched
zefir-git Jan 5, 2024
8207d3d
move interest timer to minuteLoopTasks
zefir-git Jan 5, 2024
b2bcc62
add err msg for when account is already owned by "new owner" player
zefir-git Jan 5, 2024
977f552
change owner command
zefir-git Jan 5, 2024
5dc8dee
Merge branch 'master' into 65-add-the-ability-to-transfer-ownership-o…
zefir-git Jan 14, 2024
96be807
register change owner sub-command
zefir-git Jan 14, 2024
2be20d4
Merge branch 'master' into 65-add-the-ability-to-transfer-ownership-o…
zefir-git Jan 15, 2024
8b2dec9
Merge branch 'master' into 65-add-the-ability-to-transfer-ownership-o…
zefir-git Jan 15, 2024
46e4ba1
accept ownership change sub-command
zefir-git Jan 15, 2024
ca654e5
Merge branch 'master' into 65-add-the-ability-to-transfer-ownership-o…
zefir-git Jan 21, 2024
eb1fec7
use new config
zefir-git Jan 21, 2024
ddb1fb3
Merge branch 'master' into 65-add-the-ability-to-transfer-ownership-o…
zefir-git Feb 6, 2024
c68b82f
allow sending message to any audience
zefir-git Feb 6, 2024
87e52dd
send account owner change request message
zefir-git Feb 6, 2024
fca0b46
de-register minute loop tasks
zefir-git Feb 8, 2024
8679196
fix config path for change owner req msg
zefir-git Feb 8, 2024
6485ede
message for account ownership request sent
zefir-git Feb 8, 2024
52a6a7e
successfully accepted account ownership message
zefir-git Feb 8, 2024
cbaa08a
Merge branch 'master' into 65-add-the-ability-to-transfer-ownership-o…
zefir-git Feb 28, 2024
da3ea69
Merge branch 'master' into 65-add-the-ability-to-transfer-ownership-o…
zefir-git May 3, 2024
9cec648
Merge branch 'master' into 65-add-the-ability-to-transfer-ownership-o…
zefir-git May 29, 2024
bff92a1
Merge branch 'master' into 65-add-the-ability-to-transfer-ownership-o…
zefir-git Sep 16, 2024
641eb68
Revert "move interest timer to minuteLoopTasks"
zefir-git Sep 16, 2024
0031341
remove unused import
zefir-git Sep 16, 2024
d77ec31
Merge branch 'master' into 65-add-the-ability-to-transfer-ownership-o…
zefir-git Sep 17, 2024
568c4c6
Merge branch 'master' into 65-add-the-ability-to-transfer-ownership-o…
zefir-git Nov 5, 2024
223df5c
Merge branch 'master' into 65-add-the-ability-to-transfer-ownership-o…
zefir-git Feb 24, 2025
5580d06
allow Message command result to use null audience
zefir-git Feb 25, 2025
4bd8660
fix transfer ownership send & accept
zefir-git Feb 25, 2025
d73d9b7
Merge branch 'master' into 65-add-the-ability-to-transfer-ownership-o…
zefir-git Feb 25, 2025
f83c8a9
Merge branch 'master' into 65-add-the-ability-to-transfer-ownership-o…
zefir-git Mar 3, 2025
37cecdb
fix: check for request expiration in accept command
zefir-git Feb 25, 2025
f841adb
Merge branch 'master' into 65-add-the-ability-to-transfer-ownership-o…
zefir-git Oct 18, 2025
325b022
finish account transfers
zefir-git Nov 23, 2026
02f5448
Merge branch 'master' into 65-add-the-ability-to-transfer-ownership-o…
zefir-git Nov 23, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 218 additions & 8 deletions src/main/java/pro/cloudnode/smp/bankaccounts/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
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
Expand All @@ -40,7 +42,7 @@ public class Account {
/**
* Account owner
*/
public final @NotNull OfflinePlayer owner;
public @NotNull OfflinePlayer owner;
/**
* Account type
*/
Expand Down Expand Up @@ -381,13 +383,14 @@ public void insert() {
*/
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);
final @NotNull PreparedStatement stmt = conn.prepareStatement("UPDATE `bank_accounts` SET `owner` = ?, `name` = ?, `balance` = ?, `frozen` = ? WHERE `id` = ?")) {
stmt.setString(1, owner.getUniqueId().toString());
if (name == null) stmt.setNull(2, java.sql.Types.VARCHAR);
else stmt.setString(2, name);
if (balance == null) stmt.setNull(3, java.sql.Types.DECIMAL);
else stmt.setBigDecimal(3, balance);
stmt.setBoolean(4, frozen);
stmt.setString(5, id);

stmt.executeUpdate();
} catch (final @NotNull Exception e) {
Expand All @@ -406,6 +409,7 @@ public void delete() {
} catch (final @NotNull Exception e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not delete account: " + id, e);
}
ChangeOwnerRequest.delete(id);
}

/**
Expand Down Expand Up @@ -525,4 +529,210 @@ public enum Type {
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
*/
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("account");
this.newOwner = BankAccounts.getInstance().getServer().getOfflinePlayer(UUID.fromString(rs.getString("new_owner")));
this.created = new Date(rs.getTimestamp("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() * 60_000L;
}

/**
* Confirm/accept the request
* <p>
* This also deletes any other pending requests for the same account.
*
* @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();
ChangeOwnerRequest.delete(this.account);
return true;
}

/**
* Insert into database, replacing any existing request for the same account and new owner
*/
public void insert() {
this.delete();
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.getUniqueId().toString());
stmt.setTimestamp(3, new java.sql.Timestamp(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.getUniqueId(), 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.getUniqueId().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.getUniqueId(), e);
}
}

/**
* Delete all requests to transfer a certain account
*
* @param account Account ID
*/
public static void delete(final @NotNull String 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);
stmt.executeUpdate();
}
catch (final @NotNull Exception e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not delete account ownership change requests. account: " + account, e);
}
}

/**
* Delete expired requests
*/
public 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.setTimestamp(1, new java.sql.Timestamp(System.currentTimeMillis() - BankAccounts.getInstance().config().changeOwnerTimeout() * 60_000L));
stmt.executeUpdate();
}
catch (final @NotNull Exception e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not delete expired account ownership change requests", e);
}
}

/**
* Get account ownership change request
*
* @param account Account ID
* @param newOwner New owner
*/
public static @NotNull Optional<@NotNull ChangeOwnerRequest> get(final @NotNull String account, final @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 that have not expired.
*
* @param player The player whose requests to list
*/
public static @NotNull ChangeOwnerRequest @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 ChangeOwnerRequest> requests = new ArrayList<>();
while (rs.next()) {
final @NotNull ChangeOwnerRequest request = new ChangeOwnerRequest(rs);
if (!request.expired()) requests.add(request);
}
return requests.toArray(new ChangeOwnerRequest[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 ChangeOwnerRequest[0];
}
}

/**
* List a player's outgoing (sent) account ownership change requests that have not expired.
* <p>
* These are requests for accounts that the player currently owns.
*
* @param player The player whose requests to list
*/
public static @NotNull ChangeOwnerRequest @NotNull [] outgoing(final @NotNull OfflinePlayer player) {
final @NotNull String @NotNull [] ids = Arrays.stream(Account.get(player)).map(a -> a.id).toArray(String[]::new);
if (ids.length == 0) return new ChangeOwnerRequest[0];
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 = 0; i < ids.length; i++)
stmt.setString(i + 1, ids[i]);
final @NotNull ResultSet rs = stmt.executeQuery();

final @NotNull List<@NotNull ChangeOwnerRequest> requests = new ArrayList<>();
while (rs.next()) {
final @NotNull ChangeOwnerRequest request = new ChangeOwnerRequest(rs);
if (!request.expired()) requests.add(request);
}
return requests.toArray(new ChangeOwnerRequest[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 ChangeOwnerRequest[0];
}
}
}
}
11 changes: 11 additions & 0 deletions src/main/java/pro/cloudnode/smp/bankaccounts/BankAccounts.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public static void reload() {
getInstance().invoiceNotificationTask = null;
}
getInstance().setupInvoiceNotificationTimer();
getInstance().setupChangeOwnerCleanupTimer();
}

/**
Expand Down Expand Up @@ -270,6 +271,16 @@ private void setupInvoiceNotificationTimer() {
}, config().invoiceNotifyInterval() * 20L, config().invoiceNotifyInterval() * 20L);
}

private @Nullable BukkitTask changeOwnerCleanupTask = null;

/**
* Periodically delete expired account ownership change requests
*/
private void setupChangeOwnerCleanupTimer() {
if (changeOwnerCleanupTask != null) changeOwnerCleanupTask.cancel();
changeOwnerCleanupTask = getServer().getScheduler().runTaskTimerAsynchronously(this, Account.ChangeOwnerRequest::deleteExpired, 0L, 20L * 60 * 5);
}

private void interestPayment(final @NotNull Account account, final @NotNull BigDecimal amount, final double rate, final @NotNull Account serverAccount) {
if (account.balance == null) return;
if (account.id.equals(serverAccount.id)) return;
Expand Down
Loading
Loading