Skip to content

Commit e91eff0

Browse files
committed
update example for ConfigAPI
1 parent 7654309 commit e91eff0

10 files changed

Lines changed: 283 additions & 50 deletions

File tree

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
{
2+
"type" : "rml:group",
3+
"name" : "test_config2",
4+
"elements" : [
5+
{
6+
"type" : "rml:group",
7+
"name" : "client",
8+
"elements": [
9+
{
10+
"type" : "rml:int",
11+
"name" : "integer",
12+
"defaultValue" : 114514
13+
},
14+
{
15+
"type" : "rml:ints",
16+
"name" : "integerArray",
17+
"defaultValue" : 0
18+
},
19+
{
20+
"type" : "rml:ranged_int",
21+
"name" : "rangedInteger",
22+
"defaultValue" : "0",
23+
"min" : -10,
24+
"max" : 10
25+
},
26+
{
27+
"type" : "rml:ranged_int",
28+
"name" : "rangedInteger",
29+
"defaultValue" : "0",
30+
"min" : -10,
31+
"max" : 10
32+
},
33+
{
34+
"type" : "rml:double",
35+
"name" : "double",
36+
"defaultValue" : 0.0
37+
},
38+
{
39+
"type" : "rml:doubles",
40+
"name" : "doubles",
41+
"defaultValue" : 0.0
42+
},
43+
{
44+
"type" : "rml:ranged_double",
45+
"name" : "rangedDouble",
46+
"defaultValue" : 0.0
47+
},
48+
{
49+
"type" : "rml:strings",
50+
"name" : "stringArray",
51+
"defaultValue" : "value0"
52+
},
53+
{
54+
"type" : "rml:bool",
55+
"name" : "bool",
56+
"defaultValue" : false
57+
},
58+
{
59+
"type" : "rml:bools",
60+
"name" : "bools",
61+
"defaultValue" : [false, true, true]
62+
},
63+
{
64+
"type" : "rml:enum",
65+
"name" : "enum",
66+
"defaultValue" : "ONE",
67+
"enums" : "ONE"
68+
},
69+
{
70+
"type" : "rml:group",
71+
"name" : "mapping",
72+
"elements": [
73+
{
74+
"type" : "rml:map",
75+
"name" : "booleanArrayMap",
76+
"map_type" : "BOOLS",
77+
"defaultValue": {
78+
"name" : [true]
79+
}
80+
},
81+
{
82+
"type" : "rml:map",
83+
"name" : "doubleArrayMap",
84+
"map_type" : "DOUBLES",
85+
"defaultValue": {
86+
"name" : 0
87+
}
88+
},
89+
{
90+
"type" : "rml:map",
91+
"name" : "stringArrayMap",
92+
"map_type" : "STRINGS",
93+
"defaultValue": {
94+
"name" : "0"
95+
}
96+
},
97+
{
98+
"type" : "rml:map",
99+
"name" : "intArrayMap",
100+
"map_type" : "INTS",
101+
"defaultValue": {
102+
"name" : 0
103+
}
104+
},
105+
{
106+
"type" : "rml:map",
107+
"name" : "intMap",
108+
"map_type" : "INT"
109+
},
110+
{
111+
"type" : "rml:map",
112+
"name" : "boolMap",
113+
"map_type" : "BOOL"
114+
},
115+
{
116+
"type" : "rml:map",
117+
"name" : "stringMap",
118+
"map_type" : "STRING"
119+
},
120+
{
121+
"type" : "rml:map",
122+
"name" : "doubleMap",
123+
"map_type" : "DOUBLE"
124+
}
125+
]
126+
}
127+
]
128+
},
129+
{
130+
"type" : "rml:group",
131+
"name" : "server",
132+
"elements": [
133+
{
134+
"type" : "rml:group",
135+
"name" : "testSub",
136+
"elements" : [
137+
{
138+
"type" : "rml:ranged_double",
139+
"name" : "rangedDouble",
140+
"defaultValue" : 0,
141+
"min" : -1,
142+
"max" : 1,
143+
"display" : {
144+
"requiresMcRestart": true,
145+
"requiresWorldRestart": true,
146+
"sliding": true,
147+
"displayName" : "Display Name",
148+
"langKey": "config.test_config.server.testSub.test.desc",
149+
"comment": "hello comments"
150+
}
151+
}
152+
]
153+
}
154+
]
155+
},
156+
{
157+
"type" : "rml:string",
158+
"name" : "at_root",
159+
"defaultValue" : "root"
160+
}
161+
]
162+
}

