No Such Method saçmalığı

javabey

git add Brain.java in/earth/people/brainless
Mesajlar
176
En iyi cevaplar
0
Beğeniler
180
Puanları
330
Ruh hali
reflectionlarla uğraştığımdan beri her zaman karşılaştığım hatalardan biridir kendisi, şimdi asıl sorunu biliyorum method normalde var sendPacket metodu clasic nms.Packet.class ı alıyor domain olarak ancak ben her hangi bir packet yollayınca atıyorum PacketPlayOutTitle, bu packetin super class ı nms.Packet.class olmasına rağmen o methodu bulamıyor, nms.Packet.class.cast(benimPacket) yapsam dahi olmuyor her seferinde
'net.minecraft.server.v1_13_R2.PlayerConnection.sendPacket(net.minecraft.server.v1_13_R2.PacketPlayOutTitle)'
bu hatayı veriyor, tamam anlıyorum domain olarak PlayOut u almıyor ama önceden böyle değildi bi çalışıyor bi çalışmıyor garip davranıyor.

(sendpacket methodu en aşalarda)
ReflectionUtil.clas:
Java:
package me.utsukushihito.utsutil.misc;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
* Wrapper methods that allow accesss to reflection for backward compatible code.
*
* @since 1.8
*/

public class ReflectionUtil {

    private static ReflectionUtil instance;

    /**
     * Returns the NMS version.
     * @return the NMS version (i.e. "v1_10").
     * @since 1.9
     */
    private String getNMSVersion() {
        return nms().split("\\.")[3];
    }

    /**
     * Returns the real packagename for the net.minecraft.server.
     * @return the real packagename for the net.minecraft.server.
     * @since 1.9
     */
    private String nms() {
        Object nmsServer = exec(Bukkit.getServer(), "getServer");
        return nmsServer != null ? nmsServer.getClass().getPackage().getName() : "net.minecraft.server";
    }

    /**
     * Returns the corresponding Bukkit class, given a CraftBukkit implementation object.
     *
     * @param craftObject A CraftBukkit implementation of a Bukkit class.
     * @return the corresponding Bukkit class, given a CraftBukkit implementation object.
     * @since 1.8
     */
    public Class<?> getBukkitClass(Object craftObject) {
        Class clazz = craftObject != null ? craftObject.getClass() : null;
        while (clazz != null && clazz.getCanonicalName().contains(".craftbukkit.")) {
            clazz = clazz.getSuperclass();
        }
        return clazz;
    }

    public Class<?> getCustomBukkitClass(String className) {
        try {
            return Class.forName("org.bukkit.craftbukkit." + getNMSVersion() + "." + className);
        } catch (ClassNotFoundException e) {
            return null;
        }
    }

    /**
     * Returns the corresponding Bukkit class, given a CraftBukkit implementation object.
     *
     * @param name a Bukkit implementation of a NMS class.
     * @return the corresponding Bukkit class, given a NMS implementation object.
     * @since 1.8
     */
    public Class<?> getNMSClass(String name) {
        try {
            return Class.forName("net.minecraft.server." + getNMSVersion() + "." + name);
        } catch (ClassNotFoundException e) {
            return null;
        }
    }

