From f366a91b36dd4e1b9732162a41c3b4540dc41407 Mon Sep 17 00:00:00 2001 From: Allink Date: Sat, 20 May 2023 05:36:43 +0100 Subject: [PATCH] Add transaction logging classes --- .../economy/SimpleLoggedTransactor.java | 37 ++++++++++++++++ .../economy/SimpleTransactionLogger.java | 43 +++++++++++++++++++ .../economy/TransactionLogger.java | 6 +++ 3 files changed, 86 insertions(+) create mode 100644 Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleLoggedTransactor.java create mode 100644 Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransactionLogger.java create mode 100644 Patchwork/src/main/java/me/totalfreedom/economy/TransactionLogger.java diff --git a/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleLoggedTransactor.java b/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleLoggedTransactor.java new file mode 100644 index 0000000..4354cea --- /dev/null +++ b/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleLoggedTransactor.java @@ -0,0 +1,37 @@ +package me.totalfreedom.fossil.economy; + +import me.totalfreedom.economy.CompletedTransaction; +import me.totalfreedom.economy.Transaction; +import me.totalfreedom.economy.TransactionLogger; +import me.totalfreedom.economy.Transactor; + +public class SimpleLoggedTransactor implements Transactor +{ + private final Transactor transactor; + private final TransactionLogger transactionLogger; + + public SimpleLoggedTransactor() + { + this(new SimpleTransactor(), new SimpleTransactionLogger()); + } + + public SimpleLoggedTransactor(Transactor transactor, TransactionLogger transactionLogger) + { + this.transactor = transactor; + this.transactionLogger = transactionLogger; + } + + @Override + public CompletedTransaction handleTransaction(Transaction transaction) + { + CompletedTransaction completedTransaction = transactor.handleTransaction(transaction); + + transactionLogger.logTransaction(completedTransaction); + return completedTransaction; + } + + public TransactionLogger getTransactionLogger() + { + return this.transactionLogger; + } +} diff --git a/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransactionLogger.java b/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransactionLogger.java new file mode 100644 index 0000000..2f6d9cd --- /dev/null +++ b/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransactionLogger.java @@ -0,0 +1,43 @@ +package me.totalfreedom.fossil.economy; + +import me.totalfreedom.audience.MutableAudienceForwarder; +import me.totalfreedom.economy.*; +import me.totalfreedom.utils.FreedomLogger; +import net.kyori.adventure.text.Component; +import org.bukkit.Bukkit; + +public class SimpleTransactionLogger implements TransactionLogger +{ + private MutableAudienceForwarder audience = MutableAudienceForwarder.from(FreedomLogger.getLogger("Fossil"), Bukkit.getConsoleSender()); + + @Override + public void logTransaction(CompletedTransaction completedTransaction) + { + StringBuilder transactionLoggingStatementBuilder = new StringBuilder(); + TransactionResult result = completedTransaction.getResult(); + boolean resultSuccess = result.isSuccessful(); + String resultMessage = result.getMessage(); + + Transaction transaction = completedTransaction.getTransaction(); + EconomicEntity source = transaction.getSource(); + EconomicEntity destination = transaction.getDestination(); + long transactionAmount = transaction.getTransferAmount(); + + transactionLoggingStatementBuilder.append(resultSuccess ? "Successful" : "Unsuccessful") + .append(" (") + .append(resultMessage) + .append(") ") + .append(" transaction between ") + .append(source.getName()) + .append(" ") + .append(destination.getName()) + .append(" where the volume of currency transferred was ") + .append("$") + .append(transactionAmount) + .append("."); + + Component message = Component.text(transactionLoggingStatementBuilder.toString()); + + audience.sendMessage(message); + } +} diff --git a/Patchwork/src/main/java/me/totalfreedom/economy/TransactionLogger.java b/Patchwork/src/main/java/me/totalfreedom/economy/TransactionLogger.java new file mode 100644 index 0000000..f8dce2d --- /dev/null +++ b/Patchwork/src/main/java/me/totalfreedom/economy/TransactionLogger.java @@ -0,0 +1,6 @@ +package me.totalfreedom.economy; + +public interface TransactionLogger +{ + void logTransaction(CompletedTransaction completedTransaction); +}