Skip to content

Commit

Permalink
Revert "Drop caching resources with no benefit"
Browse files Browse the repository at this point in the history
This reverts commit 9a27d56.
  • Loading branch information
AlexProgrammerDE committed Jan 24, 2025
1 parent 1235f70 commit 021902e
Showing 1 changed file with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import net.kyori.adventure.key.Key;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

Expand All @@ -45,6 +46,7 @@ public Key read(JsonReader in) throws IOException {
return Key.key(key);
}
};
private static final Map<String, JsonArray> LOADED_DATA = new HashMap<>();
private static final Function<Map<Class<?>, Object>, Gson> GSON_FACTORY = (typeAdapters) -> {
var builder = new GsonBuilder()
.registerTypeAdapter(Key.class, RESOURCE_KEY_ADAPTER)
Expand All @@ -64,14 +66,19 @@ public static <T> T fromJson(String dataFile, String dataKey, Class<T> clazz) {
public static <T> T fromJson(String dataFile, String dataKey, Class<T> clazz,
Map<Class<?>, Object> typeAdapters) {
var gson = createGson(typeAdapters);
var array = new JsonArray();
try {
array =
gson.fromJson(SFHelpers.getResourceAsString(dataFile), JsonArray.class);
} catch (Exception e) {
throw new RuntimeException("Failed to load data file " + dataFile, e);
}

var array =
LOADED_DATA.computeIfAbsent(
dataFile,
file -> {
var data = new JsonArray();
try {
data =
gson.fromJson(SFHelpers.getResourceAsString(file), JsonArray.class);
} catch (Exception e) {
throw new RuntimeException("Failed to load data file " + file, e);
}
return data;
});
for (var element : array) {
if (element.getAsJsonObject().get("key").getAsString().equals(dataKey)) {
return gson.fromJson(element, clazz);
Expand Down

0 comments on commit 021902e

Please sign in to comment.