> For the complete documentation index, see [llms.txt](https://foresttech.gitbook.io/redshop/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://foresttech.gitbook.io/redshop/developer-api/custom-currency.md).

# Custom currency

You can add your custom currency. You have to extend our abstract class **SupportedCurrency** and implement your own methods for transfering and checking funds.

### Constructor parameters

{% tabs %}
{% tab title="Fullname" %}
Full name of the currency. This name will be used on signs.&#x20;

{% hint style="danger" %}
Must be unique across currencies!
{% endhint %}
{% endtab %}

{% tab title="Enabled" %}
Whether the currency shall be enabled.&#x20;
{% endtab %}

{% tab title="Symbol" %}
Currency symbol used as suffix in the messages.

{% hint style="danger" %}
Must be unique across currencies!
{% endhint %}
{% endtab %}

{% tab title="Aliases" %}
List of possible aliases players can use in currency line instead of currency name.

{% hint style="danger" %}
All elements must be unique across all currencies!
{% endhint %}
{% endtab %}

{% tab title="Offline Mode" %}
Whether the currency class can handle offline players. If set to `false`, you can use `giveOffline(offlinePlayer, amount)` method in the #giveAmount method, so RedShop will save data and process the payment when player joins.

See the example below.
{% endtab %}

{% tab title="Plugin name" %}
Name of the currency (usually plugin's name) - unique identifier of the currency.

{% hint style="danger" %}
Must be unique across currencies!
{% endhint %}
{% endtab %}
{% endtabs %}

### ExampleCurrency.java

```java
/**
 *  Your currency object needs to extend SupportedCurrency abstract class
 */
public class ExampleCurrency extends SupportedCurrency {

    //Your economy class
    private ExampleEconomy myEconomy;

    /**
     *  Currency constructor. You can use your own variables if needed.
     *
     *  Fun-fact: Check config.yml - you can see we use the same system for pre-defined currencies
     */
    public ExampleCurrency() {
        //Calling abstract class constructor - REQUIRED!
        super(
            "ExampleDollars", //used as display name on signs, MUST BE UNIQUE!
            true, //if the currency is available (enabled)
            "$", //symbol used as suffix in messages, MUST BE UNIQUE!
            Arrays.asList("dol", "doll"), //list of aliases players can use instead of fullname, MUST BE UNIQUE!
            false, //if the currency/economy can handle offline player transactions - if not, we'll use pending.yml file to store the pending payments until player joins
            "ExampleCurrency" //system name of the currency (usually name of the plugin), MUST BE UNIQUE!
        );

        if (enabled) {
            //Setup your economy
            myEconomy = MyPlugin.getMyEconomy();
            if (myEconomy == null) {
                enabled = false;
            }
        }
    }

    /**
     *   Shall the currency use double or integer?
     *
     *   true = allows prices with decimal (e.g. 15.56)
     *   false = allows only integer prices (e.g. 15)
     */
    @Override
    public boolean useDoubleValues() {
        return true;
    }

    @Override
    public double getAmount(OfflinePlayer offlinePlayer) {
        if (!enabled) {
            return 0;
        }
        //Plugin checks balance of only online players, so we can cast it to Player
        return myEconomy.getBalance((Player) offlinePlayer);
    }

    @Override
    public boolean giveAmount(OfflinePlayer offlinePlayer, double amount) {
        if (!enabled) {
            return false;
        }
        //If the seller is offline and currency cannot pay offlinePlayer, we need to handle it
        if (!offlinePlayer().isOnline() && !offlineReady()) {
            //API method saves the amount to pending.yml and auto-pays the amount when the player joins
            return giveOffline(offlinePlayer, amount);
        }
 
        //Otherwise, OfflinePlayer is online, so we can cast it to Player
        return myEconomy.depositPlayer(offlinePlayer.getPlayer(), amount).transactionSuccess();
    }

    @Override
    public boolean takeAmount(OfflinePlayer offlinePlayer, double amount) {
        if (!enabled) {
            return false;
        }
        //Plugin takes money only from online players, so we can cast it to Player
        return myEconomy.withdrawPlayer((Player) offlinePlayer, amount).transactionSuccess();
    }

}
```

### Main.java

```java
@Override
public void onEnable() {
    //... Your stuff ...
    if (setupCurrency()) {
        this.getServer().getConsoleSender().sendMessage("Custom RedShop currency successfully registered!");
    } else {
        this.getServer().getConsoleSender().sendMessage("Custom RedShop currency registration failed!");
    }
    //... Your other stuff ...
}

/**
 *   setupCurrency method registers your currency to RedShop
 *
 *   Choose ONE of the method below - both does the same thing
 */
private boolean setupCurrency() {
    /* METHOD ONE */
    ExampleCurrency customCurrency = new ExampleCurrency();
    return customCurrency.register();

    /* METHOD TWO */
    RedShop redShopAPI = this.getServer().getPluginManager().getPlugin("RedShop");
    if (redShopAPI == null || redShopAPI.isEnabled()) {
        //RedShop is not installed OR is disabled
        return false;
    }
    return redShopAPI.registerCurrency(new ExampleCurrency());
}
```
