1.8.8 [NMS] Help with Reflection addGlow in item

Discussion in 'Spigot Plugin Development' started by GabrielBlink, Jan 31, 2020.

  1. Hi guys, i'm trying to add glow effect to a item in minecraft with NMS , i made this code bellow:
    Code (Java):
    package com.atlasplugins.encantar.apis;

    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;

    import org.bukkit.inventory.ItemStack;

    import com.atlasplugins.encantar.apis.ReflectionUtils.PackageType;

    public class Reflections {

        private static Class nbtTagList = null;
        private static Class nbtTagComponent = null;
        private static Class itemStackNMS = null;
        private static Class craftItemStack = null;
        private static Class nbtBase = null;
        private static Method asNMSCopy = null;
        private static Method hasTag = null;
        private static Method setTag = null;
        private static Method getTag = null;
        private static Method set = null;
        private static Method asCraftMirror = null;
       
        public Reflections() {
            try {
                nbtBase = PackageType.MINECRAFT_SERVER.getClass("NBTBase");
                nbtTagList = PackageType.MINECRAFT_SERVER.getClass("NBTTagList");
                nbtTagComponent = PackageType.MINECRAFT_SERVER.getClass("NBTTagCompound");
                craftItemStack = PackageType.CRAFTBUKKIT_INVENTORY.getClass("CraftItemStack");
                itemStackNMS = PackageType.MINECRAFT_SERVER.getClass("ItemStack");
                asNMSCopy = ReflectionUtils.getMethod(craftItemStack, "asNMSCopy", ItemStack.class);
                hasTag = ReflectionUtils.getMethod(itemStackNMS, "hasTag");
                setTag = ReflectionUtils.getMethod(itemStackNMS, "setTag", nbtTagComponent);
                getTag = ReflectionUtils.getMethod(itemStackNMS, "getTag");
                set = ReflectionUtils.getMethod(nbtTagComponent, "set", String.class, nbtBase);
                asCraftMirror = ReflectionUtils.getMethod(craftItemStack, "asCraftMirror", itemStackNMS);
            } catch (ClassNotFoundException | NoSuchMethodException e) {
                e.printStackTrace();
            }
        }
       
        @SuppressWarnings("unchecked")
        public static ItemStack addGlow(ItemStack item) {
            try {
                Object nmsStack = asNMSCopy.invoke(null, item);
                Object tag = null;
                boolean b = (boolean) hasTag.invoke(nmsStack);
                if(!b) {
                    tag = nbtTagComponent.getConstructor().newInstance();
                    setTag.invoke(nmsStack, tag);
                }
                if(tag == null) {
                    tag = getTag.invoke(nbtTagComponent);
                }
                Object ench = nbtTagList.getConstructor().newInstance();
                set.invoke(tag, "ench", nbtBase.cast(ench));
                setTag.invoke(nmsStack, tag);
                return (ItemStack) asCraftMirror.invoke(craftItemStack, nmsStack);
            } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException | NoSuchMethodException | SecurityException e) {
                e.printStackTrace();
            }
            return item;
        }
    }
     
    this code does not work when i try to add the glow effect, where is my error?