Tutorial How to create glowing items without actually enchanting them (1.14+)

Discussion in 'Resources' started by Minesuchtiiii, Jun 8, 2019.

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

    Minesuchtiiii

    Hey, so this is my first tutorial on here, so if you have any suggestions on how to make a better tutorial feel free to tell me :D

    So to create a glowing item without actually enchanting it we first need to create a new Class (if you want you can also create a new package, and the new class inside the package).

    I created a new package and a new class named Glow.


    Code:
    package glow;
    
    public class Glow {
    
    
    }

    After creating the class we want it to extends Enchantment, to do this we simply edit the code in the following way:


    Code:
    package glow;
    
    public class Glow extends Enchantment {
    
    }

    Also don't forget to import "import org.bukkit.enchantments.Enchantment;". When you're done with this "Glow" will be underlined red telling us that there's an error. To get rid of it just generate the methods the quick-fix suggests. After that your class should look like this:


    Code:
    package glow;
    
    import org.bukkit.NamespacedKey;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.enchantments.EnchantmentTarget;
    import org.bukkit.inventory.ItemStack;
    
    public class Glow extends Enchantment {
    
        public Glow(NamespacedKey i) {
            super(i);
            // TODO Auto-generated constructor stub
        }
    
        @Override
        public boolean canEnchantItem(ItemStack arg0) {
            // TODO Auto-generated method stub
            return false;
        }
    
        @Override
        public boolean conflictsWith(Enchantment arg0) {
            // TODO Auto-generated method stub
            return false;
        }
    
        @Override
        public EnchantmentTarget getItemTarget() {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public int getMaxLevel() {
            // TODO Auto-generated method stub
            return 0;
        }
    
        @Override
        public String getName() {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public int getStartLevel() {
            // TODO Auto-generated method stub
            return 0;
        }
    
        @Override
        public boolean isCursed() {
            // TODO Auto-generated method stub
            return false;
        }
    
        @Override
        public boolean isTreasure() {
            // TODO Auto-generated method stub
            return false;
        }
    
    }
    

    Good news! We are already done in this class! Next go into your main class and create a method called registerGlow() (or any name you want..) The method should look 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 {
                 NamespacedKey key = new NamespacedKey(this, getDescription().getName());
                   
                    Glow glow = new Glow(key);
                    Enchantment.registerEnchantment(glow);
                }
                catch (IllegalArgumentException e){
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }


    Import everything you need, watch out when importing "Field", it's the "java.lang.reflect.Field;" import!
    After that call this method in your onEnable() part



    Code:
    public void onEnable() {
    
    //Your code etc...
    registerGlow();
    
    }

    Now that the glow is registered we're almost done! To actually add the glow to an item now we'd have to create an itemstack, and add the glow the the items' meta. This can be done like this:


    Code:
    ItemStack stick = new ItemStack(Material.STICK);
    ItemMeta stickmeta = stick.getItemMeta();
    NamespacedKey key = new NamespacedKey(this, getDescription().getName());
    Glow glow = new Glow(key);
    ismeta.addEnchant(glow, 1, true);
    is.setItemMeta(ismeta);

    Now your item is actually glowing without having an enchanted lore! Now you can just give it a player, add it to an inventory or do whatever you want with it!

    So that's it with this tutorial, I hope you learned something useful! :) If you have any problems feel free to leave a comment!
     

    Attached Files:

    • 1.png
      1.png
      File size:
      5 KB
      Views:
      22
    ThemisH likes this.
  2. Offline

    KarimAKL

    @Minesuchtiiii I think something like this would work as well:
    Code:Java
    1. ItemStack item = new ItemStack(Material.STICK);
    2. item.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 1);
    3. ItemMeta meta = item.getItemMeta();
    4. meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
    5. item.setItemMeta(meta);
     
  3. Offline

    Shqep

    HideEnchants flag does not disable enchantments on items I believe? So flags are better for creating Menus/GUIs.

    And the Glow thing is good for adding random glowing items to players’ inventory. Both are nice. Learned something today, thanks both. :d
     
  4. Offline

    KarimAKL

    @Shqep Yeah, it's only the enchantment "lore" that gets hidden, it doesn't remove the enchantment.
    But you can just add a useless enchant, something that does nothing on the specific item, an example could be the bow enchantment "punch", that does nothing on a stick.
     
Thread Status:
Not open for further replies.

Share This Page