1111import java .lang .reflect .Method ;
1212import java .util .Collections ;
1313import java .util .Map ;
14+ import java .util .function .Supplier ;
1415
1516public final class ConfigHandler implements InvocationHandler
1617{
@@ -37,6 +38,31 @@ private String path(String key)
3738 return (path .isEmpty () ? "" : (path + "." )) + key ;
3839 }
3940
41+ private Supplier <Object > loadValue (Object proxy , Method method , String path )
42+ {
43+ ModConfigSpec .ConfigValue <?> configValue = spec .getValues ().get (path );
44+ if (manager .codecs ().has (method .getReturnType ()))
45+ {
46+ var codec = manager .codecs ().get (method .getReturnType ());
47+ return () -> codec .parse (TomlOps .INSTANCE , configValue .get ()).mapOrElse (
48+ (result ) -> result ,
49+ (error ) ->
50+ {
51+ Tesseract .LOGGER .error ("Failed to parse input value: {}" , error .message ());
52+ try
53+ {
54+ return InvocationHandler .invokeDefault (proxy , method );
55+ }
56+ catch (Throwable ex )
57+ {
58+ throw new RuntimeException (ex );
59+ }
60+ }
61+ );
62+ }
63+ return configValue ::get ;
64+ }
65+
4066 void loadValues (Class <?> configClass , Object proxy )
4167 {
4268 Map <String , Object > values = Maps .newHashMap ();
@@ -56,27 +82,7 @@ void loadValues(Class<?> configClass, Object proxy)
5682 }
5783 else
5884 {
59- ModConfigSpec .ConfigValue <?> configValue = spec .getValues ().get (path );
60- value = configValue .get ();
61- if (manager .codecs ().has (method .getReturnType ()))
62- {
63- var codec = manager .codecs ().get (method .getReturnType ());
64- value = codec .parse (TomlOps .INSTANCE , value ).mapOrElse (
65- (result ) -> result ,
66- (error ) ->
67- {
68- Tesseract .LOGGER .error ("Failed to parse input value: {}" , error .message ());
69- try
70- {
71- return InvocationHandler .invokeDefault (proxy , method );
72- }
73- catch (Throwable ex )
74- {
75- throw new RuntimeException (ex );
76- }
77- }
78- );
79- }
85+ value = this .loadValue (proxy , method , path );
8086 }
8187 values .put (method .getName (), value );
8288 }
@@ -89,6 +95,14 @@ void loadValues(Class<?> configClass, Object proxy)
8995 public Object invoke (Object proxy , Method method , Object [] args ) throws Throwable
9096 {
9197 var value = values .get (method .getName ());
92- return value instanceof ConfigInstance <?> config ? config .config () : value ;
98+ if (value instanceof ConfigInstance <?> config )
99+ {
100+ return config .config ();
101+ }
102+ else if (value instanceof Supplier <?> supplier )
103+ {
104+ return supplier .get ();
105+ }
106+ return value ;
93107 }
94108}
0 commit comments