mirror of
https://github.com/SimplexDevelopment/FreedomNetworkSuite.git
synced 2024-11-22 08:54:59 +00:00
Add transactor & transaction result
This commit is contained in:
parent
57853a37c5
commit
0e31bea33f
@ -6,6 +6,10 @@ import net.kyori.adventure.text.format.NamedTextColor;
|
||||
|
||||
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 Component component;
|
||||
private final boolean successful;
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package me.totalfreedom.economy;
|
||||
|
||||
public interface Transactor
|
||||
{
|
||||
CompletedTransaction handleTransaction(Transaction transaction);
|
||||
}
|
Loading…
Reference in New Issue
Block a user