/
minecraftbackup
/
ChatSentinel
Обзор
Документация
Войти
/
minecraftbackup
/
ChatSentinel
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/main/java/dev/_2lstudios/chatsentinel/shared/utils/ReflectionUtil.java
64 строки
2 KB
LinsaFTW
Cache player language (Great Optimization)
22 янв 2023, 19:01
22 янв 2023, 19:01
e961e79
Код
Авторство
О чём код?
package dev._2lstudios.chatsentinel.shared.utils; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; import java.lang.reflect.Field; import java.lang.reflect.Method; import org.bukkit.entity.Player; import org.bukkit.entity.Player.Spigot; public final class ReflectionUtil { private static final MethodHandle getLocalePlayerMethod = localePlayer(); private static final MethodHandle getLocaleSpigotMethod = localeSpigot(); private static final MethodHandle getHandleMethod = handleMethod(); private static Field pingField = null; private static MethodHandle localePlayer() { try { MethodHandles.Lookup lookup = MethodHandles.publicLookup(); return lookup.findVirtual(Player.class, "getLocale", MethodType.methodType(String.class)); } catch (NoSuchMethodException | IllegalAccessException e) { return null; } } private static MethodHandle localeSpigot() { try { MethodHandles.Lookup lookup = MethodHandles.publicLookup(); return lookup.findVirtual(Spigot.class, "getLocale", MethodType.methodType(String.class)); } catch (NoSuchMethodException | IllegalAccessException e) { return null; } } private static MethodHandle handleMethod() { MethodHandles.Lookup lookup = MethodHandles.lookup(); try { Method method = Player.class.getMethod("getHandle"); method.setAccessible(true); return lookup.unreflect(method); } catch (IllegalAccessException | NoSuchMethodException | SecurityException e) { return null; } } public static MethodHandle getLocalePlayerMethod() { return getLocalePlayerMethod; } public static MethodHandle getLocaleSpigotMethod() { return getLocaleSpigotMethod; } public static MethodHandle getHandleMethod() { return getHandleMethod; } public static Field getPingField(Object playerHandle) throws NoSuchFieldException, SecurityException { return pingField == null ? pingField = playerHandle.getClass().getField("ping") : pingField; } private ReflectionUtil() {} }