Skip to content

Commit 52ccf22

Browse files
authored
Merge pull request #64 from myth-MC/dev/1.0
1.1.0
2 parents f6e3722 + 8bed1d0 commit 52ccf22

File tree

87 files changed

+2042
-1299
lines changed

Some content is hidden

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

87 files changed

+2042
-1299
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.3
1+
1.1.0

api/pom.xml

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,34 @@
66
<parent>
77
<groupId>ovh.mythmc</groupId>
88
<artifactId>banco</artifactId>
9-
<version>1.0.3</version>
9+
<version>1.1.0</version>
1010
</parent>
1111

1212
<artifactId>banco-api</artifactId>
1313

14-
<properties>
15-
<maven.compiler.source>21</maven.compiler.source>
16-
<maven.compiler.target>21</maven.compiler.target>
17-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18-
</properties>
14+
<build>
15+
<plugins>
16+
<!-- Build JavaDoc -->
17+
<plugin>
18+
<groupId>org.apache.maven.plugins</groupId>
19+
<artifactId>maven-javadoc-plugin</artifactId>
20+
<version>3.11.2</version>
21+
<configuration>
22+
<doclint>none</doclint>
23+
<sourcepath>${project.basedir}/src;${project.basedir}/target/generated-sources/annotations</sourcepath>
24+
</configuration>
25+
<executions>
26+
<execution>
27+
<id>attach-javadocs</id>
28+
<phase>package</phase>
29+
<goals>
30+
<goal>jar</goal>
31+
</goals>
32+
</execution>
33+
</executions>
34+
</plugin>
35+
</plugins>
36+
</build>
1937

