package de.hype.bbsentials.packets; import com.google.gson.Gson; import de.hype.bbsentials.client.CustomGson; import de.hype.bbsentials.communication.BBsentialConnection; import java.util.function.Consumer; public class PacketUtils { public static final Gson gson = CustomGson.create(); public static String parsePacketToJson(AbstractPacket packet) { return gson.toJson(packet); } public static void tryToProcessPacket(Packet packet, String rawJson) { Class clazz = packet.getClazz(); Consumer consumer = packet.getConsumer(); T abstractPacket = gson.fromJson(rawJson, clazz); consumer.accept(abstractPacket); } private static void showError(Throwable t, String errorMessage) { System.out.println(errorMessage + " because of: " + t.getClass().getSimpleName() + ": " + t.getMessage()); new Error(errorMessage, t).printStackTrace(); } public static class APIException extends Error { public APIException(String errorMessage, Throwable t) { super(errorMessage, t); } public APIException(String errorMessage) { super(errorMessage); } } public static T getAsPacket(String message, Class clazz) { if (!message.contains(".")) return null; String packetName = message.split("\\.")[0]; String rawJson = message.substring(packetName.length() + 1); if (!packetName.equals(clazz.getSimpleName())) { try { T parsedPacket = gson.fromJson(rawJson, clazz); return parsedPacket; } catch (Throwable t) { showError(t, "Could not process packet '" + packetName + "' from " + EnviromentPacketConfig.notEnviroment); } } String errorMessage = "Could not process packet '" + packetName + "' from " + EnviromentPacketConfig.notEnviroment; showError(new APIException("Found unknown packet: " + packetName + "'"), errorMessage); return null; } // public static T getAsPacket(String message) { // if (!message.contains(".")) return null; // String packetName = message.split("\\.")[0]; // String rawJson = message.substring(packetName.length() + 1); // // for (Class clazz : PacketManager.getAllPacketClasses()) { // if (packetName.equals(clazz.getSimpleName())) { // try { // if (clazz.isAssignableFrom(clazz)) { // //TODO the problem // T parsedPacket = (T) gson.fromJson(rawJson, clazz); // return parsedPacket; // } // else { // return null; // } // } catch (Throwable t) { // showError(t, "Could not process packet '" + packetName + "' from " + EnviromentPacketConfig.notEnviroment); // } // } // } // return null; // } public static boolean isPacket(String message, Class clazz) { if (!message.contains(".")) return false; String packetName = message.split("\\.")[0]; if (packetName.equals(clazz.getSimpleName())) { return true; } return false; } public static boolean isPacket(String message) { if (!message.contains(".")) return false; String packetName = message.split("\\.")[0]; for (Class packetClass : PacketManager.getAllPacketClasses()) { if (!packetName.equals(packetClass.getSimpleName())) { return true; } } return false; } public static boolean handleIfPacket(BBsentialConnection connection, String message) { //Return = is Packet if (!message.contains(".")) return false; String packetName = message.split("\\.")[0]; String rawJson = message.substring(packetName.length() + 1); PacketManager manager = new PacketManager(connection); for (Packet packet : manager.getPackets()) { if (!packetName.equals(packet.getClazz().getSimpleName())) continue; try { tryToProcessPacket(packet, rawJson); return true; } catch (Throwable t) { showError(t, "Could not process packet '" + packetName + "' from " + EnviromentPacketConfig.notEnviroment); } } String errorMessage = "Could not process packet '" + packetName + "' from " + EnviromentPacketConfig.notEnviroment; showError(new APIException("Found unknown packet: " + packetName + "'"), errorMessage); return false; } }