16
16
17
17
package net .fabricmc .loader .impl ;
18
18
19
+ import com .electronwill .nightconfig .core .UnmodifiableConfig ;
20
+ import com .mojang .logging .LogUtils ;
19
21
import net .fabricmc .loader .api .Version ;
20
22
import net .fabricmc .loader .api .metadata .*;
23
+ import net .fabricmc .loader .impl .metadata .CustomValueImpl ;
21
24
import net .fabricmc .loader .impl .metadata .SimplePerson ;
22
25
import net .neoforged .neoforgespi .language .IModInfo ;
26
+ import org .jetbrains .annotations .Nullable ;
27
+ import org .slf4j .Logger ;
23
28
24
29
import java .util .*;
30
+ import java .util .stream .Collectors ;
25
31
import java .util .stream .Stream ;
26
32
27
33
import static cpw .mods .modlauncher .api .LambdaExceptionUtils .uncheck ;
28
34
29
35
public class FMLModMetadata implements ModMetadata {
36
+ private static final Logger LOGGER = LogUtils .getLogger ();
37
+
30
38
private final IModInfo modInfo ;
31
39
private final Version version ;
32
40
private final Collection <Person > authors ;
41
+ private final Map <String , CustomValue > customValues ;
33
42
34
43
public FMLModMetadata (IModInfo modInfo ) {
35
44
this .modInfo = modInfo ;
@@ -38,6 +47,8 @@ public FMLModMetadata(IModInfo modInfo) {
38
47
.flatMap (obj -> obj instanceof List list ? ((List <String >) list ).stream () : Stream .of (obj .toString ().split ("," )))
39
48
.<Person >map (SimplePerson ::new )
40
49
.toList ();
50
+ this .customValues = this .modInfo .getModProperties ().entrySet ().stream ()
51
+ .collect (Collectors .toUnmodifiableMap (Map .Entry ::getKey , e -> convertModProperty (e .getValue ())));
41
52
}
42
53
43
54
@ Override
@@ -115,21 +126,51 @@ public Optional<String> getIconPath(int size) {
115
126
116
127
@ Override
117
128
public boolean containsCustomValue (String key ) {
118
- return false ;
129
+ return this . modInfo . getModProperties (). containsKey ( key ) ;
119
130
}
120
131
121
132
@ Override
122
133
public CustomValue getCustomValue (String key ) {
123
- return null ;
134
+ return this . customValues . get ( key ) ;
124
135
}
125
136
126
137
@ Override
127
138
public Map <String , CustomValue > getCustomValues () {
128
- return Map . of () ;
139
+ return this . customValues ;
129
140
}
130
141
131
142
@ Override
132
143
public boolean containsCustomElement (String key ) {
133
- return false ;
144
+ return containsCustomValue (key );
145
+ }
146
+
147
+ @ Nullable
148
+ private static CustomValue convertModProperty (@ Nullable Object value ) {
149
+ switch (value ) {
150
+ case null -> {
151
+ return CustomValueImpl .NULL ;
152
+ }
153
+ case UnmodifiableConfig config -> {
154
+ Map <String , CustomValue > entries = config .entrySet ().stream ()
155
+ .collect (Collectors .toMap (UnmodifiableConfig .Entry ::getKey , e -> convertModProperty (e .getValue ())));
156
+ return new CustomValueImpl .ObjectImpl (entries );
157
+ }
158
+ case ArrayList <?> list -> {
159
+ List <CustomValue > contents = list .stream ().map (FMLModMetadata ::convertModProperty ).toList ();
160
+ return new CustomValueImpl .ArrayImpl (contents );
161
+ }
162
+ case Boolean b -> {
163
+ return b ? CustomValueImpl .BOOLEAN_TRUE : CustomValueImpl .BOOLEAN_FALSE ;
164
+ }
165
+ case String str -> {
166
+ return new CustomValueImpl .StringImpl (str );
167
+ }
168
+ case Number num -> {
169
+ return new CustomValueImpl .NumberImpl (num );
170
+ }
171
+ default -> {}
172
+ }
173
+ LOGGER .warn ("Ignoring custom mod property value '{}' of unsupported type '{}'" , value , value .getClass ().getName ());
174
+ return null ;
134
175
}
135
176
}
0 commit comments