Skip to main content

Creating A New Currency Java

Creating a new currency is very easy simply extend our Currency class

Currency

Once extended you should implement its methods and end up with a class like

package org.insurgencedev.mobcoins;

import lombok.NonNull;
import org.bukkit.entity.Player;
import org.insurgencedev.insurgencesets.api.ISetsAPI;
import org.insurgencedev.insurgencesets.api.currency.Currency;
import org.insurgencedev.insurgencesets.models.currency.TransactionTypes;

public class ExampleCurrency extends Currency {


    public ExampleCurrency() {
        super("example", "ex");
    }

    @Override
    public boolean canAfford(@NonNull Player player, @NonNull Object amount) {
        return true;
    }

    @NonNull
    @Override
    public TransactionTypes handleDeposit(@NonNull Player player, @NonNull Object amount, String armorSetName) {
       return TransactionTypes.SUCCESS;
    }

    @NonNull
    @Override
    public TransactionTypes handleTransaction(@NonNull Player player, @NonNull Object amount, String armorSetName) {
     return TransactionTypes.SUCCESS;
    }
}

MobCoins Example

In the next example we are gonna make a currency for MobCoins

package org.insurgencedev.mobcoins;

import lombok.NonNull;
import me.swanis.mobcoins.MobCoinsAPI;
import org.bukkit.entity.Player;
import org.insurgencedev.insurgencesets.api.ISetsAPI;
import org.insurgencedev.insurgencesets.api.currency.Currency;
import org.insurgencedev.insurgencesets.models.currency.TransactionTypes;

public class MobCoinCurrency extends Currency {


    public MobCoinCurrency() {
        super("SM-Mobcoins", "SM");
    }

    @Override
    public boolean canAfford(@NonNull Player player, @NonNull Object amount) {
        return MobCoinsAPI.getProfileManager().getProfile(player).getMobCoins() >= (long) amount;
    }

    @NonNull
    @Override
    public TransactionTypes handleDeposit(@NonNull Player player, @NonNull Object amount, String armorSetName) {
        if (isInvalidSet(armorSetName)) {
            return TransactionTypes.FAIL;
        }

        MobCoinsAPI.getProfileManager().getProfile(player).setMobCoins(MobCoinsAPI.getProfileManager().getProfile(player).getMobCoins() + (long) amount);
        return TransactionTypes.SUCCESS;
    }

    @NonNull
    @Override
    public TransactionTypes handleTransaction(@NonNull Player player, @NonNull Object amount, String armorSetName) {
        if (isInvalidSet(armorSetName)) {
            return TransactionTypes.FAIL;
        }

        long amountL = (long) amount;
        if (MobCoinsAPI.getProfileManager().getProfile(player).getMobCoins() < amountL) {
            return TransactionTypes.FAIL_INSUFFICIENT_FUNDS;
        }

        MobCoinsAPI.getProfileManager().getProfile(player).setMobCoins(MobCoinsAPI.getProfileManager().getProfile(player).getMobCoins() - (long) amount);
        return TransactionTypes.SUCCESS;
    }

    private boolean isInvalidSet(String armorSet) {
        return armorSet == null || ISetsAPI.getArmorSetManager().findArmorSet(armorSet) == null;
    }
}

Registering the currency

ISetsAPI.getCurrencyManager().registerCurrency(new MobCoinCurrency());

Make sure to register it again when our plugin is reloaded