Skip to content

Commit fd5d7c0

Browse files
committed
better config merging
1 parent b329ad3 commit fd5d7c0

2 files changed

Lines changed: 46 additions & 18 deletions

File tree

‎src/main/java/invtweaks/config/Category.java‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.electronwill.nightconfig.core.CommentedConfig;
44
import invtweaks.InvTweaksMod;
5-
import net.minecraft.ResourceLocationException;
65
import net.minecraft.core.registries.BuiltInRegistries;
76
import net.minecraft.resources.ResourceLocation;
87
import net.minecraft.tags.TagKey;
@@ -40,36 +39,41 @@ private static Optional<Predicate<ItemStack>> compileClause(String clause) {
4039
}
4140

4241
String[] parts = clause.split(":", 2);
42+
String argument = parts.length > 1 ? parts[1] : "";
4343
if (parts[0].equals("/tag")) {
44-
TagKey<Item> itemKey = TagKey.create(BuiltInRegistries.ITEM.key(), ResourceLocation.parse(parts[1]));
45-
TagKey<Block> blockKey = TagKey.create(BuiltInRegistries.BLOCK.key(), ResourceLocation.parse(parts[1]));
44+
ResourceLocation tagId = ResourceLocation.tryParse(argument);
45+
if (tagId == null) {
46+
InvTweaksMod.LOGGER.warn("Invalid tag id, ignoring clause: {}", clause);
47+
return Optional.empty();
48+
}
49+
TagKey<Item> itemKey = TagKey.create(BuiltInRegistries.ITEM.key(), tagId);
50+
TagKey<Block> blockKey = TagKey.create(BuiltInRegistries.BLOCK.key(), tagId);
4651

4752
return Optional.of(stack -> stack.is(itemKey) || (
4853
stack.getItem() instanceof BlockItem blockItem
4954
&& blockItem.getBlock().defaultBlockState().is(blockKey))
5055
);
5156
} else if (parts[0].equals("/instanceof") || parts[0].equals("/class")) { // use this for e.g. pickaxes
5257
try {
53-
Class<?> clazz = Class.forName(parts[1]);
58+
Class<?> clazz = Class.forName(argument);
5459
if (parts[0].equals("/instanceof")) {
5560
return Optional.of(st -> clazz.isInstance(st.getItem()));
5661
} else {
5762
return Optional.of(st -> st.getItem().getClass().equals(clazz));
5863
}
59-
} catch (ClassNotFoundException e) {
60-
InvTweaksMod.LOGGER.warn("Class not found! Ignoring clause");
64+
} catch (ClassNotFoundException | LinkageError e) {
65+
InvTweaksMod.LOGGER.warn("Class not found, ignoring clause: {}", clause);
6166
return Optional.empty();
6267
}
6368
} else if (parts[0].equals("/isFood")) {
6469
return Optional.of(stack -> stack.getFoodProperties(null) != null);
6570
} else { // default to standard item checking
66-
try {
67-
return Optional.of(
68-
st -> Objects.equals(BuiltInRegistries.ITEM.getKey(st.getItem()), ResourceLocation.parse(clause)));
69-
} catch (ResourceLocationException e) {
70-
InvTweaksMod.LOGGER.warn("Invalid item resource location found.");
71+
ResourceLocation itemId = ResourceLocation.tryParse(clause);
72+
if (itemId == null) {
73+
InvTweaksMod.LOGGER.warn("Invalid item id, ignoring clause: {}", clause);
7174
return Optional.empty();
7275
}
76+
return Optional.of(st -> itemId.equals(BuiltInRegistries.ITEM.getKey(st.getItem())));
7377
}
7478
}
7579

‎src/main/java/invtweaks/config/InvTweaksConfig.java‎

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.electronwill.nightconfig.core.file.CommentedFileConfig;
55
import com.electronwill.nightconfig.core.io.WritingMode;
66
import com.google.common.collect.ImmutableMap;
7+
import invtweaks.InvTweaksMod;
78
import invtweaks.network.PacketUpdateConfig;
89
import net.minecraft.core.registries.BuiltInRegistries;
910
import net.minecraft.util.thread.BlockableEventLoop;
@@ -19,7 +20,9 @@
1920

2021
import java.nio.file.Path;
2122
import java.util.*;
23+
import java.util.concurrent.ConcurrentHashMap;
2224
import java.util.regex.Pattern;
25+
import java.util.regex.PatternSyntaxException;
2326
import java.util.stream.Collectors;
2427

