aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRule.java
blob: 0fe1c3f1be544b5092e66a1aa2afbc31fa251a89 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package de.hysky.skyblocker.skyblock.chat;

import de.hysky.skyblocker.utils.Utils;
import net.minecraft.client.sound.Sound;
import net.minecraft.item.ItemStack;

import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * Data class to contain all the settings for a chat rule
 */
public class ChatRule {

    private String name;
    //inputs

    private Boolean enabled;
    private Boolean isPartialMatch;
    private Boolean isRegex;
    private Boolean isIgnoreCase;
    private String filter;
    private String validLocations;

    //output
    private Boolean hideMessage;
    private Boolean showActionBar;
    private Boolean showAnnouncement;
    private String replaceMessage; //todo extract parts of original message
    private Sound customSound;
    /**
     * Creates a chat rule with default options.
     */
    public ChatRule(){
        this.name = "New Rule";

        this.enabled = true;
        this.isPartialMatch = false;
        this.isRegex = false;
        this.isIgnoreCase = true;
        this.filter = "";
        this.validLocations = "";

        this.hideMessage = true;
        this.showActionBar = false;
        this.showAnnouncement = false;
        this.replaceMessage = null;
        this.customSound = null;
    }


    public ChatRule(String name, Boolean enabled, Boolean isPartialMatch, Boolean isRegex, Boolean isIgnoreCase, String filter, String validLocation, List<ItemStack> validItems, Boolean hideMessage, Boolean showActionBar, Boolean showAnnouncement, String replaceMessage, Sound customSound) {
        this.name = name;
        this.enabled = enabled;
        this.isPartialMatch = isPartialMatch;
        this.isRegex = isRegex;
        this.isIgnoreCase = isIgnoreCase;
        this.filter = filter;
        this.validLocations = validLocation;
        this.hideMessage = hideMessage;
        this.showActionBar = showActionBar;
        this.showAnnouncement = showAnnouncement;
        this.replaceMessage = replaceMessage;
        this.customSound = customSound;
    }

    public Boolean getEnabled() { //todo remove unused getters and set
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public Boolean getPartialMatch() {
        return isPartialMatch;
    }

    public void setPartialMatch(Boolean partialMatch) {
        isPartialMatch = partialMatch;
    }

    public Boolean getRegex() {
        return isRegex;
    }

    public void setRegex(Boolean regex) {
        isRegex = regex;
    }

    public Boolean getIgnoreCase() {
        return isIgnoreCase;
    }

    public void setIgnoreCase(Boolean ignoreCase) {
        isIgnoreCase = ignoreCase;
    }

    public String getFilter() {
        return filter;
    }

    public void setFilter(String filter) {
        this.filter = filter;
    }

    public Boolean getHideMessage() {
        return hideMessage;
    }

    public void setHideMessage(Boolean hideMessage) {
        this.hideMessage = hideMessage;
    }

    public Boolean getShowActionBar() {
        return showActionBar;
    }

    public void setShowActionBar(Boolean showActionBar) {
        this.showActionBar = showActionBar;
    }

    public Boolean getShowAnnouncement() {
        return showAnnouncement;
    }

    public void setShowAnnouncement(Boolean showAnnouncement) {
        this.showAnnouncement = showAnnouncement;
    }

    public String getReplaceMessage() {
        return replaceMessage;
    }

    public void setReplaceMessage(String replaceMessage) {
        this.replaceMessage = replaceMessage;
    }

    public Sound getCustomSound() {
       return customSound;
    }

    public void setCustomSound(Sound customSound) {
        this.customSound = customSound;
    }

    public String getValidLocations() {
        return validLocations;
    }

    public void setValidLocations(String validLocations) {
        this.validLocations = validLocations;
    }

    /**
     * checks every input option and if the games state and the inputted str matches them returns true.
     * @param inputString the chat message to check if fits
     * @return if the inputs are all true and the outputs should be performed
     */
    public Boolean isMatch(String inputString){
        //enabled
        if (!enabled) return false;

        //ignore case
        String testString;
        String testFilter;
        if (isIgnoreCase){
            testString = inputString.toLowerCase();
            testFilter = filter.toLowerCase();
        }else {
            testString = inputString;
            testFilter = filter;
        }

        //filter
        if (testFilter.isBlank()) return false;
        if(isRegex) {
            if (isPartialMatch) {
               if (! Pattern.compile(testFilter).matcher(testString).find()) return false;
            }else {
                if (!testString.matches(testFilter)) return false;
            }
        } else{
            if (isPartialMatch) {
                if (!testString.contains(testFilter)) return false;
            }else {
                if (!testFilter.equals(testString)) return false;
            }
        }

        //location
        if (validLocations.isBlank()){ //if no locations do not check
            return true;
        }
        String rawLocation = Utils.getLocationRaw();
        Boolean isLocationValid = null;
        for (String validLocation : validLocations.replace(" ", "").split(",")) {//the locations are raw locations split by "," and start with ! if not locations
                if (validLocation.startsWith("!")) {//not location (
                    if (Objects.equals(validLocation.substring(1), rawLocation)) {
                        isLocationValid = false;
                        break;
                    }
                }else {
                    if (Objects.equals(validLocation, rawLocation)) { //normal location
                        isLocationValid = true;
                        break;
                    }
                }
        }
        if (isLocationValid == null || isLocationValid){//if location is not in the list at all and is a not a "!" location or and is a normal location
            return true;
        }


        return false;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public enum LocationOption {
        None,
        Island,
        Hub,
        Garden; //todo add more

    }


}