Solved [NMS] Making an entity invisible using reflection

Discussion in 'Spigot Plugin Development' started by Nakitsuro, Jun 19, 2016.

Thread Status:
Not open for further replies.
  1. Hi,
    I'm actually trying to make an entity invisible, using the reflection.
    The method doesn't throw errors, but actually it doesn't work either..
    If anyone could help, it would be appreciate !

    Code (Text):
    Object et = null;
            try {
                Method m = e.getClass().getDeclaredMethod("getHandle");
                m.setAccessible(true);
                et = m.invoke(e);
            } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
                    | InvocationTargetException e1) {
                e1.printStackTrace();
            }
            if (!(et == null)) {
                try {
                    Class<?> superc = et.getClass();
                    while (superc.getSuperclass() != Object.class) superc = superc.getSuperclass();
                    Method inv = superc.getDeclaredMethod("setInvisible", boolean.class);
                    inv.setAccessible(true);
                    inv.invoke(et, true);
                } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) {
                    e1.printStackTrace();
                }
            }

    Thanks :)
     
    #1 Nakitsuro, Jun 19, 2016
    Last edited: Jun 20, 2016
  2. Just apply an invisibility potion. ezpz
     
    • Like Like x 1
  3. Your code doesn't work because every tick, if the entity has no effects, it is called the method setInvisible(false). The nms method I'm talking about is EntityLiving#F(). The easier way is to add the invisible potion effect with the bukkit api (unless you want to create custom entities and override that method, and still I don't know wether it would work).
     
    • Like Like x 1
  4. Um okey, ty, is there a way to make this entity invisible w/ no aprticules, or the only way is to create a custom entity ?
     
  5. LivingEntity#addPotionEffect has a boolean parameter for particles. Pass false to disable them.
     
    • Agree Agree x 2
  6. Thank you all for your help :)
     
Thread Status:
Not open for further replies.