Search in sources :

Example 11 with JSONParser

use of com.loohp.interactivechat.libs.org.json.simple.parser.JSONParser in project InteractiveChat-DiscordSRV-Addon by LOOHP.

the class ResourceManager method loadResources.

public synchronized ResourcePackInfo loadResources(File resourcePackFile) {
    String resourcePackName = resourcePackFile.getName();
    if (!resourcePackFile.exists()) {
        new IllegalArgumentException(resourcePackFile.getAbsolutePath() + " is not a directory nor is a zip file.").printStackTrace();
        ResourcePackInfo info = new ResourcePackInfo(this, null, resourcePackName, "Resource Pack is not a directory nor a zip file.");
        resourcePackInfo.add(0, info);
        return info;
    }
    ResourcePackFile resourcePack;
    if (resourcePackFile.isDirectory()) {
        resourcePack = new ResourcePackSystemFile(resourcePackFile);
    } else {
        try {
            resourcePack = new ResourcePackZipEntryFile(resourcePackFile);
        } catch (IOException e) {
            new IllegalArgumentException(resourcePackFile.getAbsolutePath() + " is an invalid zip file.").printStackTrace();
            ResourcePackInfo info = new ResourcePackInfo(this, null, resourcePackName, "Resource Pack is an invalid zip file.");
            resourcePackInfo.add(0, info);
            return info;
        }
    }
    ResourcePackFile packMcmeta = resourcePack.getChild("pack.mcmeta");
    if (!packMcmeta.exists()) {
        new ResourceLoadingException(resourcePackName + " does not have a pack.mcmeta").printStackTrace();
        ResourcePackInfo info = new ResourcePackInfo(this, resourcePack, resourcePackName, "pack.mcmeta not found");
        resourcePackInfo.add(0, info);
        return info;
    }
    JSONObject json;
    try (InputStreamReader reader = new InputStreamReader(new BOMInputStream(packMcmeta.getInputStream()), StandardCharsets.UTF_8)) {
        json = (JSONObject) new JSONParser().parse(reader);
    } catch (Throwable e) {
        new ResourceLoadingException("Unable to read pack.mcmeta for " + resourcePackName, e).printStackTrace();
        ResourcePackInfo info = new ResourcePackInfo(this, resourcePack, resourcePackName, "Unable to read pack.mcmeta");
        resourcePackInfo.add(0, info);
        return info;
    }
    int format;
    Component description = null;
    try {
        JSONObject packJson = (JSONObject) json.get("pack");
        format = ((Number) packJson.get("pack_format")).intValue();
        String rawDescription = packJson.get("description").toString();
        if (JsonUtils.isValid(rawDescription)) {
            try {
                description = InteractiveChatComponentSerializer.gson().deserialize(rawDescription);
            } catch (Exception e) {
                description = null;
            }
        }
        if (description == null) {
            description = LegacyComponentSerializer.legacySection().deserialize(rawDescription);
        }
        if (description.color() == null) {
            description = description.color(NamedTextColor.GRAY);
        }
    } catch (Exception e) {
        new ResourceLoadingException("Invalid pack.mcmeta for " + resourcePackName, e).printStackTrace();
        ResourcePackInfo info = new ResourcePackInfo(this, resourcePack, resourcePackName, "Invalid pack.mcmeta");
        resourcePackInfo.add(0, info);
        return info;
    }
    BufferedImage icon = null;
    ResourcePackFile packIcon = resourcePack.getChild("pack.png");
    if (packIcon.exists()) {
        try {
            icon = ImageIO.read(packIcon.getInputStream());
        } catch (Exception ignore) {
        }
    }
    ResourcePackFile assetsFolder = resourcePack.getChild("assets");
    try {
        loadAssets(assetsFolder);
    } catch (Exception e) {
        new ResourceLoadingException("Unable to load assets for " + resourcePackName, e).printStackTrace();
        ResourcePackInfo info = new ResourcePackInfo(this, resourcePack, resourcePackName, false, "Unable to load assets", format, description, icon);
        resourcePackInfo.add(0, info);
        return info;
    }
    ResourcePackInfo info = new ResourcePackInfo(this, resourcePack, resourcePackName, true, null, format, description, icon);
    resourcePackInfo.add(0, info);
    return info;
}
Also used : InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) BOMInputStream(com.loohp.interactivechat.libs.org.apache.commons.io.input.BOMInputStream) JSONObject(com.loohp.interactivechat.libs.org.json.simple.JSONObject) JSONParser(com.loohp.interactivechat.libs.org.json.simple.parser.JSONParser) Component(com.loohp.interactivechat.libs.net.kyori.adventure.text.Component)

Aggregations

JSONObject (com.loohp.interactivechat.libs.org.json.simple.JSONObject)11 JSONParser (com.loohp.interactivechat.libs.org.json.simple.parser.JSONParser)11 InputStreamReader (java.io.InputStreamReader)7 HashMap (java.util.HashMap)6 BOMInputStream (com.loohp.interactivechat.libs.org.apache.commons.io.input.BOMInputStream)5 BufferedImage (java.awt.image.BufferedImage)5 ArrayList (java.util.ArrayList)5 ResourceLoadingException (com.loohp.interactivechatdiscordsrvaddon.resources.ResourceLoadingException)4 ResourcePackFile (com.loohp.interactivechatdiscordsrvaddon.resources.ResourcePackFile)4 IOException (java.io.IOException)4 JSONArray (com.loohp.interactivechat.libs.org.json.simple.JSONArray)3 ICPlayer (com.loohp.interactivechat.objectholders.ICPlayer)3 OfflineICPlayer (com.loohp.interactivechat.objectholders.OfflineICPlayer)3 ModelOverrideType (com.loohp.interactivechatdiscordsrvaddon.resources.models.ModelOverride.ModelOverrideType)3 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 JsonParser (com.google.gson.JsonParser)2 XMaterial (com.loohp.interactivechat.libs.com.cryptomorin.xseries.XMaterial)2 Component (com.loohp.interactivechat.libs.net.kyori.adventure.text.Component)2 GeneratedTextureResource (com.loohp.interactivechatdiscordsrvaddon.resources.textures.GeneratedTextureResource)2