From 19178d28757caa2560f0560d4f05269d12a64f95 Mon Sep 17 00:00:00 2001 From: Brettflan Date: Fri, 4 Jan 2013 09:54:00 -0600 Subject: [PATCH] (by jastice) Handle EconomyResponse objects from Vault on all transactions. Vault's EconomyResponse was ignored for some cases, such as money transfers and deposits. This would cause money to get lost if a transfer failed. This change refunds the initiatior of a transfer and notifies them of the failure. Failing transfers can happen often when using economy plugins based on physical currency, such as Gringotts, for example when no space is available in an account. --- .../factions/integration/Econ.java | 47 +++++++++++++------ 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/src/com/massivecraft/factions/integration/Econ.java b/src/com/massivecraft/factions/integration/Econ.java index acb29309..b9ec1dbf 100644 --- a/src/com/massivecraft/factions/integration/Econ.java +++ b/src/com/massivecraft/factions/integration/Econ.java @@ -18,6 +18,7 @@ import com.massivecraft.factions.struct.Role; import com.massivecraft.factions.util.RelationUtil; import net.milkbowl.vault.economy.Economy; +import net.milkbowl.vault.economy.EconomyResponse; public class Econ @@ -148,14 +149,24 @@ public class Econ } // Transfer money - econ.withdrawPlayer(from.getAccountId(), amount); - econ.depositPlayer(to.getAccountId(), amount); - - // Inform + EconomyResponse erw = econ.withdrawPlayer(from.getAccountId(), amount); + + if (erw.transactionSuccess()) { + EconomyResponse erd = econ.depositPlayer(to.getAccountId(), amount); + if (erd.transactionSuccess()) { + if (notify) sendTransferInfo(invoker, from, to, amount); + return true; + } else { + // transaction failed, refund account + econ.depositPlayer(from.getAccountId(), amount); + } + } + + // if we get here something with the transaction failed if (notify) - sendTransferInfo(invoker, from, to, amount); - - return true; + invoker.msg("Unable to transfer %s to %s from %s.", moneyString(amount), to.describeTo(invoker), from.describeTo(invoker, true)); + + return false; } public static Set getFplayers(EconomyParticipator ep) @@ -245,22 +256,28 @@ public class Econ if (delta > 0) { // The player should gain money - // There is no risk of failure - econ.depositPlayer(acc, delta); - modifyUniverseMoney(-delta); - if (forDoingThis != null && !forDoingThis.isEmpty()) - ep.msg("%s gained %s %s.", You, moneyString(delta), forDoingThis); - return true; + // The account might not have enough space + EconomyResponse er = econ.depositPlayer(acc, delta); + if (er.transactionSuccess()) { + modifyUniverseMoney(-delta); + if (forDoingThis != null && !forDoingThis.isEmpty()) + ep.msg("%s gained %s %s.", You, moneyString(delta), forDoingThis); + return true; + } else { + // transfer to account failed + if (forDoingThis != null && !forDoingThis.isEmpty()) + ep.msg("%s would have gained %s %s, but the deposit failed.", You, moneyString(delta), forDoingThis); + return false; + } } else { // The player should loose money // The player might not have enough. - if (econ.has(acc, -delta)) + if (econ.has(acc, -delta) && econ.withdrawPlayer(acc, -delta).transactionSuccess()) { // There is enough money to pay - econ.withdrawPlayer(acc, -delta); modifyUniverseMoney(-delta); if (forDoingThis != null && !forDoingThis.isEmpty()) ep.msg("%s lost %s %s.", You, moneyString(-delta), forDoingThis);