20
20
import static com .google .common .base .Preconditions .checkNotNull ;
21
21
22
22
import java .lang .reflect .Method ;
23
+ import java .util .Arrays ;
23
24
24
25
import com .comphenix .protocol .PacketType ;
25
26
import com .comphenix .protocol .events .PacketContainer ;
@@ -78,16 +79,7 @@ public byte[] getBytes() {
78
79
* @param output Output to write to
79
80
*/
80
81
public void writeId (ByteBuf output ) {
81
- checkNotNull (output , "output cannot be null!" );
82
- // From PacketDataSerializer#d(int)
83
-
84
- int i = id ;
85
- while ((i & -128 ) != 0 ) {
86
- output .writeByte (i & 127 | 128 );
87
- i >>>= 7 ;
88
- }
89
-
90
- output .writeByte (i );
82
+ writeVarInt (output , id );
91
83
}
92
84
93
85
/**
@@ -118,6 +110,39 @@ public ByteBuf serialize() {
118
110
return buffer ;
119
111
}
120
112
113
+ @ Override
114
+ public boolean equals (Object obj ) {
115
+ if (this == obj ) return true ;
116
+
117
+ if (obj instanceof WirePacket ) {
118
+ WirePacket that = (WirePacket ) obj ;
119
+ return this .id == that .id &&
120
+ Arrays .equals (this .bytes , that .bytes );
121
+ }
122
+
123
+ return false ;
124
+ }
125
+
126
+ @ Override
127
+ public int hashCode () {
128
+ final int prime = 31 ;
129
+ int result = 1 ;
130
+ result = prime * result + Arrays .hashCode (bytes );
131
+ result = prime * result + id ;
132
+ return result ;
133
+ }
134
+
135
+ @ Override
136
+ public String toString () {
137
+ return "WirePacket[id=" + id + ", bytes=" + Arrays .toString (bytes ) + "]" ;
138
+ }
139
+
140
+ private static byte [] getBytes (ByteBuf buffer ) {
141
+ byte [] array = new byte [buffer .readableBytes ()];
142
+ buffer .readBytes (array );
143
+ return array ;
144
+ }
145
+
121
146
/**
122
147
* Creates a WirePacket from an existing PacketContainer
123
148
* @param packet Existing packet
@@ -137,7 +162,7 @@ public static WirePacket fromPacket(PacketContainer packet) {
137
162
throw new RuntimeException ("Failed to serialize packet contents." , ex );
138
163
}
139
164
140
- return new WirePacket (id , buffer . array ( ));
165
+ return new WirePacket (id , getBytes ( buffer ));
141
166
}
142
167
143
168
/**
@@ -162,6 +187,36 @@ public static WirePacket fromPacket(Object packet) {
162
187
throw new RuntimeException ("Failed to serialize packet contents." , ex );
163
188
}
164
189
165
- return new WirePacket (id , buffer .array ());
190
+ return new WirePacket (id , getBytes (buffer ));
191
+ }
192
+
193
+ public static void writeVarInt (ByteBuf output , int i ) {
194
+ checkNotNull (output , "output cannot be null!" );
195
+
196
+ while ((i & -128 ) != 0 ) {
197
+ output .writeByte (i & 127 | 128 );
198
+ i >>>= 7 ;
199
+ }
200
+
201
+ output .writeByte (i );
202
+ }
203
+
204
+ public static int readVarInt (ByteBuf input ) {
205
+ checkNotNull (input , "input cannot be null!" );
206
+
207
+ int i = 0 ;
208
+ int j = 0 ;
209
+
210
+ byte b0 ;
211
+
212
+ do {
213
+ b0 = input .readByte ();
214
+ i |= (b0 & 127 ) << j ++ * 7 ;
215
+ if (j > 5 ) {
216
+ throw new RuntimeException ("VarInt too big" );
217
+ }
218
+ } while ((b0 & 128 ) == 128 );
219
+
220
+ return i ;
166
221
}
167
222
}
0 commit comments