Skip to content

Creating Custom Enchants

Ján Kluka edited this page Nov 10, 2022 · 3 revisions

Create your own Custom Enchant

From version 1.1.12 of UltraPrisonCore you are able to create custom enchants. With little of Java Development knowledge along with Spigot API, you should be able to easily create your own enchants. Please see example how to create your custom enchants here

Main class example

package me.drawethree.ultraprisontestenchant;

import dev.drawethree.ultraprisoncore.UltraPrisonCore;
import dev.drawethree.ultraprisoncore.enchants.UltraPrisonEnchants;
import dev.drawethree.ultraprisoncore.enchants.api.UltraPrisonEnchantsAPI;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

/**
 * Main class of our plugin. Before creating a plugin, please make sure you have UltraPrisonCore and spigot in your classpath!
 */
public final class UltraPrisonTestEnchant extends JavaPlugin {

    private static final String UPC_PLUGIN_NAME = "UltraPrisonCore";

    private TestEnchant enchant;

    private UltraPrisonEnchantsAPI api;

    @Override
    public void onEnable() {

        // Check if we have UltraPrisonCore plugin enabled
        if (!Bukkit.getPluginManager().isPluginEnabled(UPC_PLUGIN_NAME)) {
            this.getLogger().warning("Unable to hook into UltraPrisonCore! Disabling...");
            this.getServer().getPluginManager().disablePlugin(this);
            return;
        }

        UltraPrisonCore ultraPrisonCore = UltraPrisonCore.getInstance();

        // Check if we have enchants module enabled
        if (!ultraPrisonCore.isModuleEnabled(UltraPrisonEnchants.MODULE_NAME)) {
            this.getLogger().warning("Enchants module is disabled! Disabling...");
            this.getServer().getPluginManager().disablePlugin(this);
            return;
        }


        // Get the API for Enchants module
        this.api = ultraPrisonCore.getEnchants().getApi();

        //Create new instance of our custom enchant
        this.enchant = new TestEnchant();

        //Register it via API
        this.api.registerEnchant(this.enchant);

        //You are done. Have fun with your custom enchants! :)

    }

    @Override
    public void onDisable() {
        // Unregistering of enchant when we disable the plugin
        this.api.unregisterEnchant(this.enchant);
    }
}

TestEnchant class example

package me.drawethree.ultraprisontestenchant;

import dev.drawethree.ultraprisoncore.enchants.UltraPrisonEnchants;
import dev.drawethree.ultraprisoncore.enchants.model.UltraPrisonEnchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.ItemStack;

import java.util.concurrent.ThreadLocalRandom;

/**
 * Class representing your custom enchant. When creating custom enchants please make sure that your class extends 'UltraPrisonEnchantment'
 */
public class TestEnchant extends UltraPrisonEnchantment {

    /**
     * Class attribute chance - Represent the chance of triggering the enchant
     */
    private double chance;

    /**
     * Constructor of your custom enchant. You can load any parameters after calling super() constructors. Make sure to include unique id!
     * Please make sure that enchants.yml config in UltraPrisonCore (not in this plugin) has this enchant properties in config!
     */
    public TestEnchant() {
        super(UltraPrisonEnchants.getInstance(), 99);
        this.chance = this.plugin.getEnchantsConfig().getYamlConfig().getDouble("enchants." + id + ".Chance");
    }

    /**
     * Overridden method what should be done when player equips pickaxe with this enchant
     *
     * @param p       Player who equipped
     * @param pickAxe ItemStack of pickaxe that is held in hand
     * @param level   level of enchant
     */
    @Override
    public void onEquip(Player p, ItemStack pickAxe, int level) {
        p.sendMessage("You have equipped a pickaxe with TestEnchant!");
    }

    /**
     * Overridden method what should be done when player un-equips pickaxe with this enchant
     *
     * @param p       Player who equipped
     * @param pickAxe ItemStack of pickaxe that is held in hand
     * @param level   level of enchant
     */
    @Override
    public void onUnequip(Player p, ItemStack pickAxe, int level) {
        p.sendMessage("You have unequipped a pickaxe with TestEnchant!");
    }


    /**
     * Overridden method what should be done when player breaks a block in mine with this enchant
     *
     * @param e            BlockBreakEvent
     * @param enchantLevel level of enchant
     */
    @Override
    public void onBlockBreak(BlockBreakEvent e, int enchantLevel) {
        if (this.chance * enchantLevel >= ThreadLocalRandom.current().nextDouble(100)) {
            e.getPlayer().sendMessage("You have triggered the enchant!");
        }
    }

    /**
     * This method is called when UltraPrisonCore is reloaded, it should reload every needed attribute from config.
     */
    @Override
    public void reload() {
        this.chance = this.plugin.getEnchantsConfig().getYamlConfig().getDouble("enchants." + id + ".Chance");
    }

    /**
     * Overridden method to get author who created this enchant.
     *
     * @return String - name of author
     */
    @Override
    public String getAuthor() {
        return "Drawethree";
    }
}

This is what you will add into enchants.yml of UltraPrisonCore

  '99':
    RawName: test
    Name: '&bTest Enchant'
    Material: BOOK
    Enabled: true
    InGuiSlot: 47
    Increase-Cost-by: 5000
    Max: 250
    Chance: 9.0E-5
    Cost: 5000
    Description: '&7&o(( This is our custom enchant! ))'
    Refund:
      Enabled: true
      InGuiSlot: 47