diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/compat/guava/Guava.java b/ProtocolLib/src/main/java/com/comphenix/protocol/compat/guava/Guava.java index c8fdbec7..6f94113a 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/compat/guava/Guava.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/compat/guava/Guava.java @@ -31,22 +31,21 @@ import com.google.common.collect.Range; public class Guava { private static GuavaCompat compat; - private static GuavaCompat getCompat() { - if (compat == null) { + static { + try { + Range.closed(1, 2); + compat = new Guava17(); + } catch (Throwable ex) { try { - Range.closed(1, 2); - return compat = new Guava17(); - } catch (Throwable ex) { - try { - ProtocolLibrary.log("Falling back to Guava 10 compat"); - Class clazz = Class.forName("com.comphenix.protocol.compat.guava.Guava10"); - return compat = (GuavaCompat) clazz.newInstance(); - } catch (Throwable ex1) { - ProtocolLibrary.getStaticLogger().log(Level.SEVERE, "Failed to create Guava 10 compat:", ex1); - } + Class clazz = Class.forName("com.comphenix.protocol.compat.guava.Guava10"); + compat = (GuavaCompat) clazz.newInstance(); + } catch (Throwable ex1) { + ProtocolLibrary.getStaticLogger().log(Level.SEVERE, "Failed to create Guava 10 compat:", ex1); } } + } + private static GuavaCompat getCompat() { return compat; } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/compat/netty/Netty.java b/ProtocolLib/src/main/java/com/comphenix/protocol/compat/netty/Netty.java index f1830540..f79c5678 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/compat/netty/Netty.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/compat/netty/Netty.java @@ -35,22 +35,21 @@ import com.comphenix.protocol.wrappers.WrappedServerPing.CompressedImage; public class Netty { private static NettyCompat compat; - private static NettyCompat getCompat() { - if (compat == null) { + static { + try { + Class.forName("io.netty.buffer.ByteBuf"); + compat = new IndependentNetty(); + } catch (ClassNotFoundException ex) { try { - Class.forName("io.netty.buffer.ByteBuf"); - return compat = new IndependentNetty(); - } catch (Throwable ex) { - try { - ProtocolLibrary.log("Falling back to legacy Netty compat"); - Class clazz = Class.forName("com.comphenix.protocol.compat.netty.shaded.ShadedNetty"); - return compat = (NettyCompat) clazz.newInstance(); - } catch (Throwable ex1) { - ProtocolLibrary.getStaticLogger().log(Level.SEVERE, "Failed to create legacy netty compat:", ex1); - } + Class clazz = Class.forName("com.comphenix.protocol.compat.netty.shaded.ShadedNetty"); + compat = (NettyCompat) clazz.newInstance(); + } catch (Exception ex1) { + ProtocolLibrary.getStaticLogger().log(Level.SEVERE, "Failed to create legacy netty compat:", ex1); } } + } + private static NettyCompat getCompat() { return compat; } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/compat/netty/independent/IndependentNetty.java b/ProtocolLib/src/main/java/com/comphenix/protocol/compat/netty/independent/IndependentNetty.java index c7925466..49f2eaf1 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/compat/netty/independent/IndependentNetty.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/compat/netty/independent/IndependentNetty.java @@ -45,15 +45,14 @@ public class IndependentNetty implements NettyCompat { @Override public WrappedByteBuf createPacketBuffer() { - return getPacketDataSerializer(allocateUnpooled()); + return getPacketDataSerializer(UnpooledByteBufAllocator.DEFAULT.buffer()); } - private WrappedByteBuf getPacketDataSerializer(WrappedByteBuf buffer) { + private WrappedByteBuf getPacketDataSerializer(ByteBuf buffer) { Class packetSerializer = MinecraftReflection.getPacketDataSerializerClass(); try { - return new NettyByteBuf((ByteBuf) packetSerializer.getConstructor(MinecraftReflection.getByteBufClass()) - .newInstance(buffer.getHandle())); + return new NettyByteBuf((ByteBuf) packetSerializer.getConstructor(MinecraftReflection.getByteBufClass()).newInstance(buffer)); } catch (Exception e) { throw new RuntimeException("Cannot construct packet serializer.", e); } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/compat/netty/independent/NettyByteBuf.java b/ProtocolLib/src/main/java/com/comphenix/protocol/compat/netty/independent/NettyByteBuf.java index 12c757a3..ce37d03b 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/compat/netty/independent/NettyByteBuf.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/compat/netty/independent/NettyByteBuf.java @@ -21,6 +21,7 @@ import io.netty.buffer.ByteBuf; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import java.lang.ref.WeakReference; import com.comphenix.protocol.compat.netty.WrappedByteBuf; @@ -29,49 +30,49 @@ import com.comphenix.protocol.compat.netty.WrappedByteBuf; */ public class NettyByteBuf implements WrappedByteBuf { - private final ByteBuf handle; + private WeakReference handle; public NettyByteBuf(ByteBuf handle) { - this.handle = handle; + this.handle = new WeakReference(handle); } @Override public void writeBytes(ObjectInputStream input, int id) throws IOException { - handle.writeBytes(input, id); + handle.get().writeBytes(input, id); } @Override public Object getHandle() { - return handle; + return handle.get(); } @Override public int readableBytes() { - return handle.readableBytes(); + return handle.get().readableBytes(); } @Override public void readBytes(ObjectOutputStream output, int readableBytes) throws IOException { - handle.readBytes(output, readableBytes); + handle.get().readBytes(output, readableBytes); } @Override public void readBytes(byte[] data) { - handle.readBytes(data); + handle.get().readBytes(data); } @Override public void writeByte(byte b) { - handle.writeByte(b); + handle.get().writeByte(b); } @Override public void writeByte(int i) { - handle.writeByte(i); + handle.get().writeByte(i); } @Override public void writeBytes(byte[] bytes) { - handle.writeBytes(bytes); + handle.get().writeBytes(bytes); } } \ No newline at end of file diff --git a/modules/v1_7_R4/src/main/java/com/comphenix/protocol/compat/netty/shaded/ShadedByteBuf.java b/modules/v1_7_R4/src/main/java/com/comphenix/protocol/compat/netty/shaded/ShadedByteBuf.java index f3899894..0cbb3a4c 100644 --- a/modules/v1_7_R4/src/main/java/com/comphenix/protocol/compat/netty/shaded/ShadedByteBuf.java +++ b/modules/v1_7_R4/src/main/java/com/comphenix/protocol/compat/netty/shaded/ShadedByteBuf.java @@ -19,6 +19,7 @@ package com.comphenix.protocol.compat.netty.shaded; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import java.lang.ref.WeakReference; import net.minecraft.util.io.netty.buffer.ByteBuf; @@ -29,49 +30,49 @@ import com.comphenix.protocol.compat.netty.WrappedByteBuf; */ public class ShadedByteBuf implements WrappedByteBuf { - private final ByteBuf handle; + private WeakReference handle; public ShadedByteBuf(ByteBuf handle) { - this.handle = handle; + this.handle = new WeakReference(handle); } @Override public Object getHandle() { - return handle; + return handle.get(); } @Override public void writeBytes(ObjectInputStream input, int id) throws IOException { - handle.writeBytes(input, id); + handle.get().writeBytes(input, id); } @Override public int readableBytes() { - return handle.readableBytes(); + return handle.get().readableBytes(); } @Override public void readBytes(ObjectOutputStream output, int readableBytes) throws IOException { - handle.readBytes(output, readableBytes); + handle.get().readBytes(output, readableBytes); } @Override public void readBytes(byte[] data) { - handle.readBytes(data); + handle.get().readBytes(data); } @Override public void writeByte(byte b) { - handle.writeByte(b); + handle.get().writeByte(b); } @Override public void writeByte(int i) { - handle.writeByte(i); + handle.get().writeByte(i); } @Override public void writeBytes(byte[] bytes) { - handle.writeBytes(bytes); + handle.get().writeBytes(bytes); } } \ No newline at end of file diff --git a/modules/v1_7_R4/src/main/java/com/comphenix/protocol/compat/netty/shaded/ShadedNetty.java b/modules/v1_7_R4/src/main/java/com/comphenix/protocol/compat/netty/shaded/ShadedNetty.java index d2f075cd..18d65ca9 100644 --- a/modules/v1_7_R4/src/main/java/com/comphenix/protocol/compat/netty/shaded/ShadedNetty.java +++ b/modules/v1_7_R4/src/main/java/com/comphenix/protocol/compat/netty/shaded/ShadedNetty.java @@ -45,15 +45,14 @@ public class ShadedNetty implements NettyCompat { @Override public WrappedByteBuf createPacketBuffer() { - return getPacketDataSerializer(allocateUnpooled()); + return getPacketDataSerializer(UnpooledByteBufAllocator.DEFAULT.buffer()); } - private WrappedByteBuf getPacketDataSerializer(WrappedByteBuf buffer) { + private WrappedByteBuf getPacketDataSerializer(ByteBuf buffer) { Class packetSerializer = MinecraftReflection.getPacketDataSerializerClass(); try { - return new ShadedByteBuf((ByteBuf) packetSerializer.getConstructor(MinecraftReflection.getByteBufClass()) - .newInstance(buffer.getHandle())); + return new ShadedByteBuf((ByteBuf) packetSerializer.getConstructor(MinecraftReflection.getByteBufClass()).newInstance(buffer)); } catch (Exception e) { throw new RuntimeException("Cannot construct packet serializer.", e); }