Creating Custom Currency Type
Creating a Custom Currency Type for TheNewEconomy
In TheNewEconomy (TNE), currency types define the behavior of a currency, such as how it interacts with players' accounts, whether it uses virtual balances, physical items, or other mechanisms. This guide will help you implement and register a custom currency type.
Overview
What is a Currency Type?
A Currency Type is an implementation of the CurrencyType
interface that defines:
Behavior: Determines how the currency is stored, accessed, and interacted with.
Compatibility: Specifies whether it supports features like virtual balances, physical items, or both.
Integration: Provides the logic for interacting with holdings and regions.
Registration
To register your custom currency type:
TNECore.eco().currency().addType(new CustomCurrencyType());
Steps to Create a Custom Currency Type
Step 1: Implement the CurrencyType
Interface
CurrencyType
InterfaceCreate a class that implements the CurrencyType
interface. Below is an example of a custom type called CustomType
:
import net.tnemc.core.EconomyManager;
import net.tnemc.core.account.Account;
import net.tnemc.core.currency.CurrencyType;
import net.tnemc.core.utils.Identifier;
import java.math.BigDecimal;
public class CustomType implements CurrencyType {
@Override
public String name() {
return "custom";
}
@Override
public String description() {
return "A custom currency type with unique behavior.";
}
@Override
public boolean supportsVirtual() {
return false; // Specify if virtual balances are supported
}
@Override
public Identifier defaultHandler() {
return EconomyManager.VIRTUAL; // Specify the default handler
}
@Override
public boolean supportsExchange() {
return false; // Specify if this currency supports exchanging items and virtual balances
}
@Override
public boolean setHoldings(Account account, String region, Currency currency, Identifier type, BigDecimal amount) {
// Define how holdings are set for this currency type
return true;
}
}
Example: Default Currency Types in TNE
VirtualType
A simple currency type that is entirely virtual (e.g., command-based money):
@Override
public String name() {
return "virtual";
}
@Override
public String description() {
return "A simple currency type that is strictly virtual.";
}
@Override
public Identifier defaultHandler() {
return EconomyManager.VIRTUAL;
}
ItemType
A currency type based on physical items:
@Override
public String name() {
return "item";
}
@Override
public String description() {
return "A currency based solely on physical items.";
}
@Override
public Identifier defaultHandler() {
return EconomyManager.INVENTORY_ONLY;
}
MixedType
A combination of virtual and item-based currencies:
@Override
public String name() {
return "mixed";
}
@Override
public String description() {
return "A currency that is a mixture of item and virtual types.";
}
@Override
public boolean supportsExchange() {
return true; // Enables exchanging items and virtual balances
}
Registering the Custom Currency Type
To register your custom type, use the following code during your plugin's initialization:
TNECore.eco().currency().addType(new CustomType());
Key Methods in CurrencyType
CurrencyType
name()
Returns the unique name of the currency type.
description()
Returns a brief description of the currency type.
supportsVirtual()
Indicates if the currency supports virtual balances.
defaultHandler()
Specifies the default holdings handler for the currency type.
setHoldings()
Defines how the holdings are set for the currency type.
supportsExchange()
Indicates if the currency supports exchanging items and virtual balances.
Notes
Unique Behavior: Use the
setHoldings()
method to implement unique behavior for your custom type.Compatibility: Implement
supportsVirtual()
andsupportsExchange()
to ensure compatibility with TNE's features.Thorough Testing: Test your custom type to confirm its behavior aligns with your expectations.
By following this guide, you can create and integrate custom currency types into TheNewEconomy, enabling tailored behavior for your server's economy system.
Last updated