44import com .electronwill .nightconfig .core .file .CommentedFileConfig ;
55import com .electronwill .nightconfig .core .io .WritingMode ;
66import com .google .common .collect .ImmutableMap ;
7+ import invtweaks .InvTweaksMod ;
78import invtweaks .network .PacketUpdateConfig ;
89import net .minecraft .core .registries .BuiltInRegistries ;
910import net .minecraft .util .thread .BlockableEventLoop ;
1920
2021import java .nio .file .Path ;
2122import java .util .*;
23+ import java .util .concurrent .ConcurrentHashMap ;
2224import java .util .regex .Pattern ;
25+ import java .util .regex .PatternSyntaxException ;
2326import 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