24
24
import java .io .InputStream ;
25
25
import java .security .PublicKey ;
26
26
import java .util .zip .DataFormatException ;
27
+ import java .util .zip .Deflater ;
27
28
import java .util .zip .Inflater ;
28
29
29
30
@ Getter
@@ -111,27 +112,15 @@ public void sendPacket(Packet packet) {
111
112
}
112
113
113
114
if (getThreshold () >= 0 ) {
114
- // Send packet (with 0 threshold, no compression)
115
- ByteArrayDataOutput send1 = ByteStreams .newDataOutput ();
116
- Packet .writeVarInt (0 , send1 );
117
- send1 .write (buf .toByteArray ());
118
- ByteArrayDataOutput send2 = ByteStreams .newDataOutput ();
119
- Packet .writeVarInt (send1 .toByteArray ().length , send2 );
120
- send2 .write (send1 .toByteArray ());
121
115
try {
122
- out .write (send2 .toByteArray ());
123
- out .flush ();
116
+ sendCompressed (buf );
124
117
} catch (IOException e ) {
125
118
FishingBot .getLog ().severe ("Error while trying to send: " + packet .getClass ().getSimpleName ());
119
+ e .printStackTrace ();
126
120
}
127
121
} else {
128
- // Send packet (without threshold)
129
- ByteArrayDataOutput send = ByteStreams .newDataOutput ();
130
- Packet .writeVarInt (buf .toByteArray ().length , send );
131
- send .write (buf .toByteArray ());
132
122
try {
133
- out .write (send .toByteArray ());
134
- out .flush ();
123
+ sendUncompressed (buf );
135
124
} catch (IOException e ) {
136
125
FishingBot .getLog ().severe ("Error while trying to send: " + packet .getClass ().getSimpleName ());
137
126
e .printStackTrace ();
@@ -141,6 +130,58 @@ public void sendPacket(Packet packet) {
141
130
FishingBot .getLog ().info ("[" + getState ().name ().toUpperCase () + "] C >>> |S|: " + packet .getClass ().getSimpleName ());
142
131
}
143
132
133
+ private void sendCompressed (ByteArrayDataOutput buf ) throws IOException {
134
+ int length = buf .toByteArray ().length ;
135
+
136
+ if (length < getThreshold ()) {
137
+ ByteArrayDataOutput compressedBuf = ByteStreams .newDataOutput ();
138
+ Packet .writeVarInt (0 , compressedBuf );
139
+ compressedBuf .write (buf .toByteArray ());
140
+
141
+ ByteArrayDataOutput send = prependLength (compressedBuf );
142
+
143
+ out .write (send .toByteArray ());
144
+ } else {
145
+ ByteArrayDataOutput compressedBuf = compress (buf , length );
146
+
147
+ ByteArrayDataOutput send = prependLength (compressedBuf );
148
+
149
+ out .write (send .toByteArray ());
150
+ }
151
+
152
+ out .flush ();
153
+ }
154
+
155
+ private ByteArrayDataOutput compress (ByteArrayDataOutput buf , int length ) throws IOException {
156
+ ByteArrayDataOutput compressedBuf = ByteStreams .newDataOutput ();
157
+ Packet .writeVarInt (length , compressedBuf );
158
+ Deflater deflater = new Deflater ();
159
+ deflater .setInput (buf .toByteArray ());
160
+ deflater .finish ();
161
+
162
+ byte [] encodeBuf = new byte [8192 ];
163
+ while (!deflater .finished ()) {
164
+ int len = deflater .deflate (encodeBuf );
165
+ compressedBuf .write (encodeBuf , 0 , len );
166
+ }
167
+
168
+ deflater .reset ();
169
+ return compressedBuf ;
170
+ }
171
+
172
+ private void sendUncompressed (ByteArrayDataOutput buf ) throws IOException {
173
+ ByteArrayDataOutput send = prependLength (buf );
174
+ out .write (send .toByteArray ());
175
+ out .flush ();
176
+ }
177
+
178
+ private ByteArrayDataOutput prependLength (ByteArrayDataOutput buf ) {
179
+ ByteArrayDataOutput lengthPrepended = ByteStreams .newDataOutput ();
180
+ Packet .writeVarInt (buf .toByteArray ().length , lengthPrepended );
181
+ lengthPrepended .write (buf .toByteArray ());
182
+ return lengthPrepended ;
183
+ }
184
+
144
185
public void readData () throws IOException {
145
186
if (getThreshold () >= 0 ) {
146
187
int plen1 = Packet .readVarInt (in );
0 commit comments