aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hype/bbsentials/packets/PacketUtils.java
blob: 8dcd4d7b16532f25c211779a4ea5483f66308d1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package de.hype.bbsentials.packets;

import com.google.gson.Gson;
import de.hype.bbsentials.chat.Chat;
import de.hype.bbsentials.client.BBsentials;
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 <T extends AbstractPacket> void tryToProcessPacket(Packet<T> packet, String rawJson) {
        Class<T> clazz = packet.getClazz();
        Consumer<T> 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 extends AbstractPacket> T getAsPacket(String message, Class<T> 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 extends AbstractPacket> T getAsPacket(String message) {
//        if (!message.contains(".")) return null;
//        String packetName = message.split("\\.")[0];
//        String rawJson = message.substring(packetName.length() + 1);
//
//        for (Class<? extends AbstractPacket> 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<? extends AbstractPacket> 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<? extends AbstractPacket> packetClass : PacketManager.getAllPacketClasses()) {
            if (!packetName.equals(packetClass.getSimpleName())) {
                return true;
            }
        }
        return false;
    }

    public static <T extends AbstractPacket> 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<? extends AbstractPacket> packet : manager.getPackets()) {
            if (!packetName.equals(packet.getClazz().getSimpleName())) continue;
            try {
                if (BBsentials.getConfig().isDetailedDevModeEnabled()) Chat.sendPrivateMessageToSelfDebug(packetName+":"+rawJson);
                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;
    }
}