Creating Custom Transaction Type
Last updated
Last updated
Transaction types in TheNewEconomy (TNE) define the behavior of different financial operations, such as withdrawals, deposits, or currency conversions. This guide explains how to implement and register a custom transaction type.
A Transaction Type represents a specific operation within TNE's economy system. It specifies:
Identifier: A unique name for the transaction type.
Taxation Rules: How taxes are applied to the sender and recipient.
To use a custom transaction type, register it with TNE's transaction manager:
TNECore.eco().transaction().addType(new CustomTransactionType());
TransactionType
InterfaceCreate a class that implements the TransactionType
interface. Below is an example of a custom type called CustomFeeType
.
import net.tnemc.core.transaction.TransactionType;
import net.tnemc.core.transaction.tax.TaxEntry;
import org.jetbrains.annotations.NotNull;
import java.util.Optional;
public class CustomFeeType implements TransactionType {
@Override
public @NotNull String identifier() {
return "custom_fee";
}
@Override
public Optional<TaxEntry> toTax() {
// Define taxes applied to the recipient
return Optional.of(new TaxEntry("flat", 5.00));
}
@Override
public Optional<TaxEntry> fromTax() {
// Define taxes applied to the sender
return Optional.of(new TaxEntry("percent", 0.02)); // 2% fee
}
}
To register your transaction type, use the following code:
TNECore.eco().transaction().addType(new CustomFeeType());
Handles deposits into accounts:
@Override
public String identifier() {
return "deposit";
}
@Override
public Optional<TaxEntry> toTax() {
return Optional.empty(); // No tax for recipients
}
@Override
public Optional<TaxEntry> fromTax() {
// Define tax for depositors if enabled
return Optional.of(new TaxEntry("flat", 2.00));
}
Handles withdrawals from accounts:
@Override
public String identifier() {
return "withdraw";
}
@Override
public Optional<TaxEntry> toTax() {
return Optional.empty(); // No tax for recipients
}
@Override
public Optional<TaxEntry> fromTax() {
// Define tax for withdrawers if enabled
return Optional.of(new TaxEntry("percent", 0.05)); // 5% fee
}
Handles payments between players:
@Override
public String identifier() {
return "pay";
}
@Override
public Optional<TaxEntry> toTax() {
return Optional.of(new TaxEntry("flat", 1.00)); // Flat fee for recipients
}
@Override
public Optional<TaxEntry> fromTax() {
return Optional.of(new TaxEntry("percent", 0.01)); // 1% fee for senders
}
TransactionType
identifier()
Returns the unique identifier for the transaction type.
toTax()
Specifies the tax applied to the recipient of the transaction.
fromTax()
Specifies the tax applied to the sender of the transaction.
Unique Identifier: Ensure your transaction type has a unique identifier using the identifier()
method.
Tax Customization: Use toTax()
and fromTax()
to define tax rules specific to your transaction type.
Registration: Always register your custom type with TNECore.eco().transaction().addType()
.
By implementing and registering custom transaction types, you can introduce tailored financial operations to enhance your server's economy system.