finished json generation

This commit is contained in:
UnrealValentin
2021-04-12 12:54:55 +02:00
parent c1e2dc1eeb
commit f4201065a5
5 changed files with 95 additions and 1 deletions

View File

@@ -1,6 +1,7 @@
package org.hmcore.modules;
import org.hmcore.registration.ObjectInfo;
import org.hmcore.registration.config.ObjectInfoData;
/**
* Represents a module that manages the objects that should get registered to Hytale.
@@ -64,6 +65,17 @@ public abstract class Module<T, I extends ObjectInfo> {
if (!contains(name)) register(name, object);
}
/**
* Used to be able to request the name the module is registered as.
* @return The String the module is registered as. So that HMCore.modules.get(name).getName() == name
*/
public abstract String getName();
/**
* @return An Array of all objects currently registered to the module.
*/
public abstract T[] getObjects();
//
// For registering the Objects to Hytale
// Made for internal use ONLY
@@ -75,4 +87,7 @@ public abstract class Module<T, I extends ObjectInfo> {
* @return true when every object has been registered successfully.
*/
public abstract boolean registerObjects();
public abstract ObjectInfoData[] getObjectInfoArray();
}

View File

@@ -1,11 +1,30 @@
package org.hmcore.registration.config;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.hmcore.modules.Module;
public class ObjectInfoConfigHandler {
public static String generateFreshJSON(Module<?,?>[] modules) {
ModuleInfo[] moduleInfos = new ModuleInfo[modules.length];
for (int i = 0; i < modules.length; i++) {
Module<?,?> module = modules[i];
moduleInfos[i] = new ModuleInfo(module.getName(),
module.getObjectInfoArray());
}
ObjectInfoConfig objectInfoConfig = new ObjectInfoConfig(moduleInfos);
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
return gson.toJson(objectInfoConfig);
}
}

View File

@@ -6,9 +6,11 @@ public class ObjectInfoData {
public final String objectName;
public final String objectInfoChoosen;
public final String _availableOptions;
public ObjectInfoData(String objectName, @Nullable String objectInfoChoosen) {
public ObjectInfoData(String objectName, @Nullable String objectInfoChoosen, String availableOptions) {
this.objectName = objectName;
this.objectInfoChoosen = objectInfoChoosen == null ? "default" : objectInfoChoosen;
_availableOptions = availableOptions;
}
}

View File

@@ -1,6 +1,9 @@
package org.hmcore.tests;
import org.hmcore.HMCore;
import org.hmcore.modules.Module;
import org.hmcore.registration.config.ObjectInfoConfigHandler;
import org.hmcore.tests.modules.impl.JavaCustomObjectInfo;
import org.hmcore.tests.modules.impl.JavaTestModule;
import org.junit.jupiter.api.Test;
@@ -40,6 +43,17 @@ public class JavaTests {
assertTrue(testModule.registerObjects(), "Some weird behavior happened..");
testModule.addInfoToObject("test1", "opt1", new JavaCustomObjectInfo("aa", 18));
testModule.addInfoToObject("test2", "opt2", new JavaCustomObjectInfo("AI", 69));
testModule.addInfoToObject("test2", "opt3", new JavaCustomObjectInfo("drei", 420));
testModule.addInfoToObject("test3", "opt4", new JavaCustomObjectInfo("addadaaa", 181));
testModule.addInfoToObject("test3", "opt5", new JavaCustomObjectInfo("dada", 32833));
testModule.addInfoToObject("test3", "opt6", new JavaCustomObjectInfo("e3312", 2130440));
System.out.println(ObjectInfoConfigHandler.generateFreshJSON(HMCore.modules.values().toArray(new Module[0])));
}
}

View File

@@ -1,10 +1,12 @@
package org.hmcore.tests.modules.impl;
import org.hmcore.modules.Module;
import org.hmcore.registration.config.ObjectInfoData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JavaTestModule extends Module<Integer, JavaCustomObjectInfo> {
@@ -36,6 +38,16 @@ public class JavaTestModule extends Module<Integer, JavaCustomObjectInfo> {
}
@Override
public String getName() {
return "java_test";
}
@Override
public Integer[] getObjects() {
return objectMap.values().toArray(new Integer[0]);
}
@Override
public boolean registerObjects() {
@@ -55,4 +67,36 @@ public class JavaTestModule extends Module<Integer, JavaCustomObjectInfo> {
return true;
}
@Override
public ObjectInfoData[] getObjectInfoArray() {
ObjectInfoData[] objectInfoData = new ObjectInfoData[objectMap.values().size()];
int i = 0;
for(Map.Entry<String, Integer> entry: objectMap.entrySet()) {
String options = "";
boolean first = true;
for(Map.Entry<String, JavaCustomObjectInfo> entry2: infoMap.get(entry.getKey()).entrySet()) {
if(first) {
options = entry2.getKey();
first = false;
} else {
options += ", " + entry2.getKey();
}
}
objectInfoData[i] = new ObjectInfoData(entry.getKey(), null, options);
i++;
}
return objectInfoData;
}
}