Tutorial How to make an item glow with no enchant

Discussion in 'Resources' started by hammy2899, Jul 19, 2015.

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

    hammy2899

    Hello, Hami here.

    Today I'm going to teach you how to make an item glow like it is enchanted but with out it being enchanted and with out an enchant being in the lore of the item!

    Ok so lets start!

    First you will need to make a new class and just call it something like Glow.

    Then you want to make it extend Enchantment like this:

    Code:
    package me.Test.hammy2899;
    
    import org.bukkit.enchantments.Enchantment;
    
    public class Glow extends Enchantment {
    
    }
    
    Then it will say there is an error, that is because you need to add the missing methods.

    Add all the methods and leave them as default.

    Like this:

    Code:
    package me.Test.hammy2899;
    
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.enchantments.EnchantmentTarget;
    import org.bukkit.inventory.ItemStack;
    
    public class Glow extends Enchantment {
    
      public Glow(int id) {
          super(id);
      }
    
      @Override
      public boolean canEnchantItem(ItemStack arg0) {
          return false;
      }
    
      @Override
      public boolean conflictsWith(Enchantment arg0) {
          return false;
      }
    
      @Override
      public EnchantmentTarget getItemTarget() {
          return null;
      }
    
      @Override
      public int getMaxLevel() {
          return 0;
      }
    
      @Override
      public String getName() {
          return null;
      }
    
      @Override
      public int getStartLevel() {
          return 0;
      }
    
    }
    
    Now you need to register the enchantment. To do this just make new method in your main class or your API class like this:

    Code:
       
    public void registerGlow() {
            try {
                Field f = Enchantment.class.getDeclaredField("acceptingNew");
                f.setAccessible(true);
                f.set(null, true);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            try {
                Glow glow = new Glow(70);
                Enchantment.registerEnchantment(glow);
            }
            catch (IllegalArgumentException e){
            }
            catch(Exception e){
                e.printStackTrace();
            }
        }
    
    Then run this method onEnable like this:

    Code:
       
    @Override
        public void onEnable() {
    
            registerGlow();
    
        }   
    
    Now you have finished making the glow now all you need to do is add it to an item.
    To do this you will need to make a new ItemStack and ItemMeta like this:

    Code:
    ItemStack item = new ItemStack(Material.STICK);
    ItemMeta itemMeta = item.getItemMeta();
    
    Then you need to add the enchant like this:

    Code:
    Glow glow = new Glow(id);
    itemMeta.addEnchant(glow, 1, true);
    item.setItemMeta(itemMeta);
    
    The "id" is the id you want to set the enchantment as, because all enchantments have an id.

    Now you have done this you can give the item to a player like this:

    Code:
    player.getInventory().addItem(item);
    player.updateInventory();
    
    Then your done!

    [​IMG]

    [​IMG]

    Leave a like if this helped you and I hope you learned something today!
     
  2. Offline

    ZomBlade_Shadow

    Very nice thanks!
    Helps alot
     
  3. Offline

    BrickBoy55

    I've been looking for this for a long time. Thanks!
     
  4. Offline

    Eggspurt

    Thanks! This is a very nice tutorial :)
     
  5. Offline

    rbrick

    You could also add an empty NBTTagList to the items nbt :)
     
  6. Offline

    hammy2899

    You can but this way is simple for people who don't understand NBTTags
     
  7. @hammy2899 When I do:
    Glow glow = new Glow();
    Inside Glow(); I put 34 which is Unbreaking 1 and I still keep getting the enchantment name in the lore?

    EDIT: Anyways nice tutorial, I will use NBTTag
     
  8. Offline

    hammy2899

    The id you put in needs to be different to all the Minecraft enchantments because your making a new enchantment when I did it I used 70
     
  9. Offline

    DarkBladee12

    @rbrick
    Adding an empty NBTTagList to an item does no longer work. If you add a new NBTTagCompound to that list and set its type to 10 it tries to retrieve information from the enchantment and makes it a protection enchantment at level 0, because the lvl and id entries don't exist in the map. It will glow then, but you can also see that it has an enchantment applied.

    @hammy2899
    You should maybe add a check in the registerGlow method which verifies if the enchantment with id 70 has already been registered, otherwise it will always throw an exception if called a second time (plugin or server reload). Well it won't display an exception since there's a try and catch around it, but adding this check will prevent the unneccessary try to register the enchantment.
     
    hammy2899 likes this.
  10. Offline

    axeldu18

    Oh thanks !
     
  11. Offline

    rbrick

    @DarkBladee12 ah, ok. Its been a while, :p

    EDIT: I was doing some testing, it works in vanilla o.o (/give rbrick wool 1 0 {ench:[]}), but I guess this isn't really vanilla is it :p
     
    Last edited: Jul 20, 2015
  12. Offline

    DarkBladee12

    @rbrick
    Yeah, it seems like Bukkit/Spigot added some extra checks for validating certain nbt tags.
     
  13. Offline

    Orange Tabby

    @hammy2899
    Thanks :D. I've always wanted to do this, but never figured out how to :D
     
  14. Offline

    MCnumi

    Wouldn't you have to register the new enchantment as 69 or 71? Now that 1.10 added a new enchantment that im pretty sure took up the id 70.
     
  15. @MCnumi Correct.

    Changes could be made to this so the id is dynamic and will always change to an available id
     
  16. @hammy2899
    This is effectively doing nothing but acting as if their is an enchantment. To add custom code for this item, I would need to write events checking if the item in hand contains an enchantment, right (question mark here)
     
  17. Offline

    ArsenArsen

    Orrr just do 255, or 179, thats random enough :)
     
  18. Offline

    Zombie_Striker

    @ArsenArsen
    Or for loop through all the enchantments that exist until you find an ID that has not been taken. That way, there can never be any conflicts.
     
    ArsenArsen likes this.
  19. Offline

    xXItzLoopingXx

    Can you please make a download??
     
  20. Online

    timtower Administrator Administrator Moderator

    And what would you download then?
     
  21. Offline

    Zombie_Striker

Thread Status:
Not open for further replies.

Share This Page