Custom enchant plugin

Status
This thread has been locked.

Clems02

Feedback score
0
Posts
15
Reactions
0
Resources
0
Hi,

I am creating a custom enchantment plugin with 10 custom enchant.

The enchantments are created. I wonder how to load them into the main class so I can use them?
I can do it for 1 enchantment, but for 10 no?

Code:
public class Main extends JavaPlugin implements Listener {

    public Explosive ench = new Explosive(102);
    
    @Override
    public void onEnable() {
        loadEnchantments();     
        Bukkit.getPluginManager().registerEvents(new EventListener(), this);
        Bukkit.getPluginManager().registerEvents(new Explosive(101), this);
    }
    
    @SuppressWarnings("unchecked")
    public void onDisable() {
        try {
            Field byIdField = Enchantment.class.getDeclaredField("byId");
            Field byNameField = Enchantment.class.getDeclaredField("byName");
            
            byIdField.setAccessible(true);
            byNameField.setAccessible(true);

            HashMap<Integer, Enchantment> byId = (HashMap<Integer, Enchantment>) byIdField.get(null);
            HashMap<Integer, Enchantment> byName = (HashMap<Integer, Enchantment>) byNameField.get(null);
            
            if(byId.containsKey(ench.getId())) {
                byId.remove(ench.getId());
            }
            
            if(byName.containsKey(ench.getName())) {
                byName.remove(ench.getName());
            }
            
        } catch (Exception ignored) {
    }
    }

    private void loadEnchantments() {
        try {
            try {
                Field f = Enchantment.class.getDeclaredField("acceptingNew");
                f.setAccessible(true);
                f.set(null, true);

            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                Enchantment.registerEnchantment(ench);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

It's working for the enchantment "explosive" (id 102).
But how load the other plugin without copying everything ?

I'm on 1.12.2 spigot.

Thank's for your help.
 
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

vk2gpz

Premium
Feedback score
-1
Posts
0
Reactions
21
Resources
0
If you only need to support old (pre 1.13) server what you've done is ok, but your main class and all your custom enchant won't work if you need to make your plugin and custom enchant work for all other 1.13 and above servers.

Also, explicitly specifying the id number (magic number) should also be avoided. someone else might have another custom enchant with the same id as your custom enchant.
 

simonsejse

Feedback score
0
Posts
5
Reactions
0
Resources
0
Hi,

I am creating a custom enchantment plugin with 10 custom enchant.

The enchantments are created. I wonder how to load them into the main class so I can use them?
I can do it for 1 enchantment, but for 10 no?

Code:
public class Main extends JavaPlugin implements Listener {

    public Explosive ench = new Explosive(102);
   
    @Override
    public void onEnable() {
        loadEnchantments();    
        Bukkit.getPluginManager().registerEvents(new EventListener(), this);
        Bukkit.getPluginManager().registerEvents(new Explosive(101), this);
    }
   
    @SuppressWarnings("unchecked")
    public void onDisable() {
        try {
            Field byIdField = Enchantment.class.getDeclaredField("byId");
            Field byNameField = Enchantment.class.getDeclaredField("byName");
           
            byIdField.setAccessible(true);
            byNameField.setAccessible(true);

            HashMap<Integer, Enchantment> byId = (HashMap<Integer, Enchantment>) byIdField.get(null);
            HashMap<Integer, Enchantment> byName = (HashMap<Integer, Enchantment>) byNameField.get(null);
           
            if(byId.containsKey(ench.getId())) {
                byId.remove(ench.getId());
            }
           
            if(byName.containsKey(ench.getName())) {
                byName.remove(ench.getName());
            }
           
        } catch (Exception ignored) {
    }
    }

    private void loadEnchantments() {
        try {
            try {
                Field f = Enchantment.class.getDeclaredField("acceptingNew");
                f.setAccessible(true);
                f.set(null, true);

            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                Enchantment.registerEnchantment(ench);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

It's working for the enchantment "explosive" (id 102).
But how load the other plugin without copying everything ?

I'm on 1.12.2 spigot.

Thank's for your help.
The constructor requires a NamespacedKey object
https://hub.spigotmc.org/javadocs/spigot/org/bukkit/NamespacedKey.html
 

vk2gpz

Premium
Feedback score
-1
Posts
0
Reactions
21
Resources
0
Last edited:

simonsejse

Feedback score
0
Posts
5
Reactions
0
Resources
0
he is on 1.12.2, you do not need NamespacedKey. You do from 1.13.

This is one thing things, that makes it difficult to make Custom Enchant plugin which works across all 1.7.10 - 1.15.x server version. That's why I recommend using TokenEnchant and their Custom Enchants (CEs) because they will work on all those server versions.
Oh, ye that's true. Didn't read the version, my bad.
 
Status
This thread has been locked.
Top