Skip to main content

Creating A New Currency Java

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

Currency

After extending the class, ensure to implement its methods, resulting in a class structured as depicted below

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 going to make a currency for SuperMobCoins

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;
    }
}

You can choose to let the armor sets increase your currency earnings. When your currency is provided to the player, it must trigger an event at which you can listen to and boost accordingly. Class must implement org.bukkit.event.Listener.

package org.insurgencedev.mobcoins;

import lombok.NonNull;
import me.swanis.mobcoins.MobCoinsAPI;
import me.swanis.mobcoins.events.MobCoinsReceiveEvent;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.insurgencedev.insurgencesets.api.ISetsAPI;
import org.insurgencedev.insurgencesets.api.currency.Currency;
import org.insurgencedev.insurgencesets.libs.fo.remain.nbt.NBTItem;
import org.insurgencedev.insurgencesets.models.armorset.ArmorSet;
import org.insurgencedev.insurgencesets.models.currency.TransactionTypes;
import org.insurgencedev.insurgencesets.models.upgrade.Boost;
import org.insurgencedev.insurgencesets.models.upgrade.Upgrade;
import org.insurgencedev.insurgencesets.settings.ArmorSetData;
import org.insurgencedev.insurgencesets.settings.ISetsPlayerCache;

public class MobCoinCurrency extends CurrencyMobCoinReceiveListener implements Listener {

    private static final String name = "SM-Mobcoins";

    public MobCoinCurrency() {
        super(name, "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;
    }

    @EventHandler
    public void onEarn(MobCoinsReceiveEvent event) {
        Player player = event.getProfile().getPlayer();
        ISetsPlayerCache cache = ISetsPlayerCache.from(player);

        ItemStack[] armorContents = player.getInventory().getArmorContents();
        for (ItemStack item : armorContents) {
            if (item != null) {
                NBTItem nbtItem = new NBTItem(item);
                if (!nbtItem.hasTag("armorSet")) {
                    continue;
                }

                ArmorSet armorSet = ISetsAPI.getArmorSetManager().findArmorSet(nbtItem.getString("armorSet"));
                if (armorSet == null) {
                    continue;
                }

                String armorSetName = armorSet.getName();
                String itemType = item.getType().name().split("_")[1];
                ArmorSetData armorSetData = cache.getArmorSetData(armorSetName);
                if (armorSetData == null) {
                    continue;
                }

                Object levels = getLevelsFromType(itemType, armorSetData);
                if (levels instanceof Integer) {
                    Upgrade upgrade = armorSet.findPieceLevels(itemType, (Integer) levels);
                    if (upgrade == null) {
                        continue;
                    }

                    for (Boost boost : upgrade.getBoosts()) {
                        if ("CURRENCY".equals(boost.getNamespace()) && boost.getType().equals(name)) {
                            double boostAmount = boost.getBOOST_SETTINGS().getDouble("Boost_Amount");
                            event.setAmount(calcAmountToGive(event.getAmount(), boost, boostAmount));
                        }
                    }
                }
            }
        }
    }

    private long calcAmountToGive(long amountFromEvent, Boost boost, double boostAmount) {
        if (boost.isPercent()) {
            return (long) (amountFromEvent * (1 + boostAmount / 100));
        } else {
            return (long) (amountFromEvent * (boostAmount < 1 ? 1 + boostAmount : boostAmount));
        }
    }

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

    private Object getLevelsFromType(String type, ArmorSetData armorSetData) {
        return switch (type) {
            case "HEAD", "HELMET" -> armorSetData.getHelmetLevels();
            case "CHESTPLATE" -> armorSetData.getChestplateLevels();
            case "LEGGINGS" -> armorSetData.getLeggingsLevels();
            case "BOOTS" -> armorSetData.getBootsLevels();
            default -> false;
        };
    }

}

Main class

package org.insurgencedev.mobcoins;

import org.bukkit.Bukkit;
import org.insurgencedev.insurgencesets.api.ISetsAPI;
import org.insurgencedev.insurgencesets.api.addon.ISetsAddon;
import org.insurgencedev.insurgencesets.api.addon.InsurgenceSetsAddon;
import org.insurgencedev.insurgencesets.libs.fo.Common;

@ISetsAddon(name = "SM-Mobcoins", version = "1.0.0", author = "Insurgence Dev Team", description = "Use SuperMobCoins's mobcoins as a currency")
public class CurrencyAddon extends InsurgenceSetsAddon {

    private MobCoinCurrency mobCoinCurrency;

    @Override
    public void onAddonStart() {
    }

    @Override
    public void onAddonReloadablesStart() {
        if (Bukkit.getPluginManager().isPluginEnabled("SuperMobCoins")) {
            mobCoinCurrency = registerEvent(new MobCoinCurrency(MobCoinReceiveEvent();
            registerEvent(mobCoinCurrency));
            ISetsAPI.getCurrencyManager().registerCurrency(mobCoinCurrency)new MobCoinCurrency());
        } else {
            Common.log("SuperMobCoins plugin is required to use SM-Mobcoins");
        }
    }
@Override
    public void onAddonReload() {
    }

    @Override
    public void onAddonStop() {
    }

}