diff --git a/src/main/java/net/elytrium/fastmotd/Settings.java b/src/main/java/net/elytrium/fastmotd/Settings.java index 065e2dd..e21167e 100644 --- a/src/main/java/net/elytrium/fastmotd/Settings.java +++ b/src/main/java/net/elytrium/fastmotd/Settings.java @@ -66,6 +66,8 @@ public static class MAIN { @CommentValue("Set -1 to disable PNG recompression") }) public double PNG_QUALITY = 0.0; + @Comment(@CommentValue("Write packets outside of Netty pipeline to avoid plugins that modify packets (e.g. PacketEvents)")) + public boolean DIRECT_WRITE = false; public boolean LOG_PINGS = false; public boolean LOG_IMPROPER_PINGS = false; @Comment({ diff --git a/src/main/java/net/elytrium/fastmotd/injection/HandshakeSessionHandlerHook.java b/src/main/java/net/elytrium/fastmotd/injection/HandshakeSessionHandlerHook.java index 997fb51..632c7a4 100644 --- a/src/main/java/net/elytrium/fastmotd/injection/HandshakeSessionHandlerHook.java +++ b/src/main/java/net/elytrium/fastmotd/injection/HandshakeSessionHandlerHook.java @@ -33,6 +33,7 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.Channel; +import io.netty.channel.ChannelOutboundBuffer; import java.net.InetSocketAddress; import net.elytrium.fastmotd.FastMOTD; import net.elytrium.fastmotd.Settings; @@ -93,6 +94,26 @@ private void switchState(State oldState, State newState) { this.state = newState; } + private void sendPacket(ByteBuf packet, boolean constant) { + if (Settings.IMP.MAIN.DIRECT_WRITE) { + ChannelOutboundBuffer buffer = this.channel.unsafe().outboundBuffer(); + if (buffer == null) { + packet.release(); // connection was closed already, no need to send the packet. + } else { + // .slice() constant packet to ensure that Netty do not modify its readerIndex + if (constant) { + packet = packet.slice(); + } + + // Send the packet + buffer.addMessage(packet, packet.readableBytes(), this.channel.voidPromise()); + this.channel.flush(); + } + } else { + this.channel.writeAndFlush(packet); + } + } + @Override public boolean handle(LegacyPingPacket packet) { this.connection.close(); @@ -154,11 +175,11 @@ public void handleGeneric(MinecraftPacket packet) { buf.writeByte(9); buf.writeByte(1); packet.encode(buf, null, null); - this.channel.writeAndFlush(buf); + this.sendPacket(buf, false); this.connection.close(); } else if (packet instanceof StatusRequestPacket) { this.switchState(State.REQUEST, State.PING); - this.channel.writeAndFlush(this.plugin.getNext(this.protocolVersion, this.serverAddress)); + this.sendPacket(this.plugin.getNext(this.protocolVersion, this.serverAddress), true); } else { this.original.handleGeneric(packet); }