2038
<repositories>
2139
<repository>

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ public interface Banco {
2727

2828
@NotNull BancoSettingsProvider getSettings();
2929

30-
@Deprecated(since = "1.0") default BancoItemRegistry getItemManager() { return getItemRegistry(); }
31-
32-
@Deprecated(since = "1.0") default BancoStorageRegistry getStorageManager() { return getStorageRegistry(); }
33-
3430
@NotNull default BancoItemRegistry getItemRegistry() { return BancoItemRegistry.instance; }
3531

3632
@NotNull default BancoStorageRegistry getStorageRegistry() { return BancoStorageRegistry.instance; }

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

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
import java.nio.file.Files;
77
import java.sql.SQLException;
88
import java.util.Collection;
9-
import java.util.HashMap;
9+
import java.util.HashSet;
1010
import java.util.List;
1111
import java.util.Map;
12+
import java.util.Set;
1213
import java.util.TimerTask;
1314
import java.util.UUID;
15+
import java.util.concurrent.ConcurrentHashMap;
1416
import java.util.concurrent.Executors;
1517
import java.util.concurrent.ScheduledExecutorService;
1618
import java.util.concurrent.TimeUnit;
@@ -20,11 +22,12 @@
2022
import com.j256.ormlite.dao.Dao;
2123
import com.j256.ormlite.dao.DaoManager;
2224
import com.j256.ormlite.jdbc.JdbcConnectionSource;
23-
import com.j256.ormlite.support.ConnectionSource;
2425
import com.j256.ormlite.table.TableUtils;
2526

2627
import lombok.NoArgsConstructor;
2728
import ovh.mythmc.banco.api.Banco;
29+
import ovh.mythmc.banco.api.accounts.database.MySQLConnectionSource;
30+
import ovh.mythmc.banco.api.accounts.database.SQLiteConnectionSource;
2831
import ovh.mythmc.banco.api.logger.LoggerWrapper;
2932

3033
@NoArgsConstructor
@@ -34,7 +37,11 @@ public final class AccountDatabase {
3437

3538
private Dao<Account, UUID> accountsDao;
3639

37-
private final Map<AccountIdentifierKey, Account> cache = new HashMap<>();
40+
private JdbcConnectionSource connectionSource;
41+
42+
private final Map<AccountIdentifierKey, Account> cache = new ConcurrentHashMap<>();
43+
44+
private final Collection<AccountIdentifierKey> accountIdentifierCache = new HashSet<>();
3845

3946
private boolean firstBoot = false;
4047

@@ -58,13 +65,17 @@ public void error(final String message, final Object... args) {
5865
};
5966

6067
public void initialize(@NotNull String path) throws SQLException {
61-
ConnectionSource connectionSource = new JdbcConnectionSource("jdbc:sqlite:" + path);
68+
this.connectionSource = switch (Banco.get().getSettings().get().getDatabase().getType()) {
69+
case SQLITE -> new SQLiteConnectionSource(path);
70+
case MYSQL -> new MySQLConnectionSource();
71+
};
72+
6273
TableUtils.createTableIfNotExists(connectionSource, Account.class);
63-
accountsDao = DaoManager.createDao(connectionSource, Account.class);
74+
this.accountsDao = DaoManager.createDao(connectionSource, Account.class);
6475

6576
this.path = path;
6677

67-
firstBoot = !Banco.get().getSettings().get().getDatabase().isInitialized() &&
78+
this.firstBoot = !Banco.get().getSettings().get().getDatabase().isInitialized() &&
6879
Banco.get().getSettings().get().getDatabase().getDatabaseVersion() == 0;
6980

7081
backup("backup");
@@ -75,7 +86,19 @@ public void initialize(@NotNull String path) throws SQLException {
7586
if (Banco.get().getSettings().get().isDebug())
7687
Banco.get().getLogger().info("Loaded a total amount of " + get().size() + " accounts! (using V3 format)");
7788

78-
Banco.get().getSettings().get().getDatabase().setDatabaseInitialized();
89+
accountIdentifierCache.addAll(get().stream().map(Account::getIdentifier).toList());
90+
91+
Banco.get().getSettings().setDatabaseInitialized();
92+
}
93+
94+
private Dao<Account, UUID> getDao() {
95+
try {
96+
this.connectionSource.initialize(); // Reopen connection if necessary
97+
} catch (SQLException e) {
98+
e.printStackTrace(System.err);
99+
}
100+
101+
return this.accountsDao;
79102
}
80103

81104
public void backup(String differentiator) {
@@ -96,15 +119,20 @@ public void shutdown() {
96119

97120
public void create(@NotNull Account account) {
98121
try {
99-
accountsDao.createIfNotExists(account);
122+
getDao().createIfNotExists(account);
123+
// Cache name and account
124+
accountIdentifierCache.add(account.getIdentifier());
125+
cache.put(account.getIdentifier(), account);
100126
} catch (SQLException e) {
101127
logger.error("Exception while creating account {}", e);
102128
}
103129
}
104130

105131
public void delete(@NotNull Account account) {
106132
try {
107-
accountsDao.delete(account);
133+
getDao().delete(account);
134+
// Delete cached name
135+
accountIdentifierCache.remove(account.getIdentifier());
108136
} catch (SQLException e) {
109137
logger.error("Exception while deleting account {}", e);
110138
}
@@ -142,7 +170,7 @@ private void updateAllDatabaseEntries() {
142170

143171
private void updateDatabaseEntry(@NotNull Account account) {
144172
try {
145-
accountsDao.update(account);
173+
getDao().update(account);
146174

147175
// Clear cache value
148176
cache.remove(account.getIdentifier());
@@ -155,9 +183,13 @@ public Collection<Account> getCachedAccounts() {
155183
return cache.values();
156184
}
157185

186+
public Collection<AccountIdentifierKey> getAccountIdentifierCache() {
187+
return this.accountIdentifierCache;
188+
}
189+
158190
public List<Account> get() {
159191
try {
160-
return accountsDao.queryForAll();
192+
return getDao().queryForAll();
161193
} catch (SQLException e) {
162194
logger.error("Exception while getting every account {}", e);
163195
}
@@ -171,7 +203,7 @@ public Account getByUuid(@NotNull UUID uuid) {
171203
return cachedAccount;
172204

173205
try {
174-
Account account = accountsDao.queryForId(uuid);
206+
Account account = getDao().queryForId(uuid);
175207
if (account == null)
176208
return null;
177209

@@ -190,7 +222,7 @@ public Account getByName(@NotNull String name) {
190222
return cachedAccount;
191223

192224
try {
193-
List<Account> accounts = accountsDao.queryBuilder()
225+
List<Account> accounts = getDao().queryBuilder()
194226
.where()
195227
.like("name", name)
196228
.query();
@@ -214,7 +246,7 @@ public Account getByNameOrUuid(@NotNull String name, UUID uuid) {
214246
return cachedAccount;
215247

216248
try {
217-
List<Account> accounts = accountsDao.queryBuilder()
249+
List<Account> accounts = getDao().queryBuilder()
218250
.where()
219251
.like("name", name)
220252
.query();
@@ -234,35 +266,37 @@ public Account getByNameOrUuid(@NotNull String name, UUID uuid) {
234266
}
235267

236268
private Account findCachedAccountByUuid(@NotNull UUID uuid) {
237-
return cache.entrySet().stream()
269+
return Set.copyOf(cache.entrySet()).stream()
238270
.filter(entry -> entry.getKey().uuid().equals(uuid))
239271
.map(entry -> entry.getValue())
240272
.findFirst().orElse(null);
241273
}
242274

243275
private Account findCachedAccountByName(@NotNull String name) {
244-
return cache.entrySet().stream()
276+
return Set.copyOf(cache.entrySet()).stream()
245277
.filter(entry -> entry.getKey().name() != null)
246278
.filter(entry -> entry.getKey().name().equalsIgnoreCase(name))
247279
.map(entry -> entry.getValue())
248280
.findFirst().orElse(null);
249281
}
250282

251283
public void upgrade() {
284+
// Janky fix to upgrade from v1.0.3 to v1.1.0 since the 'initialized' variable wasn't properly set to true
285+
int oldVersion = Banco.get().getSettings().get().getDatabase().getDatabaseVersion();
286+
if(oldVersion == 1) {
287+
try {
288+
logger.info("Upgrading database...");
289+
getDao().executeRaw("ALTER TABLE `accounts` ADD COLUMN name STRING;");
290+
logger.info("Done!");
291+
} catch (SQLException e) {
292+
logger.error("Exception while upgrading database: {}", e);
293+
}
294+
}
295+
252296
if (!firstBoot) {
253-
int oldVersion = Banco.get().getSettings().get().getDatabase().getDatabaseVersion();
254-
if(oldVersion < 1) {
255-
try {
256-
logger.info("Upgrading database...");
257-
accountsDao.executeRaw("ALTER TABLE `accounts` ADD COLUMN name STRING;");
258-
logger.info("Done!");
259-
} catch (SQLException e) {
260-
logger.error("Exception while upgrading database: {}", e);
261-
}
262-
}
263297
}
264298

265-
Banco.get().getSettings().updateVersion(1);
299+
Banco.get().getSettings().updateVersion(2);
266300
}
267301

268302
}

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

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import ovh.mythmc.banco.api.Banco;
1313
import ovh.mythmc.banco.api.accounts.Transaction.Operation;
1414
import ovh.mythmc.banco.api.accounts.service.LocalUUIDResolver;
15+
import ovh.mythmc.banco.api.accounts.service.OfflinePlayerReference;
1516
import ovh.mythmc.banco.api.callback.account.BancoAccountRegister;
1617
import ovh.mythmc.banco.api.callback.account.BancoAccountUnregister;
1718
import ovh.mythmc.banco.api.callback.account.BancoAccountRegisterCallback;
@@ -43,6 +44,19 @@ public synchronized void create(final @NotNull UUID uuid) {
4344
create(account);
4445
}
4546

47+
/**
48+
* Creates an account
49+
* @param uuid uuid of the account to create and register
50+
* @param name name of the account to create and register
51+
*/
52+
public synchronized void create(final @NotNull UUID uuid, final @NotNull String name) {
53+
Account account = new Account();
54+
account.setUuid(uuid);
55+
account.setName(name);
56+
57+
create(account);
58+
}
59+
4660
/**
4761
* Creates an account
4862
* @param account account to create and register
@@ -66,7 +80,7 @@ public synchronized void delete(final @NotNull Account account) {
6680
* @param uuid uuid of account to delete and unregister
6781
*/
6882
public synchronized void delete(final @NotNull UUID uuid) {
69-
delete(get(uuid));
83+
delete(getByUuid(uuid));
7084
}
7185

7286
/**
@@ -75,17 +89,6 @@ public synchronized void delete(final @NotNull UUID uuid) {
7589
*/
7690
public @NotNull List<Account> get() { return database.get(); }
7791

78-
/**
79-
* Gets a specific account by its UUID
80-
* @deprecated use getByUuid(uuid) instead
81-
* @param uuid UUID of the player
82-
* @return an account matching the UUID or null
83-
*/
84-
@Deprecated(forRemoval = true, since = "1.0")
85-
public Account get(final @NotNull UUID uuid) {
86-
return getByUuid(uuid);
87-
}
88-
8992
/**
9093
* Gets a specific account by its UUID
9194
* @param uuid UUID of the player
@@ -110,7 +113,7 @@ public Account getByName(final @NotNull String name) {
110113
* @param amount amount of money to deposit
111114
*/
112115
public void deposit(final @NotNull UUID uuid, final @NotNull BigDecimal amount) {
113-
deposit(get(uuid), amount);
116+
deposit(getByUuid(uuid), amount);
114117
}
115118

116119
/**
@@ -134,7 +137,7 @@ public void deposit(final @NotNull Account account, final @NotNull BigDecimal am
134137
* @param amount amount of money to withdraw
135138
*/
136139
public void withdraw(final @NotNull UUID uuid, final @NotNull BigDecimal amount) {
137-
withdraw(get(uuid), amount);
140+
withdraw(getByUuid(uuid), amount);
138141
}
139142

140143
/**
@@ -158,7 +161,7 @@ public void withdraw(final @NotNull Account account, final @NotNull BigDecimal a
158161
* @param amount amount of money to set
159162
*/
160163
public void set(final @NotNull UUID uuid, final @NotNull BigDecimal amount) {
161-
set(get(uuid), amount);
164+
set(getByUuid(uuid), amount);
162165
}
163166

164167
/**
@@ -183,7 +186,7 @@ public void set(final @NotNull Account account, final @NotNull BigDecimal amount
183186
* @return true if account has more than the specified amount
184187
*/
185188
public boolean has(final @NotNull UUID uuid, final @NotNull BigDecimal amount) {
186-
return has(get(uuid), amount);
189+
return has(getByUuid(uuid), amount);
187190
}
188191

189192
/**
@@ -202,7 +205,7 @@ public boolean has(final @NotNull Account account, final @NotNull BigDecimal amo
202205
* @return Account's balance
203206
*/
204207
public @NotNull BigDecimal amount(final @NotNull UUID uuid) {
205-
return amount(get(uuid));
208+
return amount(getByUuid(uuid));
206209
}
207210

208211
/**
@@ -215,8 +218,12 @@ public boolean has(final @NotNull Account account, final @NotNull BigDecimal amo
215218
if (!Bukkit.getOfflinePlayer(account.getUuid()).hasPlayedBefore())
216219
return account.getTransactions().add(getValueOfPlayer(account.getUuid(), false));
217220

218-
// Online players
219-
if (Bukkit.getOfflinePlayer(account.getUuid()).isOnline()) {
221+
final Optional<OfflinePlayerReference> optionalOfflinePlayerReference = uuidResolver.resolveOfflinePlayer(account.getUuid());
222+
223+
// Update balance
224+
if (optionalOfflinePlayerReference.isPresent() && // Safeguard against weird thing that apparently can happen in laggy server environments?
225+
uuidResolver.resolveOfflinePlayer(account.getUuid()).get().toOfflinePlayer().isOnline()) {
226+
220227
account.setAmount(getValueOfPlayer(account.getUuid(), true));
221228
database.update(account);
222229
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class Transaction {
3030

3131
public void transact() {
3232
// Callback
33-
var callback = new BancoTransactionProcess(this);
33+
final var callback = new BancoTransactionProcess(this);
3434
BancoTransactionProcessCallback.INSTANCE.invoke(callback);
3535

3636
if (callback.cancelled())

0 commit comments

Comments
 (0)