dev/example/rml.info

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@
6969
"name":"rml:MOD_GROOVY_SCRIPT",
7070
"location":"mods/groovy_script/run_config.json",
7171
"forceLoaded":true
72-
}
72+
},
73+
{
74+
"name" : "rml:config_nodes",
75+
"location":"mods/forge/config/nodes/config.json",
76+
"forceLoaded":true
77+
}
7378
]
7479
},
7580
{

src/main/java/rml/layer/compat/crt/RMLCrTLoader.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,6 @@ public static IScriptProvider getScriptProviders(){
6565
}
6666

6767
@PrivateAPI public static void registerWrappers(CrTZenClassRegisterEvent event) {
68-
event.register(ConfigUtils.class);
69-
event.register(ConfigBoolean.class);
70-
event.register(ConfigBooleanArray.class);
71-
event.register(ConfigGroup.class);
72-
event.register(ConfigDouble.class);
73-
event.register(ConfigDoubleArray.class);
74-
event.register(ConfigElement.class);
75-
event.register(ConfigEnum.class);
76-
event.register(ConfigInt.class);
77-
event.register(ConfigIntArray.class);
78-
event.register(ConfigMap.class);
79-
event.register(ConfigPrimitive.class);
80-
event.register(ConfigRangedDouble.class);
81-
event.register(ConfigRangedInt.class);
82-
event.register(ConfigString.class);
83-
event.register(ConfigStringArray.class);
84-
event.register(ConfigMap.HashDataMap.class);
68+
8569
}
8670
}

