Skip to content

Commit

Permalink
smaller event cache file
Browse files Browse the repository at this point in the history
by not storaging id twice
  • Loading branch information
ZZZank committed Aug 25, 2024
1 parent 96ef225 commit 780ee69
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
10 changes: 4 additions & 6 deletions src/main/java/zzzank/probejs/features/kubejs/EventJSInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.bsideup.jabel.Desugar;
import com.google.gson.JsonObject;
import com.mojang.datafixers.util.Pair;
import dev.latvian.kubejs.event.EventJS;
import dev.latvian.kubejs.script.ScriptType;
import lombok.val;
Expand Down Expand Up @@ -31,9 +32,7 @@ public EventJSInfo(ScriptType t, EventJS event, String id, @Nullable String sub)
}

@Nullable
public static EventJSInfo fromJson(JsonObject json) {
//id
val id = json.get("id").getAsString();
public static EventJSInfo fromJson(String id, JsonObject json) {
//class
val clazz = ReflectUtils.classOrNull(json.get("class").getAsString());
if (clazz == null || !EventJS.class.isAssignableFrom(clazz)) {
Expand Down Expand Up @@ -62,17 +61,16 @@ public boolean hasSub() {
return sub.get() != null;
}

public JsonObject toJson() {
public Pair<String, JsonObject> toJson() {
val m = CollectUtils.ofMap(
"id", id,
"class", clazzRaw.getName(),
"type", CollectUtils.mapToList(scriptTypes, ScriptType::name),
"cancellable", this.cancellable
);
if (sub.notNull()) {
m.put("sub", sub.get());
}
return (JsonObject) JsonUtils.parseObject(m);
return Pair.of(id, (JsonObject) JsonUtils.parseObject(m));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static void loadFrom(Path path) {
}
for (val entry : obj.entrySet()) {
val id = entry.getKey();
val info = EventJSInfo.fromJson(entry.getValue().getAsJsonObject());
val info = EventJSInfo.fromJson(id, entry.getValue().getAsJsonObject());
if (info != null) {
KNOWN.put(id, info);
}
Expand All @@ -52,7 +52,8 @@ public static void writeTo(Path path) {
try (val writer = Files.newBufferedWriter(path)) {
val obj = new JsonObject();
for (val info : KNOWN.values()) {
obj.add(info.id(), info.toJson());
val pair = info.toJson();
obj.add(pair.getFirst(), pair.getSecond());
}
//dont use ProbeJS.GSON_WRITER, there's too many lines
ProbeJS.GSON.toJson(obj, writer);
Expand Down

0 comments on commit 780ee69

Please sign in to comment.