Skip to content

Commit a122e69

Browse files
feat: fix parsing in mapping
1 parent a524abb commit a122e69

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

src/main/java/cz/coffee/skjson/skript/base/JsonBase.java

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@
1818
import com.google.gson.JsonPrimitive;
1919
import cz.coffee.skjson.SkJsonElements;
2020
import cz.coffee.skjson.api.FileHandler;
21-
import cz.coffee.skjson.api.SkJsonLogger;
2221
import cz.coffee.skjson.json.JsonParser;
2322
import cz.coffee.skjson.parser.ParserUtil;
24-
import cz.coffee.skjson.utils.ConsoleColors;
25-
import cz.coffee.skjson.utils.Logger;
2623
import cz.coffee.skjson.utils.PatternUtil;
2724
import cz.coffee.skjson.utils.Util;
2825
import org.bukkit.event.Event;
@@ -635,56 +632,71 @@ public static class MapJson extends Effect {
635632
private boolean isLocal;
636633
private boolean async;
637634

635+
private static boolean cannotBeParsed(JsonElement element) {
636+
return from(element) instanceof JsonElement;
637+
}
638+
639+
640+
638641
private static void toList(@NotNull String name, JsonElement inputJson, boolean isLocal, Event event) {
639642
if (inputJson.isJsonPrimitive()) {
640643
primitive(name, inputJson.getAsJsonPrimitive(), isLocal, event);
641644
} else if (inputJson.isJsonObject()) {
645+
Object parsed = from(inputJson);
646+
642647
JsonObject jsonObject = inputJson.getAsJsonObject();
643648
for (String key : jsonObject.keySet()) {
644649
JsonElement element = jsonObject.get(key);
645650
String newName = name + key + SEPARATOR;
646-
if (element.isJsonPrimitive()) {
647-
primitive(newName, element.getAsJsonPrimitive(), isLocal, event);
648-
} else {
651+
Object parsed_ = from(element);
652+
653+
if (cannotBeParsed(element)) {
649654
toList(newName, element, isLocal, event);
655+
} else {
656+
parsed(newName, parsed_, isLocal, event);
650657
}
651658
}
652659
} else if (inputJson.isJsonArray()) {
653660
JsonArray jsonArray = inputJson.getAsJsonArray();
654661
for (int i = 0; i < jsonArray.size(); i++) {
655662
JsonElement element = jsonArray.get(i);
656663
String newName = name + (i + 1) + SEPARATOR;
657-
if (element.isJsonPrimitive()) {
658-
primitive(newName, element.getAsJsonPrimitive(), isLocal, event);
659-
} else {
664+
Object parsed = from(element);
665+
666+
if (cannotBeParsed(element)) {
660667
toList(newName, element, isLocal, event);
668+
} else {
669+
parsed(newName, parsed, isLocal, event);
661670
}
662671
}
663672
}
664673
}
665674

666675

676+
667677
static <T> void parsed(String name, T object, boolean isLocal, Event event) {
668-
if (PROJECT_DEBUG && LOGGING_LEVEL > 2) info("PARSED -> (Variable) %s => &e%s", name, object);
669-
Variables.setVariable(name, object, event, isLocal);
678+
if (ParserUtil.isClassicType(object)) {
679+
primitive(name, object, isLocal, event);
680+
} else {
681+
if (PROJECT_DEBUG && LOGGING_LEVEL > 2) info("PARSED -> (%s) %s => &a%s", object.getClass().getName(), name, object);
682+
Variables.setVariable(name, object, event, isLocal);
683+
}
670684
}
671685

672-
static void primitive(String name, JsonPrimitive input, boolean isLocal, Event event) {
686+
static void primitive(String name, Object input, boolean isLocal, Event event) {
673687
if (name == null || input == null || event == null) return;
674-
Object o = jsonToType(defaultConverter(input));
675-
if (o != null) {
676-
if (PROJECT_DEBUG && LOGGING_LEVEL > 2) info("PRIMITIVE -> (Variable) %s => &e%s", name, input);
677-
}
678-
assert o != null;
679-
Variables.setVariable(name, o, event, isLocal);
688+
if (PROJECT_DEBUG && LOGGING_LEVEL > 2) info("&8PRIMITIVE&r -> (%s) %s => &e%s", input.getClass().getName(),name, input);
689+
Variables.setVariable(name, input, event, isLocal);
680690
}
681691

682692
@Override
683693
protected void execute(@NotNull Event e) {
684694
Object jsonInputSingle = jsonInput.getSingle(e);
685695
JsonElement json = ParserUtil.parse(jsonInputSingle);
696+
686697
String vv = variableString.getSingle(e);
687698
String var = vv.substring(0, vv.length() - 3);
699+
688700
if (json == null) return;
689701
if (async) {
690702
CompletableFuture.runAsync(() -> toList(var + SEPARATOR, json, isLocal, e));

0 commit comments

Comments
 (0)