Add transaction logging classes

This commit is contained in:
Allink 2023-05-20 05:36:43 +01:00
parent bdd8c1fb68
commit f366a91b36
No known key found for this signature in database
3 changed files with 86 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,6 @@
package me.totalfreedom.economy;
public interface TransactionLogger
{
void logTransaction(CompletedTransaction completedTransaction);
}