Skip to content

Developer API

byteful edited this page Jul 10, 2023 · 2 revisions

NightMarket Developer API

Welcome! This wiki page will help you get started with NightMarket's developer API.

Soon you'll be able to:

  • Get NightMarket's plugin instance
  • Register custom currency handlers
  • Access player data such as current shop items and purchased items
  • Register custom shop items via API
  • Modify NightMarket database contents

How to get NightMarket's plugin instance:

final NightMarketPlugin nightMarket = NightMarketPlugin.getInstance();

How to register custom currency handlers:

class CurrencyListener implements Listener {
    @EventHandler
    public void on(CurrencyRegisterEvent event) {
        event.register(new MyCustomCurrency());
    }
}

// Copied from VaultCurrency class
class MyCustomCurrency implements Currency {
    private transient Economy eco;

    @Override
    public String getId() {
        return "vault";
    }

    @Override
    public void load() {
        final RegisteredServiceProvider<Economy> ecoP = Bukkit.getServicesManager().getRegistration(Economy.class);
        if (ecoP == null) {
            throw new RuntimeException("Failed to find Vault binding for Economy.");
        }

        this.eco = ecoP.getProvider();
    }

    @Override
    public boolean canLoad() {
        return Bukkit.getPluginManager().isPluginEnabled("Vault");
    }

    @Override
    public boolean canPlayerAfford(UUID player, double price) {
        return eco.has(Bukkit.getOfflinePlayer(player), Math.abs(price));
    }

    @Override
    public void withdraw(UUID player, double amount) {
        eco.withdrawPlayer(Bukkit.getOfflinePlayer(player), Math.abs(amount));
    }
}

How to access player data:

final NightMarketPlugin nightMarket = NightMarketPlugin.getInstance();

// If player is online
final PlayerShop shop = nightMarket.getPlayerShopManager().get(UUID);

// If player is offline (recommended to do this DB call async)
final Optional<PlayerShop> shop = nightMarket.getDataStoreProvider().getPlayerShop(UUID); // Returns an optional which may not have a shop if the player has never joined.

// If player has never joined and is offline, you will need to make a new shop.
final PlayerShop shop = new PlayerShop(nightMarket.getShopItemRegistry(), UUID);

How to register custom shop items:

// First, make the actual shop item object.
final ShopItem item = new ShopItem(id, icon, command, currency, amount, rarity, purchaseLimit);

NightMarketPlugin.getInstance().getShopItemRegistry().register(item);

How to modify NightMarket DB contents:

// You probably shouldn't use these methods unless the player is offline and their data is only available in DB.
final DataStoreProvider dataStore = NightMarketPlugin.getInstance().getDataStoreProvider();
dataStore.setPlayerShop(PlayerShop);
dataStore.getPlayerShop(UUID);