Skip to content

Commit 267c75b

Browse files
committed
Actually use validation functions.
1 parent dad0c7e commit 267c75b

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed

fabric/src/main/java/noobanidus/mods/lootr/fabric/config/ConfigManager.java

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import noobanidus.mods.lootr.common.api.registry.LootrRegistry;
2727

2828
import java.util.*;
29+
import java.util.function.Function;
2930
import java.util.stream.Collectors;
3031

3132
@Config(name = LootrAPI.MODID)
@@ -97,7 +98,7 @@ public static void reset() {
9798
REFRESH_TABLES = null;
9899
}
99100

100-
private static Set<String> validateStringList(List<String> incomingList, String listKey) {
101+
private static Set<String> validateStringList(Collection<String> incomingList, String listKey) {
101102
Set<String> validatedList = new HashSet<>();
102103
for (String entry : incomingList) {
103104
if (entry == null || entry.isEmpty()) {
@@ -109,29 +110,40 @@ private static Set<String> validateStringList(List<String> incomingList, String
109110
return validatedList;
110111
}
111112

112-
private static Set<ResourceKey<Level>> validateDimensions(List<String> incomingList, String listKey) {
113-
Set<ResourceKey<Level>> validatedList = new HashSet<>();
113+
private static Set<ResourceKey<Level>> validateDimensions(Collection<String> incomingList, String listKey) {
114+
return validateResourceKeyList(incomingList, listKey, o -> ResourceKey.create(Registries.DIMENSION, o));
115+
}
116+
117+
private static <T> Set<ResourceKey<T>> validateResourceKeyList (Collection<String> incomingList, String listKey, Function<ResourceLocation, ResourceKey<T>> builder) {
118+
Set<ResourceKey<T>> validatedList = new HashSet<>();
114119
for (String entry : incomingList) {
115120
if (entry == null || entry.isEmpty()) {
116-
throw new RuntimeException("Error found when validating a configuration list for '" + listKey + "'. One of the entries is null or empty and cannot be converted to a dimension identifier.");
121+
throw new RuntimeException("Error found when validating a configuration list for '" + listKey + "'. One of the entries is null or empty and cannot be converted to a ResourceLocation.");
122+
}
123+
ResourceLocation location;
124+
try {
125+
location = ResourceLocation.parse(entry);
126+
} catch (Exception e) {
127+
throw new RuntimeException("Error found when validating a configuration list for '" + listKey + "'. The value found in the list, '" + entry + "', is not a valid ResourceLocation.", e);
117128
}
129+
118130
try {
119-
validatedList.add(ResourceKey.create(Registries.DIMENSION, ResourceLocation.withDefaultNamespace(entry)));
131+
validatedList.add(builder.apply(location));
120132
} catch (Exception e) {
121-
throw new RuntimeException("Error found when validating a configuration list for '" + listKey + "'. The value found in the list, '" + entry + "', is not a valid dimension identifier.", e);
133+
throw new RuntimeException("Error found when validating a configuration list for '" + listKey + "'. The value found in the list, '" + entry + "', is not valid to create a ResourceKey.", e);
122134
}
123135
}
124136
return validatedList;
125137
}
126138

127-
private static Set<ResourceLocation> validateResourceLocationList(List<String> incomingList, String listKey) {
139+
private static Set<ResourceLocation> validateResourceLocationList(Collection<String> incomingList, String listKey) {
128140
Set<ResourceLocation> validatedList = new HashSet<>();
129141
for (String entry : incomingList) {
130142
if (entry == null || entry.isEmpty()) {
131143
throw new RuntimeException("Error found when validating a configuration list for '" + listKey + "'. One of the entries is null or empty and cannot be converted to a ResourceLocation.");
132144
}
133145
try {
134-
validatedList.add(ResourceLocation.withDefaultNamespace(entry));
146+
validatedList.add(ResourceLocation.parse(entry));
135147
} catch (Exception e) {
136148
throw new RuntimeException("Error found when validating a configuration list for '" + listKey + "'. The value found in the list, '" + entry + "', is not a valid ResourceLocation.", e);
137149
}
@@ -145,49 +157,49 @@ public static ConfigManager get() {
145157

146158
public static Set<ResourceKey<Level>> getDimensionWhitelist() {
147159
if (DIM_WHITELIST == null) {
148-
DIM_WHITELIST = get().lists.dimension_whitelist.stream().map(o -> ResourceKey.create(Registries.DIMENSION, ResourceLocation.parse(o))).collect(Collectors.toSet());
160+
DIM_WHITELIST = validateDimensions(get().lists.dimension_whitelist, "dimension_whitelist");
149161
}
150162
return DIM_WHITELIST;
151163
}
152164

153165
public static Set<String> getDimensionModidWhitelist() {
154166
if (MODID_DIM_WHITELIST == null) {
155-
MODID_DIM_WHITELIST = get().lists.modid_dimension_whitelist.stream().map(o -> o.toLowerCase(Locale.ROOT)).collect(Collectors.toSet());
167+
MODID_DIM_WHITELIST = validateStringList(get().lists.modid_dimension_whitelist, "modid_dimension_whitelist");
156168
}
157169
return MODID_DIM_WHITELIST;
158170
}
159171

160172
public static Set<ResourceKey<Level>> getDimensionBlacklist() {
161173
if (DIM_BLACKLIST == null) {
162-
DIM_BLACKLIST = get().lists.dimension_blacklist.stream().map(o -> ResourceKey.create(Registries.DIMENSION, ResourceLocation.parse(o))).collect(Collectors.toSet());
174+
DIM_BLACKLIST = validateDimensions(get().lists.dimension_blacklist, "dimension_blacklist");
163175
}
164176
return DIM_BLACKLIST;
165177
}
166178

167179
public static Set<String> getDimensionModidBlacklist() {
168180
if (MODID_DIM_BLACKLIST == null) {
169-
MODID_DIM_BLACKLIST = get().lists.modid_dimension_blacklist.stream().map(o -> o.toLowerCase(Locale.ROOT)).collect(Collectors.toSet());
181+
MODID_DIM_BLACKLIST = validateStringList(get().lists.modid_dimension_blacklist, "modid_dimension_blacklist");
170182
}
171183
return MODID_DIM_BLACKLIST;
172184
}
173185

174186
public static Set<ResourceKey<Level>> getDecayDimensions() {
175187
if (DECAY_DIMS == null) {
176-
DECAY_DIMS = get().decay.decay_dimensions.stream().map(o -> ResourceKey.create(Registries.DIMENSION, ResourceLocation.parse(o))).collect(Collectors.toSet());
188+
DECAY_DIMS = validateDimensions(get().decay.decay_dimensions, "decay_dimensions");
177189
}
178190
return DECAY_DIMS;
179191
}
180192

181193
public static Set<ResourceKey<Level>> getRefreshDimensions() {
182194
if (REFRESH_DIMS == null) {
183-
REFRESH_DIMS = get().refresh.refresh_dimensions.stream().map(o -> ResourceKey.create(Registries.DIMENSION, ResourceLocation.parse(o))).collect(Collectors.toSet());
195+
REFRESH_DIMS = validateDimensions(get().refresh.refresh_dimensions, "refresh_dimensions");
184196
}
185197
return REFRESH_DIMS;
186198
}
187199

188200
public static Set<ResourceKey<LootTable>> getLootBlacklist() {
189201
if (LOOT_BLACKLIST == null) {
190-
LOOT_BLACKLIST = get().lists.loot_table_blacklist.stream().map(o -> ResourceKey.create(Registries.LOOT_TABLE, ResourceLocation.parse(o))).collect(Collectors.toSet());
202+
LOOT_BLACKLIST = validateResourceKeyList(get().lists.loot_table_blacklist, "loot_blacklist", o -> ResourceKey.create(Registries.LOOT_TABLE, o));
191203
// Fixes for #79 and #74
192204
PROBLEMATIC_CHESTS.forEach(o -> LOOT_BLACKLIST.add(ResourceKey.create(Registries.LOOT_TABLE, o)));
193205
}
@@ -196,7 +208,7 @@ public static Set<ResourceKey<LootTable>> getLootBlacklist() {
196208

197209
public static Set<String> getLootModidsBlacklist() {
198210
if (LOOT_MODIDS == null) {
199-
LOOT_MODIDS = get().lists.loot_modid_blacklist.stream().map(o -> o.toLowerCase(Locale.ROOT)).collect(Collectors.toSet());
211+
LOOT_MODIDS = validateStringList(get().lists.loot_modid_blacklist, "loot_modid_blacklist");
200212
}
201213
return LOOT_MODIDS;
202214
}
@@ -211,28 +223,28 @@ public static boolean isBlacklisted(ResourceKey<LootTable> table) {
211223

212224
public static Set<ResourceKey<LootTable>> getDecayingTables() {
213225
if (DECAY_TABLES == null) {
214-
DECAY_TABLES = get().decay.decay_loot_tables.stream().map(o -> ResourceKey.create(Registries.LOOT_TABLE, ResourceLocation.parse(o))).collect(Collectors.toSet());
226+
DECAY_TABLES = validateResourceKeyList(get().decay.decay_loot_tables, "decay_loot_tables", o -> ResourceKey.create(Registries.LOOT_TABLE, o));
215227
}
216228
return DECAY_TABLES;
217229
}
218230

219231
public static Set<String> getDecayMods() {
220232
if (DECAY_MODS == null) {
221-
DECAY_MODS = get().decay.decay_modids.stream().map(o -> o.toLowerCase(Locale.ROOT)).collect(Collectors.toSet());
233+
DECAY_MODS = validateStringList(get().decay.decay_modids, "decay_mods");
222234
}
223235
return DECAY_MODS;
224236
}
225237

226238
public static Set<ResourceKey<LootTable>> getRefreshingTables() {
227239
if (REFRESH_TABLES == null) {
228-
REFRESH_TABLES = get().refresh.refresh_loot_tables.stream().map(o -> ResourceKey.create(Registries.LOOT_TABLE, ResourceLocation.parse(o))).collect(Collectors.toSet());
240+
REFRESH_TABLES = validateResourceKeyList(get().refresh.refresh_loot_tables, "refresh_tables", o -> ResourceKey.create(Registries.LOOT_TABLE, o));
229241
}
230242
return REFRESH_TABLES;
231243
}
232244

233245
public static Set<String> getRefreshMods() {
234246
if (REFRESH_MODS == null) {
235-
REFRESH_MODS = get().refresh.refresh_modids.stream().map(o -> o.toLowerCase(Locale.ROOT)).collect(Collectors.toSet());
247+
REFRESH_MODS = validateStringList(get().refresh.refresh_modids, "refresh_modids");
236248
}
237249
return REFRESH_MODS;
238250
}

0 commit comments

Comments
 (0)