aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hype/bbsentials/packets/AbstractPacket.java
blob: 152e3d896f30f686530d8b68f01f3ff682431d35 (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
package de.hype.bbsentials.packets;

import de.hype.bbsentials.chat.Chat;
import de.hype.bbsentials.client.BBsentials;
import de.hype.bbsentials.client.Config;
import de.hype.bbsentials.communication.BBsentialConnection;
import de.hype.bbsentials.packets.packets.InvalidCommandFeedbackPacket;

import java.lang.reflect.Field;

public class AbstractPacket {
    public final int apiVersionMin;
    public final int apiVersionMax;

    protected AbstractPacket(int apiVersionMin, int apiVersionMax) {
        this.apiVersionMax = apiVersionMax;
        this.apiVersionMin = apiVersionMin;

    }

    public boolean isValid(BBsentialConnection connection, String[] allowedNullFields) {
        if (isApiSupported(BBsentials.config)) {
            Chat.sendPrivateMessageToSelfFatal("You are using an outdated version of the mod");
        }
        return true;
    }

    public boolean isApiSupported(Config config) {
        //int version = Core.getConfig().getVersion();
        int version = config.getApiVersion();
        if (version >= apiVersionMin && version <= apiVersionMax) {
            return true;
        }
        return false;
    }

    public String hasNullFields(String[] allowedNullFields) {
        Field[] fields = this.getClass().getDeclaredFields();
        if (!this.getClass().getSimpleName().equals(InvalidCommandFeedbackPacket.class.getSimpleName())) {
            for (Field field : fields) {
                field.setAccessible(true);
                try {
                    if (field.get(this) == null) {
                        if (allowedNullFields == null) return field.getName();
                        if (isAllowedNull(allowedNullFields, field.getName())) {
                            return field.getName();
                        }
                    }
                } catch (IllegalAccessException e) {
                    // Handle the exception if needed
                    e.printStackTrace();
                }
            }
        }
        return null;

    }

    public boolean isAllowedNull(String[] allowedFields, String fieldName) {
        for (String allowedField : allowedFields) {
            if (allowedField.equals(fieldName)) {
                return true;
            }
        }
        return false;
    }
}