forked from SteamWar/SteamWar
Move and rename Reflection fields
This commit is contained in:
@@ -1,416 +0,0 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2024 SteamWar.de-Serverteam
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.comphenix.tinyprotocol;
|
||||
|
||||
import de.steamwar.core.Core;
|
||||
import jdk.internal.misc.Unsafe;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.lang.reflect.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* An utility class that simplifies reflection in Bukkit plugins.
|
||||
*
|
||||
* @author Kristian
|
||||
*/
|
||||
public final class Reflection {
|
||||
/**
|
||||
* An interface for invoking a specific constructor.
|
||||
*/
|
||||
public interface ConstructorInvoker {
|
||||
/**
|
||||
* Invoke a constructor for a specific class.
|
||||
*
|
||||
* @param arguments - the arguments to pass to the constructor.
|
||||
* @return The constructed object.
|
||||
*/
|
||||
Object invoke(Object... arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* An interface for invoking a specific method.
|
||||
*/
|
||||
public interface MethodInvoker {
|
||||
/**
|
||||
* Invoke a method on a specific target object.
|
||||
*
|
||||
* @param target - the target object, or NULL for a static method.
|
||||
* @param arguments - the arguments to pass to the method.
|
||||
* @return The return value, or NULL if is void.
|
||||
*/
|
||||
Object invoke(Object target, Object... arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* An interface for retrieving the field content.
|
||||
*
|
||||
* @param <T> - field type.
|
||||
*/
|
||||
public interface FieldAccessor<T> {
|
||||
/**
|
||||
* Retrieve the content of a field.
|
||||
*
|
||||
* @param target - the target object, or NULL for a static field.
|
||||
* @return The value of the field.
|
||||
*/
|
||||
T get(Object target);
|
||||
|
||||
/**
|
||||
* Set the content of a field.
|
||||
*
|
||||
* @param target - the target object, or NULL for a static field.
|
||||
* @param value - the new value of the field.
|
||||
*/
|
||||
void set(Object target, Object value);
|
||||
|
||||
/**
|
||||
* Determine if the given object has this field.
|
||||
*
|
||||
* @param target - the object to test.
|
||||
* @return TRUE if it does, FALSE otherwise.
|
||||
*/
|
||||
boolean hasField(Object target);
|
||||
}
|
||||
|
||||
// Deduce the net.minecraft.server.v* package
|
||||
private static final String OBC_PREFIX = Bukkit.getServer().getClass().getPackage().getName();
|
||||
private static final String NMS_PREFIX = OBC_PREFIX.replace("org.bukkit.craftbukkit", "net.minecraft.server");
|
||||
private static final String VERSION = OBC_PREFIX.replace("org.bukkit.craftbukkit", "").replace(".", "");
|
||||
|
||||
// Variable replacement
|
||||
private static final Pattern MATCH_VARIABLE = Pattern.compile("\\{([^\\}]+)\\}");
|
||||
|
||||
private Reflection() {
|
||||
// Seal class
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a field accessor for a specific field type and name.
|
||||
*
|
||||
* @param target - the target type.
|
||||
* @param name - the name of the field, or NULL to ignore.
|
||||
* @param fieldType - a compatible field type.
|
||||
* @return The field accessor.
|
||||
*/
|
||||
public static <T> FieldAccessor<T> getField(Class<?> target, String name, Class<T> fieldType) {
|
||||
return getField(target, name, fieldType, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a field accessor for a specific field type and name.
|
||||
*
|
||||
* @param className - lookup name of the class, see {@link #getClass(String)}.
|
||||
* @param name - the name of the field, or NULL to ignore.
|
||||
* @param fieldType - a compatible field type.
|
||||
* @return The field accessor.
|
||||
*/
|
||||
public static <T> FieldAccessor<T> getField(String className, String name, Class<T> fieldType) {
|
||||
return getField(getClass(className), name, fieldType, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a field accessor for a specific field type and name.
|
||||
*
|
||||
* @param target - the target type.
|
||||
* @param fieldType - a compatible field type.
|
||||
* @param index - the number of compatible fields to skip.
|
||||
* @return The field accessor.
|
||||
*/
|
||||
public static <T> FieldAccessor<T> getField(Class<?> target, Class<T> fieldType, int index) {
|
||||
return getField(target, null, fieldType, index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a field accessor for a specific field type and name.
|
||||
*
|
||||
* @param className - lookup name of the class, see {@link #getClass(String)}.
|
||||
* @param fieldType - a compatible field type.
|
||||
* @param index - the number of compatible fields to skip.
|
||||
* @return The field accessor.
|
||||
*/
|
||||
public static <T> FieldAccessor<T> getField(String className, Class<T> fieldType, int index) {
|
||||
return getField(getClass(className), fieldType, index);
|
||||
}
|
||||
|
||||
public static <T> FieldAccessor<T> getField(Class<?> target, Class<T> fieldType, int index, Class<?>... parameters) {
|
||||
return getField(target, null, fieldType, index, parameters);
|
||||
}
|
||||
|
||||
private static <T> FieldAccessor<T> getField(Class<?> target, String name, Class<T> fieldType, int index, Class<?>... parameters) {
|
||||
for (final Field field : target.getDeclaredFields()) {
|
||||
if(matching(field, name, fieldType, parameters) && index-- <= 0) {
|
||||
field.setAccessible(true);
|
||||
|
||||
return new FieldAccessor<T>() {
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public T get(Object target) {
|
||||
try {
|
||||
return (T) field.get(target);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new IllegalArgumentException("Cannot access reflection.", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(Object target, Object value) {
|
||||
try {
|
||||
field.set(target, value);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new IllegalArgumentException("Cannot access reflection.", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasField(Object target) {
|
||||
// target instanceof DeclaringClass
|
||||
return field.getDeclaringClass().isAssignableFrom(target.getClass());
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Search in parent classes
|
||||
if (target.getSuperclass() != null)
|
||||
return getField(target.getSuperclass(), name, fieldType, index);
|
||||
|
||||
throw new IllegalArgumentException("Cannot find field with type " + fieldType);
|
||||
}
|
||||
|
||||
private static <T> boolean matching(Field field, String name, Class<T> fieldType, Class<?>... parameters) {
|
||||
if(name != null && !field.getName().equals(name))
|
||||
return false;
|
||||
|
||||
if(!fieldType.isAssignableFrom(field.getType()))
|
||||
return false;
|
||||
|
||||
if(parameters.length > 0) {
|
||||
Type[] arguments = ((ParameterizedType)field.getGenericType()).getActualTypeArguments();
|
||||
|
||||
for(int i = 0; i < parameters.length; i++) {
|
||||
if(arguments[i] instanceof ParameterizedType ? ((ParameterizedType) arguments[i]).getRawType() != parameters[i] : arguments[i] != parameters[i])
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for the first publicly and privately defined method of the given name and parameter count.
|
||||
*
|
||||
* @param className - lookup name of the class, see {@link #getClass(String)}.
|
||||
* @param methodName - the method name, or NULL to skip.
|
||||
* @param params - the expected parameters.
|
||||
* @return An object that invokes this specific method.
|
||||
* @throws IllegalStateException If we cannot find this method.
|
||||
*/
|
||||
public static MethodInvoker getMethod(String className, String methodName, Class<?>... params) {
|
||||
return getTypedMethod(getClass(className), methodName, null, params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for the first publicly and privately defined method of the given name and parameter count.
|
||||
*
|
||||
* @param clazz - a class to start with.
|
||||
* @param methodName - the method name, or NULL to skip.
|
||||
* @param params - the expected parameters.
|
||||
* @return An object that invokes this specific method.
|
||||
* @throws IllegalStateException If we cannot find this method.
|
||||
*/
|
||||
public static MethodInvoker getMethod(Class<?> clazz, String methodName, Class<?>... params) {
|
||||
return getTypedMethod(clazz, methodName, null, params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for the first publicly and privately defined method of the given name and parameter count.
|
||||
*
|
||||
* @param clazz - a class to start with.
|
||||
* @param methodName - the method name, or NULL to skip.
|
||||
* @param returnType - the expected return type, or NULL to ignore.
|
||||
* @param params - the expected parameters.
|
||||
* @return An object that invokes this specific method.
|
||||
* @throws IllegalStateException If we cannot find this method.
|
||||
*/
|
||||
public static MethodInvoker getTypedMethod(Class<?> clazz, String methodName, Class<?> returnType, Class<?>... params) {
|
||||
for (final Method method : clazz.getDeclaredMethods()) {
|
||||
if ((methodName == null || method.getName().equals(methodName))
|
||||
&& (returnType == null || method.getReturnType().equals(returnType))
|
||||
&& Arrays.equals(method.getParameterTypes(), params)) {
|
||||
method.setAccessible(true);
|
||||
|
||||
return (target, arguments) -> {
|
||||
try {
|
||||
return method.invoke(target, arguments);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("Cannot invoke method " + method, e);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Search in every superclass
|
||||
if (clazz.getSuperclass() != null)
|
||||
return getTypedMethod(clazz.getSuperclass(), methodName, returnType, params);
|
||||
|
||||
throw new IllegalStateException(String.format("Unable to find method %s (%s).", methodName, Arrays.asList(params)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for the first publically and privately defined constructor of the given name and parameter count.
|
||||
*
|
||||
* @param className - lookup name of the class, see {@link #getClass(String)}.
|
||||
* @param params - the expected parameters.
|
||||
* @return An object that invokes this constructor.
|
||||
* @throws IllegalStateException If we cannot find this method.
|
||||
*/
|
||||
public static ConstructorInvoker getConstructor(String className, Class<?>... params) {
|
||||
return getConstructor(getClass(className), params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for the first publically and privately defined constructor of the given name and parameter count.
|
||||
*
|
||||
* @param clazz - a class to start with.
|
||||
* @param params - the expected parameters.
|
||||
* @return An object that invokes this constructor.
|
||||
* @throws IllegalStateException If we cannot find this method.
|
||||
*/
|
||||
public static ConstructorInvoker getConstructor(Class<?> clazz, Class<?>... params) {
|
||||
for (final Constructor<?> constructor : clazz.getDeclaredConstructors()) {
|
||||
if (Arrays.equals(constructor.getParameterTypes(), params)) {
|
||||
constructor.setAccessible(true);
|
||||
|
||||
return arguments -> {
|
||||
try {
|
||||
return constructor.newInstance(arguments);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("Cannot invoke constructor " + constructor, e);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
throw new IllegalStateException(String.format("Unable to find constructor for %s (%s).", clazz, Arrays.asList(params)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a class from its full name, without knowing its type on compile time.
|
||||
* <p>
|
||||
* This is useful when looking up fields by a NMS or OBC type.
|
||||
* <p>
|
||||
*
|
||||
* @param lookupName - the class name with variables.
|
||||
* @return The class.
|
||||
*/
|
||||
public static Class<Object> getUntypedClass(String lookupName) {
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
Class<Object> clazz = (Class) getClass(lookupName);
|
||||
return clazz;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a class from its full name.
|
||||
* <p>
|
||||
* Strings enclosed with curly brackets - such as {TEXT} - will be replaced according to the following table:
|
||||
* <p>
|
||||
* <table border="1">
|
||||
* <tr>
|
||||
* <th>Variable</th>
|
||||
* <th>Content</th>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>{nms}</td>
|
||||
* <td>Actual package name of net.minecraft.server.VERSION</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>{obc}</td>
|
||||
* <td>Actual pacakge name of org.bukkit.craftbukkit.VERSION</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>{version}</td>
|
||||
* <td>The current Minecraft package VERSION, if any.</td>
|
||||
* </tr>
|
||||
* </table>
|
||||
*
|
||||
* @param lookupName - the class name with variables.
|
||||
* @return The looked up class.
|
||||
* @throws IllegalArgumentException If a variable or class could not be found.
|
||||
*/
|
||||
public static Class<?> getClass(String lookupName) {
|
||||
try {
|
||||
return Class.forName(expandVariables(lookupName));
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new IllegalArgumentException("Cannot find " + expandVariables(lookupName), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Expand variables such as "{nms}" and "{obc}" to their corresponding packages.
|
||||
*
|
||||
* @param name - the full name of the class.
|
||||
* @return The expanded string.
|
||||
*/
|
||||
private static String expandVariables(String name) {
|
||||
StringBuffer output = new StringBuffer();
|
||||
Matcher matcher = MATCH_VARIABLE.matcher(name);
|
||||
|
||||
while (matcher.find()) {
|
||||
String variable = matcher.group(1);
|
||||
String replacement;
|
||||
|
||||
// Expand all detected variables
|
||||
if (variable.startsWith("nms")) {
|
||||
if(Core.getVersion() >= 17)
|
||||
replacement = "net.minecraft" + variable.substring(3);
|
||||
else
|
||||
replacement = NMS_PREFIX;
|
||||
} else if ("obc".equals(variable))
|
||||
replacement = OBC_PREFIX;
|
||||
else if ("version".equals(variable))
|
||||
replacement = VERSION;
|
||||
else
|
||||
throw new IllegalArgumentException("Unknown variable: " + variable);
|
||||
|
||||
// Assume the expanded variables are all packages, and append a dot
|
||||
if (replacement.length() > 0 && matcher.end() < name.length() && name.charAt(matcher.end()) != '.')
|
||||
replacement += ".";
|
||||
matcher.appendReplacement(output, Matcher.quoteReplacement(replacement));
|
||||
}
|
||||
|
||||
matcher.appendTail(output);
|
||||
return output.toString();
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public static Object newInstance(Class<?> clazz) {
|
||||
try {
|
||||
if (Core.getVersion() > 15) {
|
||||
return Unsafe.getUnsafe().allocateInstance(clazz);
|
||||
} else {
|
||||
return clazz.newInstance();
|
||||
}
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
throw new SecurityException("Could not create object", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,8 @@
|
||||
|
||||
package com.comphenix.tinyprotocol;
|
||||
|
||||
import com.comphenix.tinyprotocol.Reflection.FieldAccessor;
|
||||
import de.steamwar.Reflection;
|
||||
import de.steamwar.Reflection.Field;
|
||||
import de.steamwar.core.Core;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelDuplexHandler;
|
||||
@@ -44,17 +45,17 @@ public class TinyProtocol implements Listener {
|
||||
|
||||
private static final Class<?> craftServer = Reflection.getClass("{obc}.CraftServer");
|
||||
private static final Class<?> dedicatedPlayerList = Reflection.getClass("{nms.server.dedicated}.DedicatedPlayerList");
|
||||
private static final FieldAccessor<?> getPlayerList = Reflection.getField(craftServer, dedicatedPlayerList, 0);
|
||||
private static final Field<?> getPlayerList = Reflection.getField(craftServer, dedicatedPlayerList, 0);
|
||||
private static final Class<?> playerList = Reflection.getClass("{nms.server.players}.PlayerList");
|
||||
private static final Class<?> minecraftServer = Reflection.getClass("{nms.server}.MinecraftServer");
|
||||
private static final FieldAccessor<?> getMinecraftServer = Reflection.getField(playerList, minecraftServer, 0);
|
||||
private static final Field<?> getMinecraftServer = Reflection.getField(playerList, minecraftServer, 0);
|
||||
public static final Class<?> serverConnection = Reflection.getClass("{nms.server.network}.ServerConnection");
|
||||
private static final FieldAccessor<?> getServerConnection = Reflection.getField(minecraftServer, serverConnection, 0);
|
||||
private static final Field<?> getServerConnection = Reflection.getField(minecraftServer, serverConnection, 0);
|
||||
public static Object getServerConnection(Plugin plugin) {
|
||||
return getServerConnection.get(getMinecraftServer.get(getPlayerList.get(plugin.getServer())));
|
||||
}
|
||||
private static final Class<?> networkManager = Reflection.getClass("{nms.network}.NetworkManager");
|
||||
public static final FieldAccessor<List> networkManagers = Reflection.getField(serverConnection, List.class, 0, networkManager);
|
||||
public static final Field<List> networkManagers = Reflection.getField(serverConnection, List.class, 0, networkManager);
|
||||
|
||||
public static final TinyProtocol instance = new TinyProtocol(Core.getInstance());
|
||||
private static int id = 0;
|
||||
@@ -136,8 +137,8 @@ public class TinyProtocol implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
private static final FieldAccessor<Channel> getChannel = Reflection.getField(networkManager, Channel.class, 0);
|
||||
private static final FieldAccessor<UUID> getUUID = Reflection.getField(networkManager, UUID.class, 0);
|
||||
private static final Field<Channel> getChannel = Reflection.getField(networkManager, Channel.class, 0);
|
||||
private static final Field<UUID> getUUID = Reflection.getField(networkManager, UUID.class, 0);
|
||||
|
||||
private final class PacketInterceptor extends ChannelDuplexHandler {
|
||||
private final Player player;
|
||||
|
||||
@@ -0,0 +1,206 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2024 SteamWar.de-Serverteam
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.steamwar;
|
||||
|
||||
import de.steamwar.core.Core;
|
||||
import jdk.internal.misc.Unsafe;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
@UtilityClass
|
||||
public final class Reflection {
|
||||
|
||||
private static final String ORG_BUKKIT_CRAFTBUKKIT = Bukkit.getServer().getClass().getPackage().getName();
|
||||
public static final int MAJOR_VERSION = Integer.parseInt(Bukkit.getServer().getClass().getPackage().getName().split("_", 3)[1]);
|
||||
private static final String LEGACY_NET_MINECRAFT_SERVER = ORG_BUKKIT_CRAFTBUKKIT.replace("org.bukkit.craftbukkit", "net.minecraft.server");
|
||||
|
||||
public static Class<?> getClass(String name) {
|
||||
try {
|
||||
if(name.startsWith("org.bukkit.craftbukkit")) {
|
||||
return Class.forName(ORG_BUKKIT_CRAFTBUKKIT + name.substring(22));
|
||||
} else if(MAJOR_VERSION < 17 && name.startsWith("net.minecraft")) {
|
||||
return Class.forName(LEGACY_NET_MINECRAFT_SERVER + "." + name.split("[.](?=[^.]*$)")[1]);
|
||||
} else {
|
||||
return Class.forName(name);
|
||||
}
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new IllegalArgumentException("Cannot find " + name, e);
|
||||
}
|
||||
}
|
||||
|
||||
@AllArgsConstructor
|
||||
public static class Field<T> {
|
||||
private final java.lang.reflect.Field field;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public T get(Object target) {
|
||||
try {
|
||||
return (T) field.get(target);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new IllegalArgumentException("Cannot read field", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void set(Object target, Object value) {
|
||||
try {
|
||||
field.set(target, value);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new IllegalArgumentException("Cannot write field", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> Field<T> getField(Class<?> target, String name, Class<T> fieldType) {
|
||||
return getField(target, name, fieldType, 0);
|
||||
}
|
||||
|
||||
public static <T> Field<T> getField(String className, String name, Class<T> fieldType) {
|
||||
return getField(getClass(className), name, fieldType, 0);
|
||||
}
|
||||
|
||||
public static <T> Field<T> getField(Class<?> target, Class<T> fieldType, int index) {
|
||||
return getField(target, null, fieldType, index);
|
||||
}
|
||||
|
||||
public static <T> Field<T> getField(String className, Class<T> fieldType, int index) {
|
||||
return getField(getClass(className), fieldType, index);
|
||||
}
|
||||
|
||||
public static <T> Field<T> getField(Class<?> target, Class<T> fieldType, int index, Class<?>... parameters) {
|
||||
return getField(target, null, fieldType, index, parameters);
|
||||
}
|
||||
|
||||
private static <T> Field<T> getField(Class<?> target, String name, Class<T> fieldType, int index, Class<?>... parameters) {
|
||||
for (final java.lang.reflect.Field field : target.getDeclaredFields()) {
|
||||
if(matching(field, name, fieldType, parameters) && index-- <= 0) {
|
||||
field.setAccessible(true);
|
||||
return new Field<>(field);
|
||||
}
|
||||
}
|
||||
|
||||
// Search in parent classes
|
||||
if (target.getSuperclass() != null)
|
||||
return getField(target.getSuperclass(), name, fieldType, index);
|
||||
|
||||
throw new IllegalArgumentException("Cannot find field with type " + fieldType);
|
||||
}
|
||||
|
||||
private static <T> boolean matching(java.lang.reflect.Field field, String name, Class<T> fieldType, Class<?>... parameters) {
|
||||
if(name != null && !field.getName().equals(name))
|
||||
return false;
|
||||
|
||||
if(!fieldType.isAssignableFrom(field.getType()))
|
||||
return false;
|
||||
|
||||
if(parameters.length > 0) {
|
||||
Type[] arguments = ((ParameterizedType)field.getGenericType()).getActualTypeArguments();
|
||||
|
||||
for(int i = 0; i < parameters.length; i++) {
|
||||
if(arguments[i] instanceof ParameterizedType ? ((ParameterizedType) arguments[i]).getRawType() != parameters[i] : arguments[i] != parameters[i])
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@AllArgsConstructor
|
||||
public static class Method {
|
||||
private final java.lang.reflect.Method method;
|
||||
|
||||
public Object invoke(Object target, Object... arguments) {
|
||||
try {
|
||||
return method.invoke(target, arguments);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("Cannot invoke method " + method, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Method getMethod(String className, String methodName, Class<?>... params) {
|
||||
return getTypedMethod(getClass(className), methodName, null, params);
|
||||
}
|
||||
|
||||
public static Method getMethod(Class<?> clazz, String methodName, Class<?>... params) {
|
||||
return getTypedMethod(clazz, methodName, null, params);
|
||||
}
|
||||
|
||||
public static Method getTypedMethod(Class<?> clazz, String methodName, Class<?> returnType, Class<?>... params) {
|
||||
for (final java.lang.reflect.Method method : clazz.getDeclaredMethods()) {
|
||||
if ((methodName == null || method.getName().equals(methodName))
|
||||
&& (returnType == null || method.getReturnType().equals(returnType))
|
||||
&& Arrays.equals(method.getParameterTypes(), params)) {
|
||||
method.setAccessible(true);
|
||||
return new Method(method);
|
||||
}
|
||||
}
|
||||
|
||||
// Search in every superclass
|
||||
if (clazz.getSuperclass() != null)
|
||||
return getTypedMethod(clazz.getSuperclass(), methodName, returnType, params);
|
||||
|
||||
throw new IllegalArgumentException(String.format("Cannot find method %s (%s).", methodName, Arrays.asList(params)));
|
||||
}
|
||||
|
||||
@AllArgsConstructor
|
||||
public static class Constructor {
|
||||
private final java.lang.reflect.Constructor<?> constructor;
|
||||
|
||||
public Object invoke(Object... arguments) {
|
||||
try {
|
||||
return constructor.newInstance(arguments);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("Cannot invoke constructor " + constructor, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Constructor getConstructor(String className, Class<?>... params) {
|
||||
return getConstructor(getClass(className), params);
|
||||
}
|
||||
|
||||
public static Constructor getConstructor(Class<?> clazz, Class<?>... params) {
|
||||
for (final java.lang.reflect.Constructor<?> constructor : clazz.getDeclaredConstructors()) {
|
||||
if (Arrays.equals(constructor.getParameterTypes(), params)) {
|
||||
constructor.setAccessible(true);
|
||||
return new Constructor(constructor);
|
||||
}
|
||||
}
|
||||
|
||||
throw new IllegalStateException(String.format("Unable to find constructor for %s (%s).", clazz, Arrays.asList(params)));
|
||||
}
|
||||
|
||||
public static Object newInstance(Class<?> clazz) {
|
||||
try {
|
||||
if (Core.getVersion() > 15) {
|
||||
return Unsafe.getUnsafe().allocateInstance(clazz);
|
||||
} else {
|
||||
return clazz.newInstance();
|
||||
}
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
throw new SecurityException("Could not create object", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
package de.steamwar.core;
|
||||
|
||||
import com.comphenix.tinyprotocol.Reflection;
|
||||
import de.steamwar.Reflection;
|
||||
import com.comphenix.tinyprotocol.TinyProtocol;
|
||||
import com.viaversion.viaversion.api.Via;
|
||||
import de.steamwar.sql.internal.Statement;
|
||||
@@ -91,8 +91,8 @@ class CheckpointUtilsJ9 {
|
||||
}
|
||||
|
||||
|
||||
private static final Reflection.FieldAccessor<List> channelFutures = Reflection.getField(TinyProtocol.serverConnection, List.class, 0, ChannelFuture.class);
|
||||
private static final Reflection.MethodInvoker bind = Reflection.getMethod(TinyProtocol.serverConnection, null, InetAddress.class, int.class);
|
||||
private static final Reflection.Field<List> channelFutures = Reflection.getField(TinyProtocol.serverConnection, List.class, 0, ChannelFuture.class);
|
||||
private static final Reflection.Method bind = Reflection.getMethod(TinyProtocol.serverConnection, null, InetAddress.class, int.class);
|
||||
private static void freezeInternal(Path path) throws Exception {
|
||||
Bukkit.getPluginManager().callEvent(new CRIUSleepEvent());
|
||||
Bukkit.getWorlds().forEach(FlatteningWrapper.impl::syncSave);
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
package de.steamwar.core;
|
||||
|
||||
import com.comphenix.tinyprotocol.Reflection;
|
||||
import de.steamwar.Reflection;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
@@ -29,8 +29,8 @@ import java.util.Map;
|
||||
|
||||
@UtilityClass
|
||||
public class CommandRemover {
|
||||
private static final Reflection.FieldAccessor<SimpleCommandMap> commandMap = Reflection.getField("{obc}.CraftServer", "commandMap", SimpleCommandMap.class);
|
||||
private static final Reflection.FieldAccessor<Map> knownCommands = Reflection.getField(SimpleCommandMap.class, "knownCommands", Map.class);
|
||||
private static final Reflection.Field<SimpleCommandMap> commandMap = Reflection.getField("{obc}.CraftServer", "commandMap", SimpleCommandMap.class);
|
||||
private static final Reflection.Field<Map> knownCommands = Reflection.getField(SimpleCommandMap.class, "knownCommands", Map.class);
|
||||
public static void removeAll(String... cmds) {
|
||||
Map<String, Command> knownCmds = knownCommands.get(commandMap.get(Bukkit.getServer()));
|
||||
for (String cmd : cmds) {
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
package de.steamwar.core;
|
||||
|
||||
import com.comphenix.tinyprotocol.TinyProtocol;
|
||||
import de.steamwar.Reflection;
|
||||
import de.steamwar.command.*;
|
||||
import de.steamwar.core.authlib.AuthlibInjector;
|
||||
import de.steamwar.core.events.*;
|
||||
@@ -44,10 +45,8 @@ public class Core extends JavaPlugin{
|
||||
|
||||
public static final Message MESSAGE = new Message("SpigotCore", Core.class.getClassLoader());
|
||||
|
||||
private static final int VERSION = Integer.parseInt(Bukkit.getServer().getClass().getPackage().getName().split("_", 3)[1]);
|
||||
|
||||
public static int getVersion(){
|
||||
return VERSION;
|
||||
return Reflection.MAJOR_VERSION;
|
||||
}
|
||||
|
||||
private static JavaPlugin instance;
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
package de.steamwar.core;
|
||||
|
||||
import com.comphenix.tinyprotocol.Reflection;
|
||||
import de.steamwar.Reflection;
|
||||
import de.steamwar.sql.SWException;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
@@ -40,7 +40,7 @@ public class ErrorHandler extends Handler {
|
||||
Logger.getLogger("").addHandler(this);
|
||||
|
||||
Class<?> watchdogThread = Reflection.getClass("org.spigotmc.WatchdogThread");
|
||||
Reflection.FieldAccessor<?> getInstance = Reflection.getField(watchdogThread, watchdogThread, 0);
|
||||
Reflection.Field<?> getInstance = Reflection.getField(watchdogThread, watchdogThread, 0);
|
||||
Thread watchdog = (Thread) getInstance.get(null);
|
||||
watchdogThreadId = watchdog.getId();
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
package de.steamwar.core;
|
||||
|
||||
import com.comphenix.tinyprotocol.Reflection;
|
||||
import de.steamwar.Reflection;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.EntityType;
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
package de.steamwar.core;
|
||||
|
||||
import com.comphenix.tinyprotocol.Reflection;
|
||||
import de.steamwar.Reflection;
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import org.bukkit.GameMode;
|
||||
|
||||
@@ -33,7 +33,7 @@ public interface ProtocolWrapper {
|
||||
Class<?> equipmentPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutEntityEquipment");
|
||||
|
||||
Class<?> enumGamemode = Reflection.getClass(Core.getVersion() > 9 ? "{nms.world.level}.EnumGamemode" : "{nms}.WorldSettings$EnumGamemode");
|
||||
Reflection.MethodInvoker getGameModeById = Reflection.getTypedMethod(enumGamemode, null, enumGamemode, int.class);
|
||||
Reflection.Method getGameModeById = Reflection.getTypedMethod(enumGamemode, null, enumGamemode, int.class);
|
||||
|
||||
// 0: hand, 1: offhand, 2: feet, 3: legs, 4: chest, 5: head
|
||||
Object[] itemSlots = Core.getVersion() > 8 ? Reflection.getClass("{nms.world.entity}.EnumItemSlot").getEnumConstants() : new Integer[]{0, 0, 1, 2, 3, 4};
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
package de.steamwar.core;
|
||||
|
||||
import com.comphenix.tinyprotocol.Reflection;
|
||||
import de.steamwar.Reflection;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
public class TPSWatcher {
|
||||
@@ -80,8 +80,8 @@ public class TPSWatcher {
|
||||
}
|
||||
|
||||
private static final Class<?> minecraftServer = Reflection.getClass("{nms.server}.MinecraftServer");
|
||||
private static final Reflection.MethodInvoker getServer = Reflection.getTypedMethod(minecraftServer, "getServer", minecraftServer);
|
||||
private static final Reflection.FieldAccessor<double[]> recentTps = Reflection.getField(minecraftServer, "recentTps", double[].class);
|
||||
private static final Reflection.Method getServer = Reflection.getTypedMethod(minecraftServer, "getServer", minecraftServer);
|
||||
private static final Reflection.Field<double[]> recentTps = Reflection.getField(minecraftServer, "recentTps", double[].class);
|
||||
private static double[] getSpigotTPS() {
|
||||
return recentTps.get(getServer.invoke(null));
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
package de.steamwar.core.authlib;
|
||||
|
||||
import com.comphenix.tinyprotocol.Reflection;
|
||||
import de.steamwar.Reflection;
|
||||
import com.mojang.authlib.GameProfileRepository;
|
||||
import com.mojang.authlib.yggdrasil.YggdrasilGameProfileRepository;
|
||||
|
||||
@@ -28,7 +28,7 @@ public class AuthlibInjector {
|
||||
|
||||
public static void inject() {
|
||||
Class<?> minecraftServerClass = Reflection.getClass("{nms.server}.MinecraftServer");
|
||||
Reflection.FieldAccessor<GameProfileRepository> gameProfile = Reflection.getField(minecraftServerClass, GameProfileRepository.class, 0);
|
||||
Reflection.Field<GameProfileRepository> gameProfile = Reflection.getField(minecraftServerClass, GameProfileRepository.class, 0);
|
||||
Object minecraftServer = Reflection.getTypedMethod(minecraftServerClass, "getServer", minecraftServerClass).invoke(null);
|
||||
gameProfile.set(minecraftServer, new SteamwarGameProfileRepository((YggdrasilGameProfileRepository) gameProfile.get(minecraftServer)));
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
package de.steamwar.core.events;
|
||||
|
||||
import com.comphenix.tinyprotocol.Reflection;
|
||||
import de.steamwar.Reflection;
|
||||
import com.comphenix.tinyprotocol.TinyProtocol;
|
||||
import de.steamwar.core.Core;
|
||||
import de.steamwar.sql.SWException;
|
||||
@@ -59,8 +59,8 @@ public class AntiNocom implements Listener {
|
||||
Function<Object, Object> getPosition;
|
||||
if(Core.getVersion() > 12) {
|
||||
Class<?> movingObjectPositionBlock = Reflection.getClass("{nms.world.phys}.MovingObjectPositionBlock");
|
||||
Reflection.FieldAccessor<?> useItemPosition = Reflection.getField(useItem, movingObjectPositionBlock, 0);
|
||||
Reflection.FieldAccessor<?> movingBlockPosition = Reflection.getField(movingObjectPositionBlock, TechHider.blockPosition, 0);
|
||||
Reflection.Field<?> useItemPosition = Reflection.getField(useItem, movingObjectPositionBlock, 0);
|
||||
Reflection.Field<?> movingBlockPosition = Reflection.getField(movingObjectPositionBlock, TechHider.blockPosition, 0);
|
||||
|
||||
getPosition = (packet) -> movingBlockPosition.get(useItemPosition.get(packet));
|
||||
} else {
|
||||
@@ -74,7 +74,7 @@ public class AntiNocom implements Listener {
|
||||
}
|
||||
|
||||
private static final Class<?> blockDig = Reflection.getClass("{nms.network.protocol.game}.PacketPlayInBlockDig");
|
||||
private static final Reflection.FieldAccessor<?> digPosition = Reflection.getField(blockDig, TechHider.blockPosition, 0);
|
||||
private static final Reflection.Field<?> digPosition = Reflection.getField(blockDig, TechHider.blockPosition, 0);
|
||||
private Object onDig(Player player, Object packet) {
|
||||
Object pos = digPosition.get(packet);
|
||||
return isValid(player, "Dig", TechHider.blockPositionX.get(pos), TechHider.blockPositionZ.get(pos)) ? packet : null;
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
package de.steamwar.core.events;
|
||||
|
||||
import com.comphenix.tinyprotocol.Reflection;
|
||||
import de.steamwar.Reflection;
|
||||
import com.comphenix.tinyprotocol.TinyProtocol;
|
||||
import com.viaversion.viaversion.api.Via;
|
||||
import com.viaversion.viaversion.api.ViaAPI;
|
||||
@@ -39,9 +39,9 @@ public class PartialChunkFixer {
|
||||
|
||||
private static final int PROTOCOL1_17 = 755;
|
||||
private static final Class<?> mapChunk = Reflection.getClass("{nms}.PacketPlayOutMapChunk");
|
||||
private static final Reflection.FieldAccessor<Boolean> fullChunkFlag = Reflection.getField(mapChunk, boolean.class, 0);
|
||||
private static final Reflection.FieldAccessor<Integer> chunkX = Reflection.getField(mapChunk, int.class, 0);
|
||||
private static final Reflection.FieldAccessor<Integer> chunkZ = Reflection.getField(mapChunk, int.class, 1);
|
||||
private static final Reflection.Field<Boolean> fullChunkFlag = Reflection.getField(mapChunk, boolean.class, 0);
|
||||
private static final Reflection.Field<Integer> chunkX = Reflection.getField(mapChunk, int.class, 0);
|
||||
private static final Reflection.Field<Integer> chunkZ = Reflection.getField(mapChunk, int.class, 1);
|
||||
|
||||
private final ViaAPI<Player> via = Via.getAPI();
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
package de.steamwar.entity;
|
||||
|
||||
import com.comphenix.tinyprotocol.Reflection;
|
||||
import de.steamwar.Reflection;
|
||||
import de.steamwar.core.*;
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
import it.unimi.dsi.fastutil.ints.IntList;
|
||||
@@ -161,8 +161,8 @@ public class REntity {
|
||||
}
|
||||
|
||||
private static final Class<?> animationPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutAnimation");
|
||||
private static final Reflection.FieldAccessor<Integer> animationEntity = Reflection.getField(animationPacket, int.class, Core.getVersion() > 15 ? (Core.getVersion() > 19 ? 5 : 6) : 0);
|
||||
private static final Reflection.FieldAccessor<Integer> animationAnimation = Reflection.getField(animationPacket, int.class, Core.getVersion() > 15 ? (Core.getVersion() > 19 ? 6 : 7) : 1);
|
||||
private static final Reflection.Field<Integer> animationEntity = Reflection.getField(animationPacket, int.class, Core.getVersion() > 15 ? (Core.getVersion() > 19 ? 5 : 6) : 0);
|
||||
private static final Reflection.Field<Integer> animationAnimation = Reflection.getField(animationPacket, int.class, Core.getVersion() > 15 ? (Core.getVersion() > 19 ? 6 : 7) : 1);
|
||||
public void showAnimation(byte animation) {
|
||||
Object packet = Reflection.newInstance(animationPacket);
|
||||
animationEntity.set(packet, entityId);
|
||||
@@ -171,10 +171,10 @@ public class REntity {
|
||||
}
|
||||
|
||||
private static final Class<?> velocityPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutEntityVelocity");
|
||||
private static final Reflection.FieldAccessor<Integer> velocityEntity = Reflection.getField(velocityPacket, int.class, 0);
|
||||
private static final Reflection.FieldAccessor<Integer> velocityX = Reflection.getField(velocityPacket, int.class, 1);
|
||||
private static final Reflection.FieldAccessor<Integer> velocityY = Reflection.getField(velocityPacket, int.class, 2);
|
||||
private static final Reflection.FieldAccessor<Integer> velocityZ = Reflection.getField(velocityPacket, int.class, 3);
|
||||
private static final Reflection.Field<Integer> velocityEntity = Reflection.getField(velocityPacket, int.class, 0);
|
||||
private static final Reflection.Field<Integer> velocityX = Reflection.getField(velocityPacket, int.class, 1);
|
||||
private static final Reflection.Field<Integer> velocityY = Reflection.getField(velocityPacket, int.class, 2);
|
||||
private static final Reflection.Field<Integer> velocityZ = Reflection.getField(velocityPacket, int.class, 3);
|
||||
public void setVelocity(double dX, double dY, double dZ) {
|
||||
Object packet = Reflection.newInstance(velocityPacket);
|
||||
velocityEntity.set(packet, entityId);
|
||||
@@ -185,8 +185,8 @@ public class REntity {
|
||||
}
|
||||
|
||||
private static final Class<?> statusPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutEntityStatus");
|
||||
private static final Reflection.FieldAccessor<Integer> statusEntity = Reflection.getField(statusPacket, int.class, 0);
|
||||
private static final Reflection.FieldAccessor<Byte> statusStatus = Reflection.getField(statusPacket, byte.class, 0);
|
||||
private static final Reflection.Field<Integer> statusEntity = Reflection.getField(statusPacket, int.class, 0);
|
||||
private static final Reflection.Field<Byte> statusStatus = Reflection.getField(statusPacket, byte.class, 0);
|
||||
public void showDamage() {
|
||||
Object packet = Reflection.newInstance(statusPacket);
|
||||
statusEntity.set(packet, entityId);
|
||||
@@ -293,7 +293,7 @@ public class REntity {
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
private static final Reflection.FieldAccessor<Integer> additionalData = Reflection.getField(ProtocolWrapper.spawnPacket, int.class, objectDataOffset());
|
||||
private static final Reflection.Field<Integer> additionalData = Reflection.getField(ProtocolWrapper.spawnPacket, int.class, objectDataOffset());
|
||||
private Object spawnPacketGenerator() {
|
||||
Object packet = spawnPacketGenerator.apply(this);
|
||||
additionalData.set(packet, objectData);
|
||||
@@ -347,7 +347,7 @@ public class REntity {
|
||||
}
|
||||
|
||||
private static final Class<?> destroyPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutEntityDestroy");
|
||||
private static final Reflection.FieldAccessor<?> destroyEntities;
|
||||
private static final Reflection.Field<?> destroyEntities;
|
||||
static {
|
||||
if(Core.getVersion() > 15)
|
||||
destroyEntities = Reflection.getField(destroyPacket, IntList.class, 0);
|
||||
@@ -394,7 +394,7 @@ public class REntity {
|
||||
}
|
||||
|
||||
public static final Class<?> teleportPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutEntityTeleport");
|
||||
public static final Reflection.FieldAccessor<Integer> teleportEntity = Reflection.getField(teleportPacket, int.class, 0);
|
||||
public static final Reflection.Field<Integer> teleportEntity = Reflection.getField(teleportPacket, int.class, 0);
|
||||
public static final BountifulWrapper.PositionSetter teleportPosition = BountifulWrapper.impl.getPositionSetter(teleportPacket, Core.getVersion() == 8 ? 1 : 0);
|
||||
private Object getTeleportPacket(){
|
||||
Object packet = Reflection.newInstance(teleportPacket);
|
||||
@@ -404,7 +404,7 @@ public class REntity {
|
||||
}
|
||||
|
||||
private static final Class<?> entityPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutEntity");
|
||||
private static final Reflection.FieldAccessor<Integer> moveEntityId = Reflection.getField(entityPacket, int.class, 0);
|
||||
private static final Reflection.Field<Integer> moveEntityId = Reflection.getField(entityPacket, int.class, 0);
|
||||
private static final BountifulWrapper.PositionSetter movePosition = BountifulWrapper.impl.getRelMoveSetter(entityPacket);
|
||||
private static final Class<?> lookPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutEntity$PacketPlayOutEntityLook");
|
||||
private static final Class<?> movePacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutEntity$PacketPlayOutRelEntityMove");
|
||||
@@ -429,8 +429,8 @@ public class REntity {
|
||||
}
|
||||
|
||||
private static final Class<?> headRotationPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutEntityHeadRotation");
|
||||
private static final Reflection.FieldAccessor<Integer> headRotationEntity = Reflection.getField(headRotationPacket, int.class, 0);
|
||||
private static final Reflection.FieldAccessor<Byte> headRotationYaw = Reflection.getField(headRotationPacket, byte.class, 0);
|
||||
private static final Reflection.Field<Integer> headRotationEntity = Reflection.getField(headRotationPacket, int.class, 0);
|
||||
private static final Reflection.Field<Byte> headRotationYaw = Reflection.getField(headRotationPacket, byte.class, 0);
|
||||
private Object getHeadRotationPacket(){
|
||||
Object packet = Reflection.newInstance(headRotationPacket);
|
||||
headRotationEntity.set(packet, entityId);
|
||||
@@ -438,10 +438,10 @@ public class REntity {
|
||||
return packet;
|
||||
}
|
||||
|
||||
private static final Reflection.FieldAccessor<Integer> equipmentEntity = Reflection.getField(ProtocolWrapper.equipmentPacket, int.class, 0);
|
||||
private static final Reflection.Field<Integer> equipmentEntity = Reflection.getField(ProtocolWrapper.equipmentPacket, int.class, 0);
|
||||
|
||||
private static final Class<?> craftItemStack = Reflection.getClass("{obc}.inventory.CraftItemStack");
|
||||
private static final Reflection.MethodInvoker asNMSCopy = Reflection.getTypedMethod(REntity.craftItemStack, "asNMSCopy", ProtocolWrapper.itemStack, ItemStack.class);
|
||||
private static final Reflection.Method asNMSCopy = Reflection.getTypedMethod(REntity.craftItemStack, "asNMSCopy", ProtocolWrapper.itemStack, ItemStack.class);
|
||||
protected Object getEquipmentPacket(Object slot, ItemStack stack){
|
||||
Object packet = Reflection.newInstance(ProtocolWrapper.equipmentPacket);
|
||||
equipmentEntity.set(packet, entityId);
|
||||
@@ -462,7 +462,7 @@ public class REntity {
|
||||
}
|
||||
|
||||
protected static Function<REntity, Object> spawnPacketGenerator(Class<?> spawnPacket, int posOffset) {
|
||||
Reflection.FieldAccessor<Integer> entityId = Reflection.getField(spawnPacket, int.class, 0);
|
||||
Reflection.Field<Integer> entityId = Reflection.getField(spawnPacket, int.class, 0);
|
||||
BountifulWrapper.PositionSetter position = BountifulWrapper.impl.getPositionSetter(spawnPacket, posOffset);
|
||||
|
||||
return entity -> {
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
package de.steamwar.entity;
|
||||
|
||||
import com.comphenix.tinyprotocol.Reflection;
|
||||
import de.steamwar.Reflection;
|
||||
import com.comphenix.tinyprotocol.TinyProtocol;
|
||||
import de.steamwar.core.Core;
|
||||
import de.steamwar.core.FlatteningWrapper;
|
||||
@@ -48,13 +48,13 @@ public class REntityServer implements Listener {
|
||||
private static final HashSet<Player> emptyPlayers = new HashSet<>(0);
|
||||
|
||||
private static final Class<?> useEntity = Reflection.getClass("{nms.network.protocol.game}.PacketPlayInUseEntity");
|
||||
private static final Reflection.FieldAccessor<Integer> useEntityTarget = Reflection.getField(useEntity, int.class, 0);
|
||||
private static final Reflection.Field<Integer> useEntityTarget = Reflection.getField(useEntity, int.class, 0);
|
||||
private static final Class<?> useEntityEnumAction = Reflection.getClass("{nms.network.protocol.game}.PacketPlayInUseEntity$EnumEntityUseAction");
|
||||
private static final Reflection.FieldAccessor<?> useEntityAction = Reflection.getField(useEntity, useEntityEnumAction, 0);
|
||||
private static final Reflection.Field<?> useEntityAction = Reflection.getField(useEntity, useEntityEnumAction, 0);
|
||||
private static final Function<Object, Integer> getEntityAction;
|
||||
static {
|
||||
if(Core.getVersion() > 15) {
|
||||
Reflection.MethodInvoker useEntityGetAction = Reflection.getMethod(useEntityEnumAction, "a");
|
||||
Reflection.Method useEntityGetAction = Reflection.getMethod(useEntityEnumAction, "a");
|
||||
getEntityAction = value -> ((Enum<?>) useEntityGetAction.invoke(value)).ordinal();
|
||||
} else {
|
||||
getEntityAction = value -> ((Enum<?>) value).ordinal();
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
package de.steamwar.entity;
|
||||
|
||||
import com.comphenix.tinyprotocol.Reflection;
|
||||
import de.steamwar.Reflection;
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import de.steamwar.core.BountifulWrapper;
|
||||
import de.steamwar.core.Core;
|
||||
@@ -93,7 +93,7 @@ public class RPlayer extends REntity {
|
||||
|
||||
private static final Class<?> namedSpawnPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutNamedEntitySpawn");
|
||||
private static final Function<REntity, Object> namedSpawnPacketGenerator = spawnPacketGenerator(namedSpawnPacket, Core.getVersion() == 8 ? 1 : 0);
|
||||
private static final Reflection.FieldAccessor<UUID> namedSpawnUUID = Reflection.getField(namedSpawnPacket, UUID.class, 0);
|
||||
private static final Reflection.Field<UUID> namedSpawnUUID = Reflection.getField(namedSpawnPacket, UUID.class, 0);
|
||||
private Object getNamedSpawnPacket() {
|
||||
Object packet = namedSpawnPacketGenerator.apply(this);
|
||||
namedSpawnUUID.set(packet, uuid);
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
package de.steamwar.network.handlers;
|
||||
|
||||
import com.comphenix.tinyprotocol.Reflection;
|
||||
import de.steamwar.Reflection;
|
||||
import com.comphenix.tinyprotocol.TinyProtocol;
|
||||
|
||||
public class ServerDataHandler {
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
package de.steamwar.techhider;
|
||||
|
||||
import com.comphenix.tinyprotocol.Reflection;
|
||||
import de.steamwar.Reflection;
|
||||
import com.google.common.primitives.Bytes;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
package de.steamwar.techhider;
|
||||
|
||||
import com.comphenix.tinyprotocol.Reflection;
|
||||
import de.steamwar.Reflection;
|
||||
import com.comphenix.tinyprotocol.TinyProtocol;
|
||||
import de.steamwar.core.Core;
|
||||
import lombok.Getter;
|
||||
@@ -37,19 +37,19 @@ public class TechHider {
|
||||
|
||||
public static final Class<?> blockPosition = Reflection.getClass("{nms.core}.BlockPosition");
|
||||
private static final Class<?> baseBlockPosition = Reflection.getClass("{nms.core}.BaseBlockPosition");
|
||||
public static final Reflection.FieldAccessor<Integer> blockPositionX = Reflection.getField(baseBlockPosition, int.class, 0);
|
||||
public static final Reflection.FieldAccessor<Integer> blockPositionY = Reflection.getField(baseBlockPosition, int.class, 1);
|
||||
public static final Reflection.FieldAccessor<Integer> blockPositionZ = Reflection.getField(baseBlockPosition, int.class, 2);
|
||||
public static final Reflection.Field<Integer> blockPositionX = Reflection.getField(baseBlockPosition, int.class, 0);
|
||||
public static final Reflection.Field<Integer> blockPositionY = Reflection.getField(baseBlockPosition, int.class, 1);
|
||||
public static final Reflection.Field<Integer> blockPositionZ = Reflection.getField(baseBlockPosition, int.class, 2);
|
||||
|
||||
public static final Class<?> iBlockData = Reflection.getClass("{nms.world.level.block.state}.IBlockData");
|
||||
public static final Class<?> block = Reflection.getClass("{nms.world.level.block}.Block");
|
||||
private static final Reflection.MethodInvoker getBlockDataByBlock = Reflection.getTypedMethod(block, null, iBlockData);
|
||||
private static final Reflection.Method getBlockDataByBlock = Reflection.getTypedMethod(block, null, iBlockData);
|
||||
|
||||
public static final Class<?> craftMagicNumbers = Reflection.getClass("{obc}.util.CraftMagicNumbers");
|
||||
private static final Reflection.MethodInvoker getBlockByMaterial = Reflection.getTypedMethod(craftMagicNumbers, "getBlock", block, Material.class);
|
||||
private static final Reflection.Method getBlockByMaterial = Reflection.getTypedMethod(craftMagicNumbers, "getBlock", block, Material.class);
|
||||
|
||||
private static final Reflection.MethodInvoker getBlockByBlockData = Reflection.getTypedMethod(iBlockData, null, block);
|
||||
private static final Reflection.MethodInvoker getMaterialByBlock = Reflection.getTypedMethod(craftMagicNumbers, "getMaterial", Material.class, block);
|
||||
private static final Reflection.Method getBlockByBlockData = Reflection.getTypedMethod(iBlockData, null, block);
|
||||
private static final Reflection.Method getMaterialByBlock = Reflection.getTypedMethod(craftMagicNumbers, "getMaterial", Material.class, block);
|
||||
public boolean iBlockDataHidden(Object iBlockData) {
|
||||
return obfuscate.contains((Material) getMaterialByBlock.invoke(null, getBlockByBlockData.invoke(iBlockData)));
|
||||
}
|
||||
@@ -110,8 +110,8 @@ public class TechHider {
|
||||
|
||||
private static final Class<?> blockChangePacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutBlockChange");
|
||||
private static final Function<Object, Object> blockChangeCloner = ProtocolUtils.shallowCloneGenerator(blockChangePacket);
|
||||
private static final Reflection.FieldAccessor<?> blockChangePosition = Reflection.getField(blockChangePacket, blockPosition, 0);
|
||||
private static final Reflection.FieldAccessor<?> blockChangeBlockData = Reflection.getField(blockChangePacket, iBlockData, 0);
|
||||
private static final Reflection.Field<?> blockChangePosition = Reflection.getField(blockChangePacket, blockPosition, 0);
|
||||
private static final Reflection.Field<?> blockChangeBlockData = Reflection.getField(blockChangePacket, iBlockData, 0);
|
||||
private Object blockChangeHider(Player p, Object packet) {
|
||||
switch (locationEvaluator.checkBlockPos(p, blockChangePosition.get(packet))) {
|
||||
case SKIP:
|
||||
@@ -132,7 +132,7 @@ public class TechHider {
|
||||
}
|
||||
|
||||
private static final Class<?> blockActionPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutBlockAction");
|
||||
private static final Reflection.FieldAccessor<?> blockActionPosition = Reflection.getField(blockActionPacket, blockPosition, 0);
|
||||
private static final Reflection.Field<?> blockActionPosition = Reflection.getField(blockActionPacket, blockPosition, 0);
|
||||
private Object blockActionHider(Player p, Object packet) {
|
||||
if (locationEvaluator.checkBlockPos(p, blockActionPosition.get(packet)) == State.SKIP)
|
||||
return packet;
|
||||
@@ -140,7 +140,7 @@ public class TechHider {
|
||||
}
|
||||
|
||||
public static final Class<?> tileEntityDataPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutTileEntityData");
|
||||
private static final Reflection.FieldAccessor<?> tileEntityDataPosition = Reflection.getField(tileEntityDataPacket, blockPosition, 0);
|
||||
private static final Reflection.Field<?> tileEntityDataPosition = Reflection.getField(tileEntityDataPacket, blockPosition, 0);
|
||||
private Object tileEntityDataHider(Player p, Object packet) {
|
||||
switch (locationEvaluator.checkBlockPos(p, tileEntityDataPosition.get(packet))) {
|
||||
case SKIP:
|
||||
|
||||
Reference in New Issue
Block a user