aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/cowtipper/cowlection/partyfinder/Rule.java
blob: 34dd03f0464dee273794fbf6c896ae7d5352a896 (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
package de.cowtipper.cowlection.partyfinder;

import de.cowtipper.cowlection.data.DataHelper;
import de.cowtipper.cowlection.util.Utils;
import net.minecraft.util.EnumChatFormatting;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class Rule {
    private final transient List<String> patternException;
    private boolean isEnabled;
    private transient boolean isValid;
    private TriggerType triggerType;
    private String triggerText;
    private transient Pattern triggerRegex;
    private String middleText;
    private DataHelper.PartyType partyType;

    public Rule(boolean isEnabled, String trigger, TriggerType triggerType, String middleText, DataHelper.PartyType partyType) {
        this.patternException = new ArrayList<>();
        this.isEnabled = isEnabled;
        this.triggerText = trigger;
        this.triggerType = triggerType;
        this.middleText = middleText;
        this.partyType = partyType;
        postConstructor();
    }

    /**
     * No-args constructor for GSON
     */
    @SuppressWarnings("unused")
    public Rule() {
        patternException = new ArrayList<>();
    }

    public void postConstructor() {
        setTriggerTextAndRevalidateRule(triggerText);
        if (partyType == null) {
            partyType = DataHelper.PartyType.SUITABLE;
        }
    }

    public boolean isEnabled() {
        return isEnabled;
    }

    public void setEnabled(boolean enabled) {
        isEnabled = enabled;
    }

    public boolean isTriggered(String notes) {
        return isEnabled && isValid && (
                (triggerType == TriggerType.TEXT && notes.contains(triggerText))
                        || (triggerRegex != null && triggerRegex.matcher(notes).find()));
    }

    public TriggerType getTriggerType() {
        return triggerType;
    }

    /**
     * @return human readable trigger text
     */
    public String getTriggerText() {
        return this.triggerText;
    }

    public String getTriggerRegex() {
        return triggerRegex != null ? triggerRegex.pattern() : "";
    }

    public void setTriggerTextAndRevalidateRule(String trigger) {
        if (trigger == null) {
            trigger = "";
        }
        if (trigger.isEmpty()) {
            triggerText = "";
            triggerRegex = null;
            isValid = false;
            patternException.clear();
            patternException.add(EnumChatFormatting.RED + "Pattern cannot be empty!");
        } else {
            isValid = true;
            triggerText = trigger.toLowerCase(Locale.ROOT);
        }
        boolean hasCommas = trigger.contains(",");
        boolean hasBrackets = trigger.contains("(") || trigger.contains(")");
        if (triggerType == TriggerType.REGEX) {
            try {
                triggerRegex = Pattern.compile(trigger);
            } catch (PatternSyntaxException e) {
                handleInvalidPattern(e);
            }
        } else if (hasCommas || hasBrackets) {
            if (triggerType == TriggerType.TEXT) {
                this.triggerType = TriggerType.SIMPLE_REGEX;
            }
            try {
                trigger = regexify(triggerText, hasBrackets, hasCommas);
                triggerRegex = Pattern.compile(trigger);
            } catch (PatternSyntaxException e) {
                handleInvalidPattern(e);
            }
        } else {
            triggerType = TriggerType.TEXT;
            triggerRegex = null;
        }
    }

    private String regexify(String trigger, boolean hasBrackets, boolean hasCommas) {
        trigger = trigger.replaceAll("([\\\\+*?\\[\\]{}|.^$])", "\\\\$1");
        if (hasBrackets) {
            Matcher matcher = Pattern.compile("([^ ])?\\(([^)]+)\\)([^ ])?").matcher(trigger);
            if (matcher.find()) {
                if (matcher.group(1) == null) {
                    // negative lookbehind
                    trigger = matcher.replaceFirst("(?<!$2)$3");
                } else {
                    // negative lookahead
                    trigger = matcher.replaceFirst("$1(?!$2)");
                }
                // escape additional brackets
                trigger = trigger.replaceAll("\\(([^?][^)]*)\\)", "\\\\($1\\\\)");
            }
        }
        if (hasCommas) {
            trigger = trigger.replace(",", "|");
        }
        return trigger;
    }

    private void handleInvalidPattern(PatternSyntaxException e) {
        isValid = false;
        triggerRegex = null;
        patternException.clear();
        patternException.add(EnumChatFormatting.RED + "Invalid pattern:");
        String exceptionMessage = e.getLocalizedMessage();
        if (exceptionMessage == null) {
            patternException.add(EnumChatFormatting.ITALIC + "unknown cause");
        } else if (exceptionMessage.contains("\n")) {
            patternException.addAll(Arrays.asList(exceptionMessage.split("\r?\n")));
        }
    }

    /**
     * @return true, if triggerText is a valid value; false if regex is invalid
     */
    public boolean isValid() {
        return isValid;
    }

    public List<String> getPatternException() {
        return patternException;
    }

    public void toggleTriggerType() {
        if (this.triggerType == TriggerType.REGEX) {
            this.triggerType = TriggerType.TEXT;
        } else { // TEXT or SIMPLE_REGEX:
            this.triggerType = TriggerType.REGEX;
        }
    }

    public String getMiddleText() {
        return middleText;
    }

    public void setMiddleText(String middleText) {
        this.middleText = Utils.toMcColorCodes(middleText);
    }

    public DataHelper.PartyType getPartyType() {
        return partyType;
    }

    public void setPartyType(DataHelper.PartyType partyType) {
        this.partyType = partyType;
    }

    enum TriggerType {
        TEXT("Text"), SIMPLE_REGEX("Text+"), REGEX("Regex");

        private final String buttonText;

        TriggerType(String fancyString) {
            this.buttonText = fancyString;
        }

        public String toButtonText() {
            return this.buttonText;
        }
    }
}