src/main/java/rml/loader/ResourceModLoader.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,25 @@ public static void loadModule(ModuleType module, ContainerHolder.ModuleConsumer
9696
public static void loadModuleFindAssets(ModuleType module, FileHelper.ModFileConsumer consumer){
9797
Set<ContainerHolder> containerHolders = RMLModuleLoadingEvent.post(getCurrentRMLContainerHolders(module), module);
9898
for(ContainerHolder containerHolder : containerHolders){
99-
ModContainer oldActive = Loader.instance().activeModContainer();
100-
Loader.instance().setActiveModContainer(containerHolder.container);
101-
FileHelper.findAssets(containerHolder, containerHolder.modules.get(module), consumer);
102-
Loader.instance().setActiveModContainer(oldActive);
99+
if (containerHolder.hasModule(module)) {
100+
ModContainer oldActive = Loader.instance().activeModContainer();
101+
Loader.instance().setActiveModContainer(containerHolder.container);
102+
FileHelper.findAssets(containerHolder, containerHolder.modules.get(module), consumer);
103+
Loader.instance().setActiveModContainer(oldActive);
104+
}
103105
}
104106
}
105107

106108
public static void loadModuleFindAssets(ModuleType module, ContainerHolder.ModuleConsumer moduleConsumer, FileHelper.ModFileConsumer consumer){
107109
Set<ContainerHolder> containerHolders = RMLModuleLoadingEvent.post(getCurrentRMLContainerHolders(module), module);
108110
for(ContainerHolder containerHolder : containerHolders){
109-
ModContainer oldActive = Loader.instance().activeModContainer();
110-
Loader.instance().setActiveModContainer(containerHolder.container);
111-
moduleConsumer.accept(module, containerHolder);
112-
FileHelper.findAssets(containerHolder, containerHolder.modules.get(module), consumer);
113-
Loader.instance().setActiveModContainer(oldActive);
111+
if (containerHolder.hasModule(module)) {
112+
ModContainer oldActive = Loader.instance().activeModContainer();
113+
Loader.instance().setActiveModContainer(containerHolder.container);
114+
moduleConsumer.accept(module, containerHolder);
115+
FileHelper.findAssets(containerHolder, containerHolder.modules.get(module), consumer);
116+
Loader.instance().setActiveModContainer(oldActive);
117+
}
114118
}
115119
}
116120

src/main/java/rml/loader/api/config/v2/config/elements/ConfigMap.java

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,87 @@
11
package rml.loader.api.config.v2.config.elements;
22

3+
import net.minecraft.util.ResourceLocation;
34
import org.objectweb.asm.MethodVisitor;
45
import org.objectweb.asm.Opcodes;
56
import org.objectweb.asm.Type;
6-
7-
import rml.loader.api.config.v2.config.ConfigUtils;
7+
import rml.deserializer.*;
8+
import rml.loader.api.utils.ObjectHelper;
9+
import rml.loader.deserialize.Deserializer;
810

911
import java.util.HashMap;
10-
import java.util.List;
1112
import java.util.Map;
1213
import java.util.function.BiConsumer;
1314

1415
public class ConfigMap extends ConfigPrimitive {
16+
public enum ConfigMapType{
17+
INT(Integer.class),
18+
DOUBLE(Double.class),
19+
STRING(String.class),
20+
BOOL(Boolean.class),
21+
INTS(Integer[].class),
22+
DOUBLES(Double[].class),
23+
STRINGS(String[].class),
24+
BOOLS(Boolean[].class)
25+
;
26+
27+
public static final AbstractDeserializer<ConfigMapType> DESERIALIZER = Deserializer.MANAGER.addDefaultEntry(
28+
new AbstractDeserializer<>(new ResourceLocation("rml", "type"),
29+
ConfigMapType.class, jsonElement -> {
30+
try {
31+
return ConfigMapType.valueOf(Deserializer.decode(String.class, jsonElement));
32+
} catch (IllegalArgumentException e) {
33+
throw new JsonDeserializeException(jsonElement, "ConfigMapType not found", e);
34+
}
35+
}));
36+
37+
private final Class<?> cls;
38+
ConfigMapType(Class<?> cls){
39+
this.cls = cls;
40+
}
41+
42+
public Class<?> getType() {
43+
return cls;
44+
}
45+
}
46+
public static final AbstractDeserializer<ConfigElement> DESERIALIZER = Deserializer.named(ConfigElement.class, new ResourceLocation("rml", "map"))
47+
.optionalDefault(ConfigDescription.class, "display", ConfigDescription.EMPTY)
48+
.require(String.class, "name")
49+
.require(ConfigMapType.class, "map_type")
50+
.action((manager, jsonObject, context) -> {
51+
if (context.getJsonObject().has("defaultValue")) {
52+
Argument.map("defaultValue", context.get(ConfigMapType.class, "map_type").getType()).execute(Deserializer.MANAGER, context.getJsonObject().get("defaultValue").getAsJsonObject(), context);
53+
} else context.put("defaultValue", new HashMap<>());
54+
})
55+
.decode(context -> new ConfigMap(
56+
context.get(ConfigDescription.class, "display"),
57+
context.get(String.class, "name"),
58+
context.get(ConfigMapType.class, "map_type").getType(),
59+
ObjectHelper.static_cast(context.get(Map.class, "defaultValue")))).build();
1560

1661
protected Class<?> type;
1762
protected Map<String, ?> defaultVal;
1863

19-
public ConfigMap(ConfigGroup parentIn, String nameIn, Class<?> type, Map<String, ?> defaultVal) {
64+
public ConfigMap(ConfigDescription parentIn, String nameIn, Class<?> type, Map<String, ?> defaultVal) {
2065
super(parentIn, nameIn);
2166
this.type = type;
2267
this.defaultVal = defaultVal == null ? new HashMap<>() : defaultVal;
2368
}
2469

2570
@Override
2671
public String createDescription() {
27-
return "Lyouyihj/zenutils/api/config/elements/ConfigMap$HashDataMap;";
72+
return "Ljava/util/HashMap;";
2873
}
2974

3075
@Override
3176
public String createSignature() {
32-
return "Lyouyihj/zenutils/api/config/elements/ConfigMap$HashDataMap<Ljava/lang/String;"+ Type.getDescriptor(this.type) +">;";
77+
return "Ljava/util/HashMap<Ljava/lang/String;"+ Type.getDescriptor(this.type) +">;";
3378
}
3479

3580
@Override
3681
public void createToStack(MethodVisitor methodVisitor) {
37-
methodVisitor.visitTypeInsn(Opcodes.NEW, "youyihj/zenutils/api/config/elements/ConfigMap$HashDataMap");
82+
methodVisitor.visitTypeInsn(Opcodes.NEW, "java/util/HashMap");
3883
methodVisitor.visitInsn(Opcodes.DUP);
39-
methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "youyihj/zenutils/api/config/elements/ConfigMap$HashDataMap", "<init>", "()V", false);
84+
methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/HashMap", "<init>", "()V", false);
4085

4186
BiConsumer<MethodVisitor, ?> consumer = cast(STACK_PUTTER.get(this.type));
4287

@@ -90,8 +135,4 @@ else if (aBoolean == Boolean.TRUE) {
90135
});
91136
addStackPutter(String.class, MethodVisitor::visitLdcInsn);
92137
}
93-
94-
public static class HashDataMap<K extends String, V> extends HashMap<String, V> {
95-
96-
}
97138
}

