Creating Custom Account Type
Adding Custom Account Types to TheNewEconomy
TheNewEconomy allows developers to create and integrate custom account types for various plugins or unique server setups. This guide provides an example of adding custom account types using the Towny plugin as a reference.
Steps to Add Custom Account Types
1. Define Your Custom Account Class
Custom account types should extend the NonPlayerAccount
or PlayerAccount
class. For example, here’s a NationAccount
linked to a Towny Nation:
public class NationAccount extends NonPlayerAccount {
public NationAccount(final UUID identifier, final String name) {
super(identifier, name);
this.identifier = identifier;
}
@Override
public String type() {
return "nation";
}
@Override
public UUID generateIdentifier(final String name) {
try {
return Objects.requireNonNull(TownyAPI.getInstance().getNation(name)).getUUID();
} catch (final Exception ignore) {
return super.generateIdentifier(name);
}
}
}
Similarly, a TownAccount
can represent Towny Town accounts:
public class TownAccount extends NonPlayerAccount {
public TownAccount(final UUID identifier, final String name) {
super(identifier, name);
this.identifier = identifier;
}
@Override
public String type() {
return "town";
}
@Override
public UUID generateIdentifier(final String name) {
try {
return Objects.requireNonNull(TownyAPI.getInstance().getTown(name)).getUUID();
} catch (final Exception ignore) {
return super.generateIdentifier(name);
}
}
}
2. Implement Account Type Validation
Create validation classes by implementing the AccountTypeCheck
interface. This ensures only valid identifiers are used for your custom accounts:
NationCheck
public class NationCheck implements AccountTypeCheck {
@Override
public Function<String, Boolean> check() {
return value -> {
try {
return value.contains(TownySettings.getNationAccountPrefix());
} catch (Exception e) {
return false;
}
};
}
}
TownCheck
public class TownCheck implements AccountTypeCheck {
@Override
public Function<String, Boolean> check() {
return value -> {
try {
return value.contains(TownySettings.getTownAccountPrefix());
} catch (Exception e) {
return false;
}
};
}
}
3. Register the Custom Account Types
In a handler class, register your custom account types with TNE using the CallbackManager
. Below is an example of how to integrate Towny account types:
public class TownyHandler {
public static void addTypes() {
TNECore.eco().account().addAccountType(TownAccount.class, new TownCheck().check());
TNECore.eco().account().addAccountType(NationAccount.class, new NationCheck().check());
}
}
4. Hook into Plugin Callbacks
Ensure your custom account types are added during the appropriate lifecycle events by using the CallbackManager
. Below is an example:
@Override
public void registerCallbacks(final CallbackManager callbackManager) {
super.registerCallbacks(callbackManager);
callbackManager.addConsumer(TNECallbacks.ACCOUNT_TYPES.toString(), (callback -> {
if (Bukkit.getPluginManager().getPlugin("Towny") != null) {
PluginCore.log().debug("Adding Towny Account Types");
TownyHandler.addTypes();
}
return false;
}));
}
Key Points to Remember
Account Type Name: Ensure the
type()
method in your custom account class returns a unique, descriptive string (e.g.,"town"
or"nation"
).Validation: Use
AccountTypeCheck
to validate identifiers for your account type.Integration: Register custom account types through the
CallbackManager
to ensure they are loaded when TNE initializes.
By following these steps, you can create and integrate custom account types tailored to your server’s needs. For further details, refer to the TNE Developer Documentation or the source files provided.
Last updated