Creating Custom Currency Loader & Saver
Creating a Custom Currency Loader and Saver for TheNewEconomy
TheNewEconomy provides flexibility for managing currencies by allowing developers to implement custom currency loaders and savers. These components enable advanced control over how currencies are read from or written to storage.
Overview
Currency Loader
A Currency Loader is responsible for loading currencies and their denominations from a specified storage or configuration source.
Currency Saver
A Currency Saver handles saving currencies and their denominations to storage or configuration files.
Registration
To use your custom loader or saver, register it during the plugin's initialization:
TNECore.eco().currency().setLoader(new CustomCurrencyLoader());
TNECore.eco().currency().setSaver(new CustomCurrencySaver());
Creating a Custom Currency Loader
Step 1: Implement the CurrencyLoader
Interface
CurrencyLoader
InterfaceCreate a class that implements the CurrencyLoader
interface. Below is an example:
import net.tnemc.core.currency.Currency;
import net.tnemc.core.currency.CurrencyLoader;
import net.tnemc.core.utils.exceptions.NoValidCurrenciesException;
import java.io.File;
public class CustomCurrencyLoader implements CurrencyLoader {
@Override
public void loadCurrencies(File directory) throws NoValidCurrenciesException {
// Logic to load all currencies from the directory
}
@Override
public boolean loadCurrency(File directory, String name) {
// Logic to load a specific currency by name
return true;
}
@Override
public boolean loadCurrency(File directory, File curDirectory) {
// Logic to load a specific currency by file
return true;
}
@Override
public boolean loadDenominations(File directory, Currency currency) {
// Logic to load all denominations for a currency
return true;
}
@Override
public boolean loadDenomination(File directory, Currency currency, String name) {
// Logic to load a specific denomination
return true;
}
@Override
public boolean loadDenomination(Currency currency, File denomFile) {
// Logic to load a specific denomination by file
return true;
}
}
Creating a Custom Currency Saver
Step 1: Implement the CurrencySaver
Interface
CurrencySaver
InterfaceCreate a class that implements the CurrencySaver
interface. Below is an example:
import net.tnemc.core.currency.Currency;
import net.tnemc.core.currency.CurrencySaver;
import net.tnemc.core.currency.Denomination;
import java.io.File;
public class CustomCurrencySaver implements CurrencySaver {
@Override
public void saveCurrencies(File directory) {
// Logic to save all currencies
}
@Override
public void saveCurrenciesUUID(File directory) {
// Logic to save only currency UUIDs
}
@Override
public void saveCurrency(File directory, Currency currency) {
// Logic to save a specific currency
}
@Override
public void saveDenomination(File directory, Currency currency, Denomination denomination) {
// Logic to save a specific denomination
}
}
Example: Registering Custom Loader and Saver
To activate your custom loader and saver, register them as follows:
TNECore.eco().currency().setLoader(new CustomCurrencyLoader());
TNECore.eco().currency().setSaver(new CustomCurrencySaver());
Notes
Error Handling: Ensure your loader and saver handle errors gracefully to avoid corrupting currency data.
File Structure: Maintain a clear file structure for storing and retrieving currencies and denominations.
Testing: Thoroughly test your loader and saver to ensure compatibility with the TNE ecosystem.
By implementing and registering custom loaders and savers, you can extend TheNewEconomy's capabilities to integrate with unique storage solutions or advanced configurations.
Last updated