Money, ensure accounts exist before using them. If they are not players set balance to zero.

This commit is contained in:
Olof Larsson 2013-04-26 08:03:22 +02:00
parent 2c70489885
commit ff3219ea6e
2 changed files with 36 additions and 12 deletions

View File

@ -10,7 +10,7 @@ public class Money
private static MoneyMixin mixin = null; private static MoneyMixin mixin = null;
public static MoneyMixin mixin() { return mixin; }; public static MoneyMixin mixin() { return mixin; };
public static void mixin(MoneyMixin newMixin) { mixin = newMixin; } public static void mixin(MoneyMixin mixin) { Money.mixin = mixin; }
// -------------------------------------------- // // -------------------------------------------- //
// EXTRACT // EXTRACT

View File

@ -3,6 +3,8 @@ package com.massivecraft.mcore.money;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.RegisteredServiceProvider;
import com.massivecraft.mcore.util.MUtil;
import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.Economy;
@ -83,7 +85,7 @@ public class MoneyMixinVault extends MoneyMixinAbstract
@Override @Override
public boolean create(String universe, String accountId) public boolean create(String universe, String accountId)
{ {
return this.economy.createPlayerAccount(accountId, universe); return this.ensureExists(universe, accountId);
} }
// -------------------------------------------- // // -------------------------------------------- //
@ -93,12 +95,16 @@ public class MoneyMixinVault extends MoneyMixinAbstract
@Override @Override
public double get(String universe, String accountId) public double get(String universe, String accountId)
{ {
this.ensureExists(universe, accountId);
return this.economy.getBalance(accountId, universe); return this.economy.getBalance(accountId, universe);
} }
@Override @Override
public boolean has(String universe, String accountId, double amount) public boolean has(String universe, String accountId, double amount)
{ {
this.ensureExists(universe, accountId);
return this.economy.has(accountId, universe, amount); return this.economy.has(accountId, universe, amount);
} }
@ -116,21 +122,39 @@ public class MoneyMixinVault extends MoneyMixinAbstract
@Override @Override
public boolean add(String universe, String accountId, double amount) public boolean add(String universe, String accountId, double amount)
{ {
if (amount < 0) if (amount < 0) return subtract(universe, accountId, -amount);
{
return subtract(universe, accountId, -amount); this.ensureExists(universe, accountId);
}
return economy.depositPlayer(accountId, universe, amount).transactionSuccess(); return economy.depositPlayer(accountId, universe, amount).transactionSuccess();
} }
@Override @Override
public boolean subtract(String universe, String accountId, double amount) public boolean subtract(String universe, String accountId, double amount)
{ {
if (amount < 0) if (amount < 0) return add(universe, accountId, -amount);
{
return add(universe, accountId, -amount); this.ensureExists(universe, accountId);
}
return economy.withdrawPlayer(accountId, universe, amount).transactionSuccess(); 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;
}
} }