Solved Custom Enchantments

Discussion in 'Plugin Development' started by Sylian, Jul 28, 2019.

Thread Status:
Not open for further replies.
  1. Offline

    Sylian

    So i have been working on creating my first custom enchantment but since the video i followed to learn about it is outdated so the "super(id);" is not an int but a NameSpacedKey now and i have tried but sadly it doesnt work so now i have used a whole day but not gonna give up so please if someone here can help then i would be so happy!


    (This is the main file)
    MainEnchants.java
    Code:
    package Sylian.CustomEnchantsPlugin;
    import java.lang.reflect.Field;
    import java.util.ArrayList;
    import java.util.HashMap;
    
    import org.bukkit.NamespacedKey;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class MainEnchants extends JavaPlugin implements Listener  {
    
        public FireEnchantment ench = new FireEnchantment(new NamespacedKey( this, "TEST_ENCH"));
       
        public void onEnable() {
            LoadEnchantsments();
            this.getServer().getPluginManager().registerEvents(this, this);
        }
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            Player player = e.getPlayer();
           
            ItemStack item = new ItemStack(Material.DIAMOND_AXE);
           
            ItemMeta itemMeta = item.getItemMeta();
           
            ArrayList<String> lore = new ArrayList<String>();
           
            lore.add(ChatColor.DARK_AQUA + ench.getName());
           
            itemMeta.setDisplayName(ChatColor.RED + "Test Axe");
           
            itemMeta.setLore(lore);
    
            item.setItemMeta(itemMeta);
           
            item.addUnsafeEnchantment(ench, 6);
           
            player.getInventory().addItem(item);
           
           
           
        }
       
        @SuppressWarnings("unchecked")
        public void onDisable() {
            try {
               
                Field byKeyField = Enchantment.class.getDeclaredField("byKey");
                Field byNameField = Enchantment.class.getDeclaredField("byName");
                byKeyField.setAccessible(true);
               
                byNameField.setAccessible(true);
               
                HashMap<Integer, Enchantment> byKey = (HashMap<Integer, Enchantment>) byKeyField.get(null);
                HashMap<Integer, Enchantment> byName = (HashMap<Integer, Enchantment>) byNameField.get(null);
               
                if(byKey.containsKey(ench.GetKey())) {
                    byKey.remove(ench.GetKey());
                }
               
                if(byName.containsKey(ench.getName())) {
                    byName.remove(ench.getName());
                }
               
               
            }catch(Exception ignored) {
               
            }
        }
    
        private void LoadEnchantsments() {
           
            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();
            }
        }
       
    }
    


    (This is my custom enchatment)
    FireEnchatment.java
    Code:
    package Sylian.CustomEnchantsPlugin;
    
    import org.bukkit.NamespacedKey;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.enchantments.EnchantmentTarget;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.inventory.ItemStack;
    
    public class FireEnchantment extends Enchantment implements Listener {
       
    
       
        public FireEnchantment(NamespacedKey id) {
            super(id);
        }
    
        public void onHit(EntityDamageByEntityEvent e) {
            if(e.getDamager() instanceof Player) {
                Player player = (Player) e.getDamager();
               
                ItemStack mainHand = player.getInventory().getItemInMainHand();
               
                if(mainHand.containsEnchantment(this)) {
                    player.sendMessage("THIS IS WORKING!");
                }
            }
        }
       
        public String GetKey() {
            return "TEST_ENCH";
        }
    
        @Override
        public boolean canEnchantItem(ItemStack arg0) {
           
            return true;
        }
    
        @Override
        public boolean conflictsWith(Enchantment arg0) {
           
            return false;
        }
    
        @Override
        public EnchantmentTarget getItemTarget() {
           
            return null;
        }
    
        @Override
        public int getMaxLevel() {
           
            return 15;
        }
    
        @Override
        public String getName() {
           
            return "Fire Touch";
        }
    
        @Override
        public int getStartLevel() {
           
            return 1;
        }
    
        @Override
        public boolean isCursed() {
           
            return false;
        }
    
        @Override
        public boolean isTreasure() {
           
            return false;
        }
    
    }

    The youtube video i followed is this one.


    Thank you all in advance!
     
  2. Offline

    Kars

    What exactly is not working?
     
  3. Offline

    Sylian

    Sorry just woke up and was tired last night.

    I get the weapon and the custom enchantment is on it like in the picture i uploaded here.

    But when i attack it doesnt do what the custom enchantment should do like the test i did where it should do
    player.sendMessage("THIS IS WORKING!");

    Off topic here....

    I really want to make a level system on the weapons but how would i store the level and xp data on the weapon ?

    I know i could save the lvl and xp in a config for each player but thats just a mess and i cant really see any way i can connect those data to the weapon.
     

    Attached Files:

  4. Online

    timtower Administrator Administrator Moderator

    @Sylian You are missing the @EventHandler
     
  5. Offline

    Sylian

    Just added it but didnt do anything.

    Btw the
    EntityDamageByEntityEvent goes for when i attack zombies and so on right and not only players ?
     
  6. Online

    timtower Administrator Administrator Moderator

    And entity->entity, any form of projectile.
     
  7. Offline

    Sylian

    When you say projectile it means like diamond axe and swords also right ?

    Can it be the NamespacedKey im doing wrong ?


    (Gonna go to work now so i might not reply for the next few hours but will try everything you say when i get back.)
     
  8. Online

    timtower Administrator Administrator Moderator

    Projectile as in arrows, swords are items the player are holding.

    No idea about the NamespacedKey.
     
  9. Offline

    Sylian

    Ahh okay thanks for explaining!
    Did they change how enchantsments work since it's not an ID but a NameSpacedKey now?
     
  10. Online

    timtower Administrator Administrator Moderator

  11. Offline

    Sylian

    Thanks for Linking that page!

    I have tried with creating the key in the code i added to the post.

    Would you be so kind to show an example?

    Having a hard time understading how it's done since i cant see whats wrong with the key i made.
     
  12. Online

    timtower Administrator Administrator Moderator

    Make it in the onEnable, not before.
     
    Sylian likes this.
  13. Offline

    Sylian

    Ahhhh i think i get it now
    Gonna try when i get home from work!

    Btw the fields i have in the code.

    byNameField and
    byKeyField.

    Are they correct or is there anything else i need to edit?
     
  14. Online

    timtower Administrator Administrator Moderator

    @Sylian Don't know for sure, never did this before.
     
    Sylian likes this.
  15. Offline

    Sylian

    Got it working via lore instead since a kind person added me on Discord and told me to do it that way instead since its easier and not as many bugs and so on.
     
Thread Status:
Not open for further replies.

Share This Page