Creating Custom Transaction Checks
Creating Custom Transaction Checks for TheNewEconomy
Transaction checks in TheNewEconomy (TNE) are used to validate and enforce rules during economic transactions. These checks ensure transactions adhere to server rules, such as ensuring balances don't exceed limits or items meet specific criteria.
Overview
What is a Transaction Check?
A Transaction Check is a validation step that runs during a transaction. It ensures that the transaction is valid based on predefined rules.
Registration
Custom transaction checks must be registered with the transaction manager:
TNECore.eco().transaction().addCheck(new CustomTransactionCheck());
TNECore.eco().transaction().addCheck(new CustomTransactionCheck(), "custom_group");
Steps to Create a Custom Transaction Check
Step 1: Implement the TransactionCheck
Interface
TransactionCheck
InterfaceCreate a class that implements the TransactionCheck
interface. Below is an example of a custom check that prevents transactions below a minimum amount.
import net.tnemc.core.transaction.TransactionCheck;
import net.tnemc.core.transaction.Transaction;
import net.tnemc.core.transaction.TransactionParticipant;
import net.tnemc.core.account.holdings.modify.HoldingsModifier;
import net.tnemc.core.actions.EconomyResponse;
import net.tnemc.core.actions.response.GeneralResponse;
import org.jetbrains.annotations.NotNull;
import java.math.BigDecimal;
public class CustomMinimumAmountCheck implements TransactionCheck {
@Override
public String identifier() {
return "min_amount";
}
@Override
public EconomyResponse checkParticipant(Transaction transaction, @NotNull TransactionParticipant participant, HoldingsModifier modifier) {
if (modifier.getAmount().compareTo(new BigDecimal("10.00")) < 0) {
return new EconomyResponse(false, "Transaction amount must be at least 10.00!");
}
return GeneralResponse.SUCCESS;
}
}
Step 2: Register the Custom Check
After creating your check, register it with TNE's transaction manager:
TNECore.eco().transaction().addCheck(new CustomMinimumAmountCheck());
You can also group checks under a custom group identifier for better organization:
TNECore.eco().transaction().addCheck(new CustomMinimumAmountCheck(), "custom_group");
Example: Default Checks in TNE
MinimumBalanceCheck
Ensures the account balance does not go below a minimum threshold.
@Override
public String identifier() {
return "minbal";
}
@Override
public EconomyResponse checkParticipant(Transaction transaction, @NotNull TransactionParticipant participant, HoldingsModifier modifier) {
if (participant.getCombinedEnding().compareTo(currency.getMinBalance()) < 0) {
return HoldingsResponse.MIN_HOLDINGS;
}
return GeneralResponse.SUCCESS;
}
MaximumBalanceCheck
Prevents transactions that would exceed the maximum allowed balance.
@Override
public String identifier() {
return "maxbal";
}
@Override
public EconomyResponse checkParticipant(Transaction transaction, @NotNull TransactionParticipant participant, HoldingsModifier modifier) {
if (participant.getCombinedEnding().compareTo(currency.getMaxBalance()) > 0) {
return HoldingsResponse.MAX_HOLDINGS;
}
return GeneralResponse.SUCCESS;
}
Step 3: Grouping Checks
You can group checks using TransactionCheckGroup
for easy management of related validations:
TransactionCheckGroup customGroup = new TransactionCheckGroup("custom_group");
customGroup.addCheck("min_amount");
TNECore.eco().transaction().addCheckGroup(customGroup);
Key Methods in TransactionCheck
TransactionCheck
identifier()
Returns the unique identifier for the check.
checkParticipant()
Validates the transaction for a specific participant and returns an EconomyResponse
.
process()
Executes the check for all participants in the transaction.
Notes
Unique Identifier: Each check must have a unique identifier returned by
identifier()
.Comprehensive Validation: Use
checkParticipant()
to enforce custom rules for each transaction participant.Organized Checks: Use groups to organize and manage multiple related checks.
By creating and registering custom transaction checks, you can enforce complex and unique economic rules tailored to your server's needs.
Last updated