Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Parses the data received and returns the corresponding <code>Packet</code> object
- *
- * @param bytes The data received
- * @return A new packet object based on <code>bytes</code>, or null if no matching packet was detected
- * @throws InstantiationException
- * @throws IllegalArgumentException
- * @throws InvocationTargetException
- * @throws IllegalAccessException
- */
- public static Packet parsePacket(byte[] bytes) throws InstantiationException, IllegalArgumentException, InvocationTargetException, IllegalAccessException {
- String c = Integer.toHexString(bytes[4]);
- String cc = Integer.toHexString(bytes[5]);
- if (c.length() == 1) c = "0" + c;
- if (cc.length() == 1) cc = "0" + cc;
- if (c.equalsIgnoreCase("01") && cc.equalsIgnoreCase("01")) {
- c = c+cc;
- String cOld = Integer.toHexString(bytes[8]);
- String ccOld = Integer.toHexString(bytes[9]);
- if (cOld.length() == 1) cOld = "0" + cOld;
- if (ccOld.length() == 1) ccOld = "0" + ccOld;
- cc = new StringBuilder("_").append(cOld).append(ccOld).toString();
- }
- String byteName = new StringBuilder(c).append(cc).toString();
- String classname = "tfmbot.packets.Packet" + byteName + TFMBot.packetNames.get(byteName);
- //Return a new packet object with the specified data, or nul if it is an unknow packet
- try {
- return (Packet) Class.forName(classname).getDeclaredConstructor(bytes.getClass()).newInstance(bytes);
- } catch (ClassNotFoundException ex) {
- TFMBot.log("Connection: ", "[WARNING] ", new StringBuilder("Unknow packet detected: ").append(classname).toString());
- return null;
- } catch (NoSuchMethodException ex) {
- TFMBot.log("Connection: " + "[ERROR] " + "Can't parse packet " + classname + ": Missing constructor");
- return null;
- }
- }
Add Comment
Please, Sign In to add comment