Basically rewrite the entire core.
This commit is contained in:
parent
d1b302e779
commit
f554013c16
|
@ -6,10 +6,10 @@
|
|||
<parent>
|
||||
<groupId>org.appledash</groupId>
|
||||
<artifactId>SaneEconomy</artifactId>
|
||||
<version>0.7.2-SNAPSHOT</version>
|
||||
<version>0.8.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>SaneEconomyCore</artifactId>
|
||||
<version>0.7.2-SNAPSHOT</version>
|
||||
<version>0.8.0-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
|
|
@ -7,8 +7,10 @@ import org.appledash.saneeconomy.command.exception.type.usage.InvalidUsageExcept
|
|||
import org.appledash.saneeconomy.command.exception.type.usage.NeedPlayerException;
|
||||
import org.appledash.saneeconomy.command.exception.type.usage.TooFewArgumentsException;
|
||||
import org.appledash.saneeconomy.economy.EconomyManager;
|
||||
import org.appledash.saneeconomy.economy.TransactionReason;
|
||||
import org.appledash.saneeconomy.economy.economable.Economable;
|
||||
import org.appledash.saneeconomy.economy.transaction.Transaction;
|
||||
import org.appledash.saneeconomy.economy.transaction.TransactionReason;
|
||||
import org.appledash.saneeconomy.economy.transaction.TransactionResult;
|
||||
import org.appledash.saneeconomy.utils.MessageUtils;
|
||||
import org.appledash.saneeconomy.utils.NumberUtils;
|
||||
import org.appledash.saneeconomy.utils.PlayerUtils;
|
||||
|
@ -79,7 +81,10 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
|
|||
}
|
||||
|
||||
if (subCommand.equalsIgnoreCase("give")) {
|
||||
double newAmount = ecoMan.addBalance(economable, amount, TransactionReason.ADMIN);
|
||||
Transaction transaction = new Transaction(Economable.wrap(sender), Economable.wrap(targetPlayer), amount, TransactionReason.ADMIN);
|
||||
TransactionResult result = ecoMan.transact(transaction);
|
||||
|
||||
double newAmount = result.getToBalance();
|
||||
|
||||
MessageUtils.sendMessage(sender, _("Added %s to %s. Their balance is now %s."),
|
||||
ecoMan.getCurrency().formatAmount(amount),
|
||||
|
@ -90,7 +95,10 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
|
|||
}
|
||||
|
||||
if (subCommand.equalsIgnoreCase("take")) {
|
||||
double newAmount = ecoMan.subtractBalance(economable, amount, TransactionReason.ADMIN);
|
||||
Transaction transaction = new Transaction(Economable.wrap(sender), Economable.wrap(targetPlayer), amount, TransactionReason.ADMIN);
|
||||
TransactionResult result = ecoMan.transact(transaction);
|
||||
|
||||
double newAmount = result.getFromBalance();
|
||||
|
||||
MessageUtils.sendMessage(sender, _("Took %s from %s. Their balance is now %s."),
|
||||
ecoMan.getCurrency().formatAmount(amount),
|
||||
|
@ -101,8 +109,16 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
|
|||
}
|
||||
|
||||
if (subCommand.equalsIgnoreCase("set")) {
|
||||
ecoMan.setBalance(economable, amount, TransactionReason.ADMIN);
|
||||
ecoMan.setBalance(economable, amount);
|
||||
MessageUtils.sendMessage(sender, _("Balance for %s set to %s."), sTargetPlayer, ecoMan.getCurrency().formatAmount(amount));
|
||||
|
||||
// FIXME: This is a silly hack to get it to log.
|
||||
saneEconomy.getTransactionLogger().logTransaction(new Transaction(
|
||||
economable, Economable.CONSOLE, ecoMan.getBalance(economable), TransactionReason.ADMIN
|
||||
));
|
||||
saneEconomy.getTransactionLogger().logTransaction(new Transaction(
|
||||
Economable.CONSOLE, economable, amount, TransactionReason.ADMIN
|
||||
));
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,9 @@ import org.appledash.saneeconomy.command.exception.CommandException;
|
|||
import org.appledash.saneeconomy.command.exception.type.usage.NeedPlayerException;
|
||||
import org.appledash.saneeconomy.economy.EconomyManager;
|
||||
import org.appledash.saneeconomy.economy.economable.Economable;
|
||||
import org.appledash.saneeconomy.economy.transaction.Transaction;
|
||||
import org.appledash.saneeconomy.economy.transaction.TransactionReason;
|
||||
import org.appledash.saneeconomy.economy.transaction.TransactionResult;
|
||||
import org.appledash.saneeconomy.utils.MessageUtils;
|
||||
import org.appledash.saneeconomy.utils.NumberUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
|
@ -71,9 +74,10 @@ public class PayCommand extends SaneEconomyCommand {
|
|||
}
|
||||
|
||||
/* Perform the actual transfer. False == They didn't have enough money */
|
||||
boolean result = ecoMan.transfer(Economable.wrap(fromPlayer), Economable.wrap(toPlayer), amount);
|
||||
Transaction transaction = new Transaction(Economable.wrap(fromPlayer), Economable.wrap(toPlayer), amount, TransactionReason.PLAYER_PAY);
|
||||
TransactionResult result = ecoMan.transact(transaction);
|
||||
|
||||
if (!result) {
|
||||
if (result.getStatus() != TransactionResult.Status.SUCCESS) {
|
||||
MessageUtils.sendMessage(sender, "You do not have enough money to transfer %s to %s.",
|
||||
ecoMan.getCurrency().formatAmount(amount),
|
||||
sToPlayer
|
||||
|
|
|
@ -3,6 +3,9 @@ package org.appledash.saneeconomy.economy;
|
|||
import org.appledash.saneeconomy.ISaneEconomy;
|
||||
import org.appledash.saneeconomy.economy.backend.EconomyStorageBackend;
|
||||
import org.appledash.saneeconomy.economy.economable.Economable;
|
||||
import org.appledash.saneeconomy.economy.transaction.Transaction;
|
||||
import org.appledash.saneeconomy.economy.transaction.TransactionReason;
|
||||
import org.appledash.saneeconomy.economy.transaction.TransactionResult;
|
||||
import org.appledash.saneeconomy.utils.NumberUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
@ -60,6 +63,10 @@ public class EconomyManager {
|
|||
* @return Player's balance
|
||||
*/
|
||||
public double getBalance(Economable targetPlayer) {
|
||||
if (targetPlayer == Economable.CONSOLE) {
|
||||
return Double.MAX_VALUE;
|
||||
}
|
||||
|
||||
return backend.getBalance(targetPlayer);
|
||||
}
|
||||
|
||||
|
@ -71,6 +78,10 @@ public class EconomyManager {
|
|||
* @return True if they have requiredBalance or more, false otherwise
|
||||
*/
|
||||
public boolean hasBalance(Economable targetPlayer, double requiredBalance) {
|
||||
if (targetPlayer == Economable.CONSOLE) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return getBalance(targetPlayer) >= requiredBalance;
|
||||
}
|
||||
|
||||
|
@ -81,16 +92,20 @@ public class EconomyManager {
|
|||
* @return Player's new balance
|
||||
* @throws IllegalArgumentException If amount is negative
|
||||
*/
|
||||
public double addBalance(Economable targetPlayer, double amount, TransactionReason reason) {
|
||||
private double 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.MAX_VALUE;
|
||||
}
|
||||
|
||||
double newAmount = backend.getBalance(targetPlayer) + amount;
|
||||
|
||||
setBalance(targetPlayer, newAmount, reason);
|
||||
setBalance(targetPlayer, newAmount);
|
||||
|
||||
return newAmount;
|
||||
}
|
||||
|
@ -103,13 +118,17 @@ public class EconomyManager {
|
|||
* @return Player's new balance
|
||||
* @throws IllegalArgumentException If amount is negative
|
||||
*/
|
||||
public double subtractBalance(Economable targetPlayer, double amount, TransactionReason reason) {
|
||||
private double 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.MAX_VALUE;
|
||||
}
|
||||
|
||||
double newAmount = backend.getBalance(targetPlayer) - amount;
|
||||
|
||||
|
||||
|
@ -118,65 +137,55 @@ public class EconomyManager {
|
|||
newAmount = 0.0D;
|
||||
}
|
||||
|
||||
setBalance(targetPlayer, newAmount, reason);
|
||||
setBalance(targetPlayer, newAmount);
|
||||
|
||||
return newAmount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a player's balance.
|
||||
* Set a player's balance. This does NOT log.
|
||||
* @param targetPlayer Player to set balance of
|
||||
* @param amount Amount to set balance to
|
||||
* @throws IllegalArgumentException If amount is negative
|
||||
*/
|
||||
public void setBalance(Economable targetPlayer, double amount, TransactionReason reason) {
|
||||
public void setBalance(Economable targetPlayer, double amount) {
|
||||
amount = NumberUtils.filterAmount(currency, amount);
|
||||
|
||||
if (amount < 0) {
|
||||
throw new IllegalArgumentException("Cannot set balance to a negative value!");
|
||||
}
|
||||
|
||||
double oldAmount = backend.getBalance(targetPlayer);
|
||||
if (targetPlayer == Economable.CONSOLE) {
|
||||
return;
|
||||
}
|
||||
|
||||
backend.setBalance(targetPlayer, amount);
|
||||
|
||||
if (saneEconomy.shouldLogTransactions() && reason != TransactionReason.PLAYER_PAY) { // Player pay is handled in the transfer() method.
|
||||
if (oldAmount > amount) { // Lower amount now
|
||||
saneEconomy.getTransactionLogger().logSubtraction(targetPlayer, oldAmount - amount, reason);
|
||||
} else if (oldAmount < amount) { // Higher amount now
|
||||
saneEconomy.getTransactionLogger().logAddition(targetPlayer, amount - oldAmount, reason);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfer money from one Economable to another.
|
||||
* @param from Economable to transfer from
|
||||
* @param to Economable to transfer to
|
||||
* @param amount Amount to transfer
|
||||
* @return True if success, false if from has insufficient funds.
|
||||
* @throws IllegalArgumentException If amount is negative
|
||||
* Perform a transaction.
|
||||
* @param transaction Transaction to perform.
|
||||
*/
|
||||
public boolean transfer(Economable from, Economable to, double amount) {
|
||||
amount = NumberUtils.filterAmount(currency, amount);
|
||||
public TransactionResult transact(Transaction transaction) {
|
||||
Economable sender = transaction.getSender();
|
||||
Economable receiver = transaction.getReceiver();
|
||||
double amount = transaction.getAmount();
|
||||
|
||||
if (amount < 0) {
|
||||
throw new IllegalArgumentException("Cannot transfer a negative amount!");
|
||||
if (!transaction.isFree()) { // If the transaction is occurring because of another plugin or because of an admin.
|
||||
if (!hasBalance(sender, amount) && transaction.getReason() != TransactionReason.TEST) {
|
||||
return new TransactionResult(transaction, TransactionResult.Status.ERR_NOT_ENOUGH_FUNDS);
|
||||
}
|
||||
|
||||
subtractBalance(sender, amount);
|
||||
}
|
||||
|
||||
if (!hasBalance(from, amount)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Perform the actual transfer. TODO: Maybe return their new balances in some way? */
|
||||
subtractBalance(from, amount, TransactionReason.PLAYER_PAY);
|
||||
addBalance(to, amount, TransactionReason.PLAYER_PAY);
|
||||
addBalance(receiver, amount);
|
||||
|
||||
if (saneEconomy.shouldLogTransactions()) {
|
||||
saneEconomy.getTransactionLogger().logTransfer(from, to, amount);
|
||||
saneEconomy.getTransactionLogger().logTransaction(transaction);
|
||||
}
|
||||
|
||||
return true;
|
||||
return new TransactionResult(transaction, getBalance(sender), getBalance(receiver));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,18 +1,35 @@
|
|||
package org.appledash.saneeconomy.economy.economable;
|
||||
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
/**
|
||||
* Created by appledash on 7/19/16.
|
||||
* Blackjack is still best pony.
|
||||
*/
|
||||
public interface Economable {
|
||||
Economable CONSOLE = new EconomableConsole();
|
||||
Economable PLUGIN = new EconomablePlugin();
|
||||
|
||||
String getUniqueIdentifier();
|
||||
|
||||
static Economable wrap(OfflinePlayer player) {
|
||||
return new EconomablePlayer(player);
|
||||
}
|
||||
|
||||
static Economable wrap(CommandSender sender) {
|
||||
if (sender instanceof OfflinePlayer) {
|
||||
return wrap(((OfflinePlayer) sender));
|
||||
}
|
||||
|
||||
return CONSOLE;
|
||||
}
|
||||
|
||||
static Economable wrap(Player player) {
|
||||
return wrap(((OfflinePlayer) player));
|
||||
}
|
||||
|
||||
static Economable wrap(String identifier) {
|
||||
if (identifier.startsWith("faction-")) {
|
||||
return new EconomableFaction(identifier.replace("faction-", ""));
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
package org.appledash.saneeconomy.economy.economable;
|
||||
|
||||
/**
|
||||
* Created by appledash on 9/21/16.
|
||||
* Blackjack is best pony.
|
||||
*/
|
||||
public class EconomableConsole implements Economable {
|
||||
@Override
|
||||
public String getUniqueIdentifier() {
|
||||
return "CONSOLE";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package org.appledash.saneeconomy.economy.economable;
|
||||
|
||||
/**
|
||||
* Created by appledash on 9/21/16.
|
||||
* Blackjack is best pony.
|
||||
*/
|
||||
public class EconomablePlugin implements Economable {
|
||||
@Override
|
||||
public String getUniqueIdentifier() {
|
||||
return "PLUGIN";
|
||||
}
|
||||
}
|
|
@ -1,14 +1,11 @@
|
|||
package org.appledash.saneeconomy.economy.logger;
|
||||
|
||||
import org.appledash.saneeconomy.economy.TransactionReason;
|
||||
import org.appledash.saneeconomy.economy.economable.Economable;
|
||||
import org.appledash.saneeconomy.economy.transaction.Transaction;
|
||||
|
||||
/**
|
||||
* Created by AppleDash on 8/15/2016.
|
||||
* Blackjack is still best pony.
|
||||
*/
|
||||
public interface TransactionLogger {
|
||||
void logAddition(Economable economable, double amount, TransactionReason reason);
|
||||
void logSubtraction(Economable economable, double amount, TransactionReason reason);
|
||||
void logTransfer(Economable from, Economable to, double amount);
|
||||
void logTransaction(Transaction transaction);
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package org.appledash.saneeconomy.economy.logger;
|
||||
|
||||
import org.appledash.saneeconomy.economy.TransactionReason;
|
||||
import org.appledash.saneeconomy.economy.economable.Economable;
|
||||
import org.appledash.saneeconomy.economy.transaction.Transaction;
|
||||
import org.appledash.saneeconomy.economy.transaction.TransactionReason;
|
||||
import org.appledash.saneeconomy.utils.DatabaseCredentials;
|
||||
import org.appledash.saneeconomy.utils.MySQLConnection;
|
||||
|
||||
|
@ -20,28 +20,14 @@ public class TransactionLoggerMySQL implements TransactionLogger {
|
|||
this.dbConn = new MySQLConnection(credentials);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logAddition(Economable economable, double amount, TransactionReason reason) {
|
||||
logGeneric(reason.toString(), economable.getUniqueIdentifier(), amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logSubtraction(Economable economable, double amount, TransactionReason reason) {
|
||||
logGeneric(reason.toString(), economable.getUniqueIdentifier(), -amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logTransfer(Economable from, Economable to, double amount) {
|
||||
logGeneric(from.getUniqueIdentifier(), to.getUniqueIdentifier(), amount);
|
||||
}
|
||||
|
||||
private void logGeneric(String from, String to, double change) {
|
||||
private void logGeneric(String from, String to, double change, TransactionReason reason) {
|
||||
this.dbConn.executeAsyncOperation((conn) -> {
|
||||
try {
|
||||
PreparedStatement ps = conn.prepareStatement("INSERT INTO transaction_logs (`source`, `destination`, `amount`) VALUES (?, ?, ?)");
|
||||
PreparedStatement ps = conn.prepareStatement("INSERT INTO transaction_logs (`source`, `destination`, `amount`, `reason`) VALUES (?, ?, ?, ?)");
|
||||
ps.setString(1, from);
|
||||
ps.setString(2, to);
|
||||
ps.setDouble(3, change);
|
||||
ps.setString(4, reason.toString());
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException("Error occurred logging addition", e);
|
||||
|
@ -60,10 +46,15 @@ public class TransactionLoggerMySQL implements TransactionLogger {
|
|||
|
||||
private void createTables() {
|
||||
try (Connection conn = dbConn.openConnection()) {
|
||||
PreparedStatement ps = conn.prepareStatement("CREATE TABLE IF NOT EXISTS `transaction_logs` (`source` VARCHAR(128), `destination` VARCHAR(128), `amount` DECIMAL(18, 2))");
|
||||
PreparedStatement ps = conn.prepareStatement("CREATE TABLE IF NOT EXISTS `transaction_logs` (`source` VARCHAR(128), `destination` VARCHAR(128), `amount` DECIMAL(18, 2), `reason` VARCHAR(128))");
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException("Failed to create transaction logger tables", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logTransaction(Transaction transaction) {
|
||||
logGeneric(transaction.getSender().getUniqueIdentifier(), transaction.getReceiver().getUniqueIdentifier(), transaction.getAmount(), transaction.getReason());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
package org.appledash.saneeconomy.economy.transaction;
|
||||
|
||||
import org.appledash.saneeconomy.economy.economable.Economable;
|
||||
|
||||
/**
|
||||
* Created by appledash on 9/21/16.
|
||||
* Blackjack is best pony.
|
||||
*/
|
||||
public class Transaction {
|
||||
private final Economable sender;
|
||||
private final Economable receiver;
|
||||
private final double amount;
|
||||
private final TransactionReason reason;
|
||||
|
||||
public Transaction(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.reason = reason;
|
||||
}
|
||||
|
||||
public Economable getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
public Economable getReceiver() {
|
||||
return receiver;
|
||||
}
|
||||
|
||||
public double getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public TransactionReason getReason() {
|
||||
return reason;
|
||||
}
|
||||
|
||||
public boolean isFree() {
|
||||
return sender == Economable.CONSOLE || sender == Economable.PLUGIN || reason == TransactionReason.ADMIN;
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package org.appledash.saneeconomy.economy;
|
||||
package org.appledash.saneeconomy.economy.transaction;
|
||||
|
||||
/**
|
||||
* Created by AppleDash on 8/15/2016.
|
||||
|
@ -20,5 +20,9 @@ public enum TransactionReason {
|
|||
/**
|
||||
* Initial starting balance on join.
|
||||
*/
|
||||
STARTING_BALANCE
|
||||
STARTING_BALANCE,
|
||||
/**
|
||||
* Used in unit tests.
|
||||
*/
|
||||
TEST
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package org.appledash.saneeconomy.economy.transaction;
|
||||
|
||||
/**
|
||||
* Created by appledash on 9/21/16.
|
||||
* Blackjack is best pony.
|
||||
*/
|
||||
public class TransactionResult {
|
||||
private final Transaction transaction;
|
||||
private final double fromBalance;
|
||||
private final double toBalance;
|
||||
private Status status;
|
||||
|
||||
public TransactionResult(Transaction transaction, double fromBalance, double toBalance) {
|
||||
this.transaction = transaction;
|
||||
this.fromBalance = fromBalance;
|
||||
this.toBalance = toBalance;
|
||||
this.status = Status.SUCCESS;
|
||||
}
|
||||
|
||||
public TransactionResult(Transaction transaction, Status status) {
|
||||
this(transaction, -1, -1);
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public Transaction getTransaction() {
|
||||
return transaction;
|
||||
}
|
||||
|
||||
public double getFromBalance() {
|
||||
return fromBalance;
|
||||
}
|
||||
|
||||
public double getToBalance() {
|
||||
return toBalance;
|
||||
}
|
||||
|
||||
public Status getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public enum Status {
|
||||
SUCCESS,
|
||||
|
||||
ERR_NOT_ENOUGH_FUNDS
|
||||
}
|
||||
}
|
|
@ -1,10 +1,12 @@
|
|||
package org.appledash.saneeconomy.listeners;
|
||||
|
||||
import org.appledash.saneeconomy.SaneEconomy;
|
||||
import org.appledash.saneeconomy.economy.TransactionReason;
|
||||
import org.appledash.saneeconomy.economy.economable.Economable;
|
||||
import org.appledash.saneeconomy.economy.transaction.Transaction;
|
||||
import org.appledash.saneeconomy.economy.transaction.TransactionReason;
|
||||
import org.appledash.saneeconomy.updates.GithubVersionChecker;
|
||||
import org.appledash.saneeconomy.utils.MessageUtils;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
|
@ -24,12 +26,14 @@ public class JoinQuitListener implements Listener {
|
|||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent evt) {
|
||||
Player player = evt.getPlayer();
|
||||
Economable economable = Economable.wrap(player);
|
||||
Economable economable = Economable.wrap((OfflinePlayer) player);
|
||||
double startBalance = plugin.getConfig().getDouble("economy.start-balance", 0.0D);
|
||||
|
||||
/* A starting balance is configured AND they haven't been given it yet. */
|
||||
if ((startBalance > 0) && !plugin.getEconomyManager().accountExists(economable)) {
|
||||
plugin.getEconomyManager().setBalance(economable, startBalance, TransactionReason.STARTING_BALANCE);
|
||||
plugin.getEconomyManager().transact(new Transaction(
|
||||
Economable.CONSOLE, economable, startBalance, TransactionReason.STARTING_BALANCE
|
||||
));
|
||||
MessageUtils.sendMessage(player, "You've been issued a starting balance of %s!", plugin.getEconomyManager().getCurrency().formatAmount(startBalance));
|
||||
}
|
||||
|
||||
|
|
|
@ -4,8 +4,10 @@ import com.google.common.collect.ImmutableList;
|
|||
import net.milkbowl.vault.economy.Economy;
|
||||
import net.milkbowl.vault.economy.EconomyResponse;
|
||||
import org.appledash.saneeconomy.SaneEconomy;
|
||||
import org.appledash.saneeconomy.economy.TransactionReason;
|
||||
import org.appledash.saneeconomy.economy.economable.Economable;
|
||||
import org.appledash.saneeconomy.economy.transaction.Transaction;
|
||||
import org.appledash.saneeconomy.economy.transaction.TransactionReason;
|
||||
import org.appledash.saneeconomy.economy.transaction.TransactionResult;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
|
@ -142,11 +144,9 @@ public class EconomySaneEconomy implements Economy {
|
|||
economable = Economable.wrap(playerName);
|
||||
}
|
||||
|
||||
if (!has(playerName, v)) {
|
||||
return new EconomyResponse(v, getBalance(playerName), EconomyResponse.ResponseType.FAILURE, "Insufficient funds.");
|
||||
}
|
||||
|
||||
return new EconomyResponse(v, SaneEconomy.getInstance().getEconomyManager().subtractBalance(economable, v, TransactionReason.PLUGIN), EconomyResponse.ResponseType.SUCCESS, null);
|
||||
return transact(new Transaction(
|
||||
Economable.PLUGIN, economable, v, TransactionReason.PLUGIN
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -155,7 +155,9 @@ public class EconomySaneEconomy implements Economy {
|
|||
return new EconomyResponse(v, getBalance(offlinePlayer), EconomyResponse.ResponseType.FAILURE, "Insufficient funds.");
|
||||
}
|
||||
|
||||
return new EconomyResponse(v, SaneEconomy.getInstance().getEconomyManager().subtractBalance(Economable.wrap(offlinePlayer), v, TransactionReason.PLUGIN), EconomyResponse.ResponseType.SUCCESS, null);
|
||||
return transact(new Transaction(
|
||||
Economable.PLUGIN, Economable.wrap(offlinePlayer), v, TransactionReason.PLUGIN
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -177,12 +179,16 @@ public class EconomySaneEconomy implements Economy {
|
|||
economable = Economable.wrap(playerName);
|
||||
}
|
||||
|
||||
return new EconomyResponse(v, SaneEconomy.getInstance().getEconomyManager().addBalance(economable, v, TransactionReason.PLUGIN), EconomyResponse.ResponseType.SUCCESS, null);
|
||||
return transact(new Transaction(
|
||||
economable, Economable.PLUGIN, v, TransactionReason.PLUGIN
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
public EconomyResponse depositPlayer(OfflinePlayer offlinePlayer, double v) {
|
||||
return new EconomyResponse(v, SaneEconomy.getInstance().getEconomyManager().addBalance(Economable.wrap(offlinePlayer), v, TransactionReason.PLUGIN), EconomyResponse.ResponseType.SUCCESS, null);
|
||||
return transact(new Transaction(
|
||||
Economable.wrap(offlinePlayer), Economable.PLUGIN, v, TransactionReason.PLUGIN
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -278,4 +284,14 @@ public class EconomySaneEconomy implements Economy {
|
|||
private boolean validatePlayer(String playerName) {
|
||||
return Bukkit.getServer().getPlayer(playerName) != null;
|
||||
}
|
||||
|
||||
private EconomyResponse transact(Transaction transaction) {
|
||||
TransactionResult result = SaneEconomy.getInstance().getEconomyManager().transact(transaction);
|
||||
|
||||
if (result.getStatus() == TransactionResult.Status.SUCCESS) {
|
||||
return new EconomyResponse(transaction.getAmount(), result.getToBalance(), EconomyResponse.ResponseType.SUCCESS, null);
|
||||
}
|
||||
|
||||
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, result.getStatus().toString());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
name: SaneEconomy
|
||||
author: AppleDash
|
||||
main: org.appledash.saneeconomy.SaneEconomy
|
||||
version: 0.7.2
|
||||
version: 0.8.0
|
||||
loadbefore: [Vault]
|
||||
commands:
|
||||
balance:
|
||||
|
|
|
@ -2,8 +2,10 @@ package org.appledash.saneeconomy.test;
|
|||
|
||||
import org.appledash.saneeconomy.economy.Currency;
|
||||
import org.appledash.saneeconomy.economy.EconomyManager;
|
||||
import org.appledash.saneeconomy.economy.TransactionReason;
|
||||
import org.appledash.saneeconomy.economy.economable.Economable;
|
||||
import org.appledash.saneeconomy.economy.transaction.Transaction;
|
||||
import org.appledash.saneeconomy.economy.transaction.TransactionReason;
|
||||
import org.appledash.saneeconomy.economy.transaction.TransactionResult;
|
||||
import org.appledash.saneeconomy.test.mock.MockEconomyStorageBackend;
|
||||
import org.appledash.saneeconomy.test.mock.MockOfflinePlayer;
|
||||
import org.appledash.saneeconomy.test.mock.MockSaneEconomy;
|
||||
|
@ -38,7 +40,7 @@ public class EconomyManagerTest {
|
|||
Assert.assertEquals(economyManager.getBalance(playerOne), 0.0D, 0.0);
|
||||
Assert.assertEquals(economyManager.getBalance(playerTwo), 0.0D, 0.0);
|
||||
|
||||
economyManager.setBalance(playerOne, 100.0D, TransactionReason.PLUGIN);
|
||||
economyManager.setBalance(playerOne, 100.0D);
|
||||
|
||||
// Now one should have an account, but two should not
|
||||
Assert.assertTrue(economyManager.accountExists(playerOne));
|
||||
|
@ -49,16 +51,24 @@ public class EconomyManagerTest {
|
|||
Assert.assertEquals(economyManager.getBalance(playerTwo), 0.0, 0.0);
|
||||
|
||||
// One should be able to transfer to two
|
||||
Assert.assertTrue(economyManager.transfer(playerOne, playerTwo, 50.0));
|
||||
Assert.assertTrue(economyManager.transact(new Transaction(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(economyManager.getBalance(playerOne), 50.0, 0.0);
|
||||
Assert.assertEquals(economyManager.getBalance(playerTwo), 50.0, 0.0);
|
||||
|
||||
// Ensure that balance addition and subtraction works...
|
||||
Assert.assertEquals(economyManager.subtractBalance(playerOne, 25.0, TransactionReason.PLUGIN), 25.0, 0.0);
|
||||
Assert.assertEquals(economyManager.addBalance(playerOne, 25.0, TransactionReason.PLUGIN), 50.0, 0.0);
|
||||
Assert.assertEquals(economyManager.subtractBalance(playerTwo, Double.MAX_VALUE, TransactionReason.PLUGIN), 0.0, 0.0);
|
||||
Assert.assertEquals(economyManager.transact(
|
||||
new Transaction(playerOne, Economable.CONSOLE, 25.0, TransactionReason.TEST)
|
||||
).getFromBalance(), 25.0, 0.0);
|
||||
|
||||
Assert.assertEquals(economyManager.transact(
|
||||
new Transaction(Economable.CONSOLE, playerOne, 25.0, TransactionReason.TEST)
|
||||
).getToBalance(), 50.0, 0.0);
|
||||
|
||||
Assert.assertEquals(economyManager.transact(
|
||||
new Transaction(playerTwo, Economable.CONSOLE, Double.MAX_VALUE, TransactionReason.TEST)
|
||||
).getFromBalance(), 0.0, 0.0);
|
||||
|
||||
// Ensure that hasBalance works
|
||||
Assert.assertTrue(economyManager.hasBalance(playerOne, 50.0));
|
||||
|
@ -70,6 +80,6 @@ public class EconomyManagerTest {
|
|||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testNegativeBalance() {
|
||||
Economable economable = Economable.wrap(new MockOfflinePlayer("Bob"));
|
||||
economyManager.setBalance(economable, -1.0, TransactionReason.PLUGIN);
|
||||
economyManager.setBalance(economable, -1.0);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue