-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2fe34c2
commit 28c1deb
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
.../java/com/mmodding/mmodding_lib/library/resources/loaders/IdentifiableJsonDataLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.mmodding.mmodding_lib.library.resources.loaders; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonParseException; | ||
import com.mmodding.mmodding_lib.MModdingLib; | ||
import net.minecraft.resource.Resource; | ||
import net.minecraft.resource.ResourceManager; | ||
import net.minecraft.util.Identifier; | ||
import net.minecraft.util.JsonHelper; | ||
import net.minecraft.util.profiler.Profiler; | ||
import org.quiltmc.qsl.resource.loader.api.reloader.SimpleResourceReloader; | ||
|
||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Executor; | ||
|
||
public interface IdentifiableJsonDataLoader extends SimpleResourceReloader<Map<Identifier, JsonElement>> { | ||
|
||
String SUFFIX = ".json"; | ||
|
||
Gson getGson(); | ||
|
||
String getDataType(); | ||
|
||
@Override | ||
default CompletableFuture<Map<Identifier, JsonElement>> load(ResourceManager resourceManager, Profiler profiler, Executor executor) { | ||
Map<Identifier, JsonElement> map = new HashMap<>(); | ||
|
||
for(Map.Entry<Identifier, Resource> entry : resourceManager.findResources(this.getDataType(), id -> id.getPath().endsWith(SUFFIX)).entrySet()) { | ||
String string = entry.getKey().getPath(); | ||
Identifier resource = new Identifier(entry.getKey().getNamespace(), string.substring(this.getDataType().length() + 1, string.length() - SUFFIX.length())); | ||
try { | ||
Reader reader = entry.getValue().openBufferedReader(); | ||
try { | ||
JsonElement deserialized = JsonHelper.deserialize(this.getGson(), reader, JsonElement.class); | ||
if (deserialized != null) { | ||
JsonElement element = map.put(resource, deserialized); | ||
if (element != null) { | ||
throw new IllegalStateException("Duplicate data file ignored with ID " + resource); | ||
} | ||
} else { | ||
MModdingLib.LIBRARY_CONTAINER.getLogger().error("Couldn't load data file {} from {} as it's null or empty", resource, entry.getKey()); | ||
} | ||
} catch (IllegalStateException illegalStateException) { | ||
if (reader != null) { | ||
try { | ||
reader.close(); | ||
} catch (IOException ioException) { | ||
illegalStateException.addSuppressed(ioException); | ||
} | ||
} | ||
throw illegalStateException; | ||
} | ||
reader.close(); | ||
} catch (IllegalArgumentException | IOException | JsonParseException jsonParseException) { | ||
MModdingLib.LIBRARY_CONTAINER.getLogger().error("Couldn't parse data file {} from {}", resource, entry.getKey(), jsonParseException); | ||
} | ||
} | ||
|
||
return CompletableFuture.supplyAsync(() -> map, executor); | ||
} | ||
} |