Skip to content

Commit

Permalink
feat: fix mapping nested variables
Browse files Browse the repository at this point in the history
  • Loading branch information
cooffeeRequired committed Nov 25, 2024
1 parent 95d38c8 commit a524abb
Showing 1 changed file with 24 additions and 35 deletions.
59 changes: 24 additions & 35 deletions src/main/java/cz/coffee/skjson/skript/base/JsonBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
import com.google.gson.JsonPrimitive;
import cz.coffee.skjson.SkJsonElements;
import cz.coffee.skjson.api.FileHandler;
import cz.coffee.skjson.api.SkJsonLogger;
import cz.coffee.skjson.json.JsonParser;
import cz.coffee.skjson.parser.ParserUtil;
import cz.coffee.skjson.utils.ConsoleColors;
import cz.coffee.skjson.utils.Logger;
import cz.coffee.skjson.utils.PatternUtil;
import cz.coffee.skjson.utils.Util;
import org.bukkit.event.Event;
Expand Down Expand Up @@ -635,46 +638,32 @@ public static class MapJson extends Effect {
private static void toList(@NotNull String name, JsonElement inputJson, boolean isLocal, Event event) {
if (inputJson.isJsonPrimitive()) {
primitive(name, inputJson.getAsJsonPrimitive(), isLocal, event);
} else if (inputJson.isJsonObject() || inputJson.isJsonArray()) {
if (inputJson instanceof JsonArray list) {
for (int index = 0; index < list.size(); index++) {
JsonElement element = list.get(index);
Object parsed = from(element);
if (parsed == null) {
if (element.isJsonPrimitive()) {
primitive(name + (index + 1), element.getAsJsonPrimitive(), isLocal, event);
} else {
toList(name + (index + 1) + SEPARATOR, element, isLocal, event);
}
} else {
if (PROJECT_DEBUG && LOGGING_LEVEL > 2)
info("List-Element &b %s &fParsed? -> &a %s", element, parsed);
parsed(name + (index + 1), parsed, isLocal, event);
}
} else if (inputJson.isJsonObject()) {
JsonObject jsonObject = inputJson.getAsJsonObject();
for (String key : jsonObject.keySet()) {
JsonElement element = jsonObject.get(key);
String newName = name + key + SEPARATOR;
if (element.isJsonPrimitive()) {
primitive(newName, element.getAsJsonPrimitive(), isLocal, event);
} else {
toList(newName, element, isLocal, event);
}
}
} else if (inputJson.isJsonArray()) {
JsonArray jsonArray = inputJson.getAsJsonArray();
for (int i = 0; i < jsonArray.size(); i++) {
JsonElement element = jsonArray.get(i);
String newName = name + (i + 1) + SEPARATOR;
if (element.isJsonPrimitive()) {
primitive(newName, element.getAsJsonPrimitive(), isLocal, event);
} else {
toList(newName, element, isLocal, event);
}
} else if (inputJson instanceof JsonObject map) {
map.keySet().stream().filter(Objects::nonNull).forEach(key -> {
try {
JsonElement element = map.get(key);
Object parsed = from(element);
// parsed means that return a parsed value from BUKKIT/SKRIPT Objects
if (parsed == null) {
if (element.isJsonPrimitive()) {
primitive(name + key, element.getAsJsonPrimitive(), isLocal, event);
} else {
toList(name + key + SEPARATOR, element, isLocal, event);
}
} else {
parsed(name + key, parsed, isLocal, event);
}
} catch (Exception e) {
error(e);
}
});
}
}
}


static <T> void parsed(String name, T object, boolean isLocal, Event event) {
if (PROJECT_DEBUG && LOGGING_LEVEL > 2) info("PARSED -> (Variable) %s => &e%s", name, object);
Variables.setVariable(name, object, event, isLocal);
Expand Down

0 comments on commit a524abb

Please sign in to comment.