Callbacks
Working with Callbacks in TheNewEconomy
TheNewEconomy (TNE) provides a robust callback system that allows developers to hook into specific points of the plugin's lifecycle. This is in place of using events because this allows TNE callbacks to be the same regardless of platform, whether it's Spigot, Paper, Folia, or Sponge. Callbacks enable you to extend functionality, integrate with external systems, or monitor critical events.
Overview
What is a Callback?
A Callback represents a specific event or action that can trigger custom logic. Each callback is associated with a name identifier and a set of consumers (functions) that handle the event.
Registering a Callback
Step 1: Define the Callback
Before you can add a consumer to a callback, you must register it using the PluginCore
:
Step 2: Add a Consumer
Once the callback is registered, add a consumer to handle its events:
Default Callbacks in TNE
Account Callbacks
ACCOUNT_TYPES
Triggered when account types are loaded.
ACCOUNT_LOAD
Triggered when an account is loaded.
ACCOUNT_SAVE
Triggered when an account is saved.
ACCOUNT_CREATE
Triggered when a new account is created.
ACCOUNT_DELETE
Triggered when an account is deleted.
Transaction Callbacks
TRANSACTION_PRE
Triggered before a transaction is executed.
TRANSACTION_POST
Triggered after a transaction is processed.
Currency Callbacks
CURRENCY_DROP
Triggered when a currency is removed.
CURRENCY_LOAD
Triggered when currencies are loaded.
DENOMINATION_LOAD
Triggered when denominations are loaded.
Example: PreTransactionCallback
The PreTransactionCallback
is invoked before a transaction is executed, allowing you to cancel the transaction if needed.
Example: PostTransactionCallback
The PostTransactionCallback
is invoked after a transaction has been processed. This is ideal for logging or analytics.
Creating Custom Callbacks
Step 1: Implement the Callback
Interface
Callback
InterfaceDefine a custom callback by implementing the Callback
interface:
Step 2: Register the Custom Callback
Use the PluginCore
to register your custom callback:
Step 3: Add a Consumer
Add a consumer to handle the custom callback:
Key Components of the Callback System
CallbackManager
CallbackManager
Manages all callbacks, including registration and execution.
CallbackEntry
CallbackEntry
Represents a single callback and its associated consumers.
TNECallbacks
TNECallbacks
An enumeration of all default callback names.
Notes
Callback Registration: Callbacks must be registered before adding consumers.
Cancellability: Only certain callbacks, like
TRANSACTION_PRE
, can be canceled.Extensibility: Custom callbacks allow seamless integration with external systems.
By leveraging TNE's callback system, you can build powerful extensions and enhance the plugin’s functionality.
Last updated