From 7b4b9e4e5dc4286e2aa0b5f97ce1b16546801dba Mon Sep 17 00:00:00 2001 From: Sleaker Date: Wed, 6 Jun 2012 06:20:04 -0700 Subject: [PATCH] added javadoc & missing negative fund checks to some economy implementations. --- README.md | 8 ++------ src/net/milkbowl/vault/economy/Economy.java | 19 ++++++++++++------ .../economy/plugins/Economy_Craftconomy.java | 18 ++++++++++++++++- .../economy/plugins/Economy_CurrencyCore.java | 20 +++++++++++++++++-- .../economy/plugins/Economy_Essentials.java | 8 ++++++++ .../economy/plugins/Economy_MineConomy.java | 2 +- .../economy/plugins/Economy_iConomy5.java | 8 ++++++++ .../economy/plugins/Economy_iConomy6.java | 16 +++++++++++++++ 8 files changed, 83 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index c8b51af..f4f72ae 100644 --- a/README.md +++ b/README.md @@ -110,11 +110,7 @@ Github and we'll get to it at our convenience. ## Implementing Vault Implementing Vault is quite simple through obtaining an instance through the -Bukkit PluginManager class by using the string "Vault". An example plugin with -limited functionality is located within the VaultExamplePlugin repository -(https://github.com/MilkBowl/VaultExamplePlugin). - -Example: +Bukkit PluginManager class by using the string "Vault". See the example below: ```java package com.example.plugin; @@ -190,7 +186,7 @@ public class ExamplePlugin extends JavaPlugin { if(command.getLabel().equals("test-economy")) { // Lets give the player 1.05 currency (note that SOME economic plugins require rounding! - sender.sendMessage(String.format("You have %s", vault.getEconomy().format(vault.getEconomy().getBalance(player.getName()).amount))); + sender.sendMessage(String.format("You have %s", econ.format(econ.getBalance(player.getName()).amount))); EconomyResponse r = econ.depositPlayer(player.getName(), 1.05); if(r.transactionSuccess()) { sender.sendMessage(String.format("You were given %s and now have %s", econ.format(r.amount), econ.format(r.balance))); diff --git a/src/net/milkbowl/vault/economy/Economy.java b/src/net/milkbowl/vault/economy/Economy.java index cca5eff..537c1ac 100644 --- a/src/net/milkbowl/vault/economy/Economy.java +++ b/src/net/milkbowl/vault/economy/Economy.java @@ -86,14 +86,17 @@ public interface Economy { public double getBalance(String playerName); /** - * Checks if the player account has the amount + * Checks if the player account has the amount - DO NOT USE NEGATIVE AMOUNTS + * * @param playerName * @param amount * @return */ public boolean has(String playerName, double amount); + /** - * Withdraw an amount from a player + * Withdraw an amount from a player - DO NOT USE NEGATIVE AMOUNTS + * * @param playerName Name of player * @param amount Amount to withdraw * @return Detailed response of transaction @@ -101,7 +104,8 @@ public interface Economy { public EconomyResponse withdrawPlayer(String playerName, double amount); /** - * Deposit an amount to a player + * Deposit an amount to a player - DO NOT USE NEGATIVE AMOUNTS + * * @param playerName Name of player * @param amount Amount to deposit * @return Detailed response of transaction @@ -131,7 +135,8 @@ public interface Economy { public EconomyResponse bankBalance(String name); /** - * Returns true or false whether the bank has the amount specified + * Returns true or false whether the bank has the amount specified - DO NOT USE NEGATIVE AMOUNTS + * * @param name * @param amount * @return @@ -139,7 +144,8 @@ public interface Economy { public EconomyResponse bankHas(String name, double amount); /** - * Withdraw an amount from a bank account + * Withdraw an amount from a bank account - DO NOT USE NEGATIVE AMOUNTS + * * @param name * @param amount * @return @@ -147,7 +153,8 @@ public interface Economy { public EconomyResponse bankWithdraw(String name, double amount); /** - * Deposit an amount into a bank account + * Deposit an amount into a bank account - DO NOT USE NEGATIVE AMOUNTS + * * @param name * @param amount * @return diff --git a/src/net/milkbowl/vault/economy/plugins/Economy_Craftconomy.java b/src/net/milkbowl/vault/economy/plugins/Economy_Craftconomy.java index 907308f..d0d3a25 100644 --- a/src/net/milkbowl/vault/economy/plugins/Economy_Craftconomy.java +++ b/src/net/milkbowl/vault/economy/plugins/Economy_Craftconomy.java @@ -113,7 +113,7 @@ public class Economy_Craftconomy implements Economy { public String currencyNameSingular() { return CurrencyHandler.getCurrency(Config.currencyDefault, true).getName(); } - + @Override public String currencyNamePlural() { return CurrencyHandler.getCurrency(Config.currencyDefault, true).getNamePlural(); @@ -130,6 +130,10 @@ public class Economy_Craftconomy implements Economy { @Override public EconomyResponse withdrawPlayer(String playerName, double amount) { + if (amount < 0) { + return new EconomyResponse(0, getBalance(playerName), ResponseType.FAILURE, "Cannot withdraw negative funds"); + } + double balance; Account account = AccountHandler.getAccount(playerName); if (account.hasEnough(amount)) { @@ -142,6 +146,10 @@ public class Economy_Craftconomy implements Economy { @Override public EconomyResponse depositPlayer(String playerName, double amount) { + if (amount < 0) { + return new EconomyResponse(0, getBalance(playerName), ResponseType.FAILURE, "Cannot desposit negative funds"); + } + Account account = AccountHandler.getAccount(playerName); account.addMoney(amount); return new EconomyResponse(amount, account.getDefaultBalance(), ResponseType.SUCCESS, null); @@ -188,6 +196,10 @@ public class Economy_Craftconomy implements Economy { @Override public EconomyResponse bankWithdraw(String name, double amount) { + if (amount < 0) { + return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot withdraw negative funds"); + } + EconomyResponse er = bankHas(name, amount); if (!er.transactionSuccess()) return er; @@ -206,6 +218,10 @@ public class Economy_Craftconomy implements Economy { @Override public EconomyResponse bankDeposit(String name, double amount) { + if (amount < 0) { + return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot desposit negative funds"); + } + if (BankHandler.exists(name)) { Bank bank = BankHandler.getBank(name); diff --git a/src/net/milkbowl/vault/economy/plugins/Economy_CurrencyCore.java b/src/net/milkbowl/vault/economy/plugins/Economy_CurrencyCore.java index b0f1781..ba6587d 100644 --- a/src/net/milkbowl/vault/economy/plugins/Economy_CurrencyCore.java +++ b/src/net/milkbowl/vault/economy/plugins/Economy_CurrencyCore.java @@ -98,12 +98,12 @@ public class Economy_CurrencyCore implements Economy { public String format(double amount) { return this.currency.getFormatHelper().format(amount); } - + @Override public String currencyNamePlural() { return currency.getCurrencyConfig().getCurrencyMajor().get(1); } - + @Override public String currencyNameSingular() { return currency.getCurrencyConfig().getCurrencyMajor().get(0); @@ -129,6 +129,10 @@ public class Economy_CurrencyCore implements Economy { @Override public EconomyResponse withdrawPlayer(String playerName, double amount) { + if (amount < 0) { + return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot withdraw negative funds"); + } + AccountContext account = this.currency.getAccountManager().getAccount(playerName); if (account == null) { return new EconomyResponse(0.0, 0.0, ResponseType.FAILURE, "That account does not exist"); @@ -142,6 +146,10 @@ public class Economy_CurrencyCore implements Economy { @Override public EconomyResponse depositPlayer(String playerName, double amount) { + if (amount < 0) { + return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot desposit negative funds"); + } + AccountContext account = this.currency.getAccountManager().getAccount(playerName); if (account == null) { return new EconomyResponse(0.0, 0.0, ResponseType.FAILURE, "That account does not exist"); @@ -192,6 +200,10 @@ public class Economy_CurrencyCore implements Economy { @Override public EconomyResponse bankWithdraw(String name, double amount) { + if (amount < 0) { + return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot withdraw negative funds"); + } + AccountContext account = this.currency.getAccountManager().getAccount(name); if (account == null) { return new EconomyResponse(0, 0, ResponseType.FAILURE, "That account does not exist!"); @@ -205,6 +217,10 @@ public class Economy_CurrencyCore implements Economy { @Override public EconomyResponse bankDeposit(String name, double amount) { + if (amount < 0) { + return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot desposit negative funds"); + } + AccountContext account = this.currency.getAccountManager().getAccount(name); if (account == null) { return new EconomyResponse(0, 0, ResponseType.FAILURE, "That account does not exist!"); diff --git a/src/net/milkbowl/vault/economy/plugins/Economy_Essentials.java b/src/net/milkbowl/vault/economy/plugins/Economy_Essentials.java index 728adb3..2229eb5 100644 --- a/src/net/milkbowl/vault/economy/plugins/Economy_Essentials.java +++ b/src/net/milkbowl/vault/economy/plugins/Economy_Essentials.java @@ -94,6 +94,10 @@ public class Economy_Essentials implements Economy { @Override public EconomyResponse withdrawPlayer(String playerName, double amount) { + if (amount < 0) { + return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot withdraw negative funds"); + } + double balance; EconomyResponse.ResponseType type; String errorMessage = null; @@ -130,6 +134,10 @@ public class Economy_Essentials implements Economy { @Override public EconomyResponse depositPlayer(String playerName, double amount) { + if (amount < 0) { + return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot desposit negative funds"); + } + double balance; EconomyResponse.ResponseType type; String errorMessage = null; diff --git a/src/net/milkbowl/vault/economy/plugins/Economy_MineConomy.java b/src/net/milkbowl/vault/economy/plugins/Economy_MineConomy.java index 56d284a..f1acd85 100644 --- a/src/net/milkbowl/vault/economy/plugins/Economy_MineConomy.java +++ b/src/net/milkbowl/vault/economy/plugins/Economy_MineConomy.java @@ -129,7 +129,7 @@ public class Economy_MineConomy implements Economy { } @Override - public EconomyResponse withdrawPlayer(String playerName, double amount) { + public EconomyResponse withdrawPlayer(String playerName, double amount) { double balance; try { balance = MCCom.getBalance(playerName); diff --git a/src/net/milkbowl/vault/economy/plugins/Economy_iConomy5.java b/src/net/milkbowl/vault/economy/plugins/Economy_iConomy5.java index f1aff95..3ed17a7 100644 --- a/src/net/milkbowl/vault/economy/plugins/Economy_iConomy5.java +++ b/src/net/milkbowl/vault/economy/plugins/Economy_iConomy5.java @@ -81,6 +81,10 @@ public class Economy_iConomy5 implements Economy { @Override public EconomyResponse withdrawPlayer(String playerName, double amount) { + if (amount < 0) { + return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot withdraw negative funds"); + } + Holdings holdings = iConomy.getAccount(playerName).getHoldings(); if (holdings.hasEnough(amount)) { holdings.subtract(amount); @@ -92,6 +96,10 @@ public class Economy_iConomy5 implements Economy { @Override public EconomyResponse depositPlayer(String playerName, double amount) { + if (amount < 0) { + return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot desposit negative funds"); + } + Holdings holdings = iConomy.getAccount(playerName).getHoldings(); holdings.add(amount); return new EconomyResponse(amount, holdings.balance(), EconomyResponse.ResponseType.SUCCESS, null); diff --git a/src/net/milkbowl/vault/economy/plugins/Economy_iConomy6.java b/src/net/milkbowl/vault/economy/plugins/Economy_iConomy6.java index b629af3..4eb2d57 100644 --- a/src/net/milkbowl/vault/economy/plugins/Economy_iConomy6.java +++ b/src/net/milkbowl/vault/economy/plugins/Economy_iConomy6.java @@ -132,6 +132,10 @@ public class Economy_iConomy6 implements Economy { @Override public EconomyResponse withdrawPlayer(String playerName, double amount) { + if (amount < 0) { + return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot withdraw negative funds"); + } + Holdings holdings = accounts.get(playerName).getHoldings(); if (holdings.hasEnough(amount)) { holdings.subtract(amount); @@ -143,6 +147,10 @@ public class Economy_iConomy6 implements Economy { @Override public EconomyResponse depositPlayer(String playerName, double amount) { + if (amount < 0) { + return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot desposit negative funds"); + } + Holdings holdings = accounts.get(playerName).getHoldings(); holdings.add(amount); return new EconomyResponse(amount, holdings.getBalance(), ResponseType.SUCCESS, null); @@ -187,11 +195,19 @@ public class Economy_iConomy6 implements Economy { @Override public EconomyResponse bankWithdraw(String name, double amount) { + if (amount < 0) { + return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot withdraw negative funds"); + } + return withdrawPlayer(name, amount); } @Override public EconomyResponse bankDeposit(String name, double amount) { + if (amount < 0) { + return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot desposit negative funds"); + } + return depositPlayer(name, amount); }