From 19f0d8a3fe5345934601aa0844d661b89a6700b5 Mon Sep 17 00:00:00 2001 From: Nick Minkler Date: Sun, 28 Apr 2019 12:30:47 -0700 Subject: [PATCH] prevent essentials from having a race condition with NPC accounts. --- pom.xml | 2 +- .../economy/plugins/Economy_Essentials.java | 21 ++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index c566da24..baa86b01 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ net.milkbowl.vault Vault - ${api.version}.1 + ${api.version}.2 Vault https://dev.bukkit.org/projects/vault Vault is a Permissions & Economy API to allow plugins to more easily hook into these systems without needing to hook each individual system themselves. diff --git a/src/net/milkbowl/vault/economy/plugins/Economy_Essentials.java b/src/net/milkbowl/vault/economy/plugins/Economy_Essentials.java index dd9eb71d..a35ffc51 100644 --- a/src/net/milkbowl/vault/economy/plugins/Economy_Essentials.java +++ b/src/net/milkbowl/vault/economy/plugins/Economy_Essentials.java @@ -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; @@ -145,26 +147,26 @@ 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"; } } @@ -172,6 +174,11 @@ public EconomyResponse depositPlayer(String playerName, double amount) { 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;