src/main/java/rml/loader/api/mods/ContainerHolder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public HashMap<ModuleType,Module> getModules() {
3636
}
3737

3838
public boolean hasModule(ModuleType module){
39-
return modules.containsKey(module);
39+
return modules.containsKey(module) && modules.get(module) != null;
4040
}
4141

4242
public ContainerHolder(ModContainer container){

src/main/java/rml/loader/api/mods/module/ModuleType.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package rml.loader.api.mods.module;
22

3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonObject;
35
import net.minecraft.util.ResourceLocation;
46
import rml.deserializer.AbstractDeserializer;
7+
import rml.deserializer.JsonDeserializeException;
58
import rml.loader.api.annotations.PrivateAPI;
69
import rml.loader.api.annotations.PublicAPI;
710
import rml.loader.deserialize.Deserializer;
@@ -27,6 +30,19 @@ public class ModuleType{
2730
new ModuleType(context.get(ResourceLocation.class, "name"), context.get(String.class, "defaultLocation"), context.get(Boolean.class, "isFile")) :
2831
valueOf(location);
2932
}).markDefault().build();
33+
34+
public static ModuleType decode0(JsonElement jsonElement) throws JsonDeserializeException {
35+
try {
36+
JsonObject jsonObject = jsonElement.getAsJsonObject();
37+
ResourceLocation resourceLocation = new ResourceLocation(jsonObject.get("name").getAsString());
38+
boolean isFile = jsonObject.get("isFile").getAsBoolean();
39+
String location = jsonObject.get("defaultLocation").getAsString();
40+
return register(resourceLocation.toString(), location, isFile);
41+
} catch (Throwable throwable) {
42+
throw new JsonDeserializeException(jsonElement, "cant decode ModuleType");
43+
}
44+
}
45+
3046
public static final HashMap<ResourceLocation, ModuleType> REGISTRY = new HashMap<>();
3147
// public static final ModuleType STRUCTURE = register("STRUCTURE","structures", false);//TODO
3248
// public static final ModuleType DIMENSION = register("DIMENSION", "dimension", false);//TODO

src/main/java/rml/loader/core/RMLFMLLoadingPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static void makeFMLCorePluginContainsFMLMod(File file){
5959
public String[] getASMTransformerClass() {
6060
return new String[]{
6161
"rml.loader.core.RMLTransformer",
62-
"rml.loader.api.config.v2.config.ConfigUtils.ClassProvider"
62+
"rml.loader.api.config.v2.config.ConfigUtils$ClassProvider"
6363
};
6464
}
6565
@Override

0 commit comments

Comments
 (0)