codepenEnchants API

Overview

The Enchants API enables external plugins to interact with PhantomDungeons sword enchant system. Query enchant metadata, manage player progression, upgrade enchants with economy integration, and create custom enchant implementations with full control over behaviour.


Getting Started

Follow these steps to integrate with the enchant system.

1

Get the Instance

Access the singleton API instance from the static provider.

Example.java
import me.fergs.phantomdungeons.modules.enchants.api.EnchantAPI;

// Get the singleton instance
EnchantAPI enchantAPI = EnchantAPI.get();
circle-exclamation
2

Cache for Performance

Store the API instance in your plugin for better performance.

MyPlugin.java
public class MyPlugin extends JavaPlugin {
    private EnchantAPI enchantAPI;
    
    @Override
    public void onEnable() {
        try {
            this.enchantAPI = EnchantAPI.get();
            getLogger().info("✓ Hooked into Enchants!");
        } catch (IllegalStateException e) {
            getLogger().warning("PhantomDungeons not loaded!");
        }
    }
    
    public EnchantAPI getEnchantAPI() {
        return enchantAPI;
    }
}
3

Safe Null Checks

Always ensure the API is available before using it.

ExampleCheck.java
if (enchantAPI == null) {
    getLogger().warning("Enchants API not available!");
    return;
}

Core Operations

Querying Enchant Metadata

Enchant metadata is cached in memory for instant lookups. Use these methods to get enchant information.

EnchantQueries.java
// Check if an enchant exists in the system
if (!enchantAPI.hasEnchant("flame-strike")) {
    player.sendMessage("§cThis enchant does not exist!");
    return;
}

// Get detailed enchant configuration
EnchantConfig config = enchantAPI.getEnchant("flame-strike");
if (config != null) {
    System.out.println("Max Level: " + config.getProgression().maxLevel());
    System.out.println("Currency: " + config.getUpgrade().currency());
}

// Get all available enchants
Map<String, EnchantConfig> allEnchants = enchantAPI.getAllEnchants();
for (String enchantId : enchantAPI.getEnchantIds()) {
    System.out.println("Enchant: " + enchantId);
}

Checking Player Progress

Read a player's enchant level and unlock status.

Upgrading Enchants

Upgrade an enchant for a player. The API handles currency deduction and events.

Upgrade Costs

Query the cost of upgrading an enchant.


Advanced Management

Setting Enchant Levels

Directly set an enchant level for a player (use with caution).

Maximum Levels

Query or check maximum enchant levels.


Creating Custom Enchants

Step 1: Create a Config File

First, create an enchant configuration file in the enchants/ folder:

File: plugins/PhantomDungeons/enchants/my-custom-enchant.yml

Event Handling

React to enchant events in your plugin:


FAQ

chevron-rightWhere do I put the config file?hashtag

In plugins/PhantomDungeons/enchants/ with the filename {enchant-id}.yml

chevron-rightCan I have unlimited enchants?hashtag

Yes! Add as many .yml files as you want in the enchants folder.

chevron-rightDoes the API work for offline players?hashtag

Yes. Most methods accept UUID in addition to Player objects.

chevron-rightWhat happens on server restart?hashtag

All enchant data is hardcoded into the players sword, we decided to do this approach as it can allow for dynamic gameplay.

chevron-rightCan I remove/hide an enchant from the system?hashtag

Remove its .yml config file and restart the server. Players keep their unlocked levels in the sword's NBT.

chevron-rightHow do custom implementations interact with config?hashtag

Custom implementations read progression/activation from config, but override logic with their own implementation.

chevron-rightIs enchant proc async?hashtag

90% Yes. All enchant executions are async. Use Bukkit.getScheduler().runTask() if you need the main thread. However, if the event queue needs flushed due to excessive events, it will process them on the main thread (known as a Flush & Reset). Our team is actively working on a more performant approach to solve this issue.


Last updated