Skip to content

Commit bac7093

Browse files
committed
fix: add ReadWriteLock to all providers and atomic checked methods to prevent async race conditions
1 parent fc74d71 commit bac7093

6 files changed

Lines changed: 521 additions & 261 deletions

File tree

src/main/java/me/onebone/economyapi/AsyncOperator.java

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,10 @@ public void onRun() {
9292
Server.getInstance().getPluginManager().callEvent(event);
9393

9494
if (!event.isCancelled() || force) {
95-
if (EconomyAPI.getInstance().provider.accountExists(currencyName, lowerId)) {
96-
double finalAmount = event.getAmount();
97-
if (finalAmount <= EconomyAPI.getInstance().getMaxMoney(currencyName)) {
98-
EconomyAPI.getInstance().provider.setMoney(currencyName, lowerId, finalAmount);
99-
this.setResult(EconomyAPI.RET_SUCCESS);
100-
} else {
101-
this.setResult(EconomyAPI.RET_INVALID);
102-
}
103-
} else {
104-
this.setResult(EconomyAPI.RET_NO_ACCOUNT);
105-
}
95+
double finalAmount = event.getAmount();
96+
int result = EconomyAPI.getInstance().provider.setMoneyChecked(
97+
currencyName, lowerId, finalAmount, EconomyAPI.getInstance().getMaxMoney(currencyName));
98+
this.setResult(result);
10699
} else {
107100
this.setResult(EconomyAPI.RET_CANCELLED);
108101
}
@@ -182,17 +175,9 @@ public void onRun() {
182175

183176
if (!event.isCancelled() || force) {
184177
double finalAmount = event.getAmount();
185-
double money = EconomyAPI.getInstance().provider.getMoney(currencyName, lowerId);
186-
if (money != -1) {
187-
if (money + finalAmount > EconomyAPI.getInstance().getMaxMoney(currencyName)) {
188-
this.setResult(EconomyAPI.RET_INVALID);
189-
} else {
190-
EconomyAPI.getInstance().provider.addMoney(currencyName, lowerId, finalAmount);
191-
this.setResult(EconomyAPI.RET_SUCCESS);
192-
}
193-
} else {
194-
this.setResult(EconomyAPI.RET_NO_ACCOUNT);
195-
}
178+
int result = EconomyAPI.getInstance().provider.addMoneyChecked(
179+
currencyName, lowerId, finalAmount, EconomyAPI.getInstance().getMaxMoney(currencyName));
180+
this.setResult(result);
196181
} else {
197182
this.setResult(EconomyAPI.RET_CANCELLED);
198183
}
@@ -276,17 +261,9 @@ public void onRun() {
276261

277262
if (!event.isCancelled() || force) {
278263
double finalAmount = event.getAmount();
279-
double money = EconomyAPI.getInstance().provider.getMoney(currencyName, lowerId);
280-
if (money != -1) {
281-
if (money - finalAmount < 0) {
282-
this.setResult(EconomyAPI.RET_INVALID);
283-
} else {
284-
EconomyAPI.getInstance().provider.reduceMoney(currencyName, lowerId, finalAmount);
285-
this.setResult(EconomyAPI.RET_SUCCESS);
286-
}
287-
} else {
288-
this.setResult(EconomyAPI.RET_NO_ACCOUNT);
289-
}
264+
int result = EconomyAPI.getInstance().provider.reduceMoneyChecked(
265+
currencyName, lowerId, finalAmount);
266+
this.setResult(result);
290267
} else {
291268
this.setResult(EconomyAPI.RET_CANCELLED);
292269
}

src/main/java/me/onebone/economyapi/EconomyAPI.java

Lines changed: 9 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -212,18 +212,8 @@ private int setMoneyInternal(String id, double amount, boolean force) {
212212
SetMoneyEvent event = new SetMoneyEvent(id, amount);
213213
this.getServer().getPluginManager().callEvent(event);
214214
if (!event.isCancelled() || force) {
215-
if (this.provider.accountExists(id)) {
216-
amount = event.getAmount();
217-
218-
if (amount <= this.getMaxMoney()) {
219-
this.provider.setMoney(id, amount);
220-
return RET_SUCCESS;
221-
} else {
222-
return RET_INVALID;
223-
}
224-
} else {
225-
return RET_NO_ACCOUNT;
226-
}
215+
return this.provider.setMoneyChecked(
216+
MAIN_CONFIG.getDefaultCurrency().getName(), id, event.getAmount(), this.getMaxMoney());
227217
}
228218
return RET_CANCELLED;
229219
}
@@ -271,18 +261,8 @@ private int addMoneyInternal(String id, double amount, boolean force) {
271261
AddMoneyEvent event = new AddMoneyEvent(id, amount);
272262
this.getServer().getPluginManager().callEvent(event);
273263
if (!event.isCancelled() || force) {
274-
amount = event.getAmount();
275-
double money;
276-
if ((money = this.provider.getMoney(id)) != -1) {
277-
if (money + amount > this.getMaxMoney()) {
278-
return RET_INVALID;
279-
} else {
280-
this.provider.addMoney(id, amount);
281-
return RET_SUCCESS;
282-
}
283-
} else {
284-
return RET_NO_ACCOUNT;
285-
}
264+
return this.provider.addMoneyChecked(
265+
MAIN_CONFIG.getDefaultCurrency().getName(), id, event.getAmount(), this.getMaxMoney());
286266
}
287267
return RET_CANCELLED;
288268
}
@@ -331,19 +311,8 @@ private int reduceMoneyInternal(String id, double amount, boolean force) {
331311
ReduceMoneyEvent event = new ReduceMoneyEvent(id, amount);
332312
this.getServer().getPluginManager().callEvent(event);
333313
if (!event.isCancelled() || force) {
334-
amount = event.getAmount();
335-
336-
double money;
337-
if ((money = this.provider.getMoney(id)) != -1) {
338-
if (money - amount < 0) {
339-
return RET_INVALID;
340-
} else {
341-
this.provider.reduceMoney(id, amount);
342-
return RET_SUCCESS;
343-
}
344-
} else {
345-
return RET_NO_ACCOUNT;
346-
}
314+
return this.provider.reduceMoneyChecked(
315+
MAIN_CONFIG.getDefaultCurrency().getName(), id, event.getAmount());
347316
}
348317
return RET_CANCELLED;
349318
}
@@ -442,17 +411,7 @@ private int setMoneyInternal(String id, double amount, String currencyName, bool
442411
SetMoneyEvent event = new SetMoneyEvent(id, amount, currencyName);
443412
this.getServer().getPluginManager().callEvent(event);
444413
if (!event.isCancelled() || force) {
445-
if (this.provider.accountExists(currencyName, id)) {
446-
amount = event.getAmount();
447-
if (amount <= getMaxMoney(currencyName)) {
448-
this.provider.setMoney(currencyName, id, amount);
449-
return RET_SUCCESS;
450-
} else {
451-
return RET_INVALID;
452-
}
453-
} else {
454-
return RET_NO_ACCOUNT;
455-
}
414+
return this.provider.setMoneyChecked(currencyName, id, event.getAmount(), getMaxMoney(currencyName));
456415
}
457416
return RET_CANCELLED;
458417
}
@@ -500,18 +459,7 @@ private int addMoneyInternal(String id, double amount, String currencyName, bool
500459
AddMoneyEvent event = new AddMoneyEvent(id, amount, currencyName);
501460
this.getServer().getPluginManager().callEvent(event);
502461
if (!event.isCancelled() || force) {
503-
amount = event.getAmount();
504-
double money = this.provider.getMoney(currencyName, id);
505-
if (money != -1) {
506-
if (money + amount > getMaxMoney(currencyName)) {
507-
return RET_INVALID;
508-
} else {
509-
this.provider.addMoney(currencyName, id, amount);
510-
return RET_SUCCESS;
511-
}
512-
} else {
513-
return RET_NO_ACCOUNT;
514-
}
462+
return this.provider.addMoneyChecked(currencyName, id, event.getAmount(), getMaxMoney(currencyName));
515463
}
516464
return RET_CANCELLED;
517465
}
@@ -559,18 +507,7 @@ private int reduceMoneyInternal(String id, double amount, String currencyName, b
559507
ReduceMoneyEvent event = new ReduceMoneyEvent(id, amount, currencyName);
560508
this.getServer().getPluginManager().callEvent(event);
561509
if (!event.isCancelled() || force) {
562-
amount = event.getAmount();
563-
double money = this.provider.getMoney(currencyName, id);
564-
if (money != -1) {
565-
if (money - amount < 0) {
566-
return RET_INVALID;
567-
} else {
568-
this.provider.reduceMoney(currencyName, id, amount);
569-
return RET_SUCCESS;
570-
}
571-
} else {
572-
return RET_NO_ACCOUNT;
573-
}
510+
return this.provider.reduceMoneyChecked(currencyName, id, event.getAmount());
574511
}
575512
return RET_CANCELLED;
576513
}

0 commit comments

Comments
 (0)