aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pers/gwyog/gtneioreplugin/util/GT5CFGHelper.java
blob: ebc9956af1539fc7df0d20ca7e75f59d47d1889d (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
package pers.gwyog.gtneioreplugin.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import net.minecraftforge.common.config.ConfigCategory;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.config.Property;

import cpw.mods.fml.common.FMLLog;
import gregtech.api.GregTech_API;
import pers.gwyog.gtneioreplugin.GTNEIOrePlugin;

public class GT5CFGHelper {

    // Do NOT ever put a comma in this or it will break split calls later in the code. Bad, but it is what it is.
    public static final String oreVeinNotInAnyDim = "Not available in any Galactic Dim!";

    private static final File F = GregTech_API.sWorldgenFile.mConfig.getConfigFile();

    public static String GT5CFGSmallOres(String Veinname) {
        List<String> raw = new ArrayList<>();
        List<String> rawbools = new ArrayList<>();
        String st;
        Configuration c = new Configuration(F);
        ConfigCategory configCategory = c.getCategory("worldgen." + Veinname);
        for (Property p : configCategory.getOrderedValues()) {
            if (p.isBooleanValue() && p.getBoolean()) {
                raw.add(p.getName() + "=" + p.getBoolean());
            }
        }
        if (!raw.isEmpty()) {
            for (String s : raw) {
                for (int j = 0; j < DimensionHelper.DimName.length; j++) {
                    if (s.contains(DimensionHelper.DimName[j])) {
                        rawbools.add(s);
                    }
                }
            }
        } else GTNEIOrePlugin.LOG.info("Config entry not found for Vein: " + Veinname);

        StringBuilder ret = new StringBuilder(" ");

        HashSet<String> rawboolsset = new HashSet<>();
        if (!rawbools.isEmpty()) {
            for (String rawbool : rawbools) {
                st = rawbool.replace("B:", "").replace("_true", "").replace("_false", "").replaceAll(" ", "")
                        .replaceAll("\"", "");
                rawboolsset.add(st);
            }
            rawbools = new ArrayList<>(rawboolsset);
            for (int j = 0; j < DimensionHelper.DimName.length; j++) {
                for (String rawbool : rawbools) {
                    st = rawbool;
                    if (st.contains(DimensionHelper.DimName[j]) && st.contains("=true")) {
                        ret.append(DimensionHelper.DimNameDisplayed[j]).append(",");
                    }
                }
            }
        }
        ret = new StringBuilder(ret.toString().trim());
        if (ret.toString().equals("") || ret.toString().equals(" ")) {
            ret = new StringBuilder(oreVeinNotInAnyDim);
        }
        return ret.toString();
    }

    public static String GT5CFG(String Veinname) {
        // FMLLog.info(Veinname);
        if (F == null) {
            FMLLog.bigWarning("GT_CFG_NOT_found[0]");
            return "Error while Loading CFG";
        } else try {
            int buffer = (int) (0.1 * Runtime.getRuntime().freeMemory());
            if (buffer > F.length()) {
                buffer = (int) F.length();
            }
            // allocate 10% of free memory for read-in-buffer, if there is less than filesize memory available
            // FMLLog.info("GT_CFG_found[0]");
            FileReader in = new FileReader(F);
            // FMLLog.info("FileReader created");
            BufferedReader reader = new BufferedReader(in, buffer);
            // FMLLog.info("BufferedReader" +Integer.toString(buffer)+"created");
            String st;
            List<String> raw = new ArrayList<>();
            List<String> rawbools = new ArrayList<>();
            Boolean[] found = new Boolean[2];
            found[0] = false;
            found[1] = false;

            do {
                // FMLLog.info("erste");
                // read until reached eof or mix {
                st = reader.readLine();
                // FMLLog.info("st: "+st);
                if (st != null && st.trim().equals("mix {")) {
                    while (!(st == null || found[0])) {
                        // FMLLog.info("zweite");
                        st = reader.readLine();
                        // read until reached eof or Veinname {
                        // FMLLog.info("MIXst: "+st);
                        if (st != null && st.trim().equals(Veinname + " {")) {
                            // FMLLog.info("VEINNAMEst: "+st);
                            while (!(st == null || found[0])) {
                                st = reader.readLine();
                                if ((!(st == null)) && st.trim().equals("}")) {
                                    found[0] = true;
                                }
                                // FMLLog.info("dritte");
                                // add everything below Veinname { undtil } to raw
                                raw.add(st);
                            }
                        }
                    }
                }

                if (st != null && st.trim().equals("dimensions {")) {
                    while (!(st == null || found[1])) {
                        // FMLLog.info("zweite");
                        st = reader.readLine();
                        if (st != null && (st.trim().equals("mix {"))) {
                            while (!(st == null || found[1])) {
                                // FMLLog.info("dritte");
                                st = reader.readLine();
                                // read until reached eof or Veinname {
                                // FMLLog.info("MIXst: "+st);
                                if (st != null && st.trim().equals(Veinname + " {")) {
                                    // FMLLog.info("VEINNAMEst: "+st);
                                    while (!(st == null || found[1])) {
                                        st = reader.readLine();
                                        if ((!(st == null)) && st.trim().equals("}")) {
                                            found[1] = true;
                                        }
                                        // FMLLog.info("vierte");
                                        // add everything below Veinname { undtil } to raw
                                        raw.add(st);
                                    }
                                }
                            }
                        }
                    }
                }
            } while (st != null);
            reader.close(); // not needed anymore

            if (!raw.isEmpty()) {
                for (String s : raw) {
                    // filter needed booleans from raw
                    /// FMLLog.info("raw contains"+raw.get(i));
                    for (int j = 0; j < DimensionHelper.DimName.length; j++) {
                        if (s.contains(DimensionHelper.DimName[j])) {
                            rawbools.add(s);
                        }
                    }
                    // FMLLog.info("rawbools: "+rawbools.get(i));
                }
            } else {
                GTNEIOrePlugin.LOG.info("Config entry not found for Vein: " + Veinname);
            }

            StringBuilder ret = new StringBuilder(" ");

            HashSet<String> rawboolsset = new HashSet<>();
            if (!rawbools.isEmpty()) {
                // remove dublicates
                for (String rawbool : rawbools) {
                    st = rawbool.replace("B:", "").replace("_true", "").replace("_false", "").replaceAll(" ", "")
                            .replaceAll("\"", "");
                    rawboolsset.add(st);
                }
                rawbools = new ArrayList<>(rawboolsset);
                // filter for dims set to true
                for (int j = 0; j < DimensionHelper.DimName.length; j++) {
                    // FMLLog.info("RawBools:"+st);
                    for (String rawbool : rawbools) {
                        st = rawbool;
                        if (st.contains(DimensionHelper.DimName[j]) && st.contains("=true")) {
                            ret.append(DimensionHelper.DimNameDisplayed[j]).append(",");
                        }
                    }
                }
            }
            ret = new StringBuilder(ret.toString().trim());
            // FMLLog.info("ret:"+ret);
            if (ret.toString().equals("") || ret.toString().equals(" ")) {
                ret = new StringBuilder(oreVeinNotInAnyDim);
            }
            return ret.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return "Error while Loading CFG";
        }
    }
}