TNE Docs
  • Getting Started
    • Welcome
    • Currencies Key Concepts
    • Creating a Currency
    • Breakdown of Main Currency Configuration File
    • Breakdown of Denomination Configuration File
    • Commands & Permissions
    • PlaceholderAPI Placeholders
  • FAQ HOWTO
    • HOW TO: Limit Access to Currencies
    • HOWTO: Contribute Translation
    • HOWTO: Share Balances Across Servers
  • Developers
    • Welcome Developers
    • Contributing
    • API Usage
    • Callbacks
    • Platform Implementations
    • Creating Custom Account Type
    • Creating Custom Balance Handlers
    • Creating Custom Account Statuses
    • Creating Custom Balance Format Rule
    • Creating Custom Currency Loader & Saver
    • Creating Custom Currency Type
    • Creating Custom Transaction Checks
    • Creating Custom TaxType
    • Creating Custom Transaction Type
Powered by GitBook
On this page
  • Adding Custom Currency Formatting Rules for TheNewEconomy
  • Overview of Format Rules
  • Creating a Custom Format Rule
  • Examples of Default Rules
  • Example Custom Formatting Rule Usage
  • Notes
  1. Developers

Creating Custom Balance Format Rule

Adding Custom Currency Formatting Rules for TheNewEconomy

The currency formatting system in TheNewEconomy allows developers to define custom rules for displaying currencies. These rules are used in the Format configuration of a currency YAML file (e.g., USD.yml) and control how currency balances are formatted.


Overview of Format Rules

A Format Rule is a component that defines how a placeholder in the currency format is replaced with dynamic content. For example, <symbol> can be replaced with $, and <decimal> can include the decimal separator.

Example Default Format in YAML

Format: "<symbol><major><decimal><minor>"

Creating a Custom Format Rule

1. Implement the FormatRule Interface

To create a custom formatting rule, implement the FormatRule interface. Below is an example of a rule that adds a custom placeholder <custom>, which displays a fixed string.

import net.tnemc.core.account.Account;
import net.tnemc.core.account.holdings.HoldingsEntry;
import net.tnemc.core.currency.format.FormatRule;
import org.jetbrains.annotations.Nullable;

public class CustomRule implements FormatRule {

  @Override
  public String name() {
    return "<custom>";
  }

  @Override
  public String format(@Nullable Account account, HoldingsEntry entry, String format) {
    return format.replace("<custom>", "CustomValue");
  }
}

2. Register the Rule

Register your custom rule with the CurrencyFormatter class to make it available in TNE:

CurrencyFormatter.addRule(new CustomRule());

This should be done during your plugin or integration's initialization phase.


Examples of Default Rules

SymbolRule

Replaces <symbol> with the currency symbol:

public class SymbolRule implements FormatRule {
  @Override
  public String name() {
    return "<symbol>";
  }

  @Override
  public String format(@Nullable Account account, HoldingsEntry entry, String format) {
    final Optional<Currency> currency = entry.currency();
    return currency.map(value -> format.replace("<symbol>", value.getSymbol())).orElse(format);
  }
}

DecimalRule

Handles the <decimal> placeholder, adding or omitting the decimal separator based on the amount:

public class DecimalRule implements FormatRule {
  @Override
  public String name() {
    return "<decimal>";
  }

  @Override
  public String format(@Nullable Account account, HoldingsEntry entry, String format) {
    if (entry.currency().isPresent()) {
      if (entry.asMonetary().scale() == 0) {
        return format.replace("<decimal>", "");
      }
      return format.replace("<decimal>", entry.currency().get().getDecimal());
    }
    return format;
  }
}

Example Custom Formatting Rule Usage

YAML Configuration

After adding your custom rule, it can be used in the Format configuration of a currency YAML file:

Format: "<symbol><major><decimal><minor><custom>"

When applied, the <custom> placeholder will be replaced with the value defined in your CustomRule.


Notes

  1. Registration: Always register custom rules using CurrencyFormatter.addRule().

  2. Compatibility: Ensure your custom rules align with TNE's currency formatting system.

  3. Dynamic Content: Use the Account and HoldingsEntry parameters in your rule to provide dynamic formatting based on player or balance data.


By following these steps, you can expand TNE's currency formatting system to suit your server's unique needs. For further reference, review default rules such as SymbolRule, DecimalRule, and ShortenRule.

PreviousCreating Custom Account StatusesNextCreating Custom Currency Loader & Saver

Last updated 5 months ago