diff --git a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/EconomyManager.java b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/EconomyManager.java index eadf645..c913e29 100644 --- a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/EconomyManager.java +++ b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/EconomyManager.java @@ -173,7 +173,7 @@ public class EconomyManager { addBalance(to, amount, TransactionReason.PLAYER_PAY); if (saneEconomy.shouldLogTransactions()) { - saneEconomy.getTransactionLogger().logTransfer(from, to, amount, TransactionReason.PLAYER_PAY); + saneEconomy.getTransactionLogger().logTransfer(from, to, amount); } return true; diff --git a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/logger/TransactionLogger.java b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/logger/TransactionLogger.java index 61f204a..47caed5 100644 --- a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/logger/TransactionLogger.java +++ b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/logger/TransactionLogger.java @@ -10,5 +10,5 @@ import org.appledash.saneeconomy.economy.economable.Economable; 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, TransactionReason reason); + void logTransfer(Economable from, Economable to, double amount); } diff --git a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/logger/TransactionLoggerMySQL.java b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/logger/TransactionLoggerMySQL.java new file mode 100644 index 0000000..1149a42 --- /dev/null +++ b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/logger/TransactionLoggerMySQL.java @@ -0,0 +1,49 @@ +package org.appledash.saneeconomy.economy.logger; + +import org.appledash.saneeconomy.economy.TransactionReason; +import org.appledash.saneeconomy.economy.economable.Economable; +import org.appledash.saneeconomy.utils.DatabaseCredentials; +import org.appledash.saneeconomy.utils.MySQLConnection; + +import java.sql.PreparedStatement; +import java.sql.SQLException; + +/** + * Created by appledash on 9/20/16. + * Blackjack is best pony. + */ +public class TransactionLoggerMySQL implements TransactionLogger { + private MySQLConnection dbConn; + + public TransactionLoggerMySQL(DatabaseCredentials credentials) { + 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) { + this.dbConn.executeAsyncOperation((conn) -> { + try { + PreparedStatement ps = conn.prepareStatement("INSERT INTO transaction_logs (`source`, `destination`, `amount`) VALUES (?, ?, ?)"); + ps.setString(1, from); + ps.setString(2, to); + ps.setDouble(3, change); + } catch (SQLException e) { + throw new RuntimeException("Error occurred logging addition", e); + } + }); + } +} diff --git a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/utils/SaneEconomyConfiguration.java b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/utils/SaneEconomyConfiguration.java index 52663e4..6e5f57d 100644 --- a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/utils/SaneEconomyConfiguration.java +++ b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/utils/SaneEconomyConfiguration.java @@ -8,6 +8,8 @@ import org.appledash.saneeconomy.economy.backend.EconomyStorageBackend; import org.appledash.saneeconomy.economy.backend.type.EconomyStorageBackendFlatfile; import org.appledash.saneeconomy.economy.backend.type.EconomyStorageBackendMySQL; import org.appledash.saneeconomy.economy.economable.EconomableGeneric; +import org.appledash.saneeconomy.economy.logger.TransactionLogger; +import org.appledash.saneeconomy.economy.logger.TransactionLoggerMySQL; import org.bukkit.configuration.Configuration; import org.bukkit.configuration.ConfigurationSection; @@ -112,6 +114,16 @@ public class SaneEconomyConfiguration { newer.waitUntilFlushed(); } + private TransactionLogger loadLogger() { + if (!rootConfig.getBoolean("log-transactions", false)) { + return null; + } + + DatabaseCredentials credentials = loadCredentials(rootConfig.getConfigurationSection("logger-database")); + + return new TransactionLoggerMySQL(credentials); + } + /** * Load database host, port, username, password, and db name from a ConfigurationSection * @param config ConfigurationSection containing the right fields.