Add transactor & transaction result

This commit is contained in:
Allink 2023-05-20 05:20:18 +01:00
parent 57853a37c5
commit 0e31bea33f
No known key found for this signature in database
3 changed files with 57 additions and 0 deletions

View File

@ -6,6 +6,10 @@ import net.kyori.adventure.text.format.NamedTextColor;
public class SimpleTransactionResult implements TransactionResult public class SimpleTransactionResult implements TransactionResult
{ {
public static final TransactionResult SUCCESSFUL = new SimpleTransactionResult("Successful transaction.", true);
public static final TransactionResult UNAUTHORIZED = new SimpleTransactionResult("Unauthorized transaction.", false);
public static final TransactionResult AMOUNT_TOO_SMALL = new SimpleTransactionResult("Transaction transfer amount too small.", false);
public static final TransactionResult INSUFFICIENT_FUNDS = new SimpleTransactionResult("The source has insufficient funds to carry out this transaction.", false);
private final String message; private final String message;
private final Component component; private final Component component;
private final boolean successful; private final boolean successful;

View File

@ -0,0 +1,47 @@
package me.totalfreedom.fossil.economy;
import me.totalfreedom.economy.*;
public class SimpleTransactor implements Transactor
{
@Override
public CompletedTransaction handleTransaction(Transaction transaction)
{
Transaction transactionCopy = transaction.copy();
EconomicEntity source = transaction.getSource();
EconomicEntityData sourceData = source.getEconomicData();
if (sourceData.areTransactionsFrozen())
{
return new SimpleCompletedTransaction(transaction, SimpleTransactionResult.UNAUTHORIZED);
}
long transactionAmount = transaction.getTransferAmount();
if (transactionAmount >= 0)
{
return new SimpleCompletedTransaction(transaction, SimpleTransactionResult.AMOUNT_TOO_SMALL);
}
long sourceBalance = sourceData.getBalance();
long diff = sourceBalance - transactionAmount;
if (diff > 0)
{
return new SimpleCompletedTransaction(transactionCopy, SimpleTransactionResult.INSUFFICIENT_FUNDS);
}
EconomicEntity destination = transaction.getDestination();
EconomicEntityData destinationData = destination.getEconomicData();
if (destinationData.areTransactionsFrozen())
{
return new SimpleCompletedTransaction(transaction, SimpleTransactionResult.UNAUTHORIZED);
}
sourceData.removeFromBalance(transactionAmount);
destinationData.addToBalance(transactionAmount);
return new SimpleCompletedTransaction(transactionCopy, SimpleTransactionResult.SUCCESSFUL);
}
}

View File

@ -0,0 +1,6 @@
package me.totalfreedom.economy;
public interface Transactor
{
CompletedTransaction handleTransaction(Transaction transaction);
}