Creating Custom Transaction Checks
Last updated
Last updated
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.
A Transaction Check is a validation step that runs during a transaction. It ensures that the transaction is valid based on predefined rules.
Custom transaction checks must be registered with the transaction manager:
TNECore.eco().transaction().addCheck(new CustomTransactionCheck());
TNECore.eco().transaction().addCheck(new CustomTransactionCheck(), "custom_group");
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
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");
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;
}
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;
}
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);
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.
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.