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

import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;
import de.cowtipper.cowlection.Cowlection;
import de.cowtipper.cowlection.data.DataHelper;
import de.cowtipper.cowlection.util.GsonUtils;
import net.minecraft.util.MathHelper;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Rules {
    private final List<Rule> partyFinderRules;
    private final File file;
    private final Cowlection main;

    public Rules(Cowlection main, File file) {
        this.partyFinderRules = new ArrayList<>();
        this.file = file;
        this.main = main;
        if (file.exists()) {
            loadFromFile();
        } else {
            try {
                file.createNewFile();
                addDefaultRules();
                saveToFile();
            } catch (IOException e) {
                main.getLogger().error("Couldn't create dungeons party finder rules" + this.file, e);
            }
        }
    }

    public List<Rule> getRules() {
        return partyFinderRules;
    }

    public int getCount() {
        return partyFinderRules.size();
    }

    /**
     * Add new empty rule
     */
    public void add(int slotIndex) {
        this.partyFinderRules.add(slotIndex, new Rule(true, "", Rule.TriggerType.TEXT, "", DataHelper.PartyType.SUITABLE));
    }

    public Rule remove(int ruleId) {
        Rule removedRule = partyFinderRules.remove(ruleId);
        if (removedRule != null) {
            removedRule.setEnabled(false);
        }
        return removedRule;
    }

    public boolean move(int ruleId, boolean moveDown) {
        int lastIndex = partyFinderRules.size() - 1;
        if (partyFinderRules.size() >= 2 && (moveDown && ruleId != lastIndex || !moveDown && ruleId > 0)) {
            int otherRuleId = MathHelper.clamp_int(ruleId + (moveDown ? 1 : -1), 0, lastIndex);
            if (ruleId != otherRuleId) {
                Collections.swap(partyFinderRules, ruleId, otherRuleId);
                return true;
            }
        }
        return false;
    }

    public void saveToFile() {
        partyFinderRules.stream().filter(rule -> rule.getTriggerText().isEmpty()).forEach(rule -> rule.setEnabled(false));
        try {
            String partyFinderRules = GsonUtils.toJson(this.partyFinderRules);
            FileUtils.writeStringToFile(file, partyFinderRules, StandardCharsets.UTF_8);
        } catch (IOException e) {
            main.getLogger().error("Couldn't save dungeons party finder rules" + this.file, e);
        }
    }

    public void loadFromFile() {
        this.partyFinderRules.clear();
        try {
            String ruleData = FileUtils.readFileToString(this.file, StandardCharsets.UTF_8);
            if (ruleData.length() > 0) {
                this.partyFinderRules.addAll(parseJson(ruleData));
            }
        } catch (IOException e) {
            main.getLogger().error("Couldn't read dungeons party finder rules file " + this.file, e);
        } catch (JsonParseException e) {
            main.getLogger().error("Couldn't parse dungeons party finder rules file " + this.file, e);
            try {
                Files.copy(file.toPath(), file.toPath().resolveSibling("partyfinder-rules_corrupted.json"), StandardCopyOption.REPLACE_EXISTING);
            } catch (IOException ioException) {
                main.getLogger().error("Couldn't copy the corrupted dungeons party finder rules file " + this.file, e);
            }
        }
    }

    public void resetToDefault() {
        partyFinderRules.clear();
        addDefaultRules();
    }

    private void addDefaultRules() {
        partyFinderRules.add(new Rule(false,
                "(free )carr", Rule.TriggerType.SIMPLE_REGEX,
                "§bcarry", DataHelper.PartyType.SUITABLE));
        partyFinderRules.add(new Rule(false,
                "free carr", Rule.TriggerType.TEXT,
                "§acarry", DataHelper.PartyType.SUITABLE));
        partyFinderRules.add(new Rule(true,
                "hyp", Rule.TriggerType.TEXT,
                "§ahyp", DataHelper.PartyType.SUITABLE));
        partyFinderRules.add(new Rule(false,
                "pls,plz,help", Rule.TriggerType.SIMPLE_REGEX,
                "", DataHelper.PartyType.UNIDEAL));
        partyFinderRules.add(new Rule(true,
                "speed", Rule.TriggerType.TEXT,
                "§f✦", DataHelper.PartyType.SUITABLE));
        partyFinderRules.add(new Rule(true,
                "frag", Rule.TriggerType.TEXT,
                "§ffrag", DataHelper.PartyType.SUITABLE));
        partyFinderRules.add(new Rule(true,
                "exp", Rule.TriggerType.TEXT,
                "§fxp", DataHelper.PartyType.SUITABLE));
        partyFinderRules.add(new Rule(true,
                "s+", Rule.TriggerType.TEXT,
                "§fS+", DataHelper.PartyType.SUITABLE));
    }

    private List<Rule> parseJson(String rulesData) {
        Type collectionType = new TypeToken<List<Rule>>() {
        }.getType();
        List<Rule> rules = GsonUtils.fromJson(rulesData, collectionType);
        for (Rule rule : rules) {
            rule.postConstructor();
        }
        return rules;
    }
}