From 09d610279882fadc3e702671d4220564591fe754 Mon Sep 17 00:00:00 2001 From: VirtualByte Date: Tue, 5 Aug 2014 12:23:21 +0100 Subject: [PATCH] Fixed economy issues --- pom.xml | 2 +- .../factions/integration/Econ.java | 95 ++++++++++++++----- 2 files changed, 74 insertions(+), 23 deletions(-) diff --git a/pom.xml b/pom.xml index 302244b0..b0a2abfc 100644 --- a/pom.xml +++ b/pom.xml @@ -52,7 +52,7 @@ net.daboross.vault vault-api - 1.2.27 + 1.3.01 provided diff --git a/src/main/java/com/massivecraft/factions/integration/Econ.java b/src/main/java/com/massivecraft/factions/integration/Econ.java index 4c77e024..dc0370e6 100644 --- a/src/main/java/com/massivecraft/factions/integration/Econ.java +++ b/src/main/java/com/massivecraft/factions/integration/Econ.java @@ -8,13 +8,14 @@ import com.massivecraft.factions.util.RelationUtil; import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.EconomyResponse; import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; import org.bukkit.plugin.RegisteredServiceProvider; import java.util.HashSet; import java.util.Set; +import java.util.UUID; import java.util.logging.Level; - public class Econ { private static Economy econ = null; @@ -148,8 +149,23 @@ public class Econ { return false; } + OfflinePlayer fromAcc; + OfflinePlayer toAcc; + + if(isUUID(from.getAccountId())) { + fromAcc = Bukkit.getOfflinePlayer(UUID.fromString(from.getAccountId())); + } else { + fromAcc = Bukkit.getOfflinePlayer(from.getAccountId()); + } + + if(isUUID(to.getAccountId())) { + toAcc = Bukkit.getOfflinePlayer(UUID.fromString(to.getAccountId())); + } else { + toAcc = Bukkit.getOfflinePlayer(to.getAccountId()); + } + // Is there enough money for the transaction to happen? - if (!econ.has(from.getAccountId(), amount)) { + if (!econ.has(fromAcc, amount)) { // There was not enough money to pay if (invoker != null && notify) { invoker.msg("%s can't afford to transfer %s to %s.", from.describeTo(invoker, true), moneyString(amount), to.describeTo(invoker)); @@ -159,10 +175,10 @@ public class Econ { } // Transfer money - EconomyResponse erw = econ.withdrawPlayer(from.getAccountId(), amount); + EconomyResponse erw = econ.withdrawPlayer(fromAcc, amount); if (erw.transactionSuccess()) { - EconomyResponse erd = econ.depositPlayer(to.getAccountId(), amount); + EconomyResponse erd = econ.depositPlayer(toAcc, amount); if (erd.transactionSuccess()) { if (notify) { sendTransferInfo(invoker, from, to, amount); @@ -170,7 +186,7 @@ public class Econ { return true; } else { // transaction failed, refund account - econ.depositPlayer(from.getAccountId(), amount); + econ.depositPlayer(fromAcc, amount); } } @@ -185,12 +201,12 @@ public class Econ { public static Set getFplayers(EconomyParticipator ep) { Set fplayers = new HashSet(); - if (ep == null) { - // Add nothing - } else if (ep instanceof FPlayer) { - fplayers.add((FPlayer) ep); - } else if (ep instanceof Faction) { - fplayers.addAll(((Faction) ep).getFPlayers()); + if (ep != null) { + if (ep instanceof FPlayer) { + fplayers.add((FPlayer) ep); + } else if (ep instanceof Faction) { + fplayers.addAll(((Faction) ep).getFPlayers()); + } } return fplayers; @@ -226,7 +242,21 @@ public class Econ { return true; } - if (!econ.has(ep.getAccountId(), delta)) { + // going the hard way round as econ.has refuses to work. + boolean affordable = false; + double currentBalance; + + if(isUUID(ep.getAccountId())) { + currentBalance = econ.getBalance(Bukkit.getOfflinePlayer(UUID.fromString(ep.getAccountId()))); + } else { + currentBalance = econ.getBalance(Bukkit.getOfflinePlayer(ep.getAccountId())); + } + + if(currentBalance >= delta) { + affordable = true; + } + + if (!affordable) { if (toDoThis != null && !toDoThis.isEmpty()) { ep.msg("%s can't afford %s %s.", ep.describeTo(ep, true), moneyString(delta), toDoThis); } @@ -240,7 +270,14 @@ public class Econ { return false; } - String acc = ep.getAccountId(); + OfflinePlayer acc; + + if(isUUID(ep.getAccountId())) { + acc = Bukkit.getOfflinePlayer(UUID.fromString(ep.getAccountId())); + } else { + acc = Bukkit.getOfflinePlayer(ep.getAccountId()); + } + String You = ep.describeTo(ep, true); if (delta == 0) { @@ -340,35 +377,49 @@ public class Econ { // -------------------------------------------- // public static boolean hasAccount(String name) { - return econ.hasAccount(name); + return econ.hasAccount(Bukkit.getOfflinePlayer(name)); } public static double getBalance(String account) { - return econ.getBalance(account); + return econ.getBalance(Bukkit.getOfflinePlayer(account)); } public static boolean setBalance(String account, double amount) { - double current = econ.getBalance(account); + double current = econ.getBalance(Bukkit.getOfflinePlayer(account)); if (current > amount) { - return econ.withdrawPlayer(account, current - amount).transactionSuccess(); + return econ.withdrawPlayer(Bukkit.getOfflinePlayer(account), current - amount).transactionSuccess(); } else { - return econ.depositPlayer(account, amount - current).transactionSuccess(); + return econ.depositPlayer(Bukkit.getOfflinePlayer(account), amount - current).transactionSuccess(); } } public static boolean modifyBalance(String account, double amount) { if (amount < 0) { - return econ.withdrawPlayer(account, -amount).transactionSuccess(); + return econ.withdrawPlayer(Bukkit.getOfflinePlayer(account), -amount).transactionSuccess(); } else { - return econ.depositPlayer(account, amount).transactionSuccess(); + return econ.depositPlayer(Bukkit.getOfflinePlayer(account), amount).transactionSuccess(); } } public static boolean deposit(String account, double amount) { - return econ.depositPlayer(account, amount).transactionSuccess(); + return econ.depositPlayer(Bukkit.getOfflinePlayer(account), amount).transactionSuccess(); } public static boolean withdraw(String account, double amount) { - return econ.withdrawPlayer(account, amount).transactionSuccess(); + return econ.withdrawPlayer(Bukkit.getOfflinePlayer(account), amount).transactionSuccess(); } + + // --------------------------------------- + // Helpful Utilities + // --------------------------------------- + + public static boolean isUUID(String uuid) { + try { + UUID.fromString(uuid); + } catch(IllegalArgumentException ex) { + return false; + } + + return true; + } }