Version bump 0.14.0. Make the API needlessly more complex to try and fix some bugs.

This commit is contained in:
AppleDash 2018-05-25 02:27:02 -04:00
parent 6be741b047
commit 867535bb4d
17 changed files with 47 additions and 77 deletions

View File

@ -9,7 +9,7 @@
<version>0</version>
</parent>
<artifactId>SaneEconomyCore</artifactId>
<version>0.13.1-SNAPSHOT</version>
<version>0.14.0-SNAPSHOT</version>
<dependencies>
<dependency>

View File

@ -81,7 +81,7 @@ public class EconomyAdminCommand extends SaneCommand {
}
if (subCommand.equalsIgnoreCase("give")) {
Transaction transaction = new Transaction(Economable.wrap(sender), Economable.wrap(targetPlayer), amount, TransactionReason.ADMIN_GIVE);
Transaction transaction = new Transaction(ecoMan.getCurrency(), Economable.wrap(sender), Economable.wrap(targetPlayer), amount, TransactionReason.ADMIN_GIVE);
TransactionResult result = ecoMan.transact(transaction);
double newAmount = result.getToBalance();
@ -95,7 +95,7 @@ public class EconomyAdminCommand extends SaneCommand {
}
if (subCommand.equalsIgnoreCase("take")) {
Transaction transaction = new Transaction(Economable.wrap(targetPlayer), Economable.wrap(sender), amount, TransactionReason.ADMIN_TAKE);
Transaction transaction = new Transaction(ecoMan.getCurrency(), Economable.wrap(targetPlayer), Economable.wrap(sender), amount, TransactionReason.ADMIN_TAKE);
TransactionResult result = ecoMan.transact(transaction);
double newAmount = result.getFromBalance();
@ -117,12 +117,12 @@ public class EconomyAdminCommand extends SaneCommand {
// FIXME: This is a silly hack to get it to log.
if (oldBal > 0.0) {
logger.logTransaction(new Transaction(
economable, Economable.CONSOLE, oldBal, TransactionReason.ADMIN_TAKE
ecoMan.getCurrency(), economable, Economable.CONSOLE, oldBal, TransactionReason.ADMIN_TAKE
));
}
logger.logTransaction(new Transaction(
Economable.CONSOLE, economable, amount, TransactionReason.ADMIN_GIVE
ecoMan.getCurrency(), Economable.CONSOLE, economable, amount, TransactionReason.ADMIN_GIVE
));
});

View File

@ -75,7 +75,7 @@ public class PayCommand extends SaneCommand {
}
/* Perform the actual transfer. False == They didn't have enough money */
Transaction transaction = new Transaction(Economable.wrap(fromPlayer), Economable.wrap(toPlayer), amount, TransactionReason.PLAYER_PAY);
Transaction transaction = new Transaction(ecoMan.getCurrency(), Economable.wrap(fromPlayer), Economable.wrap(toPlayer), amount, TransactionReason.PLAYER_PAY);
TransactionResult result = ecoMan.transact(transaction);
if (result.getStatus() != TransactionResult.Status.SUCCESS) {

View File

@ -87,53 +87,27 @@ public class EconomyManager {
/**
* Add to a player's balance.
* This does not filter the amount.
* @param targetPlayer Player to add to
* @param amount Amount to add
* @throws IllegalArgumentException If amount is negative
*/
private void addBalance(Economable targetPlayer, double amount) {
amount = NumberUtils.filterAmount(currency, amount);
if (amount < 0) {
throw new IllegalArgumentException("Cannot add a negative amount!");
}
if (targetPlayer == Economable.CONSOLE) {
return;
}
double newAmount = backend.getBalance(targetPlayer) + amount;
setBalance(targetPlayer, newAmount);
setBalance(targetPlayer, backend.getBalance(targetPlayer) + amount);
}
/**
* Subtract from a player's balance.
* If the subtraction would result in a negative balance, the balance is instead set to 0.
* This does not filter the amount.
*
* @param targetPlayer Player to subtract from
* @param amount Amount to subtract
* @throws IllegalArgumentException If amount is negative
*/
private void subtractBalance(Economable targetPlayer, double amount) {
amount = NumberUtils.filterAmount(currency, amount);
if (amount < 0) {
throw new IllegalArgumentException("Cannot subtract a negative amount!");
}
if (targetPlayer == Economable.CONSOLE) {
return;
}
double newAmount = backend.getBalance(targetPlayer) - amount;
/* Subtracting that much would result in a negative balance - don't do that */
if (newAmount <= 0.0D) {
newAmount = 0.0D;
}
setBalance(targetPlayer, newAmount);
// Ensure we don't go negative.
setBalance(targetPlayer, Math.max(0.0, backend.getBalance(targetPlayer) - amount));
}
/**
@ -145,10 +119,6 @@ public class EconomyManager {
public void setBalance(Economable targetPlayer, double amount) {
amount = NumberUtils.filterAmount(currency, amount);
if (amount < 0) {
throw new IllegalArgumentException("Cannot subtract a negative amount!");
}
if (targetPlayer == Economable.CONSOLE) {
return;
}
@ -164,9 +134,9 @@ public class EconomyManager {
public TransactionResult transact(Transaction transaction) {
Economable sender = transaction.getSender();
Economable receiver = transaction.getReceiver();
double amount = transaction.getAmount(); // This amount is validated upon creation of Transaction
double amount = transaction.getAmount(); // This amount is validated and filtered upon creation of Transaction
if (Bukkit.getServer().getPluginManager() != null) { // Bukkit.getServer() == null from our JUnit tests.
if (Bukkit.getServer().getPluginManager() != null) { // Bukkit.getServer().getPluginManager() == null from our JUnit tests.
SaneEconomyTransactionEvent evt = new SaneEconomyTransactionEvent(transaction);
Bukkit.getServer().getPluginManager().callEvent(evt);
if (evt.isCancelled()) {

View File

@ -1,7 +1,9 @@
package org.appledash.saneeconomy.economy.transaction;
import org.appledash.saneeconomy.economy.Currency;
import org.appledash.saneeconomy.economy.economable.Economable;
import org.appledash.saneeconomy.economy.transaction.TransactionReason.AffectedParties;
import org.appledash.saneeconomy.utils.NumberUtils;
/**
* Created by appledash on 9/21/16.
@ -13,14 +15,15 @@ public class Transaction {
private final double amount;
private final TransactionReason reason;
public Transaction(Economable sender, Economable receiver, double amount, TransactionReason reason) {
public Transaction(Currency currency, Economable sender, Economable receiver, double amount, TransactionReason reason) {
if (amount <= 0.0) {
throw new IllegalArgumentException("Cannot transact a zero or negative amount!");
}
this.sender = sender;
this.receiver = receiver;
this.amount = amount;
this.amount = NumberUtils.filterAmount(currency, amount);
this.reason = reason;
}

View File

@ -34,7 +34,7 @@ public class JoinQuitListener implements Listener {
/* A starting balance is configured AND they haven't been given it yet. */
if ((startBalance > 0) && !plugin.getEconomyManager().accountExists(economable)) {
plugin.getEconomyManager().transact(new Transaction(
Economable.CONSOLE, economable, startBalance, TransactionReason.STARTING_BALANCE
plugin.getEconomyManager().getCurrency(), Economable.CONSOLE, economable, startBalance, TransactionReason.STARTING_BALANCE
));
if (plugin.getConfig().getBoolean("economy.notify-start-balance", true)) {
this.plugin.getMessenger().sendMessage(player, "You've been issued a starting balance of {1}!", plugin.getEconomyManager().getCurrency().formatAmount(startBalance));

View File

@ -45,7 +45,7 @@ public class NumberUtils {
public static double filterAmount(Currency currency, double amount) {
try {
return NumberFormat.getInstance().parse(currency.getFormat().format(amount)).doubleValue();
return NumberFormat.getInstance().parse(currency.getFormat().format(Math.abs(amount))).doubleValue();
} catch (ParseException e) {
throw new NumberFormatException();
}

View File

@ -120,7 +120,7 @@ public class EconomySaneEconomy implements Economy {
}
return transact(new Transaction(
makeEconomable(target), Economable.PLUGIN, amount, TransactionReason.PLUGIN_TAKE
SaneEconomy.getInstance().getEconomyManager().getCurrency(), makeEconomable(target), Economable.PLUGIN, amount, TransactionReason.PLUGIN_TAKE
));
}
@ -135,7 +135,7 @@ public class EconomySaneEconomy implements Economy {
}
return transact(new Transaction(
Economable.wrap(offlinePlayer), Economable.PLUGIN, amount, TransactionReason.PLUGIN_TAKE
SaneEconomy.getInstance().getEconomyManager().getCurrency(), Economable.wrap(offlinePlayer), Economable.PLUGIN, amount, TransactionReason.PLUGIN_TAKE
));
}
@ -156,7 +156,7 @@ public class EconomySaneEconomy implements Economy {
}
return transact(new Transaction(
Economable.PLUGIN, makeEconomable(target), amount, TransactionReason.PLUGIN_GIVE
SaneEconomy.getInstance().getEconomyManager().getCurrency(), Economable.PLUGIN, makeEconomable(target), amount, TransactionReason.PLUGIN_GIVE
));
}
@ -167,7 +167,7 @@ public class EconomySaneEconomy implements Economy {
}
return transact(new Transaction(
Economable.PLUGIN, Economable.wrap(offlinePlayer), v, TransactionReason.PLUGIN_GIVE
SaneEconomy.getInstance().getEconomyManager().getCurrency(), Economable.PLUGIN, Economable.wrap(offlinePlayer), v, TransactionReason.PLUGIN_GIVE
));
}

View File

@ -56,23 +56,23 @@ public class EconomyManagerTest {
Assert.assertEquals(0.0, economyManager.getBalance(playerTwo), 0.0);
// One should be able to transfer to two
Assert.assertTrue(economyManager.transact(new Transaction(playerOne, playerTwo, 50.0, TransactionReason.PLAYER_PAY)).getStatus() == TransactionResult.Status.SUCCESS);
Assert.assertTrue(economyManager.transact(new Transaction(economyManager.getCurrency(), playerOne, playerTwo, 50.0, TransactionReason.PLAYER_PAY)).getStatus() == TransactionResult.Status.SUCCESS);
// One should now have only 50 left, two should have 50 now
Assert.assertEquals(50.0, economyManager.getBalance(playerOne), 0.0);
Assert.assertEquals(50.0, economyManager.getBalance(playerTwo), 0.0);
Assert.assertEquals("Player one should have 50 dollars", 50.0, economyManager.getBalance(playerOne), 0.0);
Assert.assertEquals("Player two should have 50 dollars", 50.0, economyManager.getBalance(playerTwo), 0.0);
// Ensure that balance addition and subtraction works...
Assert.assertEquals(25.0, economyManager.transact(
new Transaction(playerOne, Economable.CONSOLE, 25.0, TransactionReason.TEST_TAKE)
new Transaction(economyManager.getCurrency(), playerOne, Economable.CONSOLE, 25.0, TransactionReason.TEST_TAKE)
).getFromBalance(), 0.0);
Assert.assertEquals(50.0, economyManager.transact(
new Transaction(Economable.CONSOLE, playerOne, 25.0, TransactionReason.TEST_GIVE)
new Transaction(economyManager.getCurrency(), Economable.CONSOLE, playerOne, 25.0, TransactionReason.TEST_GIVE)
).getToBalance(), 0.0);
Assert.assertEquals(TransactionResult.Status.ERR_NOT_ENOUGH_FUNDS, economyManager.transact(
new Transaction(playerTwo, Economable.CONSOLE, Double.MAX_VALUE, TransactionReason.TEST_TAKE)
new Transaction(economyManager.getCurrency(), playerTwo, Economable.CONSOLE, Double.MAX_VALUE, TransactionReason.TEST_TAKE)
).getStatus());
// Ensure that hasBalance works
@ -115,11 +115,4 @@ public class EconomyManagerTest {
return true;
}
@Test(expected = IllegalArgumentException.class)
public void testNegativeBalance() {
Economable economable = Economable.wrap(new MockOfflinePlayer("Bob"));
economyManager.setBalance(economable, -1.0);
}
}

View File

@ -16,7 +16,7 @@
<dependency>
<groupId>org.appledash</groupId>
<artifactId>SaneEconomyCore</artifactId>
<version>0.13.1-SNAPSHOT</version>
<version>0.14.0-SNAPSHOT</version>
</dependency>
</dependencies>

View File

@ -87,7 +87,7 @@ public class EntityDamageListener implements Listener {
}
plugin.getSaneEconomy().getEconomyManager().transact(new Transaction(
Economable.PLUGIN, Economable.wrap(offlinePlayer), thisAmount, TransactionReason.PLUGIN_GIVE
this.plugin.getSaneEconomy().getEconomyManager().getCurrency(), Economable.PLUGIN, Economable.wrap(offlinePlayer), thisAmount, TransactionReason.PLUGIN_GIVE
));
}

View File

@ -17,7 +17,7 @@
<dependency>
<groupId>org.appledash</groupId>
<artifactId>SaneEconomyCore</artifactId>
<version>0.13.1-SNAPSHOT</version>
<version>0.14.0-SNAPSHOT</version>
</dependency>
</dependencies>

View File

@ -52,7 +52,7 @@ public class SaneEconomyOnlineTime extends SanePlugin implements Listener {
this.reportingAmounts.put(player.getUniqueId(), payout.getAmount());
}
this.saneEconomy.getEconomyManager().transact(new Transaction(Economable.PLUGIN, Economable.wrap(player), payout.getAmount(), TransactionReason.PLUGIN_GIVE));
this.saneEconomy.getEconomyManager().transact(new Transaction(this.saneEconomy.getEconomyManager().getCurrency(), Economable.PLUGIN, Economable.wrap(player), payout.getAmount(), TransactionReason.PLUGIN_GIVE));
}
if ((onlineSeconds % payout.getReportInterval()) == 0) {

View File

@ -16,7 +16,7 @@
<dependency>
<groupId>org.appledash</groupId>
<artifactId>SaneEconomyCore</artifactId>
<version>0.13.1-SNAPSHOT</version>
<version>0.14.0-SNAPSHOT</version>
</dependency>
</dependencies>

View File

@ -84,7 +84,7 @@ public class InteractListener implements Listener {
EconomyManager ecoMan = plugin.getSaneEconomy().getEconomyManager();
int quantity = player.isSneaking() ? 1 : shop.getQuantity();
ShopTransaction shopTransaction = shop.makeTransaction(player, TransactionDirection.BUY, quantity);
ShopTransaction shopTransaction = shop.makeTransaction(ecoMan.getCurrency(), player, TransactionDirection.BUY, quantity);
/* No buy limits for now!
if (!plugin.getLimitManager().shouldAllowTransaction(shopTransaction)) {
@ -121,7 +121,7 @@ public class InteractListener implements Listener {
return;
}
ShopTransaction shopTransaction = shop.makeTransaction(player, TransactionDirection.SELL, quantity);
ShopTransaction shopTransaction = shop.makeTransaction(ecoMan.getCurrency(), player, TransactionDirection.SELL, quantity);
if (!plugin.getLimitManager().shouldAllowTransaction(shopTransaction)) {
this.plugin.getMessenger().sendMessage(player, "You have reached your selling limit for the time being. Try back in an hour or so.");

View File

@ -1,5 +1,6 @@
package org.appledash.saneeconomysignshop.signshop;
import org.appledash.saneeconomy.economy.Currency;
import org.appledash.saneeconomy.economy.economable.Economable;
import org.appledash.saneeconomy.economy.transaction.Transaction;
import org.appledash.saneeconomy.economy.transaction.TransactionReason;
@ -11,6 +12,7 @@ import org.bukkit.entity.Player;
* Blackjack is still best pony.
*/
public class ShopTransaction {
private final Currency currency;
// Direction is always what the player is doing. BUY = player is buying from shop.
private final TransactionDirection direction;
private final Player player;
@ -18,7 +20,8 @@ public class ShopTransaction {
private final int quantity;
private final double price;
public ShopTransaction(TransactionDirection direction, Player player, ItemInfo item, int quantity, double price) {
public ShopTransaction(Currency currency, TransactionDirection direction, Player player, ItemInfo item, int quantity, double price) {
this.currency = currency;
this.direction = direction;
this.player = player;
this.item = item;
@ -48,9 +51,9 @@ public class ShopTransaction {
public Transaction makeEconomyTransaction() {
if (direction == TransactionDirection.BUY) {
return new Transaction(Economable.wrap(player), Economable.PLUGIN, price, TransactionReason.PLUGIN_TAKE);
return new Transaction(this.currency, Economable.wrap(player), Economable.PLUGIN, price, TransactionReason.PLUGIN_TAKE);
} else {
return new Transaction(Economable.PLUGIN, Economable.wrap(player), price, TransactionReason.PLUGIN_GIVE);
return new Transaction(this.currency, Economable.PLUGIN, Economable.wrap(player), price, TransactionReason.PLUGIN_GIVE);
}
}

View File

@ -1,5 +1,6 @@
package org.appledash.saneeconomysignshop.signshop;
import org.appledash.saneeconomy.economy.Currency;
import org.appledash.saneeconomysignshop.signshop.ShopTransaction.TransactionDirection;
import org.appledash.saneeconomysignshop.util.ItemInfo;
import org.appledash.saneeconomysignshop.util.SerializableLocation;
@ -131,7 +132,7 @@ public class SignShop implements Serializable {
return quantity;
}
public ShopTransaction makeTransaction(Player player, TransactionDirection direction, int quantity) {
return new ShopTransaction(direction, player, item, quantity, (direction == TransactionDirection.BUY) ? getBuyPrice(quantity) : getSellPrice(quantity));
public ShopTransaction makeTransaction(Currency currency, Player player, TransactionDirection direction, int quantity) {
return new ShopTransaction(currency, direction, player, item, quantity, (direction == TransactionDirection.BUY) ? getBuyPrice(quantity) : getSellPrice(quantity));
}
}