Archiviert
13
0

Merge Spigot protocol hack support into main branch

Dieser Commit ist enthalten in:
Dan Mulloy 2014-11-15 16:44:58 -05:00
Ursprung ca2bc3ecc5
Commit 5804f0520f
10 geänderte Dateien mit 1168 neuen und 438 gelöschten Zeilen

Datei anzeigen

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.comphenix.protocol</groupId>
<artifactId>ProtocolLib</artifactId>
<version>3.5.0-SNAPSHOT</version>
<version>3.6.0-SNAPSHOT</version>
<packaging>jar</packaging>
<description>Provides read/write access to the Minecraft protocol.</description>

Datei anzeigen

@ -10,6 +10,7 @@ import java.util.concurrent.Future;
import org.bukkit.Bukkit;
import com.comphenix.protocol.annotations.Spigot;
import com.comphenix.protocol.events.ConnectionSide;
import com.comphenix.protocol.injector.packet.PacketRegistry;
import com.comphenix.protocol.reflect.ObjectEnum;
@ -179,6 +180,13 @@ public class PacketType implements Serializable, Comparable<PacketType> {
public static final PacketType CUSTOM_PAYLOAD = new PacketType(PROTOCOL, SENDER, 0x3F, 250);
public static final PacketType KICK_DISCONNECT = new PacketType(PROTOCOL, SENDER, 0x40, 255);
@Spigot(minimumBuild = 1628)
public static final PacketType TITLE = new PacketType(PROTOCOL, SENDER, 0x45, -1);
@Spigot(minimumBuild = 1628)
public static final PacketType TAB_HEADER = new PacketType(PROTOCOL, SENDER, 0x47, -1);
@Spigot(minimumBuild = 1628)
public static final PacketType RESOURCE_PACK_SEND = new PacketType(PROTOCOL, SENDER, 0x48, -1);
// The instance must
private final static Server INSTANCE = new Server();
@ -225,6 +233,9 @@ public class PacketType implements Serializable, Comparable<PacketType> {
public static final PacketType CLIENT_COMMAND = new PacketType(PROTOCOL, SENDER, 0x16, 205);
public static final PacketType CUSTOM_PAYLOAD = new PacketType(PROTOCOL, SENDER, 0x17, 250);
@Spigot(minimumBuild = 1628)
public static final PacketType RESOURCE_PACK_STATUS = new PacketType(PROTOCOL, SENDER, 0x19, -1);
private final static Client INSTANCE = new Client();
// Prevent accidental construction
@ -322,6 +333,9 @@ public class PacketType implements Serializable, Comparable<PacketType> {
@SuppressWarnings("deprecation")
public static final PacketType SUCCESS = new PacketType(PROTOCOL, SENDER, 0x02, Packets.Server.LOGIN_SUCCESS);
@Spigot(minimumBuild = 1628)
public static final PacketType LOGIN_COMPRESSION = new PacketType(PROTOCOL, SENDER, 0x03, -1);
private final static Server INSTANCE = new Server();
// Prevent accidental construction

Datei anzeigen

@ -0,0 +1,27 @@
package com.comphenix.protocol.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Indicate that this API and its descendants are only valid on Spigot.
* @author Kristian
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PACKAGE,
ElementType.PARAMETER, ElementType.TYPE, ElementType.FIELD})
public @interface Spigot {
/**
* The minimum build number of Spigot where this is valid.
* @return The minimum build.
*/
int minimumBuild();
/**
* The maximum build number of Spigot where this is valid, or Integer.MAX_VALUE if not set.
* @return The maximum build number.
*/
int maximumBuild() default Integer.MAX_VALUE;
}

Datei anzeigen

@ -28,6 +28,7 @@ import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.reflect.compiler.BackgroundCompiler;
import com.comphenix.protocol.reflect.compiler.CompileListener;
import com.comphenix.protocol.reflect.compiler.CompiledStructureModifier;
import com.comphenix.protocol.reflect.instances.DefaultInstances;
import com.comphenix.protocol.utility.MinecraftReflection;
/**
@ -59,18 +60,18 @@ public class StructureCache {
* @return Created packet.
*/
public static Object newPacket(PacketType type) {
try {
Class<?> clazz = PacketRegistry.getPacketClassFromType(type, true);
Class<?> clazz = PacketRegistry.getPacketClassFromType(type, true);
// Check the return value
if (clazz != null)
return clazz.newInstance();
throw new IllegalArgumentException("Cannot find associated packet class: " + type);
} catch (InstantiationException e) {
return null;
} catch (IllegalAccessException e) {
throw new RuntimeException("Access denied.", e);
// Check the return value
if (clazz != null) {
// TODO: Optimize DefaultInstances
Object result = DefaultInstances.DEFAULT.create(clazz);
if (result != null) {
return result;
}
}
throw new IllegalArgumentException("Cannot find associated packet class: " + type);
}
/**

Datei anzeigen

@ -19,6 +19,7 @@ import net.minecraft.util.io.netty.channel.ChannelHandler;
import net.minecraft.util.io.netty.channel.ChannelHandlerContext;
import net.minecraft.util.io.netty.channel.ChannelInboundHandler;
import net.minecraft.util.io.netty.channel.ChannelInboundHandlerAdapter;
import net.minecraft.util.io.netty.channel.ChannelPipeline;
import net.minecraft.util.io.netty.channel.ChannelPromise;
import net.minecraft.util.io.netty.channel.socket.SocketChannel;
import net.minecraft.util.io.netty.handler.codec.ByteToMessageDecoder;
@ -33,6 +34,7 @@ import org.bukkit.entity.Player;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.PacketType.Protocol;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.annotations.Spigot;
import com.comphenix.protocol.error.Report;
import com.comphenix.protocol.error.ReportType;
import com.comphenix.protocol.events.ConnectionSide;
@ -79,6 +81,9 @@ class ChannelInjector extends ByteToMessageDecoder implements Injector {
// For retrieving the protocol
private static FieldAccessor PROTOCOL_ACCESSOR;
// Current version
private static volatile MethodAccessor PROTOCOL_VERSION;
// The factory that created this injector
private InjectionFactory factory;
@ -114,7 +119,7 @@ class ChannelInjector extends ByteToMessageDecoder implements Injector {
*/
private final ThreadLocal<Boolean> scheduleProcessPackets = new ThreadLocal<Boolean>() {
@Override
protected Boolean initialValue() {
protected Boolean initialValue() {
return true;
};
};
@ -165,9 +170,27 @@ class ChannelInjector extends ByteToMessageDecoder implements Injector {
* Get the version of the current protocol.
* @return The version.
*/
@Spigot(minimumBuild = 1628)
@Override
public int getProtocolVersion() {
return MinecraftProtocolVersion.getCurrentVersion();
MethodAccessor accessor = PROTOCOL_VERSION;
if (accessor == null) {
try {
accessor = Accessors.getMethodAccessor(networkManager.getClass(), "getVersion");
} catch (RuntimeException e) {
// Notify user
ProtocolLibrary.getErrorReporter().reportWarning(
this, Report.newBuilder(REPORT_CANNOT_FIND_GET_VERSION).error(e));
// Fallback method
accessor = Accessors.getConstantAccessor(
MinecraftProtocolVersion.getCurrentVersion(), null);
}
PROTOCOL_VERSION = accessor;
}
return (Integer) accessor.invoke(networkManager);
}
@Override
@ -247,6 +270,26 @@ class ChannelInjector extends ByteToMessageDecoder implements Injector {
// Intercept all write methods
channelField.setValue(new ChannelProxy(originalChannel, MinecraftReflection.getPacketClass()) {
// Compatibility with Spigot 1.8 protocol hack
private final PipelineProxy pipelineProxy = new PipelineProxy(originalChannel.pipeline(), this) {
@Override
public ChannelPipeline addBefore(String baseName, String name, ChannelHandler handler) {
// Correct the position of the decoder
if ("decoder".equals(baseName)) {
if (super.get("protocol_lib_decoder") != null && guessSpigotHandler(handler)) {
super.addBefore("protocol_lib_decoder", name, handler);
return this;
}
}
return super.addBefore(baseName, name, handler);
}
};
@Override
public ChannelPipeline pipeline() {
return pipelineProxy;
}
@Override
protected <T> Callable<T> onMessageScheduled(final Callable<T> callable, FieldAccessor packetAccessor) {
final PacketEvent event = handleScheduled(callable, packetAccessor);
@ -321,6 +364,18 @@ class ChannelInjector extends ByteToMessageDecoder implements Injector {
}
}
/**
* Determine if the given object is a Spigot channel handler.
* @param handler - object to test.
* @return TRUE if it is, FALSE if not or unknown.
*/
private boolean guessSpigotHandler(ChannelHandler handler) {
String className = handler != null ? handler.getClass().getCanonicalName() : null;
return "org.spigotmc.SpigotDecompressor".equals(className) ||
"org.spigotmc.SpigotCompressor".equals(className);
}
/**
* Process a given message on the packet listeners.
* @param message - the message/packet.
@ -669,7 +724,7 @@ class ChannelInjector extends ByteToMessageDecoder implements Injector {
* @param player - current instance.
*/
@Override
public void setPlayer(Player player) {
public void setPlayer(Player player) {
this.player = player;
}
@ -678,7 +733,7 @@ class ChannelInjector extends ByteToMessageDecoder implements Injector {
* @param updated - updated instance.
*/
@Override
public void setUpdatedPlayer(Player updated) {
public void setUpdatedPlayer(Player updated) {
this.updated = updated;
}
@ -737,11 +792,12 @@ class ChannelInjector extends ByteToMessageDecoder implements Injector {
// Clear cache
factory.invalidate(player);
}
// dmulloy2 - attempt to fix memory leakage
this.player = null;
this.updated = null;
}
// dmulloy2 - clear player instances
// Should fix memory leaks
this.player = null;
this.updated = null;
}
/**

Datei anzeigen

@ -24,17 +24,20 @@ import com.google.common.collect.Maps;
abstract class ChannelProxy implements Channel {
// Mark that a certain object does not contain a message field
private static final FieldAccessor MARK_NO_MESSAGE = new FieldAccessor() {
public void set(Object instance, Object value) { }
public Object get(Object instance) { return null; }
public Field getField() { return null; };
@Override
public void set(Object instance, Object value) { }
@Override
public Object get(Object instance) { return null; }
@Override
public Field getField() { return null; };
};
// Looking up packets in inner classes
private static Map<Class<?>, FieldAccessor> MESSAGE_LOOKUP = Maps.newConcurrentMap();
// The underlying channel
private Channel delegate;
private Class<?> messageClass;
protected Channel delegate;
protected Class<?> messageClass;
// Event loop proxy
private transient EventLoopProxy loopProxy;
@ -60,31 +63,38 @@ abstract class ChannelProxy implements Channel {
*/
protected abstract Runnable onMessageScheduled(Runnable runnable, FieldAccessor packetAccessor);
public <T> Attribute<T> attr(AttributeKey<T> paramAttributeKey) {
@Override
public <T> Attribute<T> attr(AttributeKey<T> paramAttributeKey) {
return delegate.attr(paramAttributeKey);
}
public ChannelFuture bind(SocketAddress paramSocketAddress) {
@Override
public ChannelFuture bind(SocketAddress paramSocketAddress) {
return delegate.bind(paramSocketAddress);
}
public ChannelPipeline pipeline() {
@Override
public ChannelPipeline pipeline() {
return delegate.pipeline();
}
public ChannelFuture connect(SocketAddress paramSocketAddress) {
@Override
public ChannelFuture connect(SocketAddress paramSocketAddress) {
return delegate.connect(paramSocketAddress);
}
public ByteBufAllocator alloc() {
@Override
public ByteBufAllocator alloc() {
return delegate.alloc();
}
public ChannelPromise newPromise() {
@Override
public ChannelPromise newPromise() {
return delegate.newPromise();
}
public EventLoop eventLoop() {
@Override
public EventLoop eventLoop() {
if (loopProxy == null) {
loopProxy = new EventLoopProxy() {
@Override
@ -139,137 +149,169 @@ abstract class ChannelProxy implements Channel {
return accessor != MARK_NO_MESSAGE ? accessor : null;
}
public ChannelFuture connect(SocketAddress paramSocketAddress1,
@Override
public ChannelFuture connect(SocketAddress paramSocketAddress1,
SocketAddress paramSocketAddress2) {
return delegate.connect(paramSocketAddress1, paramSocketAddress2);
}
public ChannelProgressivePromise newProgressivePromise() {
@Override
public ChannelProgressivePromise newProgressivePromise() {
return delegate.newProgressivePromise();
}
public Channel parent() {
@Override
public Channel parent() {
return delegate.parent();
}
public ChannelConfig config() {
@Override
public ChannelConfig config() {
return delegate.config();
}
public ChannelFuture newSucceededFuture() {
@Override
public ChannelFuture newSucceededFuture() {
return delegate.newSucceededFuture();
}
public boolean isOpen() {
@Override
public boolean isOpen() {
return delegate.isOpen();
}
public ChannelFuture disconnect() {
@Override
public ChannelFuture disconnect() {
return delegate.disconnect();
}
public boolean isRegistered() {
@Override
public boolean isRegistered() {
return delegate.isRegistered();
}
public ChannelFuture newFailedFuture(Throwable paramThrowable) {
@Override
public ChannelFuture newFailedFuture(Throwable paramThrowable) {
return delegate.newFailedFuture(paramThrowable);
}
public ChannelFuture close() {
@Override
public ChannelFuture close() {
return delegate.close();
}
public boolean isActive() {
@Override
public boolean isActive() {
return delegate.isActive();
}
@Deprecated
@Override
@Deprecated
public ChannelFuture deregister() {
return delegate.deregister();
}
public ChannelPromise voidPromise() {
@Override
public ChannelPromise voidPromise() {
return delegate.voidPromise();
}
public ChannelMetadata metadata() {
@Override
public ChannelMetadata metadata() {
return delegate.metadata();
}
public ChannelFuture bind(SocketAddress paramSocketAddress,
@Override
public ChannelFuture bind(SocketAddress paramSocketAddress,
ChannelPromise paramChannelPromise) {
return delegate.bind(paramSocketAddress, paramChannelPromise);
}
public SocketAddress localAddress() {
@Override
public SocketAddress localAddress() {
return delegate.localAddress();
}
public SocketAddress remoteAddress() {
@Override
public SocketAddress remoteAddress() {
return delegate.remoteAddress();
}
public ChannelFuture connect(SocketAddress paramSocketAddress,
@Override
public ChannelFuture connect(SocketAddress paramSocketAddress,
ChannelPromise paramChannelPromise) {
return delegate.connect(paramSocketAddress, paramChannelPromise);
}
public ChannelFuture closeFuture() {
@Override
public ChannelFuture closeFuture() {
return delegate.closeFuture();
}
public boolean isWritable() {
@Override
public boolean isWritable() {
return delegate.isWritable();
}
public Channel flush() {
@Override
public Channel flush() {
return delegate.flush();
}
public ChannelFuture connect(SocketAddress paramSocketAddress1,
@Override
public ChannelFuture connect(SocketAddress paramSocketAddress1,
SocketAddress paramSocketAddress2, ChannelPromise paramChannelPromise) {
return delegate.connect(paramSocketAddress1, paramSocketAddress2, paramChannelPromise);
}
public Channel read() {
@Override
public Channel read() {
return delegate.read();
}
public Unsafe unsafe() {
@Override
public Unsafe unsafe() {
return delegate.unsafe();
}
public ChannelFuture disconnect(ChannelPromise paramChannelPromise) {
@Override
public ChannelFuture disconnect(ChannelPromise paramChannelPromise) {
return delegate.disconnect(paramChannelPromise);
}
public ChannelFuture close(ChannelPromise paramChannelPromise) {
@Override
public ChannelFuture close(ChannelPromise paramChannelPromise) {
return delegate.close(paramChannelPromise);
}
@Deprecated
@Override
@Deprecated
public ChannelFuture deregister(ChannelPromise paramChannelPromise) {
return delegate.deregister(paramChannelPromise);
}
public ChannelFuture write(Object paramObject) {
@Override
public ChannelFuture write(Object paramObject) {
return delegate.write(paramObject);
}
public ChannelFuture write(Object paramObject, ChannelPromise paramChannelPromise) {
@Override
public ChannelFuture write(Object paramObject, ChannelPromise paramChannelPromise) {
return delegate.write(paramObject, paramChannelPromise);
}
public ChannelFuture writeAndFlush(Object paramObject, ChannelPromise paramChannelPromise) {
@Override
public ChannelFuture writeAndFlush(Object paramObject, ChannelPromise paramChannelPromise) {
return delegate.writeAndFlush(paramObject, paramChannelPromise);
}
public ChannelFuture writeAndFlush(Object paramObject) {
@Override
public ChannelFuture writeAndFlush(Object paramObject) {
return delegate.writeAndFlush(paramObject);
}
public int compareTo(Channel o) {
@Override
public int compareTo(Channel o) {
return delegate.compareTo(o);
}
}

Datei anzeigen

@ -0,0 +1,358 @@
package com.comphenix.protocol.injector.netty;
import java.net.SocketAddress;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.minecraft.util.io.netty.channel.Channel;
import net.minecraft.util.io.netty.channel.ChannelFuture;
import net.minecraft.util.io.netty.channel.ChannelHandler;
import net.minecraft.util.io.netty.channel.ChannelHandlerContext;
import net.minecraft.util.io.netty.channel.ChannelPipeline;
import net.minecraft.util.io.netty.channel.ChannelPromise;
import net.minecraft.util.io.netty.util.concurrent.EventExecutorGroup;
/**
* A pipeline proxy.
* @author Kristian
*/
public class PipelineProxy implements ChannelPipeline {
protected final ChannelPipeline pipeline;
protected final Channel channel;
public PipelineProxy(ChannelPipeline pipeline, Channel channel) {
this.pipeline = pipeline;
this.channel = channel;
}
@Override
public ChannelPipeline addAfter(EventExecutorGroup arg0, String arg1, String arg2, ChannelHandler arg3) {
pipeline.addAfter(arg0, arg1, arg2, arg3);
return this;
}
@Override
public ChannelPipeline addAfter(String arg0, String arg1, ChannelHandler arg2) {
pipeline.addAfter(arg0, arg1, arg2);
return this;
}
@Override
public ChannelPipeline addBefore(EventExecutorGroup arg0, String arg1, String arg2, ChannelHandler arg3) {
pipeline.addBefore(arg0, arg1, arg2, arg3);
return this;
}
@Override
public ChannelPipeline addBefore(String arg0, String arg1, ChannelHandler arg2) {
pipeline.addBefore(arg0, arg1, arg2);
return this;
}
@Override
public ChannelPipeline addFirst(ChannelHandler... arg0) {
pipeline.addFirst(arg0);
return this;
}
@Override
public ChannelPipeline addFirst(EventExecutorGroup arg0, ChannelHandler... arg1) {
pipeline.addFirst(arg0, arg1);
return this;
}
@Override
public ChannelPipeline addFirst(EventExecutorGroup arg0, String arg1, ChannelHandler arg2) {
pipeline.addFirst(arg0, arg1, arg2);
return this;
}
@Override
public ChannelPipeline addFirst(String arg0, ChannelHandler arg1) {
pipeline.addFirst(arg0, arg1);
return this;
}
@Override
public ChannelPipeline addLast(ChannelHandler... arg0) {
pipeline.addLast(arg0);
return this;
}
@Override
public ChannelPipeline addLast(EventExecutorGroup arg0, ChannelHandler... arg1) {
pipeline.addLast(arg0, arg1);
return this;
}
@Override
public ChannelPipeline addLast(EventExecutorGroup arg0, String arg1, ChannelHandler arg2) {
pipeline.addLast(arg0, arg1, arg2);
return this;
}
@Override
public ChannelPipeline addLast(String arg0, ChannelHandler arg1) {
pipeline.addLast(arg0, arg1);
return this;
}
@Override
public ChannelFuture bind(SocketAddress arg0, ChannelPromise arg1) {
return pipeline.bind(arg0, arg1);
}
@Override
public ChannelFuture bind(SocketAddress arg0) {
return pipeline.bind(arg0);
}
@Override
public Channel channel() {
return channel;
}
@Override
public ChannelFuture close() {
return pipeline.close();
}
@Override
public ChannelFuture close(ChannelPromise arg0) {
return pipeline.close(arg0);
}
@Override
public ChannelFuture connect(SocketAddress arg0, ChannelPromise arg1) {
return pipeline.connect(arg0, arg1);
}
@Override
public ChannelFuture connect(SocketAddress arg0, SocketAddress arg1, ChannelPromise arg2) {
return pipeline.connect(arg0, arg1, arg2);
}
@Override
public ChannelFuture connect(SocketAddress arg0, SocketAddress arg1) {
return pipeline.connect(arg0, arg1);
}
@Override
public ChannelFuture connect(SocketAddress arg0) {
return pipeline.connect(arg0);
}
@Override
public ChannelHandlerContext context(ChannelHandler arg0) {
return pipeline.context(arg0);
}
@Override
public ChannelHandlerContext context(Class<? extends ChannelHandler> arg0) {
return pipeline.context(arg0);
}
@Override
public ChannelHandlerContext context(String arg0) {
return pipeline.context(arg0);
}
// We have to call the depreciated methods to properly implement the proxy
@SuppressWarnings("deprecation")
@Override
public ChannelFuture deregister() {
return pipeline.deregister();
}
@SuppressWarnings("deprecation")
@Override
public ChannelFuture deregister(ChannelPromise arg0) {
return pipeline.deregister(arg0);
}
@SuppressWarnings("deprecation")
@Override
public ChannelPipeline fireChannelUnregistered() {
pipeline.fireChannelUnregistered();
return this;
}
@Override
public ChannelFuture disconnect() {
return pipeline.disconnect();
}
@Override
public ChannelFuture disconnect(ChannelPromise arg0) {
return pipeline.disconnect(arg0);
}
@Override
public ChannelPipeline fireChannelActive() {
pipeline.fireChannelActive();
return this;
}
@Override
public ChannelPipeline fireChannelInactive() {
pipeline.fireChannelInactive();
return this;
}
@Override
public ChannelPipeline fireChannelRead(Object arg0) {
pipeline.fireChannelRead(arg0);
return this;
}
@Override
public ChannelPipeline fireChannelReadComplete() {
pipeline.fireChannelReadComplete();
return this;
}
@Override
public ChannelPipeline fireChannelRegistered() {
pipeline.fireChannelRegistered();
return this;
}
@Override
public ChannelPipeline fireChannelWritabilityChanged() {
pipeline.fireChannelWritabilityChanged();
return this;
}
@Override
public ChannelPipeline fireExceptionCaught(Throwable arg0) {
pipeline.fireExceptionCaught(arg0);
return this;
}
@Override
public ChannelPipeline fireUserEventTriggered(Object arg0) {
pipeline.fireUserEventTriggered(arg0);
return this;
}
@Override
public ChannelHandler first() {
return pipeline.first();
}
@Override
public ChannelHandlerContext firstContext() {
return pipeline.firstContext();
}
@Override
public ChannelPipeline flush() {
pipeline.flush();
return this;
}
@Override
public <T extends ChannelHandler> T get(Class<T> arg0) {
return pipeline.get(arg0);
}
@Override
public ChannelHandler get(String arg0) {
return pipeline.get(arg0);
}
@Override
public Iterator<Entry<String, ChannelHandler>> iterator() {
return pipeline.iterator();
}
@Override
public ChannelHandler last() {
return pipeline.last();
}
@Override
public ChannelHandlerContext lastContext() {
return pipeline.lastContext();
}
@Override
public List<String> names() {
return pipeline.names();
}
@Override
public ChannelPipeline read() {
pipeline.read();
return this;
}
@Override
public ChannelPipeline remove(ChannelHandler arg0) {
pipeline.remove(arg0);
return this;
}
@Override
public <T extends ChannelHandler> T remove(Class<T> arg0) {
return pipeline.remove(arg0);
}
@Override
public ChannelHandler remove(String arg0) {
return pipeline.remove(arg0);
}
@Override
public ChannelHandler removeFirst() {
return pipeline.removeFirst();
}
@Override
public ChannelHandler removeLast() {
return pipeline.removeLast();
}
@Override
public ChannelPipeline replace(ChannelHandler arg0, String arg1, ChannelHandler arg2) {
pipeline.replace(arg0, arg1, arg2);
return this;
}
@Override
public <T extends ChannelHandler> T replace(Class<T> arg0, String arg1, ChannelHandler arg2) {
return pipeline.replace(arg0, arg1, arg2);
}
@Override
public ChannelHandler replace(String arg0, String arg1, ChannelHandler arg2) {
return pipeline.replace(arg0, arg1, arg2);
}
@Override
public Map<String, ChannelHandler> toMap() {
return pipeline.toMap();
}
@Override
public ChannelFuture write(Object arg0, ChannelPromise arg1) {
return pipeline.write(arg0, arg1);
}
@Override
public ChannelFuture write(Object arg0) {
return pipeline.write(arg0);
}
@Override
public ChannelFuture writeAndFlush(Object arg0, ChannelPromise arg1) {
return pipeline.writeAndFlush(arg0, arg1);
}
@Override
public ChannelFuture writeAndFlush(Object arg0) {
return pipeline.writeAndFlush(arg0);
}
}

Datei anzeigen

@ -37,18 +37,22 @@ import javax.annotation.Nullable;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;
import com.comphenix.protocol.annotations.Spigot;
import com.comphenix.protocol.injector.BukkitUnwrapper;
import com.comphenix.protocol.reflect.FieldAccessException;
import com.comphenix.protocol.reflect.FieldUtils;
import com.comphenix.protocol.reflect.FuzzyReflection;
import com.comphenix.protocol.reflect.accessors.Accessors;
import com.comphenix.protocol.reflect.accessors.ConstructorAccessor;
import com.comphenix.protocol.reflect.accessors.FieldAccessor;
import com.comphenix.protocol.reflect.accessors.ReadOnlyFieldAccessor;
import com.comphenix.protocol.utility.MinecraftReflection;
import com.comphenix.protocol.wrappers.collection.ConvertedMap;
import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.collect.Iterators;
import com.google.common.collect.Maps;
/**
* Wraps a DataWatcher that is used to transmit arbitrary key-value pairs with a given entity.
@ -56,11 +60,140 @@ import com.google.common.collect.Iterators;
* @author Kristian
*/
public class WrappedDataWatcher extends AbstractWrapper implements Iterable<WrappedWatchableObject> {
/**
* Every custom watchable type in Spigot #1628 and above.
* @author Kristian
*/
@Spigot(minimumBuild = 1628)
public enum CustomType {
BYTE_SHORT("org.spigotmc.ProtocolData$ByteShort", 0, short.class),
DUAL_BYTE("org.spigotmc.ProtocolData$DualByte", 0, byte.class, byte.class),
HIDDEN_BYTE("org.spigotmc.ProtocolData$HiddenByte", 0, byte.class),
INT_BYTE("org.spigotmc.ProtocolData$IntByte", 2, int.class, byte.class),
DUAL_INT("org.spigotmc.ProtocolData$DualInt", 2, int.class, int.class);
private Class<?> spigotClass;
private ConstructorAccessor constructor;
private FieldAccessor secondaryValue;
private int typeId;
private CustomType(String className, int typeId, Class<?>... parameters) {
try {
this.spigotClass = Class.forName(className);
this.constructor = Accessors.getConstructorAccessor(spigotClass, parameters);
this.secondaryValue = parameters.length > 1 ? Accessors.getFieldAccessor(spigotClass, "value2", true) : null;
} catch (ClassNotFoundException e) {
System.out.println("[ProtocolLib] Unable to find " + className);
this.spigotClass = null;
}
this.typeId = typeId;
}
/**
* Construct a new instance of this Spigot type.
* @param value - the value. Cannot be NULL.
* @return The instance to construct.
*/
Object newInstance(Object value) {
return newInstance(value, null);
}
/**
* Construct a new instance of this Spigot type.
* <p>
* The secondary value may be NULL if this custom type does not contain a secondary value.
* @param value - the value.
* @param secondary - optional secondary value.
* @return
*/
Object newInstance(Object value, Object secondary) {
Preconditions.checkNotNull(value, "value cannot be NULL.");
if (hasSecondary()) {
return constructor.invoke(value, secondary);
} else {
if (secondary != null) {
throw new IllegalArgumentException("Cannot construct " + this + " with a secondary value");
}
return constructor.invoke(value);
}
}
/**
* Set the secondary value of a given type.
* @param instance - the instance.
* @param secondary - the secondary value.
*/
void setSecondary(Object instance, Object secondary) {
if (!hasSecondary()) {
throw new IllegalArgumentException(this + " does not have a secondary value.");
}
secondaryValue.set(instance, secondary);
}
/**
* Get the secondary value of a type.
* @param instance - the instance.
* @return The secondary value.
*/
Object getSecondary(Object instance) {
if (!hasSecondary()) {
throw new IllegalArgumentException(this + " does not have a secondary value.");
}
return secondaryValue.get(instance);
}
/**
* Determine if this type has a secondary value.
* @return TRUE if it does, FALSE otherwise.
*/
public boolean hasSecondary() {
return secondaryValue != null;
}
/**
* Underlying Spigot class.
* @return The class.
*/
public Class<?> getSpigotClass() {
return spigotClass;
}
/**
* The equivalent type ID.
* @return The equivalent ID.
*/
public int getTypeId() {
return typeId;
}
/**
* Retrieve the custom Spigot type of a value.
* @param value - the value.
* @return The Spigot type, or NULL if not found.
*/
@Spigot(minimumBuild = 1628)
public static CustomType fromValue(Object value) {
for (CustomType type : CustomType.values()) {
if (type.getSpigotClass().isInstance(value)) {
return type;
}
}
return null;
}
}
/**
* Used to assign integer IDs to given types.
*/
private static Map<Class<?>, Integer> TYPE_MAP;
/**
* Custom types in the bountiful update.
*/
private static Map<Class<?>, Integer> CUSTOM_MAP;
// Accessors
private static FieldAccessor TYPE_MAP_ACCESSOR;
private static FieldAccessor VALUE_MAP_ACCESSOR;
@ -198,7 +331,12 @@ public class WrappedDataWatcher extends AbstractWrapper implements Iterable<Wrap
*/
public static Integer getTypeID(Class<?> clazz) throws FieldAccessException {
initialize();
return TYPE_MAP.get(WrappedWatchableObject.getUnwrappedType(clazz));
Integer result = TYPE_MAP.get(WrappedWatchableObject.getUnwrappedType(clazz));
if (result == null) {
result = CUSTOM_MAP.get(clazz);
}
return result;
}
/**
@ -296,11 +434,23 @@ public class WrappedDataWatcher extends AbstractWrapper implements Iterable<Wrap
* @throws FieldAccessException Cannot read underlying field.
*/
public Object getObject(int index) throws FieldAccessException {
// The get method will take care of concurrency
WrappedWatchableObject object = getWrappedObject(index);
return object != null ? object.getValue() : null;
}
/**
* Retrieve the wrapped object by index.
* @param index - the index.
* @return The corresponding wrapper, or NULL.
*/
@Spigot(minimumBuild = 1628)
public WrappedWatchableObject getWrappedObject(int index) {
// The get method will take care of concurrency
Object watchable = getWatchedObject(index);
if (watchable != null) {
return new WrappedWatchableObject(watchable).getValue();
return new WrappedWatchableObject(watchable);
} else {
return null;
}
@ -441,7 +591,7 @@ public class WrappedDataWatcher extends AbstractWrapper implements Iterable<Wrap
* Set a watched byte.
* @param index - index of the watched byte.
* @param newValue - the new watched value.
* @param update - whether or not to refresh every listening clients.
* @param update - whether or not to refresh every listening client.
* @throws FieldAccessException Cannot read underlying field.
*/
public void setObject(int index, Object newValue, boolean update) throws FieldAccessException {
@ -470,6 +620,22 @@ public class WrappedDataWatcher extends AbstractWrapper implements Iterable<Wrap
}
}
/**
* Set a watched byte with an optional secondary value.
* @param index - index of the watched byte.
* @param newValue - the new watched value.
* @param secondary - optional secondary value.
* @param update - whether or not to refresh every listening client.
* @throws FieldAccessException Cannot read underlying field.
*/
@Spigot(minimumBuild = 1628)
public void setObject(int index, Object newValue, Object secondary, boolean update, CustomType type) throws FieldAccessException {
Object created = type.newInstance(newValue, secondary);
// Now update the watcher
setObject(index, created, update);
}
private Object getWatchedObject(int index) throws FieldAccessException {
// We use the get-method first and foremost
if (GET_KEY_VALUE_METHOD != null) {
@ -571,6 +737,9 @@ public class WrappedDataWatcher extends AbstractWrapper implements Iterable<Wrap
// Spigot workaround (not necessary
initializeSpigot(fuzzy);
// Any custom types
CUSTOM_MAP = initializeCustom();
// Initialize static type type
TYPE_MAP = (Map<Class<?>, Integer>) TYPE_MAP_ACCESSOR.get(null);
@ -588,6 +757,18 @@ public class WrappedDataWatcher extends AbstractWrapper implements Iterable<Wrap
initializeMethods(fuzzy);
}
// For Spigot's bountiful update patch
private static Map<Class<?>, Integer> initializeCustom() {
Map<Class<?>, Integer> map = Maps.newHashMap();
for (CustomType type : CustomType.values()) {
if (type.getSpigotClass() != null) {
map.put(type.getSpigotClass(), type.getTypeId());
}
}
return map;
}
// TODO: Remove, as this was fixed in build #1189 of Spigot
private static void initializeSpigot(FuzzyReflection fuzzy) {
// See if the workaround is needed

Datei anzeigen

@ -21,11 +21,13 @@ import java.lang.reflect.Constructor;
import org.bukkit.inventory.ItemStack;
import com.comphenix.protocol.annotations.Spigot;
import com.comphenix.protocol.reflect.EquivalentConverter;
import com.comphenix.protocol.reflect.FieldAccessException;
import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.reflect.instances.DefaultInstances;
import com.comphenix.protocol.utility.MinecraftReflection;
import com.comphenix.protocol.wrappers.WrappedDataWatcher.CustomType;
import com.google.common.base.Objects;
/**
@ -95,6 +97,18 @@ public class WrappedWatchableObject extends AbstractWrapper {
}
}
/**
* Construct a custom watchable object from an index, value and custom type.
* @param index - the index.
* @param primary - non-null value of specific types.
* @param secondary - optional secondary value, if the type can store it.
* @param type - the custom Spigot type.
*/
@Spigot(minimumBuild = 1628)
public WrappedWatchableObject(int index, Object value, Object secondary, CustomType type) {
this(index, type.newInstance(value, secondary));
}
// Wrap a NMS object
private void load(Object handle) {
initialize();
@ -119,6 +133,15 @@ public class WrappedWatchableObject extends AbstractWrapper {
}
}
/**
* Retrieve the custom type of this object.
* @return The custom type, or NULL if not applicable.
*/
@Spigot(minimumBuild = 1628)
public CustomType getCustomType() {
return CustomType.fromValue(getValue());
}
/**
* Retrieve the correct super type of the current value.
* @return Super type.
@ -266,6 +289,33 @@ public class WrappedWatchableObject extends AbstractWrapper {
return getWrapped(modifier.withType(Object.class).read(0));
}
/**
* Retrieve the secondary value associated with this watchable object.
* <p>
* This is only applicable for certain {@link CustomType}.
* @return The secondary value, or NULL if not found.
*/
@Spigot(minimumBuild = 1628)
public Object getSecondaryValue() {
CustomType type = getCustomType();
return type != null ? type.getSecondary(getValue()) : null;
}
/**
* Set the secondary value.
* @param secondary - the secondary value.
* @throws IllegalStateException If this watchable object does not have a secondary value.
*/
@Spigot(minimumBuild = 1628)
public void setSecondaryValue(Object secondary) {
CustomType type = getCustomType();
if (type == null) {
throw new IllegalStateException(this + " does not have a custom type.");
}
type.setSecondary(getValue(), secondary);
}
/**
* Set whether or not the value must be synchronized with the client.
* @param dirty - TRUE if the value should be synchronized, FALSE otherwise.

Datei anzeigen

@ -446,8 +446,9 @@ public class PacketContainerTest {
} catch (IllegalArgumentException e) {
if (!registered) {
// Check the same
assertEquals(e.getMessage(), "The packet ID " + type + " is not registered.");
// Let the test pass
System.err.println("The packet ID " + type + " is not registered.");
// assertEquals(e.getMessage(), "The packet ID " + type + " is not registered.");
} else {
// Something is very wrong
throw e;