mirror of
https://github.com/dmulloy2/ProtocolLib.git
synced 2024-11-24 11:36:51 +01:00
[TinyProtocol] Format TinyProtocol
This commit is contained in:
parent
66be4900a3
commit
115b5e8daa
@ -12,10 +12,12 @@ import org.bukkit.plugin.java.JavaPlugin;
|
|||||||
import com.comphenix.tinyprotocol.Reflection.ConstructorInvoker;
|
import com.comphenix.tinyprotocol.Reflection.ConstructorInvoker;
|
||||||
import com.comphenix.tinyprotocol.Reflection.FieldAccessor;
|
import com.comphenix.tinyprotocol.Reflection.FieldAccessor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents an example plugin utilizing TinyProtocol
|
||||||
|
*/
|
||||||
public class ExamplePlugin extends JavaPlugin {
|
public class ExamplePlugin extends JavaPlugin {
|
||||||
// Chat packets
|
// Chat packets
|
||||||
private FieldAccessor<String> CHAT_MESSAGE = Reflection.getField(
|
private FieldAccessor<String> CHAT_MESSAGE = Reflection.getField("{nms}.PacketPlayInChat", String.class, 0);
|
||||||
"{nms}.PacketPlayInChat", String.class, 0);
|
|
||||||
|
|
||||||
// Explosion packet
|
// Explosion packet
|
||||||
private Class<?> particleClass = Reflection.getClass("{nms}.PacketPlayOutWorldParticles");
|
private Class<?> particleClass = Reflection.getClass("{nms}.PacketPlayOutWorldParticles");
|
||||||
@ -38,6 +40,7 @@ public class ExamplePlugin extends JavaPlugin {
|
|||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
protocol = new TinyProtocol(this) {
|
protocol = new TinyProtocol(this) {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object onPacketInAsync(Player sender, Channel channel, Object packet) {
|
public Object onPacketInAsync(Player sender, Channel channel, Object packet) {
|
||||||
// Cancel chat packets
|
// Cancel chat packets
|
||||||
@ -47,9 +50,11 @@ public class ExamplePlugin extends JavaPlugin {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (particleName.hasField(packet)) {
|
if (particleName.hasField(packet)) {
|
||||||
System.out.println("Sending particle field:" + packet);
|
System.out.println("Sending particle field:" + packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
return super.onPacketInAsync(sender, channel, packet);
|
return super.onPacketInAsync(sender, channel, packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,11 +65,13 @@ public class ExamplePlugin extends JavaPlugin {
|
|||||||
playerSample.set(ping, playerSampleInvoker.invoke(1000, 0));
|
playerSample.set(ping, playerSampleInvoker.invoke(1000, 0));
|
||||||
|
|
||||||
// Which is equivalent to:
|
// Which is equivalent to:
|
||||||
//serverPing.get(packet).setPlayerSample(new ServerPingPlayerSample(1000, 0));
|
// serverPing.get(packet).setPlayerSample(new ServerPingPlayerSample(1000, 0));
|
||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
|
|
||||||
return super.onPacketOutAsync(reciever, channel, packet);
|
return super.onPacketOutAsync(reciever, channel, packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,10 +88,12 @@ public class ExamplePlugin extends JavaPlugin {
|
|||||||
protocol.injectPlayer(player);
|
protocol.injectPlayer(player);
|
||||||
sender.sendMessage(ChatColor.DARK_GREEN + "Player " + player + " has been injected.");
|
sender.sendMessage(ChatColor.DARK_GREEN + "Player " + player + " has been injected.");
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
sender.sendMessage(ChatColor.RED + "Can only be invoked by a player.");
|
sender.sendMessage(ChatColor.RED + "Can only be invoked by a player.");
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,7 +110,6 @@ public class ExamplePlugin extends JavaPlugin {
|
|||||||
|
|
||||||
// Send the packet to the player
|
// Send the packet to the player
|
||||||
protocol.sendPacket(player, explosionPacket);
|
protocol.sendPacket(player, explosionPacket);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException("Cannot send packet.", e);
|
throw new RuntimeException("Cannot send packet.", e);
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ import org.bukkit.Bukkit;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* An utility class that simplifies reflection in Bukkit plugins.
|
* An utility class that simplifies reflection in Bukkit plugins.
|
||||||
|
*
|
||||||
* @author Kristian
|
* @author Kristian
|
||||||
*/
|
*/
|
||||||
public final class Reflection {
|
public final class Reflection {
|
||||||
@ -20,6 +21,7 @@ public final class Reflection {
|
|||||||
public interface ConstructorInvoker {
|
public interface ConstructorInvoker {
|
||||||
/**
|
/**
|
||||||
* Invoke a constructor for a specific class.
|
* Invoke a constructor for a specific class.
|
||||||
|
*
|
||||||
* @param arguments - the arguments to pass to the constructor.
|
* @param arguments - the arguments to pass to the constructor.
|
||||||
* @return The constructed object.
|
* @return The constructed object.
|
||||||
*/
|
*/
|
||||||
@ -32,6 +34,7 @@ public final class Reflection {
|
|||||||
public interface MethodInvoker {
|
public interface MethodInvoker {
|
||||||
/**
|
/**
|
||||||
* Invoke a method on a specific target object.
|
* Invoke a method on a specific target object.
|
||||||
|
*
|
||||||
* @param target - the target object, or NULL for a static method.
|
* @param target - the target object, or NULL for a static method.
|
||||||
* @param arguments - the arguments to pass to the method.
|
* @param arguments - the arguments to pass to the method.
|
||||||
* @return The return value, or NULL if is void.
|
* @return The return value, or NULL if is void.
|
||||||
@ -41,11 +44,13 @@ public final class Reflection {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* An interface for retrieving the field content.
|
* An interface for retrieving the field content.
|
||||||
|
*
|
||||||
* @param <T> - field type.
|
* @param <T> - field type.
|
||||||
*/
|
*/
|
||||||
public interface FieldAccessor<T> {
|
public interface FieldAccessor<T> {
|
||||||
/**
|
/**
|
||||||
* Retrieve the content of a field.
|
* Retrieve the content of a field.
|
||||||
|
*
|
||||||
* @param target - the target object, or NULL for a static field.
|
* @param target - the target object, or NULL for a static field.
|
||||||
* @return The value of the field.
|
* @return The value of the field.
|
||||||
*/
|
*/
|
||||||
@ -53,6 +58,7 @@ public final class Reflection {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the content of a field.
|
* Set the content of a field.
|
||||||
|
*
|
||||||
* @param target - the target object, or NULL for a static field.
|
* @param target - the target object, or NULL for a static field.
|
||||||
* @param value - the new value of the field.
|
* @param value - the new value of the field.
|
||||||
*/
|
*/
|
||||||
@ -60,6 +66,7 @@ public final class Reflection {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine if the given object has this field.
|
* Determine if the given object has this field.
|
||||||
|
*
|
||||||
* @param target - the object to test.
|
* @param target - the object to test.
|
||||||
* @return TRUE if it does, FALSE otherwise.
|
* @return TRUE if it does, FALSE otherwise.
|
||||||
*/
|
*/
|
||||||
@ -80,6 +87,7 @@ public final class Reflection {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve a field accessor for a specific field type and name.
|
* Retrieve a field accessor for a specific field type and name.
|
||||||
|
*
|
||||||
* @param target - the target type.
|
* @param target - the target type.
|
||||||
* @param name - the name of the field, or NULL to ignore.
|
* @param name - the name of the field, or NULL to ignore.
|
||||||
* @param fieldType - a compatible field type.
|
* @param fieldType - a compatible field type.
|
||||||
@ -91,6 +99,7 @@ public final class Reflection {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve a field accessor for a specific field type and name.
|
* Retrieve a field accessor for a specific field type and name.
|
||||||
|
*
|
||||||
* @param className - lookup name of the class, see {@link #getClass(String)}.
|
* @param className - lookup name of the class, see {@link #getClass(String)}.
|
||||||
* @param name - the name of the field, or NULL to ignore.
|
* @param name - the name of the field, or NULL to ignore.
|
||||||
* @param fieldType - a compatible field type.
|
* @param fieldType - a compatible field type.
|
||||||
@ -102,6 +111,7 @@ public final class Reflection {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve a field accessor for a specific field type and name.
|
* Retrieve a field accessor for a specific field type and name.
|
||||||
|
*
|
||||||
* @param target - the target type.
|
* @param target - the target type.
|
||||||
* @param fieldType - a compatible field type.
|
* @param fieldType - a compatible field type.
|
||||||
* @param index - the number of compatible fields to skip.
|
* @param index - the number of compatible fields to skip.
|
||||||
@ -113,6 +123,7 @@ public final class Reflection {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve a field accessor for a specific field type and name.
|
* Retrieve a field accessor for a specific field type and name.
|
||||||
|
*
|
||||||
* @param className - lookup name of the class, see {@link #getClass(String)}.
|
* @param className - lookup name of the class, see {@link #getClass(String)}.
|
||||||
* @param fieldType - a compatible field type.
|
* @param fieldType - a compatible field type.
|
||||||
* @param index - the number of compatible fields to skip.
|
* @param index - the number of compatible fields to skip.
|
||||||
@ -125,14 +136,14 @@ public final class Reflection {
|
|||||||
// Common method
|
// Common method
|
||||||
private static <T> FieldAccessor<T> getField(Class<?> target, String name, Class<T> fieldType, int index) {
|
private static <T> FieldAccessor<T> getField(Class<?> target, String name, Class<T> fieldType, int index) {
|
||||||
for (final Field field : target.getDeclaredFields()) {
|
for (final Field field : target.getDeclaredFields()) {
|
||||||
if ((name == null || field.getName().equals(name)) &&
|
if ((name == null || field.getName().equals(name)) && fieldType.isAssignableFrom(field.getType()) && index-- <= 0) {
|
||||||
fieldType.isAssignableFrom(field.getType()) && index-- <= 0) {
|
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
|
|
||||||
// A function for retrieving a specific field value
|
// A function for retrieving a specific field value
|
||||||
return new FieldAccessor<T>() {
|
return new FieldAccessor<T>() {
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
@Override
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public T get(Object target) {
|
public T get(Object target) {
|
||||||
try {
|
try {
|
||||||
return (T) field.get(target);
|
return (T) field.get(target);
|
||||||
@ -162,11 +173,13 @@ public final class Reflection {
|
|||||||
// Search in parent classes
|
// Search in parent classes
|
||||||
if (target.getSuperclass() != null)
|
if (target.getSuperclass() != null)
|
||||||
return getField(target.getSuperclass(), name, fieldType, index);
|
return getField(target.getSuperclass(), name, fieldType, index);
|
||||||
|
|
||||||
throw new IllegalArgumentException("Cannot find field with type " + fieldType);
|
throw new IllegalArgumentException("Cannot find field with type " + fieldType);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Search for the first publically and privately defined method of the given name and parameter count.
|
* Search for the first publically and privately defined method of the given name and parameter count.
|
||||||
|
*
|
||||||
* @param className - lookup name of the class, see {@link #getClass(String)}.
|
* @param className - lookup name of the class, see {@link #getClass(String)}.
|
||||||
* @param methodName - the method name, or NULL to skip.
|
* @param methodName - the method name, or NULL to skip.
|
||||||
* @param params - the expected parameters.
|
* @param params - the expected parameters.
|
||||||
@ -179,6 +192,7 @@ public final class Reflection {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Search for the first publically and privately defined method of the given name and parameter count.
|
* Search for the first publically and privately defined method of the given name and parameter count.
|
||||||
|
*
|
||||||
* @param clazz - a class to start with.
|
* @param clazz - a class to start with.
|
||||||
* @param methodName - the method name, or NULL to skip.
|
* @param methodName - the method name, or NULL to skip.
|
||||||
* @param params - the expected parameters.
|
* @param params - the expected parameters.
|
||||||
@ -191,6 +205,7 @@ public final class Reflection {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Search for the first publically and privately defined method of the given name and parameter count.
|
* Search for the first publically and privately defined method of the given name and parameter count.
|
||||||
|
*
|
||||||
* @param clazz - a class to start with.
|
* @param clazz - a class to start with.
|
||||||
* @param methodName - the method name, or NULL to skip.
|
* @param methodName - the method name, or NULL to skip.
|
||||||
* @param returnType - the expected return type, or NULL to ignore.
|
* @param returnType - the expected return type, or NULL to ignore.
|
||||||
@ -200,12 +215,11 @@ public final class Reflection {
|
|||||||
*/
|
*/
|
||||||
public static MethodInvoker getTypedMethod(Class<?> clazz, String methodName, Class<?> returnType, Class<?>... params) {
|
public static MethodInvoker getTypedMethod(Class<?> clazz, String methodName, Class<?> returnType, Class<?>... params) {
|
||||||
for (final Method method : clazz.getDeclaredMethods()) {
|
for (final Method method : clazz.getDeclaredMethods()) {
|
||||||
if ((methodName == null || method.getName().equals(methodName)) &&
|
if ((methodName == null || method.getName().equals(methodName)) && (returnType == null) || method.getReturnType().equals(returnType) && Arrays.equals(method.getParameterTypes(), params)) {
|
||||||
(returnType == null) || method.getReturnType().equals(returnType) &&
|
|
||||||
Arrays.equals(method.getParameterTypes(), params)) {
|
|
||||||
|
|
||||||
method.setAccessible(true);
|
method.setAccessible(true);
|
||||||
|
|
||||||
return new MethodInvoker() {
|
return new MethodInvoker() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object invoke(Object target, Object... arguments) {
|
public Object invoke(Object target, Object... arguments) {
|
||||||
try {
|
try {
|
||||||
@ -214,18 +228,21 @@ public final class Reflection {
|
|||||||
throw new RuntimeException("Cannot invoke method " + method, e);
|
throw new RuntimeException("Cannot invoke method " + method, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search in every superclass
|
// Search in every superclass
|
||||||
if (clazz.getSuperclass() != null)
|
if (clazz.getSuperclass() != null)
|
||||||
return getMethod(clazz.getSuperclass(), methodName, params);
|
return getMethod(clazz.getSuperclass(), methodName, params);
|
||||||
throw new IllegalStateException(String.format(
|
|
||||||
"Unable to find method %s (%s).", methodName, Arrays.asList(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.
|
* 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 className - lookup name of the class, see {@link #getClass(String)}.
|
||||||
* @param params - the expected parameters.
|
* @param params - the expected parameters.
|
||||||
* @return An object that invokes this constructor.
|
* @return An object that invokes this constructor.
|
||||||
@ -237,6 +254,7 @@ public final class Reflection {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Search for the first publically and privately defined constructor of the given name and parameter count.
|
* Search for the first publically and privately defined constructor of the given name and parameter count.
|
||||||
|
*
|
||||||
* @param clazz - a class to start with.
|
* @param clazz - a class to start with.
|
||||||
* @param params - the expected parameters.
|
* @param params - the expected parameters.
|
||||||
* @return An object that invokes this constructor.
|
* @return An object that invokes this constructor.
|
||||||
@ -245,9 +263,10 @@ public final class Reflection {
|
|||||||
public static ConstructorInvoker getConstructor(Class<?> clazz, Class<?>... params) {
|
public static ConstructorInvoker getConstructor(Class<?> clazz, Class<?>... params) {
|
||||||
for (final Constructor<?> constructor : clazz.getDeclaredConstructors()) {
|
for (final Constructor<?> constructor : clazz.getDeclaredConstructors()) {
|
||||||
if (Arrays.equals(constructor.getParameterTypes(), params)) {
|
if (Arrays.equals(constructor.getParameterTypes(), params)) {
|
||||||
|
|
||||||
constructor.setAccessible(true);
|
constructor.setAccessible(true);
|
||||||
|
|
||||||
return new ConstructorInvoker() {
|
return new ConstructorInvoker() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object invoke(Object... arguments) {
|
public Object invoke(Object... arguments) {
|
||||||
try {
|
try {
|
||||||
@ -256,11 +275,12 @@ public final class Reflection {
|
|||||||
throw new RuntimeException("Cannot invoke constructor " + constructor, 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)));
|
throw new IllegalStateException(String.format("Unable to find constructor for %s (%s).", clazz, Arrays.asList(params)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -268,21 +288,21 @@ public final class Reflection {
|
|||||||
* <p>
|
* <p>
|
||||||
* This is useful when looking up fields by a NMS or OBC type.
|
* This is useful when looking up fields by a NMS or OBC type.
|
||||||
* <p>
|
* <p>
|
||||||
|
*
|
||||||
* @see {@link #getClass()} for more information.
|
* @see {@link #getClass()} for more information.
|
||||||
* @param lookupName - the class name with variables.
|
* @param lookupName - the class name with variables.
|
||||||
* @return The class.
|
* @return The class.
|
||||||
*/
|
*/
|
||||||
public static Class<Object> getUntypedClass(String lookupName) {
|
public static Class<Object> getUntypedClass(String lookupName) {
|
||||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||||
Class<Object> clazz = (Class<Object>)(Class)getClass(lookupName);
|
Class<Object> clazz = (Class) getClass(lookupName);
|
||||||
return clazz;
|
return clazz;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve a class from its full name.
|
* Retrieve a class from its full name.
|
||||||
* <p>
|
* <p>
|
||||||
* Strings enclosed with curly brackets - such as {TEXT} - will be replaced according
|
* Strings enclosed with curly brackets - such as {TEXT} - will be replaced according to the following table:
|
||||||
* to the following table:
|
|
||||||
* <p>
|
* <p>
|
||||||
* <table border="1">
|
* <table border="1">
|
||||||
* <tr>
|
* <tr>
|
||||||
@ -302,6 +322,7 @@ public final class Reflection {
|
|||||||
* <td>The current Minecraft package VERSION, if any.</td>
|
* <td>The current Minecraft package VERSION, if any.</td>
|
||||||
* </tr>
|
* </tr>
|
||||||
* </table>
|
* </table>
|
||||||
|
*
|
||||||
* @param lookupName - the class name with variables.
|
* @param lookupName - the class name with variables.
|
||||||
* @return The looked up class.
|
* @return The looked up class.
|
||||||
* @throws IllegalArgumentException If a variable or class could not be found.
|
* @throws IllegalArgumentException If a variable or class could not be found.
|
||||||
@ -312,6 +333,7 @@ public final class Reflection {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve a class in the net.minecraft.server.VERSION.* package.
|
* Retrieve a class in the net.minecraft.server.VERSION.* package.
|
||||||
|
*
|
||||||
* @param name - the name of the class, excluding the package.
|
* @param name - the name of the class, excluding the package.
|
||||||
* @throws IllegalArgumentException If the class doesn't exist.
|
* @throws IllegalArgumentException If the class doesn't exist.
|
||||||
*/
|
*/
|
||||||
@ -321,6 +343,7 @@ public final class Reflection {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve a class in the org.bukkit.craftbukkit.VERSION.* package.
|
* Retrieve a class in the org.bukkit.craftbukkit.VERSION.* package.
|
||||||
|
*
|
||||||
* @param name - the name of the class, excluding the package.
|
* @param name - the name of the class, excluding the package.
|
||||||
* @throws IllegalArgumentException If the class doesn't exist.
|
* @throws IllegalArgumentException If the class doesn't exist.
|
||||||
*/
|
*/
|
||||||
@ -330,6 +353,7 @@ public final class Reflection {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve a class by its canonical name.
|
* Retrieve a class by its canonical name.
|
||||||
|
*
|
||||||
* @param canonicalName - the canonical name.
|
* @param canonicalName - the canonical name.
|
||||||
* @return The class.
|
* @return The class.
|
||||||
*/
|
*/
|
||||||
@ -343,6 +367,7 @@ public final class Reflection {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Expand variables such as "{nms}" and "{obc}" to their corresponding packages.
|
* Expand variables such as "{nms}" and "{obc}" to their corresponding packages.
|
||||||
|
*
|
||||||
* @param name - the full name of the class.
|
* @param name - the full name of the class.
|
||||||
* @return The expanded string.
|
* @return The expanded string.
|
||||||
*/
|
*/
|
||||||
@ -369,6 +394,7 @@ public final class Reflection {
|
|||||||
replacement += ".";
|
replacement += ".";
|
||||||
matcher.appendReplacement(output, Matcher.quoteReplacement(replacement));
|
matcher.appendReplacement(output, Matcher.quoteReplacement(replacement));
|
||||||
}
|
}
|
||||||
|
|
||||||
matcher.appendTail(output);
|
matcher.appendTail(output);
|
||||||
return output.toString();
|
return output.toString();
|
||||||
}
|
}
|
||||||
|
@ -34,9 +34,10 @@ import com.google.common.collect.MapMaker;
|
|||||||
import com.mojang.authlib.GameProfile;
|
import com.mojang.authlib.GameProfile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a very tiny alternative to ProtocolLib in 1.7.2.
|
* Represents a very tiny alternative to ProtocolLib.
|
||||||
* <p>
|
* <p>
|
||||||
* It now supports intercepting packets during login and status ping (such as OUT_SERVER_PING)!
|
* It now supports intercepting packets during login and status ping (such as OUT_SERVER_PING)!
|
||||||
|
*
|
||||||
* @author Kristian
|
* @author Kristian
|
||||||
*/
|
*/
|
||||||
public abstract class TinyProtocol {
|
public abstract class TinyProtocol {
|
||||||
@ -85,6 +86,7 @@ public abstract class TinyProtocol {
|
|||||||
* Construct a new instance of TinyProtocol, and start intercepting packets for all connected clients and future clients.
|
* Construct a new instance of TinyProtocol, and start intercepting packets for all connected clients and future clients.
|
||||||
* <p>
|
* <p>
|
||||||
* You can construct multiple instances per plugin.
|
* You can construct multiple instances per plugin.
|
||||||
|
*
|
||||||
* @param plugin - the plugin.
|
* @param plugin - the plugin.
|
||||||
*/
|
*/
|
||||||
public TinyProtocol(Plugin plugin) {
|
public TinyProtocol(Plugin plugin) {
|
||||||
@ -102,31 +104,36 @@ public abstract class TinyProtocol {
|
|||||||
private void createServerChannelHandler() {
|
private void createServerChannelHandler() {
|
||||||
// Handle connected channels
|
// Handle connected channels
|
||||||
endInitProtocol = new ChannelInitializer<Channel>() {
|
endInitProtocol = new ChannelInitializer<Channel>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void initChannel(Channel channel) throws Exception {
|
protected void initChannel(Channel channel) throws Exception {
|
||||||
try {
|
try {
|
||||||
// This can take a while, so we need to stop the main thread from interfering
|
// This can take a while, so we need to stop the main thread from interfering
|
||||||
synchronized (networkManagers) {
|
synchronized (networkManagers) {
|
||||||
// Stop injecting channels
|
// Stop injecting channels
|
||||||
if (closed)
|
if (!closed) {
|
||||||
return;
|
|
||||||
injectChannelInternal(channel);
|
injectChannelInternal(channel);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
plugin.getLogger().log(Level.SEVERE, "Cannot inject incomming channel " + channel, e);
|
plugin.getLogger().log(Level.SEVERE, "Cannot inject incomming channel " + channel, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// This is executed before Minecraft's channel handler
|
// This is executed before Minecraft's channel handler
|
||||||
beginInitProtocol = new ChannelInitializer<Channel>() {
|
beginInitProtocol = new ChannelInitializer<Channel>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void initChannel(Channel channel) throws Exception {
|
protected void initChannel(Channel channel) throws Exception {
|
||||||
channel.pipeline().addLast(endInitProtocol);
|
channel.pipeline().addLast(endInitProtocol);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
serverChannelHandler = new ChannelInboundHandlerAdapter() {
|
serverChannelHandler = new ChannelInboundHandlerAdapter() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||||
Channel channel = (Channel) msg;
|
Channel channel = (Channel) msg;
|
||||||
@ -135,6 +142,7 @@ public abstract class TinyProtocol {
|
|||||||
channel.pipeline().addFirst(beginInitProtocol);
|
channel.pipeline().addFirst(beginInitProtocol);
|
||||||
ctx.fireChannelRead(msg);
|
ctx.fireChannelRead(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,10 +151,12 @@ public abstract class TinyProtocol {
|
|||||||
*/
|
*/
|
||||||
private void registerBukkitEvents() {
|
private void registerBukkitEvents() {
|
||||||
listener = new Listener() {
|
listener = new Listener() {
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.LOWEST)
|
@EventHandler(priority = EventPriority.LOWEST)
|
||||||
public final void onPlayerLogin(PlayerLoginEvent e) {
|
public final void onPlayerLogin(PlayerLoginEvent e) {
|
||||||
if (closed)
|
if (closed)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Channel channel = getChannel(e.getPlayer());
|
Channel channel = getChannel(e.getPlayer());
|
||||||
|
|
||||||
// Don't inject players that have been explicitly uninjected
|
// Don't inject players that have been explicitly uninjected
|
||||||
@ -161,9 +171,10 @@ public abstract class TinyProtocol {
|
|||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
this.plugin.getServer().getPluginManager().registerEvents(listener, plugin);
|
plugin.getServer().getPluginManager().registerEvents(listener, plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@ -185,7 +196,7 @@ public abstract class TinyProtocol {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
// Channel future that contains the server connection
|
// Channel future that contains the server connection
|
||||||
Channel serverChannel = ((ChannelFuture)item).channel();
|
Channel serverChannel = ((ChannelFuture) item).channel();
|
||||||
|
|
||||||
serverChannels.add(serverChannel);
|
serverChannels.add(serverChannel);
|
||||||
serverChannel.pipeline().addFirst(serverChannelHandler);
|
serverChannel.pipeline().addFirst(serverChannelHandler);
|
||||||
@ -203,6 +214,7 @@ public abstract class TinyProtocol {
|
|||||||
|
|
||||||
// Remove channel handler
|
// Remove channel handler
|
||||||
serverChannel.eventLoop().execute(new Runnable() {
|
serverChannel.eventLoop().execute(new Runnable() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
@ -211,6 +223,7 @@ public abstract class TinyProtocol {
|
|||||||
// That's fine
|
// That's fine
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -225,6 +238,7 @@ public abstract class TinyProtocol {
|
|||||||
* Invoked when the server is starting to send a packet to a player.
|
* Invoked when the server is starting to send a packet to a player.
|
||||||
* <p>
|
* <p>
|
||||||
* Note that this is not executed on the main thread.
|
* Note that this is not executed on the main thread.
|
||||||
|
*
|
||||||
* @param reciever - the receiving player, NULL for early login/status packets.
|
* @param reciever - the receiving player, NULL for early login/status packets.
|
||||||
* @param remoteAddress - remote address of the sending client. Never NULL.
|
* @param remoteAddress - remote address of the sending client. Never NULL.
|
||||||
* @param packet - the packet being sent.
|
* @param packet - the packet being sent.
|
||||||
@ -238,6 +252,7 @@ public abstract class TinyProtocol {
|
|||||||
* Invoked when the server has received a packet from a given player.
|
* Invoked when the server has received a packet from a given player.
|
||||||
* <p>
|
* <p>
|
||||||
* Use {@link Channel#remoteAddress()} to get the remote address of the client.
|
* 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 sender - the player that sent the packet, NULL for early login/status packets.
|
||||||
* @param channel - channel that received the packet. Never NULL.
|
* @param channel - channel that received the packet. Never NULL.
|
||||||
* @param packet - the packet being received.
|
* @param packet - the packet being received.
|
||||||
@ -251,6 +266,7 @@ public abstract class TinyProtocol {
|
|||||||
* Send a packet to a particular player.
|
* Send a packet to a particular player.
|
||||||
* <p>
|
* <p>
|
||||||
* Note that {@link #onPacketOutAsync(Player, Object)} will be invoked with this packet.
|
* Note that {@link #onPacketOutAsync(Player, Object)} will be invoked with this packet.
|
||||||
|
*
|
||||||
* @param player - the destination player.
|
* @param player - the destination player.
|
||||||
* @param packet - the packet to send.
|
* @param packet - the packet to send.
|
||||||
*/
|
*/
|
||||||
@ -262,6 +278,7 @@ public abstract class TinyProtocol {
|
|||||||
* Send a packet to a particular client.
|
* Send a packet to a particular client.
|
||||||
* <p>
|
* <p>
|
||||||
* Note that {@link #onPacketOutAsync(Player, Object)} will be invoked with this packet.
|
* Note that {@link #onPacketOutAsync(Player, Object)} will be invoked with this packet.
|
||||||
|
*
|
||||||
* @param channel - client identified by a channel.
|
* @param channel - client identified by a channel.
|
||||||
* @param packet - the packet to send.
|
* @param packet - the packet to send.
|
||||||
*/
|
*/
|
||||||
@ -273,6 +290,7 @@ public abstract class TinyProtocol {
|
|||||||
* Pretend that a given packet has been received from a player.
|
* Pretend that a given packet has been received from a player.
|
||||||
* <p>
|
* <p>
|
||||||
* Note that {@link #onPacketInAsync(Player, Object)} will be invoked with this packet.
|
* Note that {@link #onPacketInAsync(Player, Object)} will be invoked with this packet.
|
||||||
|
*
|
||||||
* @param player - the player that sent the packet.
|
* @param player - the player that sent the packet.
|
||||||
* @param packet - the packet that will be received by the server.
|
* @param packet - the packet that will be received by the server.
|
||||||
*/
|
*/
|
||||||
@ -284,6 +302,7 @@ public abstract class TinyProtocol {
|
|||||||
* Pretend that a given packet has been received from a given client.
|
* Pretend that a given packet has been received from a given client.
|
||||||
* <p>
|
* <p>
|
||||||
* Note that {@link #onPacketInAsync(Player, Object)} will be invoked with this packet.
|
* Note that {@link #onPacketInAsync(Player, Object)} will be invoked with this packet.
|
||||||
|
*
|
||||||
* @param channel - client identified by a channel.
|
* @param channel - client identified by a channel.
|
||||||
* @param packet - the packet that will be received by the server.
|
* @param packet - the packet that will be received by the server.
|
||||||
*/
|
*/
|
||||||
@ -295,6 +314,7 @@ public abstract class TinyProtocol {
|
|||||||
* Retrieve the name of the channel injector, default implementation is "tiny-" + plugin name + "-" + a unique ID.
|
* Retrieve the name of the channel injector, default implementation is "tiny-" + plugin name + "-" + a unique ID.
|
||||||
* <p>
|
* <p>
|
||||||
* Note that this method will only be invoked once. It is no longer necessary to override this to support multiple instances.
|
* 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.
|
* @return A unique channel handler name.
|
||||||
*/
|
*/
|
||||||
protected String getHandlerName() {
|
protected String getHandlerName() {
|
||||||
@ -302,10 +322,10 @@ public abstract class TinyProtocol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a custom channel handler to the given player's channel pipeline,
|
* Add a custom channel handler to the given player's channel pipeline, allowing us to intercept sent and received packets.
|
||||||
* allowing us to intercept sent and received packets.
|
|
||||||
* <p>
|
* <p>
|
||||||
* This will automatically be called when a player has logged in.
|
* This will automatically be called when a player has logged in.
|
||||||
|
*
|
||||||
* @param player - the player to inject.
|
* @param player - the player to inject.
|
||||||
*/
|
*/
|
||||||
public void injectPlayer(Player player) {
|
public void injectPlayer(Player player) {
|
||||||
@ -314,6 +334,7 @@ public abstract class TinyProtocol {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a custom channel handler to the given channel.
|
* Add a custom channel handler to the given channel.
|
||||||
|
*
|
||||||
* @param player - the channel to inject.
|
* @param player - the channel to inject.
|
||||||
* @return The intercepted channel, or NULL if it has already been injected.
|
* @return The intercepted channel, or NULL if it has already been injected.
|
||||||
*/
|
*/
|
||||||
@ -323,6 +344,7 @@ public abstract class TinyProtocol {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a custom channel handler to the given channel.
|
* Add a custom channel handler to the given channel.
|
||||||
|
*
|
||||||
* @param player - the channel to inject.
|
* @param player - the channel to inject.
|
||||||
* @return The packet interceptor.
|
* @return The packet interceptor.
|
||||||
*/
|
*/
|
||||||
@ -336,6 +358,7 @@ public abstract class TinyProtocol {
|
|||||||
channel.pipeline().addBefore("packet_handler", handlerName, interceptor);
|
channel.pipeline().addBefore("packet_handler", handlerName, interceptor);
|
||||||
uninjectedChannels.remove(channel);
|
uninjectedChannels.remove(channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
return interceptor;
|
return interceptor;
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
// Try again
|
// Try again
|
||||||
@ -345,6 +368,7 @@ public abstract class TinyProtocol {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve the Netty channel associated with a player. This is cached.
|
* Retrieve the Netty channel associated with a player. This is cached.
|
||||||
|
*
|
||||||
* @param player - the player.
|
* @param player - the player.
|
||||||
* @return The Netty channel.
|
* @return The Netty channel.
|
||||||
*/
|
*/
|
||||||
@ -358,11 +382,13 @@ public abstract class TinyProtocol {
|
|||||||
|
|
||||||
channelLookup.put(player.getName(), channel = getChannel.get(manager));
|
channelLookup.put(player.getName(), channel = getChannel.get(manager));
|
||||||
}
|
}
|
||||||
|
|
||||||
return channel;
|
return channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uninject a specific player.
|
* Uninject a specific player.
|
||||||
|
*
|
||||||
* @param player - the injected player.
|
* @param player - the injected player.
|
||||||
*/
|
*/
|
||||||
public void uninjectPlayer(Player player) {
|
public void uninjectPlayer(Player player) {
|
||||||
@ -373,6 +399,7 @@ public abstract class TinyProtocol {
|
|||||||
* Uninject a specific channel.
|
* Uninject a specific channel.
|
||||||
* <p>
|
* <p>
|
||||||
* This will also disable the automatic channel injection that occurs when a player has properly logged in.
|
* This will also disable the automatic channel injection that occurs when a player has properly logged in.
|
||||||
|
*
|
||||||
* @param channel - the injected channel.
|
* @param channel - the injected channel.
|
||||||
*/
|
*/
|
||||||
public void uninjectChannel(final Channel channel) {
|
public void uninjectChannel(final Channel channel) {
|
||||||
@ -383,15 +410,18 @@ public abstract class TinyProtocol {
|
|||||||
|
|
||||||
// See ChannelInjector in ProtocolLib, line 590
|
// See ChannelInjector in ProtocolLib, line 590
|
||||||
channel.eventLoop().execute(new Runnable() {
|
channel.eventLoop().execute(new Runnable() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
channel.pipeline().remove(handlerName);
|
channel.pipeline().remove(handlerName);
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine if the given player has been injected by TinyProtocol.
|
* Determine if the given player has been injected by TinyProtocol.
|
||||||
|
*
|
||||||
* @param player - the player.
|
* @param player - the player.
|
||||||
* @return TRUE if it is, FALSE otherwise.
|
* @return TRUE if it is, FALSE otherwise.
|
||||||
*/
|
*/
|
||||||
@ -401,6 +431,7 @@ public abstract class TinyProtocol {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine if the given channel has been injected by TinyProtocol.
|
* Determine if the given channel has been injected by TinyProtocol.
|
||||||
|
*
|
||||||
* @param channel - the channel.
|
* @param channel - the channel.
|
||||||
* @return TRUE if it is, FALSE otherwise.
|
* @return TRUE if it is, FALSE otherwise.
|
||||||
*/
|
*/
|
||||||
@ -419,6 +450,7 @@ public abstract class TinyProtocol {
|
|||||||
for (Player player : plugin.getServer().getOnlinePlayers()) {
|
for (Player player : plugin.getServer().getOnlinePlayers()) {
|
||||||
uninjectPlayer(player);
|
uninjectPlayer(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up Bukkit
|
// Clean up Bukkit
|
||||||
HandlerList.unregisterAll(listener);
|
HandlerList.unregisterAll(listener);
|
||||||
unregisterChannelHandler();
|
unregisterChannelHandler();
|
||||||
@ -427,6 +459,7 @@ public abstract class TinyProtocol {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Channel handler that is inserted into the player's channel pipeline, allowing us to intercept sent and received packets.
|
* Channel handler that is inserted into the player's channel pipeline, allowing us to intercept sent and received packets.
|
||||||
|
*
|
||||||
* @author Kristian
|
* @author Kristian
|
||||||
*/
|
*/
|
||||||
private final class PacketInterceptor extends ChannelDuplexHandler {
|
private final class PacketInterceptor extends ChannelDuplexHandler {
|
||||||
@ -449,6 +482,7 @@ public abstract class TinyProtocol {
|
|||||||
super.channelRead(ctx, msg);
|
super.channelRead(ctx, msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
|
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
|
||||||
try {
|
try {
|
||||||
|
Loading…
Reference in New Issue
Block a user