Guest User

Untitled

a guest
May 25th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.05 KB | None | 0 0
  1. /**
  2.      * Parses the data received and returns the corresponding <code>Packet</code> object
  3.      *
  4.      * @param bytes The data received
  5.      * @return A new packet object based on <code>bytes</code>, or null if no matching packet was detected
  6.      * @throws InstantiationException
  7.      * @throws IllegalArgumentException
  8.      * @throws InvocationTargetException
  9.      * @throws IllegalAccessException
  10.      */
  11.     public static Packet parsePacket(byte[] bytes) throws InstantiationException, IllegalArgumentException, InvocationTargetException, IllegalAccessException {
  12.                
  13.        
  14.         String c = Integer.toHexString(bytes[4]);
  15.         String cc = Integer.toHexString(bytes[5]);
  16.         if (c.length() == 1) c = "0" + c;
  17.         if (cc.length() == 1) cc = "0" + cc;
  18.        
  19.         if (c.equalsIgnoreCase("01") && cc.equalsIgnoreCase("01")) {
  20.            
  21.             c = c+cc;
  22.             String cOld = Integer.toHexString(bytes[8]);
  23.             String ccOld = Integer.toHexString(bytes[9]);
  24.             if (cOld.length() == 1) cOld = "0" + cOld;
  25.             if (ccOld.length() == 1) ccOld = "0" + ccOld;
  26.            
  27.             cc = new StringBuilder("_").append(cOld).append(ccOld).toString();
  28.         }
  29.        
  30.         String byteName = new StringBuilder(c).append(cc).toString();
  31.         String classname = "tfmbot.packets.Packet" + byteName + TFMBot.packetNames.get(byteName);
  32.        
  33.        
  34.         //Return a new packet object with the specified data, or nul if it is an unknow packet
  35.         try {
  36.             return (Packet) Class.forName(classname).getDeclaredConstructor(bytes.getClass()).newInstance(bytes);
  37.         } catch (ClassNotFoundException ex) {
  38.             TFMBot.log("Connection: ", "[WARNING] ", new StringBuilder("Unknow packet detected: ").append(classname).toString());
  39.             return null;
  40.         } catch (NoSuchMethodException ex) {
  41.             TFMBot.log("Connection: " + "[ERROR] " + "Can't parse packet " + classname + ": Missing constructor");
  42.             return null;
  43.         }
  44.        
  45.     }
Add Comment
Please, Sign In to add comment