/
ClientConfig.java
250 lines (213 loc) · 7.45 KB
/
ClientConfig.java
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
240
241
242
243
244
245
246
247
248
249
250
package mezz.jei.config;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import mezz.jei.Internal;
import mezz.jei.color.ColorGetter;
import mezz.jei.color.ColorNamer;
import mezz.jei.ingredients.IngredientSortStage;
import mezz.jei.util.GiveMode;
import net.minecraftforge.common.ForgeConfigSpec;
import org.apache.commons.lang3.EnumUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public final class ClientConfig implements IJEIConfig, IClientConfig {
private static final Logger LOGGER = LogManager.getLogger();
@Nullable
private static IClientConfig instance;
private static final int minNumColumns = 4;
private static final int defaultNumColumns = 9;
private static final int largestNumColumns = 100;
private static final int minRecipeGuiHeight = 175;
private static final int defaultRecipeGuiHeight = 350;
private static final GiveMode defaultGiveMode = GiveMode.MOUSE_PICKUP;
private static final boolean defaultCenterSearchBar = false;
public static final List<IngredientSortStage> ingredientSorterStagesDefault = Arrays.asList(
IngredientSortStage.MOD_NAME,
IngredientSortStage.INGREDIENT_TYPE,
IngredientSortStage.CREATIVE_MENU,
IngredientSortStage.ALPHABETICAL,
IngredientSortStage.WEAPON_DAMAGE,
IngredientSortStage.TOOL_TYPE,
IngredientSortStage.ARMOR,
IngredientSortStage.TAG
);
private List<IngredientSortStage> ingredientSorterStages = ingredientSorterStagesDefault;
// Forge config
private final ForgeConfigSpec.BooleanValue debugModeEnabled;
private final ForgeConfigSpec.BooleanValue centerSearchBarEnabled;
private final ForgeConfigSpec.BooleanValue lowMemorySlowSearchEnabled;
private final ForgeConfigSpec.EnumValue<GiveMode> giveMode;
private final ForgeConfigSpec.IntValue maxColumns;
private final ForgeConfigSpec.IntValue maxRecipeGuiHeight;
private final ForgeConfigSpec.ConfigValue<List<? extends String>> searchColorsCfg;
private final ForgeConfigSpec.ConfigValue<List<? extends String>> ingredientSorterStagesCfg;
public ClientConfig(ForgeConfigSpec.Builder builder) {
instance = this;
builder.push("advanced");
{
builder.comment("Debug mode enabled");
debugModeEnabled = builder.define("DebugMode", false);
builder.comment("Display search bar in the center");
centerSearchBarEnabled = builder.define("CenterSearch", defaultCenterSearchBar);
builder.comment("Set low-memory mode (makes search very slow, but uses less RAM)");
lowMemorySlowSearchEnabled = builder.define("LowMemorySlowSearchEnabled", false);
builder.comment("How items should be handed to you");
giveMode = builder.defineEnum("GiveMode", defaultGiveMode);
builder.comment("Max number of columns shown");
maxColumns = builder.defineInRange("MaxColumns", defaultNumColumns, minNumColumns, largestNumColumns);
builder.comment("Max. recipe gui height");
maxRecipeGuiHeight = builder.defineInRange("RecipeGuiHeight", defaultRecipeGuiHeight, minRecipeGuiHeight, Integer.MAX_VALUE);
}
builder.pop();
builder.push("colors");
{
builder.comment("Color values to search for");
searchColorsCfg = builder.defineList("SearchColors", Lists.newArrayList(ColorGetter.getColorDefaults()), ClientConfig::validSearchColor);
}
builder.pop();
builder.push("sorting");
{
builder.comment(String.format("Sorting order for the ingredient list. Valid stages: %s", Arrays.asList(IngredientSortStage.values())));
List<String> defaults = ingredientSorterStagesDefault.stream()
.map(Enum::name)
.collect(Collectors.toList());
Predicate<Object> elementValidator = validEnumElement(IngredientSortStage.class);
ingredientSorterStagesCfg = builder.defineList("IngredientSortStages", defaults, elementValidator);
}
builder.pop();
}
@Deprecated
public static IClientConfig getInstance() {
Preconditions.checkNotNull(instance);
return instance;
}
@Override
public void reload() {
this.ingredientSorterStages = ingredientSorterStagesCfg.get()
.stream()
.map(s -> EnumUtils.getEnum(IngredientSortStage.class, s))
.filter(Objects::nonNull)
.collect(Collectors.toList());
if (ingredientSorterStages.isEmpty()) {
this.ingredientSorterStages = ingredientSorterStagesDefault;
}
syncSearchColorsConfig();
}
@Override
public boolean isDebugModeEnabled() {
return debugModeEnabled.get();
}
@Override
public boolean isCenterSearchBarEnabled() {
return centerSearchBarEnabled.get();
}
@Override
public boolean isLowMemorySlowSearchEnabled() {
return lowMemorySlowSearchEnabled.get();
}
@Override
public GiveMode getGiveMode() {
return giveMode.get();
}
@Override
public int getMinColumns() {
return minNumColumns;
}
@Override
public int getMaxColumns() {
return maxColumns.get();
}
@Override
public int getMaxRecipeGuiHeight() {
return maxRecipeGuiHeight.get();
}
@Override
public List<IngredientSortStage> getIngredientSorterStages() {
return ingredientSorterStages;
}
private void syncSearchColorsConfig() {
final ImmutableMap.Builder<Integer, String> searchColorsMapBuilder = ImmutableMap.builder();
List<? extends String> searchColors = searchColorsCfg.get();
for (String entry : searchColors) {
try {
Map.Entry<Integer, String> result = parseSearchColor(entry);
if (result == null) {
LOGGER.error("Invalid number format for searchColor entry: {}", entry);
} else {
searchColorsMapBuilder.put(result);
}
} catch (NumberFormatException e) {
LOGGER.error("Invalid number format for searchColor entry: {}", entry, e);
}
}
final ColorNamer colorNamer = new ColorNamer(searchColorsMapBuilder.build());
Internal.setColorNamer(colorNamer);
}
@SuppressWarnings("SameParameterValue")
private static Predicate<Object> validEnumElement(Class<? extends Enum<?>> enumClass) {
Set<String> validEntries = new HashSet<>();
Enum<?>[] enumConstants = enumClass.getEnumConstants();
for (Enum<?> enumConstant : enumConstants) {
String name = enumConstant.name();
validEntries.add(name);
}
return obj -> {
if (obj instanceof String) {
String name = (String) obj;
return validEntries.contains(name);
}
return false;
};
}
@Nullable
private static Map.Entry<Integer, String> parseSearchColor(Object obj) throws NumberFormatException {
if (obj instanceof String) {
String entry = (String) obj;
String[] values = entry.split(":");
if (values.length == 2) {
String name = values[0];
Integer color = Integer.decode("0x" + values[1]);
return new ColorEntry(color, name);
}
}
return null;
}
private static boolean validSearchColor(Object obj) {
try {
Map.Entry<Integer, String> result = parseSearchColor(obj);
return result != null;
} catch (NumberFormatException ignored) {
return false;
}
}
private static class ColorEntry implements Map.Entry<Integer, String> {
private final Integer color;
private final String name;
public ColorEntry(Integer color, String name) {
this.color = color;
this.name = name;
}
@Override
public Integer getKey() {
return color;
}
@Override
public String getValue() {
return name;
}
@Override
public String setValue(String value) {
throw new UnsupportedOperationException();
}
}
}