2528
@EventBusSubscriber(bus = EventBusSubscriber.Bus.MOD)
@@ -48,7 +51,7 @@ public class InvTweaksConfig {
4851
BuiltInRegistries.ITEM.getKey(Items.POISONOUS_POTATO),
4952
BuiltInRegistries.ITEM.getKey(Items.PUFFERFISH))))
5053
.put("torch", new Category(BuiltInRegistries.ITEM.getKey(Items.TORCH).toString()))
51-
.put("cheapBlocks", new Category("/tag:forge:cobblestone", "/tag:minecraft:dirt"))
54+
.put("cheapBlocks", new Category("/tag:c:cobblestones", "/tag:minecraft:dirt"))
5255
.put("blocks", new Category("/instanceof:net.minecraft.world.item.BlockItem"))
5356
.build();
5457
public static final List<String> DEFAULT_RAW_RULES = Arrays.asList("D /LOCKED", "A1-C9 /OTHER");
@@ -77,7 +80,7 @@ public class InvTweaksConfig {
7780
.put("cy.jdkdigital.productivebees.container.gui.*", new ContOverride(NO_POS_OVERRIDE, NO_POS_OVERRIDE, ""))
7881
.put("cy.jdkdigital.productivetrees.inventory.screen.*", new ContOverride(NO_POS_OVERRIDE, NO_POS_OVERRIDE, ""))
7982
.put("com.stal111.forbidden_arcanus.client.gui.screen.*", new ContOverride(NO_POS_OVERRIDE, NO_POS_OVERRIDE, ""))
80-
.put("aztech.modern_industrialization.*.gui.*Screen.java", new ContOverride(NO_POS_OVERRIDE, NO_POS_OVERRIDE, ""))
83+
.put("aztech.modern_industrialization.*.gui.*Screen", new ContOverride(NO_POS_OVERRIDE, NO_POS_OVERRIDE, ""))
8184
.put("net.pedroksl.advanced_ae.client.gui.*", new ContOverride(NO_POS_OVERRIDE, NO_POS_OVERRIDE, ""))
8285
.put("tv.soaryn.xycraft.*", new ContOverride(NO_POS_OVERRIDE, NO_POS_OVERRIDE, ""))
8386
.put("com.mrbysco.forcecraft.menu.*", new ContOverride(NO_POS_OVERRIDE, NO_POS_OVERRIDE, ""))
@@ -103,6 +106,7 @@ public class InvTweaksConfig {
103106
private static Ruleset COMPILED_RULES = DEFAULT_RULES;
104107
public static Map<String, ContOverride> IMS_CONT_OVERRIDES = new HashMap<>();
105108
private static Map<String, ContOverride> COMPILED_CONT_OVERRIDES = DEFAULT_CONT_OVERRIDES;
109+
private static final Map<String, Optional<Pattern>> OVERRIDE_PATTERNS = new ConcurrentHashMap<>();
106110
private static boolean isDirty = false;
107111

108112
static {
@@ -165,6 +169,7 @@ public class InvTweaksConfig {
165169
"sortRange = slots to sort",
166170
"E.g. sortRange = \"5,0-2\" sorts slots 5,0,1,2 in that order",
167171
"sortRange = \"\" disables sorting for that container",
172+
"Built-in entries come back if deleted. To turn one off, keep its containerClass and remove its sortRange line",
168173
"Out-of-bound slots are ignored",
169174
"Omit sortRange to leave as default")
170175
.defineList(
@@ -180,7 +185,7 @@ public class InvTweaksConfig {
180185
{
181186
builder.comment("Tweaks").push("tweaks");
182187

183-
ENABLE_DEBUG = builder.comment("Enable debug will log the name of the screens being opened").define("enableDebug", false);
188+
ENABLE_DEBUG = builder.comment("Log the screen and menu class names whenever a GUI opens, for use in containerOverrides").define("enableDebug", false);
184189
ENABLE_AUTOREFILL = builder.comment("Enable auto-refill").define("autoRefill", true);
185190
ENABLE_QUICKVIEW =
186191
builder
@@ -294,6 +299,13 @@ public static void setPlayerContOverrides(Player ent, Map<String, ContOverride>
294299
playerToContOverrides.put(ent.getUUID(), val);
295300
}
296301

302+
public static void clearPlayer(Player ent) {
303+
playerToCats.remove(ent.getUUID());
304+
playerToRules.remove(ent.getUUID());
305+
playerAutoRefill.remove(ent.getUUID());
306+
playerToContOverrides.remove(ent.getUUID());
307+
}
308+
297309
public static Map<String, Category> getPlayerCats(Player ent) {
298310
if (FMLEnvironment.dist.isClient()) {
299311
return getSelfCompiledCats();
@@ -326,15 +338,26 @@ public static ContOverride getPlayerContOverride(Player ent, String screenClass,
326338
if (map.containsKey(contClass)) {
327339
return map.get(contClass);
328340
}
329-
for (String s : map.keySet()) {
330-
var regex = Pattern.compile(s);
331-
if (regex.matcher(screenClass).matches() || regex.matcher(contClass).matches()) {
332-
return map.get(s);
341+
for (Map.Entry<String, ContOverride> entry : map.entrySet()) {
342+
Optional<Pattern> pattern = overridePattern(entry.getKey());
343+
if (pattern.isPresent() && (pattern.get().matcher(screenClass).matches() || pattern.get().matcher(contClass).matches())) {
344+
return entry.getValue();
333345
}
334346
}
335347
return null;
336348
}
337349

350+
private static Optional<Pattern> overridePattern(String key) {
351+
return OVERRIDE_PATTERNS.computeIfAbsent(key, k -> {
352+
try {
353+
return Optional.of(Pattern.compile(k));
354+
} catch (PatternSyntaxException e) {
355+
InvTweaksMod.LOGGER.warn("Invalid container override pattern, only the exact class name will match: {}", k);
356+
return Optional.empty();
357+
}
358+
});
359+
}
360+
338361
public static boolean isSortEnabled(boolean isPlayerSort) {
339362
return isFlagEnabled(ENABLE_SORT.get(), isPlayerSort);
340363
}
@@ -377,6 +400,7 @@ public static Map<String, ContOverride> cfgToCompiledContOverrides(List<Unmodifi
377400
subCfg.getOrElse("y", NO_POS_OVERRIDE),
378401
subCfg.getOrElse("sortRange", NO_SPEC_OVERRIDE)));
379402
}
403+
DEFAULT_CONT_OVERRIDES.forEach(res::putIfAbsent);
380404
return res;
381405
}
382406
}

0 commit comments

Comments
 (0)