|
33 | 33 | import io.netty.buffer.ByteBuf;
|
34 | 34 | import io.netty.buffer.Unpooled;
|
35 | 35 | import io.netty.channel.Channel;
|
| 36 | +import io.netty.channel.ChannelOutboundBuffer; |
36 | 37 | import java.net.InetSocketAddress;
|
37 | 38 | import net.elytrium.fastmotd.FastMOTD;
|
38 | 39 | import net.elytrium.fastmotd.Settings;
|
@@ -93,6 +94,26 @@ private void switchState(State oldState, State newState) {
|
93 | 94 | this.state = newState;
|
94 | 95 | }
|
95 | 96 |
|
| 97 | + private void sendPacket(ByteBuf packet, boolean constant) { |
| 98 | + if (Settings.IMP.MAIN.DIRECT_WRITE) { |
| 99 | + ChannelOutboundBuffer buffer = this.channel.unsafe().outboundBuffer(); |
| 100 | + if (buffer == null) { |
| 101 | + packet.release(); // connection was closed already, no need to send the packet. |
| 102 | + } else { |
| 103 | + // .slice() constant packet to ensure that Netty do not modify its readerIndex |
| 104 | + if (constant) { |
| 105 | + packet = packet.slice(); |
| 106 | + } |
| 107 | + |
| 108 | + // Send the packet |
| 109 | + buffer.addMessage(packet, packet.readableBytes(), this.channel.voidPromise()); |
| 110 | + this.channel.flush(); |
| 111 | + } |
| 112 | + } else { |
| 113 | + this.channel.writeAndFlush(packet); |
| 114 | + } |
| 115 | + } |
| 116 | + |
96 | 117 | @Override
|
97 | 118 | public boolean handle(LegacyPingPacket packet) {
|
98 | 119 | this.connection.close();
|
@@ -154,11 +175,11 @@ public void handleGeneric(MinecraftPacket packet) {
|
154 | 175 | buf.writeByte(9);
|
155 | 176 | buf.writeByte(1);
|
156 | 177 | packet.encode(buf, null, null);
|
157 |
| - this.channel.writeAndFlush(buf); |
| 178 | + this.sendPacket(buf, false); |
158 | 179 | this.connection.close();
|
159 | 180 | } else if (packet instanceof StatusRequestPacket) {
|
160 | 181 | this.switchState(State.REQUEST, State.PING);
|
161 |
| - this.channel.writeAndFlush(this.plugin.getNext(this.protocolVersion, this.serverAddress)); |
| 182 | + this.sendPacket(this.plugin.getNext(this.protocolVersion, this.serverAddress), true); |
162 | 183 | } else {
|
163 | 184 | this.original.handleGeneric(packet);
|
164 | 185 | }
|
|
0 commit comments