    /**
     * Uses reflection to execute the named method on the supplied class giving the arguments.
     * Sinks all exceptions, but log an entry and returns <code>null</code>
     *
     * @param clazz      The class on which to invoke the method
     * @param methodName The name of the method to invoke
     * @param args       The arguments to supply to the method
     * @return <code>null</code> or the return-object from the method.
     * @since 1.8
     */
    public <T> T execStatic(Class<?> clazz, String methodName, Object... args) {
        try {
            Class[] argTypes = new Class[args.length];
            int ix = 0;
            for (Object arg : args) {
                argTypes[ix++] = getBukkitClass(arg);
            }
            Method method = getMethod(clazz, methodName, argTypes);
            boolean wasAccessible = method.isAccessible();
            method.setAccessible(true);
            try {
                return (T) method.invoke(null, args);
            } finally {
                method.setAccessible(wasAccessible);
            }
        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Uses reflection to execute the named method on the supplied class giving the arguments.
     * Sinks all exceptions, but log an entry and returns <code>null</code>
     *
     * @param obj        The object on which to invoke the method
     * @param methodName The name of the method to invoke
     * @param argTypes   An array of argument-types (classes).
     * @param args       The arguments to supply to the method
     * @return <code>null</code> or the return-object from the method.
     * @since 1.8
     */
    public <T> T exec(Object obj, String methodName, Class[] argTypes, Object... args) {
        if (obj == null) {
            return null;
        }
        Class<?> aClass = obj.getClass();
        try {
            Method method = getMethod(aClass, methodName, argTypes);
            boolean wasAccessible = method.isAccessible();
            method.setAccessible(true);
            try {
                return (T) method.invoke(obj, args);
            } finally {
                method.setAccessible(wasAccessible);
            }
        } catch (NoSuchMethodException | AbstractMethodError | InvocationTargetException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Method getMethod(Class<?> aClass, String methodName, Class... argTypes) throws NoSuchMethodException {
        try {
            // Declared gives access to non-public
            return aClass.getDeclaredMethod(methodName, argTypes);
        } catch (NoSuchMethodException e) {
            return aClass.getMethod(methodName, argTypes);
        }
    }

    /**
     * Uses reflection to execute the named method on the supplied class giving the arguments.
     * Sinks all exceptions, but log an entry and returns <code>null</code>
     *
     * @param obj        The object on which to invoke the method
     * @param methodName The name of the method to invoke
     * @param args       The arguments to supply to the method
     * @return <code>null</code> or the return-object from the method.
     * @since 1.8
     */
    public <T> T exec(Object obj, String methodName, Object... args) {
        if (obj == null) {
            return null;
        }
        Class[] argTypes = new Class[args.length];
        int ix = 0;
        for (Object arg : args) {
            argTypes[ix++] = arg != null ? arg.getClass() : null;
        }
        return exec(obj, methodName, argTypes, args);
    }

    /**
     * Returns the value of a field on null.
     * @param clazz         The class
     * @param fieldName     The name of the field
     * @param <T>           The type of field
     * @return the value or <code>null</code>
     * @since 1.9
     */
    public <T> T getField(Class<?> clazz, String fieldName) {
        try {
            Field field = getFieldFromClass(clazz, fieldName);
            boolean wasAccessible = field.isAccessible();
            field.setAccessible(true);
            try {
                return (T) field.get(null);
            } finally {
                field.setAccessible(wasAccessible);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Returns the value of a field on the object.
     * @param obj           The object
     * @param fieldName     The name of the field
     * @param <T>           The type of field
     * @return the value or <code>null</code>
     * @since 1.9
     */
    public <T> T getField(Object obj, String fieldName) {
        try {
            Field field = getFieldInternal(obj, fieldName);
            boolean wasAccessible = field.isAccessible();
            field.setAccessible(true);
            try {
                return (T) field.get(obj);
            } finally {
                field.setAccessible(wasAccessible);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Field getFieldInternal(Object obj, String fieldName) {
        return getFieldFromClass(obj.getClass(), fieldName);
    }

    public Field getFieldFromClass(Class<?> aClass, String fieldName) {
        if (aClass == null) {
            try {
                throw new NoSuchFieldException("Unable to locate field " + fieldName);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        try {
            return aClass.getDeclaredField(fieldName);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        try {
            return aClass.getField(fieldName);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return getFieldFromClass(aClass.getSuperclass(), fieldName);
    }

    public void sendPacket(Player player, Object packet) {
        try {
            Object nms = exec(player, "getHandle");
            Object playerConnection = getField(nms,"playerConnection");
            exec(playerConnection, "sendPacket", packet);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static ReflectionUtil getInstance() {
        if (instance == null)
            instance = new ReflectionUtil();
        return instance;
    }

}

malum method*
Java:
public void sendPacket(Packet<?> packet) {
    this.a(packet, (GenericFutureListener)null);
}

tag list: @Schaffer79 @Admicos @LifeMCServer @ByNovem @GodofMilker
 


Son düzenleme:

GodofMilker

Nether Yerlisi
Mesajlar
2,196
En iyi cevaplar
0
Beğeniler
2,562
Puanları
6,700
this.a'yı niye kullanıyorsun ki reflectionla yapıyorsan*
Protocollib kullan bir de daha rahat xd
this.a ne yapıyor bir de
 

javabey

git add Brain.java in/earth/people/brainless
Mesajlar
176
En iyi cevaplar
0
Beğeniler
180
Puanları
330
Ruh hali
this.a'yı niye kullanıyorsun ki reflectionla yapıyorsan*
Protocollib kullan bir de daha rahat xd
this.a ne yapıyor bir de
o PlayerConnection.class ında nms classında yani o methodu çalıştırmaya çalışıyorum, protocollib kullanmasını sevmiyorum.
 

ByNovem

Kızıltaş Madencisi
Mesajlar
445
En iyi cevaplar
1
Beğeniler
587
Puanları
1,200
Path ı bir elinle yazmayı denedin mi? otomatik versiyona bağlı değilde.
 

javabey

git add Brain.java in/earth/people/brainless
Mesajlar
176
En iyi cevaplar
0
Beğeniler
180
Puanları
330
Ruh hali
Path ı bir elinle yazmayı denedin mi? otomatik versiyona bağlı değilde.
elle yapınca Packet.class diyr gösterince yani oluyor sanırım reflection utilim onu super class ları algılamıyor o yüzden sadecr packetplayout.class ı gösteriyo
 

ByNovem

Kızıltaş Madencisi
Mesajlar
445
En iyi cevaplar
1
Beğeniler
587
Puanları
1,200
Böyle geniş bir reflection yerine paketi oluşturup fielde leri setprivatefield le ayarlarsan paketin özelliğini daha rahat olabilir yine reflection ama daha kolay.

zaten burada kullanmışsın
Java:
   Method method = getMethod(clazz, methodName, argTypes);
            boolean wasAccessible = method.isAccessible();
            method.setAccessible(true);
 

javabey

git add Brain.java in/earth/people/brainless
Mesajlar
176
En iyi cevaplar
0
Beğeniler
180
Puanları
330
Ruh hali
neyse bildiğim yoldan yapıyorum manual class ları göstererek yapıcam.
 

Üst