forked from SteamWar/SteamWar
Replace tinyProtocolLib with newest version
This commit is contained in:
@@ -0,0 +1,502 @@
|
|||||||
|
package com.comphenix.tinyprotocol;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.lang.reflect.ParameterizedType;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
public 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.
|
||||||
|
*/
|
||||||
|
public 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.
|
||||||
|
*/
|
||||||
|
public 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.
|
||||||
|
*/
|
||||||
|
public 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.
|
||||||
|
*/
|
||||||
|
public boolean hasField(Object target);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deduce the net.minecraft.server.v* package
|
||||||
|
private static String OBC_PREFIX = Bukkit.getServer().getClass().getPackage().getName();
|
||||||
|
private static String NMS_PREFIX = OBC_PREFIX.replace("org.bukkit.craftbukkit", "net.minecraft.server");
|
||||||
|
private static String VERSION = OBC_PREFIX.replace("org.bukkit.craftbukkit", "").replace(".", "");
|
||||||
|
|
||||||
|
// Variable replacement
|
||||||
|
private static 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Common method
|
||||||
|
private static <T> FieldAccessor<T> getField(Class<?> target, String name, Class<T> fieldType, int index) {
|
||||||
|
for (final Field field : target.getDeclaredFields()) {
|
||||||
|
if ((name == null || field.getName().equals(name)) && fieldType.isAssignableFrom(field.getType()) && index-- <= 0) {
|
||||||
|
field.setAccessible(true);
|
||||||
|
|
||||||
|
// A function for retrieving a specific field value
|
||||||
|
return new FieldAccessor<T>() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public T get(Object target) {
|
||||||
|
try {
|
||||||
|
return (T) field.get(target);
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
throw new RuntimeException("Cannot access reflection.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void set(Object target, Object value) {
|
||||||
|
try {
|
||||||
|
field.set(target, value);
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
throw new RuntimeException("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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a field with a given type and parameters. This is most useful
|
||||||
|
* when dealing with Collections.
|
||||||
|
*
|
||||||
|
* @param target the target class.
|
||||||
|
* @param fieldType Type of the field
|
||||||
|
* @param params Variable length array of type parameters
|
||||||
|
* @return The field
|
||||||
|
*
|
||||||
|
* @throws IllegalArgumentException If the field cannot be found
|
||||||
|
*/
|
||||||
|
public static Field getParameterizedField(Class<?> target, Class<?> fieldType, Class<?>... params) {
|
||||||
|
for (Field field : target.getDeclaredFields()) {
|
||||||
|
if (field.getType().equals(fieldType)) {
|
||||||
|
Type type = field.getGenericType();
|
||||||
|
if (type instanceof ParameterizedType) {
|
||||||
|
if (Arrays.equals(((ParameterizedType) type).getActualTypeArguments(), params))
|
||||||
|
return field;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new IllegalArgumentException("Unable to find a field with type " + fieldType + " and params " + Arrays.toString(params));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 new MethodInvoker() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object invoke(Object target, Object... arguments) {
|
||||||
|
try {
|
||||||
|
return method.invoke(target, arguments);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("Cannot invoke method " + method, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Search in every superclass
|
||||||
|
if (clazz.getSuperclass() != null)
|
||||||
|
return getMethod(clazz.getSuperclass(), methodName, 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 new ConstructorInvoker() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object invoke(Object... arguments) {
|
||||||
|
try {
|
||||||
|
return constructor.newInstance(arguments);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("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>
|
||||||
|
*
|
||||||
|
* @see {@link #getClass()} for more information.
|
||||||
|
* @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 with alternatives, without knowing its type on compile time.
|
||||||
|
* <p>
|
||||||
|
* This is useful when looking up fields by a NMS or OBC type.
|
||||||
|
* <p>
|
||||||
|
*
|
||||||
|
* @see {@link #getClass()} for more information.
|
||||||
|
* @param lookupName - the class name with variables.
|
||||||
|
* @param aliases - alternative names for this class.
|
||||||
|
* @return The class.
|
||||||
|
*/
|
||||||
|
public static Class<Object> getUntypedClass(String lookupName, String... aliases) {
|
||||||
|
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||||
|
Class<Object> clazz = (Class) getClass(lookupName, aliases);
|
||||||
|
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) {
|
||||||
|
return getCanonicalClass(expandVariables(lookupName));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the first class that matches the full class 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.
|
||||||
|
* @param aliases - alternative names for this class.
|
||||||
|
* @return Class object.
|
||||||
|
* @throws RuntimeException If we are unable to find any of the given classes.
|
||||||
|
*/
|
||||||
|
public static Class<?> getClass(String lookupName, String... aliases) {
|
||||||
|
try {
|
||||||
|
// Try the main class first
|
||||||
|
return getClass(lookupName);
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
Class<?> success = null;
|
||||||
|
|
||||||
|
// Try every alias too
|
||||||
|
for (String alias : aliases) {
|
||||||
|
try {
|
||||||
|
success = getClass(alias);
|
||||||
|
break;
|
||||||
|
} catch (RuntimeException e1) {
|
||||||
|
// e1.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (success != null) {
|
||||||
|
return success;
|
||||||
|
} else {
|
||||||
|
// Hack failed
|
||||||
|
throw new RuntimeException(String.format("Unable to find %s (%s)", lookupName, String.join(",", aliases)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve a class in the net.minecraft.server.VERSION.* package.
|
||||||
|
*
|
||||||
|
* @param name - the name of the class, excluding the package.
|
||||||
|
* @throws IllegalArgumentException If the class doesn't exist.
|
||||||
|
*/
|
||||||
|
public static Class<?> getMinecraftClass(String name) {
|
||||||
|
return getCanonicalClass(NMS_PREFIX + "." + name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve a class in the org.bukkit.craftbukkit.VERSION.* package.
|
||||||
|
*
|
||||||
|
* @param name - the name of the class, excluding the package.
|
||||||
|
* @throws IllegalArgumentException If the class doesn't exist.
|
||||||
|
*/
|
||||||
|
public static Class<?> getCraftBukkitClass(String name) {
|
||||||
|
return getCanonicalClass(OBC_PREFIX + "." + name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve a class by its canonical name.
|
||||||
|
*
|
||||||
|
* @param canonicalName - the canonical name.
|
||||||
|
* @return The class.
|
||||||
|
*/
|
||||||
|
private static Class<?> getCanonicalClass(String canonicalName) {
|
||||||
|
try {
|
||||||
|
return Class.forName(canonicalName);
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
throw new IllegalArgumentException("Cannot find " + canonicalName, 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 ("nms".equalsIgnoreCase(variable))
|
||||||
|
replacement = NMS_PREFIX;
|
||||||
|
else if ("obc".equalsIgnoreCase(variable))
|
||||||
|
replacement = OBC_PREFIX;
|
||||||
|
else if ("version".equalsIgnoreCase(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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,29 +1,24 @@
|
|||||||
/*
|
|
||||||
* This file is a part of the SteamWar software.
|
|
||||||
*
|
|
||||||
* Copyright (C) 2025 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;
|
package com.comphenix.tinyprotocol;
|
||||||
|
|
||||||
import de.steamwar.Reflection;
|
import io.netty.channel.Channel;
|
||||||
import de.steamwar.Reflection.Field;
|
import io.netty.channel.ChannelDuplexHandler;
|
||||||
import de.steamwar.core.Core;
|
import io.netty.channel.ChannelFuture;
|
||||||
import io.netty.channel.*;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
import lombok.Getter;
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||||
|
import io.netty.channel.ChannelInitializer;
|
||||||
|
import io.netty.channel.ChannelPipeline;
|
||||||
|
import io.netty.channel.ChannelPromise;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
@@ -31,217 +26,517 @@ import org.bukkit.event.EventPriority;
|
|||||||
import org.bukkit.event.HandlerList;
|
import org.bukkit.event.HandlerList;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.player.PlayerLoginEvent;
|
import org.bukkit.event.player.PlayerLoginEvent;
|
||||||
import org.bukkit.event.player.PlayerQuitEvent;
|
|
||||||
import org.bukkit.event.server.PluginDisableEvent;
|
import org.bukkit.event.server.PluginDisableEvent;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
import java.util.*;
|
import com.comphenix.tinyprotocol.Reflection.FieldAccessor;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import com.comphenix.tinyprotocol.Reflection.MethodInvoker;
|
||||||
import java.util.function.BiFunction;
|
import com.google.common.collect.Lists;
|
||||||
import java.util.logging.Level;
|
import com.google.common.collect.MapMaker;
|
||||||
|
import com.mojang.authlib.GameProfile;
|
||||||
|
|
||||||
public class TinyProtocol implements Listener {
|
/**
|
||||||
|
* Represents a very tiny alternative to ProtocolLib.
|
||||||
|
* <p>
|
||||||
|
* It now supports intercepting packets during login and status ping (such as OUT_SERVER_PING)!
|
||||||
|
*
|
||||||
|
* @author Kristian
|
||||||
|
*/
|
||||||
|
public abstract class TinyProtocol {
|
||||||
|
private static final AtomicInteger ID = new AtomicInteger(0);
|
||||||
|
|
||||||
private static final Class<?> craftServer = Reflection.getClass("org.bukkit.craftbukkit.CraftServer");
|
// Required Minecraft classes
|
||||||
private static final Class<?> dedicatedPlayerList = Reflection.getClass("net.minecraft.server.dedicated.DedicatedPlayerList");
|
private static final Class<?> entityPlayerClass = Reflection.getClass("{nms}.EntityPlayer", "net.minecraft.server.level.EntityPlayer");
|
||||||
private static final Field<?> getPlayerList = Reflection.getField(craftServer, dedicatedPlayerList, 0);
|
private static final Class<?> playerConnectionClass = Reflection.getClass("{nms}.PlayerConnection", "net.minecraft.server.network.PlayerConnection");
|
||||||
private static final Class<?> playerList = Reflection.getClass("net.minecraft.server.players.PlayerList");
|
private static final Class<?> networkManagerClass = Reflection.getClass("{nms}.NetworkManager", "net.minecraft.network.NetworkManager");
|
||||||
private static final Class<?> minecraftServer = Reflection.getClass("net.minecraft.server.MinecraftServer");
|
|
||||||
private static final Field<?> getMinecraftServer = Reflection.getField(playerList, minecraftServer, 0);
|
|
||||||
public static final Class<?> serverConnection = Reflection.getClass("net.minecraft.server.network.ServerConnectionListener");
|
|
||||||
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("net.minecraft.network.NetworkManager");
|
|
||||||
public static final Field<List> networkManagers = Reflection.getField(serverConnection, List.class, 0, networkManager);
|
|
||||||
|
|
||||||
private static final String HANDLER_NAME = "tiny-steamwar";
|
// Used in order to lookup a channel
|
||||||
public static final TinyProtocol instance = new TinyProtocol(Core.getInstance());
|
private static final MethodInvoker getPlayerHandle = Reflection.getMethod("{obc}.entity.CraftPlayer", "getHandle");
|
||||||
|
private static final FieldAccessor<?> getConnection = Reflection.getField(entityPlayerClass, null, playerConnectionClass);
|
||||||
|
private static final FieldAccessor<?> getManager = Reflection.getField(playerConnectionClass, null, networkManagerClass);
|
||||||
|
private static final FieldAccessor<Channel> getChannel = Reflection.getField(networkManagerClass, Channel.class, 0);
|
||||||
|
|
||||||
public static void init() {
|
// Looking up ServerConnection
|
||||||
//enforce init
|
private static final Class<Object> minecraftServerClass = Reflection.getUntypedClass("{nms}.MinecraftServer", "net.minecraft.server.MinecraftServer");
|
||||||
}
|
private static final Class<Object> serverConnectionClass = Reflection.getUntypedClass("{nms}.ServerConnection", "net.minecraft.server.network.ServerConnection");
|
||||||
|
private static final FieldAccessor<Object> getMinecraftServer = Reflection.getField("{obc}.CraftServer", minecraftServerClass, 0);
|
||||||
|
private static final FieldAccessor<Object> getServerConnection = Reflection.getField(minecraftServerClass, serverConnectionClass, 0);
|
||||||
|
|
||||||
private final Plugin plugin;
|
// Packets we have to intercept
|
||||||
private final List<?> connections;
|
private static final Class<?> PACKET_LOGIN_IN_START = Reflection.getClass("{nms}.PacketLoginInStart", "net.minecraft.network.protocol.login.PacketLoginInStart");
|
||||||
private boolean closed;
|
private static final FieldAccessor<GameProfile> getGameProfile = Reflection.getField(PACKET_LOGIN_IN_START, GameProfile.class, 0);
|
||||||
|
|
||||||
private final Map<Class<?>, List<BiFunction<Player, Object, Object>>> packetFilters = new HashMap<>();
|
// Speedup channel lookup
|
||||||
@Getter
|
private Map<String, Channel> channelLookup = new MapMaker().weakValues().makeMap();
|
||||||
private final Map<Player, PacketInterceptor> playerInterceptors = new HashMap<>();
|
private Listener listener;
|
||||||
|
|
||||||
@Override
|
// Channels that have already been removed
|
||||||
public String toString() {
|
private Set<Channel> uninjectedChannels = Collections.newSetFromMap(new MapMaker().weakKeys().<Channel, Boolean>makeMap());
|
||||||
return "TinyProtocol{" +
|
|
||||||
"plugin=" + plugin +
|
|
||||||
", connections=" + connections +
|
|
||||||
", closed=" + closed +
|
|
||||||
", packetFilters=" + packetFilters +
|
|
||||||
", playerInterceptors=" + playerInterceptors +
|
|
||||||
'}';
|
|
||||||
}
|
|
||||||
|
|
||||||
private TinyProtocol(final Plugin plugin) {
|
// List of network markers
|
||||||
this.plugin = plugin;
|
private List<Object> networkManagers;
|
||||||
this.connections = networkManagers.get(getServerConnection(plugin));
|
|
||||||
|
|
||||||
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
// Injected channel handlers
|
||||||
|
private List<Channel> serverChannels = new ArrayList<>();
|
||||||
|
private ChannelInboundHandlerAdapter serverChannelHandler;
|
||||||
|
private ChannelInitializer<Channel> beginInitProtocol;
|
||||||
|
private ChannelInitializer<Channel> endInitProtocol;
|
||||||
|
|
||||||
for (Player player : plugin.getServer().getOnlinePlayers()) {
|
// Current handler name
|
||||||
new PacketInterceptor(player);
|
private String handlerName;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.LOWEST)
|
protected volatile boolean closed;
|
||||||
public void onPlayerLogin(PlayerLoginEvent e) {
|
protected Plugin plugin;
|
||||||
plugin.getLogger().info("Creating PacketInterceptor for: " + e.getPlayer().getName() + " (" + closed + ")");
|
|
||||||
if(closed)
|
|
||||||
return;
|
|
||||||
new PacketInterceptor(e.getPlayer());
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.MONITOR)
|
/**
|
||||||
public void onPlayerDisconnect(PlayerQuitEvent e) {
|
* Construct a new instance of TinyProtocol, and start intercepting packets for all connected clients and future clients.
|
||||||
getInterceptor(e.getPlayer()).ifPresent(PacketInterceptor::close);
|
* <p>
|
||||||
}
|
* You can construct multiple instances per plugin.
|
||||||
|
*
|
||||||
|
* @param plugin - the plugin.
|
||||||
|
*/
|
||||||
|
public TinyProtocol(final Plugin plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
|
||||||
@EventHandler
|
// Compute handler name
|
||||||
public void onPluginDisable(PluginDisableEvent e) {
|
this.handlerName = getHandlerName();
|
||||||
if (e.getPlugin().equals(plugin)) {
|
|
||||||
close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addFilter(Class<?> packetType, BiFunction<Player, Object, Object> filter) {
|
// Prepare existing players
|
||||||
packetFilters.computeIfAbsent(packetType, c -> new CopyOnWriteArrayList<>()).add(filter);
|
registerBukkitEvents();
|
||||||
}
|
|
||||||
|
|
||||||
public void removeFilter(Class<?> packetType, BiFunction<Player, Object, Object> filter) {
|
try {
|
||||||
packetFilters.getOrDefault(packetType, Collections.emptyList()).remove(filter);
|
registerChannelHandler();
|
||||||
}
|
registerPlayers(plugin);
|
||||||
|
} catch (IllegalArgumentException ex) {
|
||||||
|
// Damn you, late bind
|
||||||
|
plugin.getLogger().info("[TinyProtocol] Delaying server channel injection due to late bind.");
|
||||||
|
|
||||||
public void sendPacket(Player player, Object packet) {
|
new BukkitRunnable() {
|
||||||
getInterceptor(player).ifPresent(i -> i.sendPacket(packet));
|
@Override
|
||||||
}
|
public void run() {
|
||||||
|
registerChannelHandler();
|
||||||
|
registerPlayers(plugin);
|
||||||
|
plugin.getLogger().info("[TinyProtocol] Late bind injection successful.");
|
||||||
|
}
|
||||||
|
}.runTask(plugin);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void receivePacket(Player player, Object packet) {
|
private void createServerChannelHandler() {
|
||||||
getInterceptor(player).ifPresent(i -> i.receivePacket(packet));
|
// Handle connected channels
|
||||||
}
|
endInitProtocol = new ChannelInitializer<Channel>() {
|
||||||
|
|
||||||
public final void close() {
|
@Override
|
||||||
plugin.getLogger().log(Level.INFO, "Closing PacketInterceptor", new Exception("Stacktrace"));
|
protected void initChannel(Channel channel) throws Exception {
|
||||||
|
try {
|
||||||
|
// This can take a while, so we need to stop the main thread from interfering
|
||||||
|
synchronized (networkManagers) {
|
||||||
|
// Stop injecting channels
|
||||||
|
if (!closed) {
|
||||||
|
channel.eventLoop().submit(() -> injectChannelInternal(channel));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
plugin.getLogger().log(Level.SEVERE, "Cannot inject incomming channel " + channel, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(closed)
|
};
|
||||||
return;
|
|
||||||
closed = true;
|
|
||||||
|
|
||||||
HandlerList.unregisterAll(this);
|
// This is executed before Minecraft's channel handler
|
||||||
|
beginInitProtocol = new ChannelInitializer<Channel>() {
|
||||||
|
|
||||||
for (Player player : plugin.getServer().getOnlinePlayers()) {
|
@Override
|
||||||
getInterceptor(player).ifPresent(PacketInterceptor::close);
|
protected void initChannel(Channel channel) throws Exception {
|
||||||
}
|
channel.pipeline().addLast(endInitProtocol);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Optional<PacketInterceptor> getInterceptor(Player player) {
|
};
|
||||||
synchronized (playerInterceptors) {
|
|
||||||
return Optional.ofNullable(playerInterceptors.get(player));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final Field<Channel> getChannel = Reflection.getField(networkManager, Channel.class, 0);
|
serverChannelHandler = new ChannelInboundHandlerAdapter() {
|
||||||
private static final Field<UUID> getUUID = Reflection.getField(networkManager, UUID.class, 0);
|
|
||||||
|
@Override
|
||||||
|
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||||
|
Channel channel = (Channel) msg;
|
||||||
|
|
||||||
public final class PacketInterceptor extends ChannelDuplexHandler {
|
// Prepare to initialize ths channel
|
||||||
private final Player player;
|
channel.pipeline().addFirst(beginInitProtocol);
|
||||||
@Getter
|
ctx.fireChannelRead(msg);
|
||||||
private final Channel channel;
|
}
|
||||||
|
|
||||||
private PacketInterceptor(Player player) {
|
};
|
||||||
this.player = player;
|
}
|
||||||
|
|
||||||
channel = connections.stream().filter(connection -> player.getUniqueId().equals(getUUID.get(connection))).map(getChannel::get).filter(Channel::isActive).findAny().orElseThrow(() -> {
|
/**
|
||||||
Bukkit.getScheduler().runTask(plugin, () -> player.kickPlayer("Connection failure."));
|
* Register bukkit events.
|
||||||
return new SecurityException("Could not find channel for player " + player.getName());
|
*/
|
||||||
});
|
private void registerBukkitEvents() {
|
||||||
|
listener = new Listener() {
|
||||||
|
|
||||||
if(!channel.isActive())
|
@EventHandler(priority = EventPriority.LOWEST)
|
||||||
return;
|
public final void onPlayerLogin(PlayerLoginEvent e) {
|
||||||
|
if (closed)
|
||||||
|
return;
|
||||||
|
|
||||||
synchronized (playerInterceptors) {
|
Channel channel = getChannel(e.getPlayer());
|
||||||
playerInterceptors.put(player, this);
|
|
||||||
}
|
|
||||||
plugin.getLogger().info("Adding Techhider for: " + player.getName());
|
|
||||||
|
|
||||||
try {
|
// Don't inject players that have been explicitly uninjected
|
||||||
channel.pipeline().addBefore("packet_handler", HANDLER_NAME, this);
|
if (!uninjectedChannels.contains(channel)) {
|
||||||
} catch (IllegalArgumentException | NoSuchElementException e) {
|
injectPlayer(e.getPlayer());
|
||||||
Bukkit.getScheduler().runTask(plugin, () -> player.kickPlayer("Connection failure."));
|
}
|
||||||
throw new SecurityException(e);
|
}
|
||||||
}
|
|
||||||
|
@EventHandler
|
||||||
|
public final void onPluginDisable(PluginDisableEvent e) {
|
||||||
|
if (e.getPlugin().equals(plugin)) {
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
plugin.getServer().getPluginManager().registerEvents(listener, plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
private void registerChannelHandler() {
|
||||||
|
Object mcServer = getMinecraftServer.get(Bukkit.getServer());
|
||||||
|
Object serverConnection = getServerConnection.get(mcServer);
|
||||||
|
boolean looking = true;
|
||||||
|
|
||||||
|
try {
|
||||||
|
Field field = Reflection.getParameterizedField(serverConnectionClass, List.class, networkManagerClass);
|
||||||
|
field.setAccessible(true);
|
||||||
|
|
||||||
|
networkManagers = (List<Object>) field.get(serverConnection);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
plugin.getLogger().info("Encountered an exception checking list fields" + ex);
|
||||||
|
MethodInvoker method = Reflection.getTypedMethod(serverConnectionClass, null, List.class, serverConnectionClass);
|
||||||
|
|
||||||
|
networkManagers = (List<Object>) method.invoke(null, serverConnection);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendPacket(Object packet) {
|
if (networkManagers == null) {
|
||||||
channel.pipeline().writeAndFlush(packet);
|
throw new IllegalArgumentException("Failed to obtain list of network managers");
|
||||||
}
|
}
|
||||||
|
// We need to synchronize against this list
|
||||||
|
createServerChannelHandler();
|
||||||
|
|
||||||
private void receivePacket(Object packet) {
|
// Find the correct list, or implicitly throw an exception
|
||||||
channel.pipeline().context("encoder").fireChannelRead(packet);
|
for (int i = 0; looking; i++) {
|
||||||
}
|
List<Object> list = Reflection.getField(serverConnection.getClass(), List.class, i).get(serverConnection);
|
||||||
|
|
||||||
private void close() {
|
for (Object item : list) {
|
||||||
if(channel.isActive()) {
|
if (!ChannelFuture.class.isInstance(item))
|
||||||
channel.eventLoop().execute(() -> {
|
break;
|
||||||
try {
|
|
||||||
channel.pipeline().remove(HANDLER_NAME);
|
|
||||||
} catch (NoSuchElementException e) {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
synchronized (playerInterceptors) {
|
// Channel future that contains the server connection
|
||||||
playerInterceptors.remove(player, this);
|
Channel serverChannel = ((ChannelFuture) item).channel();
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
serverChannels.add(serverChannel);
|
||||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
serverChannel.pipeline().addFirst(serverChannelHandler);
|
||||||
try {
|
looking = false;
|
||||||
msg = filterPacket(player, msg);
|
}
|
||||||
} catch (Exception e) {
|
}
|
||||||
plugin.getLogger().log(Level.SEVERE, "Error during incoming packet processing", e);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (msg != null) {
|
private void unregisterChannelHandler() {
|
||||||
super.channelRead(ctx, msg);
|
if (serverChannelHandler == null)
|
||||||
}
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
for (Channel serverChannel : serverChannels) {
|
||||||
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
|
final ChannelPipeline pipeline = serverChannel.pipeline();
|
||||||
try {
|
|
||||||
msg = filterPacket(player, msg);
|
|
||||||
} catch (Exception e) {
|
|
||||||
plugin.getLogger().log(Level.SEVERE, "Error during outgoing packet processing", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (msg != null) {
|
// Remove channel handler
|
||||||
super.write(ctx, msg, promise);
|
serverChannel.eventLoop().execute(new Runnable() {
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Object filterPacket(Player player, Object packet) {
|
@Override
|
||||||
List<BiFunction<Player, Object, Object>> filters = packetFilters.getOrDefault(packet.getClass(), Collections.emptyList());
|
public void run() {
|
||||||
|
try {
|
||||||
|
pipeline.remove(serverChannelHandler);
|
||||||
|
} catch (NoSuchElementException e) {
|
||||||
|
// That's fine
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for(BiFunction<Player, Object, Object> filter : filters) {
|
});
|
||||||
packet = filter.apply(player, packet);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(packet == null)
|
private void registerPlayers(Plugin plugin) {
|
||||||
break;
|
for (Player player : plugin.getServer().getOnlinePlayers()) {
|
||||||
}
|
injectPlayer(player);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return packet;
|
/**
|
||||||
}
|
* Invoked when the server is starting to send a packet to a player.
|
||||||
}
|
* <p>
|
||||||
}
|
* Note that this is not executed on the main thread.
|
||||||
|
*
|
||||||
|
* @param receiver - the receiving player, NULL for early login/status packets.
|
||||||
|
* @param channel - the channel that received the packet. Never NULL.
|
||||||
|
* @param packet - the packet being sent.
|
||||||
|
* @return The packet to send instead, or NULL to cancel the transmission.
|
||||||
|
*/
|
||||||
|
public Object onPacketOutAsync(Player receiver, Channel channel, Object packet) {
|
||||||
|
return packet;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoked when the server has received a packet from a given player.
|
||||||
|
* <p>
|
||||||
|
* Use {@link Channel#remoteAddress()} to get the remote address of the client.
|
||||||
|
*
|
||||||
|
* @param sender - the player that sent the packet, NULL for early login/status packets.
|
||||||
|
* @param channel - channel that received the packet. Never NULL.
|
||||||
|
* @param packet - the packet being received.
|
||||||
|
* @return The packet to recieve instead, or NULL to cancel.
|
||||||
|
*/
|
||||||
|
public Object onPacketInAsync(Player sender, Channel channel, Object packet) {
|
||||||
|
return packet;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a packet to a particular player.
|
||||||
|
* <p>
|
||||||
|
* Note that {@link #onPacketOutAsync(Player, Channel, Object)} will be invoked with this packet.
|
||||||
|
*
|
||||||
|
* @param player - the destination player.
|
||||||
|
* @param packet - the packet to send.
|
||||||
|
*/
|
||||||
|
public void sendPacket(Player player, Object packet) {
|
||||||
|
sendPacket(getChannel(player), packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a packet to a particular client.
|
||||||
|
* <p>
|
||||||
|
* Note that {@link #onPacketOutAsync(Player, Channel, Object)} will be invoked with this packet.
|
||||||
|
*
|
||||||
|
* @param channel - client identified by a channel.
|
||||||
|
* @param packet - the packet to send.
|
||||||
|
*/
|
||||||
|
public void sendPacket(Channel channel, Object packet) {
|
||||||
|
channel.pipeline().writeAndFlush(packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pretend that a given packet has been received from a player.
|
||||||
|
* <p>
|
||||||
|
* Note that {@link #onPacketInAsync(Player, Channel, Object)} will be invoked with this packet.
|
||||||
|
*
|
||||||
|
* @param player - the player that sent the packet.
|
||||||
|
* @param packet - the packet that will be received by the server.
|
||||||
|
*/
|
||||||
|
public void receivePacket(Player player, Object packet) {
|
||||||
|
receivePacket(getChannel(player), packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pretend that a given packet has been received from a given client.
|
||||||
|
* <p>
|
||||||
|
* Note that {@link #onPacketInAsync(Player, Channel, Object)} will be invoked with this packet.
|
||||||
|
*
|
||||||
|
* @param channel - client identified by a channel.
|
||||||
|
* @param packet - the packet that will be received by the server.
|
||||||
|
*/
|
||||||
|
public void receivePacket(Channel channel, Object packet) {
|
||||||
|
channel.pipeline().context("encoder").fireChannelRead(packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the name of the channel injector, default implementation is "tiny-" + plugin name + "-" + a unique ID.
|
||||||
|
* <p>
|
||||||
|
* Note that this method will only be invoked once. It is no longer necessary to override this to support multiple instances.
|
||||||
|
*
|
||||||
|
* @return A unique channel handler name.
|
||||||
|
*/
|
||||||
|
protected String getHandlerName() {
|
||||||
|
return "tiny-" + plugin.getName() + "-" + ID.incrementAndGet();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a custom channel handler to the given player's channel pipeline, allowing us to intercept sent and received packets.
|
||||||
|
* <p>
|
||||||
|
* This will automatically be called when a player has logged in.
|
||||||
|
*
|
||||||
|
* @param player - the player to inject.
|
||||||
|
*/
|
||||||
|
public void injectPlayer(Player player) {
|
||||||
|
injectChannelInternal(getChannel(player)).player = player;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a custom channel handler to the given channel.
|
||||||
|
*
|
||||||
|
* @param channel - the channel to inject.
|
||||||
|
* @return The intercepted channel, or NULL if it has already been injected.
|
||||||
|
*/
|
||||||
|
public void injectChannel(Channel channel) {
|
||||||
|
injectChannelInternal(channel);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a custom channel handler to the given channel.
|
||||||
|
*
|
||||||
|
* @param channel - the channel to inject.
|
||||||
|
* @return The packet interceptor.
|
||||||
|
*/
|
||||||
|
private PacketInterceptor injectChannelInternal(Channel channel) {
|
||||||
|
try {
|
||||||
|
PacketInterceptor interceptor = (PacketInterceptor) channel.pipeline().get(handlerName);
|
||||||
|
|
||||||
|
// Inject our packet interceptor
|
||||||
|
if (interceptor == null) {
|
||||||
|
interceptor = new PacketInterceptor();
|
||||||
|
channel.pipeline().addBefore("packet_handler", handlerName, interceptor);
|
||||||
|
uninjectedChannels.remove(channel);
|
||||||
|
}
|
||||||
|
|
||||||
|
return interceptor;
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
// Try again
|
||||||
|
return (PacketInterceptor) channel.pipeline().get(handlerName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the Netty channel associated with a player. This is cached.
|
||||||
|
*
|
||||||
|
* @param player - the player.
|
||||||
|
* @return The Netty channel.
|
||||||
|
*/
|
||||||
|
public Channel getChannel(Player player) {
|
||||||
|
Channel channel = channelLookup.get(player.getName());
|
||||||
|
|
||||||
|
// Lookup channel again
|
||||||
|
if (channel == null) {
|
||||||
|
Object connection = getConnection.get(getPlayerHandle.invoke(player));
|
||||||
|
Object manager = getManager.get(connection);
|
||||||
|
|
||||||
|
channelLookup.put(player.getName(), channel = getChannel.get(manager));
|
||||||
|
}
|
||||||
|
|
||||||
|
return channel;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uninject a specific player.
|
||||||
|
*
|
||||||
|
* @param player - the injected player.
|
||||||
|
*/
|
||||||
|
public void uninjectPlayer(Player player) {
|
||||||
|
uninjectChannel(getChannel(player));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uninject a specific channel.
|
||||||
|
* <p>
|
||||||
|
* This will also disable the automatic channel injection that occurs when a player has properly logged in.
|
||||||
|
*
|
||||||
|
* @param channel - the injected channel.
|
||||||
|
*/
|
||||||
|
public void uninjectChannel(final Channel channel) {
|
||||||
|
// No need to guard against this if we're closing
|
||||||
|
if (!closed) {
|
||||||
|
uninjectedChannels.add(channel);
|
||||||
|
}
|
||||||
|
|
||||||
|
// See ChannelInjector in ProtocolLib, line 590
|
||||||
|
channel.eventLoop().execute(new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
channel.pipeline().remove(handlerName);
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if the given player has been injected by TinyProtocol.
|
||||||
|
*
|
||||||
|
* @param player - the player.
|
||||||
|
* @return TRUE if it is, FALSE otherwise.
|
||||||
|
*/
|
||||||
|
public boolean hasInjected(Player player) {
|
||||||
|
return hasInjected(getChannel(player));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if the given channel has been injected by TinyProtocol.
|
||||||
|
*
|
||||||
|
* @param channel - the channel.
|
||||||
|
* @return TRUE if it is, FALSE otherwise.
|
||||||
|
*/
|
||||||
|
public boolean hasInjected(Channel channel) {
|
||||||
|
return channel.pipeline().get(handlerName) != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cease listening for packets. This is called automatically when your plugin is disabled.
|
||||||
|
*/
|
||||||
|
public final void close() {
|
||||||
|
if (!closed) {
|
||||||
|
closed = true;
|
||||||
|
|
||||||
|
// Remove our handlers
|
||||||
|
for (Player player : plugin.getServer().getOnlinePlayers()) {
|
||||||
|
uninjectPlayer(player);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean up Bukkit
|
||||||
|
HandlerList.unregisterAll(listener);
|
||||||
|
unregisterChannelHandler();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Channel handler that is inserted into the player's channel pipeline, allowing us to intercept sent and received packets.
|
||||||
|
*
|
||||||
|
* @author Kristian
|
||||||
|
*/
|
||||||
|
private final class PacketInterceptor extends ChannelDuplexHandler {
|
||||||
|
// Updated by the login event
|
||||||
|
public volatile Player player;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||||
|
// Intercept channel
|
||||||
|
final Channel channel = ctx.channel();
|
||||||
|
handleLoginStart(channel, msg);
|
||||||
|
|
||||||
|
try {
|
||||||
|
msg = onPacketInAsync(player, channel, msg);
|
||||||
|
} catch (Exception e) {
|
||||||
|
plugin.getLogger().log(Level.SEVERE, "Error in onPacketInAsync().", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg != null) {
|
||||||
|
super.channelRead(ctx, msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
|
||||||
|
try {
|
||||||
|
msg = onPacketOutAsync(player, ctx.channel(), msg);
|
||||||
|
} catch (Exception e) {
|
||||||
|
plugin.getLogger().log(Level.SEVERE, "Error in onPacketOutAsync().", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg != null) {
|
||||||
|
super.write(ctx, msg, promise);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleLoginStart(Channel channel, Object packet) {
|
||||||
|
if (PACKET_LOGIN_IN_START.isInstance(packet)) {
|
||||||
|
GameProfile profile = getGameProfile.get(packet);
|
||||||
|
channelLookup.put(profile.getName(), channel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user