From b61edb0d8ed702d56bcca4ddd59c2f4b6e32b686 Mon Sep 17 00:00:00 2001 From: creeper123123321 Date: Fri, 21 Sep 2018 14:46:22 -0300 Subject: [PATCH] javadoc, fix possible NPE --- .../myles/ViaVersion/api/PacketWrapper.java | 8 +++++ .../ViaVersion/api/data/UserConnection.java | 33 +++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java b/common/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java index ee03580bd..2268a9e15 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java +++ b/common/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java @@ -483,6 +483,14 @@ public class PacketWrapper { } } + /** + * Send this packet to the server. + * + * @param packetProtocol - The protocol version of the packet. + * @param skipCurrentPipeline - Skip the current pipeline + * @param currentThread - Run in the same thread + * @throws Exception if it fails to write + */ public void sendToServer(Class packetProtocol, boolean skipCurrentPipeline, boolean currentThread) throws Exception { if (!isCancelled()) { ByteBuf output = constructPacket(packetProtocol, skipCurrentPipeline, Direction.INCOMING); diff --git a/common/src/main/java/us/myles/ViaVersion/api/data/UserConnection.java b/common/src/main/java/us/myles/ViaVersion/api/data/UserConnection.java index af93f674a..4517c2305 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/data/UserConnection.java +++ b/common/src/main/java/us/myles/ViaVersion/api/data/UserConnection.java @@ -3,8 +3,10 @@ package us.myles.ViaVersion.api.data; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerContext; import io.netty.channel.socket.SocketChannel; import lombok.Data; +import lombok.NonNull; import net.md_5.bungee.api.ChatColor; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Via; @@ -19,6 +21,13 @@ import java.util.concurrent.ConcurrentHashMap; @Data public class UserConnection { + /** + * The channel of the user. + * /!\ In some unofficial platforms this is a client channel + * + * TODO - Weak this field to {@link io.netty.channel.Channel}? + */ + @NonNull private final SocketChannel channel; Map storedObjects = new ConcurrentHashMap<>(); private boolean active = true; @@ -199,6 +208,12 @@ public class UserConnection { } + /** + * Sends a raw packet to the server + * + * @param packet Raw packet to be sent + * @param currentThread If {@code true} executes immediately, {@code false} submits a task to EventLoop + */ public void sendRawPacketToServer(final ByteBuf packet, boolean currentThread) { final ByteBuf buf = packet.alloc().buffer(); try { @@ -209,17 +224,31 @@ public class UserConnection { } buf.writeBytes(packet); packet.release(); + final ChannelHandlerContext context = PipelineUtil.getPreviousContext(Via.getManager().getInjector().getDecoderName(), getChannel().pipeline()); if (currentThread) { - PipelineUtil.getPreviousContext(Via.getManager().getInjector().getDecoderName(), getChannel().pipeline()).fireChannelRead(buf); + if (context != null) { + context.fireChannelRead(buf); + } else { + getChannel().pipeline().fireChannelRead(buf); + } } else { channel.eventLoop().submit(new Runnable() { @Override public void run() { - PipelineUtil.getPreviousContext(Via.getManager().getInjector().getDecoderName(), getChannel().pipeline()).fireChannelRead(buf); + if (context != null) { + context.fireChannelRead(buf); + } else { + getChannel().pipeline().fireChannelRead(buf); + } } }); } } + /** + * Sends a raw packet to the server. It will submit a task to EventLoop + * + * @param packet Raw packet to be sent + */ public void sendRawPacketToServer(ByteBuf packet) { sendRawPacketToServer(packet, false); } }