Skip to content

Commit 3bfdf7f

Browse files
authored
Merge pull request #68 from myth-MC/dev/1.2
1.2.0
2 parents 90bb033 + 17b0b9b commit 3bfdf7f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+762
-154
lines changed

README.md

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,14 @@
5656

5757
### Compatibility chart
5858

59-
| | Compatible? | Version | Notes |
60-
|---------------------------------------------------------|-------------|---------|----------------------------------------------|
61-
| [PaperMC](https://papermc.io/) || 1.20.6+ | |
62-
| [PurpurMC](https://purpurmc.org/) || 1.20.6+ | |
63-
| [Spigot](https://www.spigotmc.org) || 1.20+ | Consider using [PaperMC](https://papermc.io) |
64-
| [Bukkit](https://bukkit.org) || 1.20+ | Consider using [PaperMC](https://papermc.io) |
65-
| [Folia](https://papermc.io/software/folia) || 1.20.6+ | |
59+
**banco** is distributed in two builds released simultaneously. The `modern` build is compatible with PaperMC and its forks starting from Minecraft 1.21.7, while the `legacy` build supports Spigot and its forks starting from Minecraft 1.20:
60+
61+
| | Minecraft version | Compatible software | Optimized | Full feature support |
62+
|-----------------|-------------------|--------------------------------------------------|-----------|----------------------|
63+
| Modern | 1.21.7+ | [PaperMC](https://papermc.io/), Folia, Purpur... |||
64+
| Legacy | 1.20+ | [Spigot](https://www.spigotmc.org) |||
65+
66+
Any software not listed here is not supported and may cause unexpected errors.
6667

6768
### Dependencies
6869

@@ -92,12 +93,3 @@ banco comes with a very simple emerald-based economy setup that can be expanded
9293
* 🏷️ [PlaceholderAPI](https://www.spigotmc.org/resources/placeholderapi.6245/)
9394
* 🐣 [social](https://github.com/myth-MC/social)
9495
* Any other plugin with Vault support, including Factions, Towny Advanced, Jobs...
95-
96-
<hr>
97-
98-
<a href="https://sponsor.mythmc.ovh/">
99-
<img src="https://assets.mythmc.ovh/banner_godlike.png" />
100-
</a>
101-
<div align="center">
102-
<p>We're sponsored by <a href="https://sponsor.mythmc.ovh/">Godlike</a>, a high performance game server hosting. Check them out!</p>
103-
</div>

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.1.1
1+
1.2.0

api/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>ovh.mythmc</groupId>
88
<artifactId>banco</artifactId>
9-
<version>1.1.1</version>
9+
<version>1.2.0</version>
1010
</parent>
1111

1212
<artifactId>banco-api</artifactId>

api/src/main/java/ovh/mythmc/banco/api/Banco.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public interface Banco {
2121
*/
2222
String version();
2323

24+
boolean isShuttingDown();
25+
2426
@NotNull AccountManager getAccountManager();
2527

2628
@NotNull LoggerWrapper getLogger();

api/src/main/java/ovh/mythmc/banco/api/accounts/AccountDatabase.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import ovh.mythmc.banco.api.accounts.database.MySQLConnectionSource;
2929
import ovh.mythmc.banco.api.accounts.database.SQLiteConnectionSource;
3030
import ovh.mythmc.banco.api.logger.LoggerWrapper;
31+
import ovh.mythmc.banco.api.scheduler.BancoScheduler;
3132

3233
@NoArgsConstructor
3334
public final class AccountDatabase {
@@ -137,7 +138,7 @@ public void delete(@NotNull Account account) {
137138
}
138139
}
139140

140-
public void update(@NotNull Account account) {
141+
public void updateCache(@NotNull Account account) {
141142
cache.put(account.getIdentifier(), account);
142143
}
143144

@@ -155,18 +156,23 @@ public void run() {
155156
}, Banco.get().getSettings().get().getDatabase().getCacheClearInterval(), TimeUnit.MINUTES);
156157
}
157158

158-
private void updateAllDatabaseEntries() {
159+
public long updateAllDatabaseEntries() {
159160
var startTime = System.currentTimeMillis();
160161

161162
if (Banco.get().getSettings().get().isDebug())
162163
Banco.get().getLogger().info("Updating " + cache.size() + " cached accounts...");
163164

164165
Map.copyOf(cache).values().forEach(this::updateDatabaseEntry);
165166

167+
final long totalTime = System.currentTimeMillis() - startTime;
168+
166169
if (Banco.get().getSettings().get().isDebug())
167-
Banco.get().getLogger().info("Done! (took " + (System.currentTimeMillis() - startTime) + "ms)");
170+
Banco.get().getLogger().info("Done! (took " + totalTime + "ms)");
171+
172+
return totalTime;
168173
}
169174

175+
/*
170176
private void updateDatabaseEntry(@NotNull Account account) {
171177
try {
172178
getDao().update(account);
@@ -177,6 +183,33 @@ private void updateDatabaseEntry(@NotNull Account account) {
177183
logger.error("Exception while updating account {}", e);
178184
}
179185
}
186+
*/
187+
188+
public long updateDatabaseEntry(@NotNull Account account) {
189+
final Runnable task = () -> {
190+
try {
191+
getDao().update(account);
192+
193+
// Clear cache value
194+
cache.remove(account.getIdentifier());
195+
} catch (SQLException e) {
196+
logger.error("Exception while updating account {}", e);
197+
}
198+
};
199+
200+
final long startTime = System.currentTimeMillis();
201+
202+
if (Banco.get().getSettings().get().getDatabase().isAsynchronousWrites()) {
203+
if (Banco.get().getSettings().get().isDebug())
204+
Banco.get().getLogger().info("Saving {} ({}) asynchronously...", account.getIdentifier().uuid(), account.getIdentifier().name());
205+
206+
BancoScheduler.get().runAsync(task);
207+
} else {
208+
task.run();
209+
}
210+
211+
return (System.currentTimeMillis() - startTime);
212+
}
180213

181214
public Collection<Account> getCachedAccounts() {
182215
return cache.values();

api/src/main/java/ovh/mythmc/banco/api/accounts/AccountManager.java

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public final class AccountManager {
3232
@Getter
3333
private final AccountDatabase database = new AccountDatabase();
3434

35+
@Getter
36+
private final TransactionHistory transactionHistory = new TransactionHistoryImpl();
37+
3538
/**
3639
* Creates an account
3740
* @param uuid uuid of account to create and register
@@ -119,16 +122,37 @@ public void deposit(final @NotNull UUID uuid, final @NotNull BigDecimal amount)
119122
deposit(getByUuid(uuid), amount);
120123
}
121124

125+
/**
126+
* Deposits an amount of money to an account
127+
* @param uuid account uuid that will be modified
128+
* @param amount amount of money to deposit
129+
* @param loggable whether this transaction can be logged
130+
*/
131+
public void deposit(final @NotNull UUID uuid, final @NotNull BigDecimal amount, final boolean loggable) {
132+
deposit(getByUuid(uuid), amount, loggable);
133+
}
134+
122135
/**
123136
* Deposits an amount of money to an account
124137
* @param account account that will be modified
125138
* @param amount amount of money to deposit
126139
*/
127140
public void deposit(final @NotNull Account account, final @NotNull BigDecimal amount) {
141+
deposit(account, amount, true);
142+
}
143+
144+
/**
145+
* Deposits an amount of money to an account
146+
* @param account account that will be modified
147+
* @param amount amount of money to deposit
148+
* @param loggable whether this transaction can be logged
149+
*/
150+
public void deposit(final @NotNull Account account, final @NotNull BigDecimal amount, final boolean loggable) {
128151
Transaction transaction = Transaction.builder()
129152
.account(account)
130153
.amount(amount)
131154
.operation(Operation.DEPOSIT)
155+
.loggable(loggable)
132156
.build();
133157

134158
transaction.queue();
@@ -143,16 +167,37 @@ public void withdraw(final @NotNull UUID uuid, final @NotNull BigDecimal amount)
143167
withdraw(getByUuid(uuid), amount);
144168
}
145169

170+
/**
171+
* Withdraws an amount of money from an account
172+
* @param account account uuid that will be modified
173+
* @param amount amount of money to withdraw
174+
* @param loggable whether this transaction can be logged
175+
*/
176+
public void withdraw(final @NotNull UUID uuid, final @NotNull BigDecimal amount, final boolean loggable) {
177+
withdraw(getByUuid(uuid), amount, loggable);
178+
}
179+
146180
/**
147181
* Withdraws an amount of money from an account
148182
* @param account account that will be modified
149183
* @param amount amount of money to withdraw
150184
*/
151185
public void withdraw(final @NotNull Account account, final @NotNull BigDecimal amount) {
186+
withdraw(account, amount, true);
187+
}
188+
189+
/**
190+
* Withdraws an amount of money from an account
191+
* @param account account that will be modified
192+
* @param amount amount of money to withdraw
193+
* @param loggable whether this transaction can be logged
194+
*/
195+
public void withdraw(final @NotNull Account account, final @NotNull BigDecimal amount, final boolean loggable) {
152196
Transaction transaction = Transaction.builder()
153197
.account(account)
154198
.amount(amount)
155199
.operation(Operation.WITHDRAW)
200+
.loggable(loggable)
156201
.build();
157202

158203
transaction.queue();
@@ -228,7 +273,7 @@ public boolean has(final @NotNull Account account, final @NotNull BigDecimal amo
228273
uuidResolver.resolveOfflinePlayer(account.getUuid()).get().toOfflinePlayer().isOnline()) {
229274

230275
account.setAmount(getValueOfPlayer(account.getUuid(), true));
231-
database.update(account);
276+
database.updateCache(account);
232277
}
233278

234279
// Offline players
@@ -254,15 +299,15 @@ private synchronized BigDecimal getValueOfPlayer(final @NotNull UUID uuid, boole
254299
public synchronized void updateTransactions(final @NotNull Account account) {
255300
BigDecimal amount = account.amount();
256301
account.setTransactions(BigDecimal.valueOf(0));
257-
database.update(account);
302+
database.updateCache(account);
258303

259304
set(account.getUuid(), amount);
260305
}
261306

262307
@ApiStatus.Internal
263308
public void updateName(final @NotNull Account account, String newName) {
264309
account.setName(newName);
265-
database.update(account);
310+
database.updateCache(account);
266311
}
267312

268313
/**

api/src/main/java/ovh/mythmc/banco/api/accounts/Transaction.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.math.BigDecimal;
44
import java.math.RoundingMode;
5+
import java.time.Instant;
56

67
import org.bukkit.Bukkit;
78
import org.jetbrains.annotations.NotNull;
@@ -28,6 +29,9 @@ public class Transaction {
2829

2930
private @NotNull BigDecimal amount;
3031

32+
@Builder.Default
33+
private boolean loggable = true;
34+
3135
public void transact() {
3236
// Callback
3337
final var callback = new BancoTransactionProcess(this);
@@ -47,6 +51,9 @@ public void transact() {
4751
set(amount);
4852
}
4953
}
54+
55+
if (loggable)
56+
Banco.get().getAccountManager().getTransactionHistory().register(account.getIdentifier(), this);
5057
}
5158

5259
public void queue() {
@@ -74,7 +81,7 @@ private void set(@NotNull BigDecimal newAmount) {
7481

7582
// Set transactions to remaining amount
7683
account.setTransactions(account.getTransactions().add(toAdd.setScale(2, RoundingMode.HALF_UP)));
77-
Banco.get().getAccountManager().getDatabase().update(account);
84+
Banco.get().getAccountManager().getDatabase().updateCache(account);
7885
return;
7986
}
8087

@@ -89,7 +96,7 @@ private void set(@NotNull BigDecimal newAmount) {
8996

9097
// Register transaction if player is not online
9198
account.setTransactions(account.getTransactions().add(toAdd));
92-
Banco.get().getAccountManager().getDatabase().update(account);
99+
Banco.get().getAccountManager().getDatabase().updateCache(account);
93100
} else { // Remove amount from account
94101
BigDecimal toRemove = account.amount().subtract(newAmount);
95102

@@ -103,7 +110,7 @@ private void set(@NotNull BigDecimal newAmount) {
103110

104111
// Set transactions to remaining amount
105112
account.setTransactions(account.getTransactions().subtract(toRemove.setScale(2, RoundingMode.HALF_UP)));
106-
Banco.get().getAccountManager().getDatabase().update(account);
113+
Banco.get().getAccountManager().getDatabase().updateCache(account);
107114
return;
108115
}
109116

@@ -118,7 +125,7 @@ private void set(@NotNull BigDecimal newAmount) {
118125

119126
// Register transaction if player is not online
120127
account.setTransactions(account.getTransactions().subtract(toRemove));
121-
Banco.get().getAccountManager().getDatabase().update(account);
128+
Banco.get().getAccountManager().getDatabase().updateCache(account);
122129
}
123130
}
124131

@@ -132,8 +139,11 @@ public static class ImmutableView {
132139

133140
private final Transaction transaction;
134141

142+
private final Instant timestamp;
143+
135144
ImmutableView(Transaction transaction) {
136145
this.transaction = transaction;
146+
this.timestamp = Instant.now();
137147
}
138148

139149
public Account account() {
@@ -148,6 +158,10 @@ public Operation operation() {
148158
return transaction.operation;
149159
}
150160

161+
public Instant timestamp() {
162+
return this.timestamp;
163+
}
164+
151165
}
152166

153167
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package ovh.mythmc.banco.api.accounts;
2+
3+
import java.util.List;
4+
5+
import org.jetbrains.annotations.NotNull;
6+
7+
public interface TransactionHistory {
8+
9+
@NotNull List<Transaction.ImmutableView> get(@NotNull AccountIdentifierKey accountIdentifierKey);
10+
11+
default @NotNull List<Transaction.ImmutableView> get(@NotNull Account account) {
12+
return get(account.getIdentifier());
13+
}
14+
15+
void register(@NotNull AccountIdentifierKey accountIdentifierKey, @NotNull Transaction.ImmutableView transaction);
16+
17+
default void register(@NotNull AccountIdentifierKey accountIdentifierKey, @NotNull Transaction transaction) {
18+
register(accountIdentifierKey, transaction.asImmutable());
19+
}
20+
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package ovh.mythmc.banco.api.accounts;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
final class TransactionHistoryImpl implements TransactionHistory {
9+
10+
private final Map<AccountIdentifierKey, List<Transaction.ImmutableView>> transactionHistory = new HashMap<>();
11+
12+
@Override
13+
public List<Transaction.ImmutableView> get(AccountIdentifierKey accountIdentifierKey) {
14+
return transactionHistory.getOrDefault(accountIdentifierKey, List.of());
15+
}
16+
17+
@Override
18+
public void register(AccountIdentifierKey accountIdentifierKey, Transaction.ImmutableView transaction) {
19+
final List<Transaction.ImmutableView> accountTransactionHistory = transactionHistory.getOrDefault(accountIdentifierKey, new ArrayList<>());
20+
accountTransactionHistory.add(transaction);
21+
22+
transactionHistory.put(accountIdentifierKey, accountTransactionHistory);
23+
}
24+
25+
}

api/src/main/java/ovh/mythmc/banco/api/configuration/sections/CommandsConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public class CommandsConfig {
2828
@Comment({"/pay - Allows players to send money to other accounts", "Permission: banco.use.pay (assigned by default)"})
2929
private SimpleCommand pay = new SimpleCommand(true);
3030

31+
@Comment({"/transactions - Allows players to see their recent transactions", "Permission: banco.use.transactions (assigned by default)"})
32+
private SimpleCommand transactions = new SimpleCommand(true);
33+
3134
public record SimpleCommand(boolean enabled) { }
3235

3336
}

0 commit comments

Comments
 (0)