From ff3219ea6ed6de4a53a2f056739be1db960bdec3 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Fri, 26 Apr 2013 08:03:22 +0200 Subject: [PATCH] Money, ensure accounts exist before using them. If they are not players set balance to zero. --- src/com/massivecraft/mcore/money/Money.java | 2 +- .../mcore/money/MoneyMixinVault.java | 46 ++++++++++++++----- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/com/massivecraft/mcore/money/Money.java b/src/com/massivecraft/mcore/money/Money.java index bd56681d..89b499be 100644 --- a/src/com/massivecraft/mcore/money/Money.java +++ b/src/com/massivecraft/mcore/money/Money.java @@ -10,7 +10,7 @@ public class Money private static MoneyMixin mixin = null; public static MoneyMixin mixin() { return mixin; }; - public static void mixin(MoneyMixin newMixin) { mixin = newMixin; } + public static void mixin(MoneyMixin mixin) { Money.mixin = mixin; } // -------------------------------------------- // // EXTRACT diff --git a/src/com/massivecraft/mcore/money/MoneyMixinVault.java b/src/com/massivecraft/mcore/money/MoneyMixinVault.java index 5d648239..b9b7feaf 100644 --- a/src/com/massivecraft/mcore/money/MoneyMixinVault.java +++ b/src/com/massivecraft/mcore/money/MoneyMixinVault.java @@ -3,6 +3,8 @@ package com.massivecraft.mcore.money; import org.bukkit.Bukkit; import org.bukkit.plugin.RegisteredServiceProvider; +import com.massivecraft.mcore.util.MUtil; + import net.milkbowl.vault.economy.Economy; @@ -83,7 +85,7 @@ public class MoneyMixinVault extends MoneyMixinAbstract @Override public boolean create(String universe, String accountId) { - return this.economy.createPlayerAccount(accountId, universe); + return this.ensureExists(universe, accountId); } // -------------------------------------------- // @@ -92,13 +94,17 @@ public class MoneyMixinVault extends MoneyMixinAbstract @Override public double get(String universe, String accountId) - { + { + this.ensureExists(universe, accountId); + return this.economy.getBalance(accountId, universe); } @Override public boolean has(String universe, String accountId, double amount) - { + { + this.ensureExists(universe, accountId); + return this.economy.has(accountId, universe, amount); } @@ -116,21 +122,39 @@ public class MoneyMixinVault extends MoneyMixinAbstract @Override public boolean add(String universe, String accountId, double amount) { - if (amount < 0) - { - return subtract(universe, accountId, -amount); - } + if (amount < 0) return subtract(universe, accountId, -amount); + + this.ensureExists(universe, accountId); + return economy.depositPlayer(accountId, universe, amount).transactionSuccess(); } @Override public boolean subtract(String universe, String accountId, double amount) { - if (amount < 0) - { - return add(universe, accountId, -amount); - } + if (amount < 0) return add(universe, accountId, -amount); + + this.ensureExists(universe, accountId); + return economy.withdrawPlayer(accountId, universe, amount).transactionSuccess(); } + // -------------------------------------------- // + // UTIL + // -------------------------------------------- // + + public boolean ensureExists(String universe, String accountId) + { + if (this.economy.hasAccount(accountId, universe)) return true; + + if (!this.economy.createPlayerAccount(accountId, universe)) return false; + + if (MUtil.isValidPlayerName(accountId)) return true; + + double balance = this.economy.getBalance(accountId, universe); + economy.withdrawPlayer(accountId, universe, balance); + + return true; + } + } \ No newline at end of file