Skip to content

Commit

Permalink
prevent essentials from having a race condition with NPC accounts.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sleaker committed Apr 28, 2019
1 parent afaaf0d commit 19f0d8a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<!-- Project information -->
<groupId>net.milkbowl.vault</groupId>
<artifactId>Vault</artifactId>
<version>${api.version}.1</version>
<version>${api.version}.2</version>
<name>Vault</name>
<url>https://dev.bukkit.org/projects/vault</url>
<description>Vault is a Permissions &amp; Economy API to allow plugins to more easily hook into these systems without needing to hook each individual system themselves.
Expand Down
21 changes: 14 additions & 7 deletions src/net/milkbowl/vault/economy/plugins/Economy_Essentials.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,13 @@ public EconomyResponse withdrawPlayer(String playerName, double amount) {
return new EconomyResponse(amount, balance, type, errorMessage);
}

@Override
public EconomyResponse depositPlayer(String playerName, double amount) {
public EconomyResponse tryDepositPlayer(String playerName, double amount, int tries) {
if (amount < 0) {
return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot desposit negative funds");
}
if (tries <= 0) {
return new EconomyResponse(amount, 0, ResponseType.FAILURE, "Failed to deposit amount.");
}

double balance;
EconomyResponse.ResponseType type;
Expand All @@ -145,33 +147,38 @@ public EconomyResponse depositPlayer(String playerName, double amount) {
try {
com.earth2me.essentials.api.Economy.add(playerName, amount);
balance = com.earth2me.essentials.api.Economy.getMoney(playerName);
type = EconomyResponse.ResponseType.SUCCESS;
type = ResponseType.SUCCESS;
} catch (UserDoesNotExistException e) {
if (createPlayerAccount(playerName)) {
return depositPlayer(playerName, amount);
return tryDepositPlayer(playerName, amount, tries--);
} else {
amount = 0;
balance = 0;
type = EconomyResponse.ResponseType.FAILURE;
type = ResponseType.FAILURE;
errorMessage = "User does not exist";
}
} catch (NoLoanPermittedException e) {
try {
balance = com.earth2me.essentials.api.Economy.getMoney(playerName);
amount = 0;
type = EconomyResponse.ResponseType.FAILURE;
type = ResponseType.FAILURE;
errorMessage = "Loan was not permitted";
} catch (UserDoesNotExistException e1) {
balance = 0;
amount = 0;
type = EconomyResponse.ResponseType.FAILURE;
type = ResponseType.FAILURE;
errorMessage = "Loan was not permitted";
}
}

return new EconomyResponse(amount, balance, type, errorMessage);
}

@Override
public EconomyResponse depositPlayer(String playerName, double amount) {
return tryDepositPlayer(playerName, amount, 2);
}

public class EconomyServerListener implements Listener {
Economy_Essentials economy = null;

Expand Down

0 comments on commit 19f0d8a

Please sign in to comment.