Search in sources :

Example 16 with JSONObject

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

the class ICPlayerEvents method populate.

private void populate(OfflineICPlayer player, boolean scheduleAsync) {
    if (scheduleAsync) {
        Bukkit.getScheduler().runTaskAsynchronously(InteractiveChatDiscordSrvAddon.plugin, () -> populate(player, false));
        return;
    }
    Map<String, Object> cachedProperties = CACHED_PROPERTIES.get(player.getUniqueId());
    if (cachedProperties == null) {
        cachedProperties = new HashMap<>();
        JSONObject json = HTTPRequestUtils.getJSONResponse(PROFILE_URL.replace("%s", player.getName()));
        if (json != null && json.containsKey("properties")) {
            JSONObject properties = (JSONObject) json.get("properties");
            for (Object obj : properties.keySet()) {
                try {
                    String key = (String) obj;
                    String value = (String) properties.get(key);
                    if (value.endsWith(".png")) {
                        BufferedImage image = ImageUtils.downloadImage(value);
                        player.addProperties(key, image);
                        cachedProperties.put(key, image);
                    } else if (value.endsWith(".bin")) {
                        byte[] data = HTTPRequestUtils.download(value);
                        player.addProperties(key, data);
                        cachedProperties.put(key, data);
                    } else {
                        player.addProperties(key, value);
                        cachedProperties.put(key, value);
                    }
                } catch (Exception ignore) {
                }
            }
        }
        CACHED_PROPERTIES.put(player.getUniqueId(), cachedProperties);
    } else {
        for (Entry<String, Object> entry : cachedProperties.entrySet()) {
            player.addProperties(entry.getKey(), entry.getValue());
        }
    }
}
Also used : JSONObject(com.loohp.interactivechat.libs.org.json.simple.JSONObject) JSONObject(com.loohp.interactivechat.libs.org.json.simple.JSONObject) BufferedImage(java.awt.image.BufferedImage)

Example 17 with JSONObject

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

the class ResourceDownloadManager method downloadExtras.

public synchronized void downloadExtras(Runnable preparation, BiConsumer<String, byte[]> dataHandler) {
    ensureData();
    try {
        if (data.containsKey("extras-entries")) {
            JSONObject extras = (JSONObject) data.get("extras-entries");
            preparation.run();
            for (Object obj : extras.keySet()) {
                String key = obj.toString();
                String value = extras.get(key).toString();
                try {
                    dataHandler.accept(value, HTTPRequestUtils.download(key));
                } catch (Exception e) {
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : JSONObject(com.loohp.interactivechat.libs.org.json.simple.JSONObject) JSONObject(com.loohp.interactivechat.libs.org.json.simple.JSONObject)

Example 18 with JSONObject

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

the class ResourceDownloadManager method downloadResources.

public synchronized void downloadResources(TriConsumer<TaskType, String, Double> progressListener) {
    ensureData();
    JSONObject client = (JSONObject) data.get("client-entries");
    String clientUrl = client.get("url").toString();
    progressListener.accept(TaskType.CLIENT_DOWNLOAD, "", 0.0);
    try (ZipArchiveInputStream zip = new ZipArchiveInputStream(new ByteArrayInputStream(HTTPRequestUtils.download(clientUrl)), StandardCharsets.UTF_8.toString(), false, true, true)) {
        while (true) {
            ZipEntry entry = zip.getNextZipEntry();
            if (entry == null) {
                break;
            }
            String name = entry.getName();
            if ((name.startsWith("assets") || name.equals("pack.png")) && !entry.isDirectory()) {
                String fileName = getEntryName(name);
                progressListener.accept(TaskType.EXTRACT, name, 0.0);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] byteChunk = new byte[4096];
                int n;
                while ((n = zip.read(byteChunk)) > 0) {
                    baos.write(byteChunk, 0, n);
                }
                byte[] currentEntry = baos.toByteArray();
                File folder = new File(packFolder, name).getParentFile();
                folder.mkdirs();
                File file = new File(folder, fileName);
                if (file.exists()) {
                    file.delete();
                }
                FileUtils.copy(new ByteArrayInputStream(currentEntry), file);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    JSONObject downloadedEntries = (JSONObject) data.get("downloaded-entries");
    int size = downloadedEntries.size();
    int i = 0;
    for (Object obj : downloadedEntries.keySet()) {
        String key = obj.toString();
        String value = downloadedEntries.get(key).toString();
        String fileName = getEntryName(key);
        double percentage = ((double) ++i / (double) size) * 100;
        String trimmedValue = (value.startsWith("/") ? value.substring(1) : value).trim();
        if (!trimmedValue.isEmpty()) {
            trimmedValue += "/";
        }
        progressListener.accept(TaskType.DOWNLOAD, trimmedValue + fileName, percentage);
        File folder = value.isEmpty() || value.equals("/") ? packFolder : new File(packFolder, value);
        folder.mkdirs();
        File file = new File(folder, fileName);
        if (file.exists()) {
            file.delete();
        }
        HTTPRequestUtils.download(file, key);
    }
    progressListener.accept(TaskType.DONE, "", 100.0);
}
Also used : JSONObject(com.loohp.interactivechat.libs.org.json.simple.JSONObject) ZipArchiveInputStream(com.loohp.interactivechat.libs.org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipEntry(java.util.zip.ZipEntry) JSONObject(com.loohp.interactivechat.libs.org.json.simple.JSONObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File)

Example 19 with JSONObject

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

the class ResourceDownloadManager method ensureVersions.

private static void ensureVersions() {
    if (MINECRAFT_VERSIONS == null) {
        JSONObject data = HTTPRequestUtils.getJSONResponse(VERSIONS_URL);
        MINECRAFT_VERSIONS = new LinkedHashSet<>();
        if (data != null && data.containsKey("versions")) {
            for (Object version : (JSONArray) data.get("versions")) {
                MINECRAFT_VERSIONS.add(version.toString());
            }
        }
        MINECRAFT_VERSIONS = Collections.unmodifiableSet(MINECRAFT_VERSIONS);
    }
}
Also used : JSONObject(com.loohp.interactivechat.libs.org.json.simple.JSONObject) JSONArray(com.loohp.interactivechat.libs.org.json.simple.JSONArray) JSONObject(com.loohp.interactivechat.libs.org.json.simple.JSONObject)

Example 20 with JSONObject

use of com.loohp.interactivechat.libs.org.json.simple.JSONObject 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)21 JSONParser (com.loohp.interactivechat.libs.org.json.simple.parser.JSONParser)11 InputStreamReader (java.io.InputStreamReader)7 BufferedImage (java.awt.image.BufferedImage)6 HashMap (java.util.HashMap)6 BOMInputStream (com.loohp.interactivechat.libs.org.apache.commons.io.input.BOMInputStream)5 File (java.io.File)5 ArrayList (java.util.ArrayList)5 JSONArray (com.loohp.interactivechat.libs.org.json.simple.JSONArray)4 ResourceLoadingException (com.loohp.interactivechatdiscordsrvaddon.resources.ResourceLoadingException)4 ResourcePackFile (com.loohp.interactivechatdiscordsrvaddon.resources.ResourcePackFile)4 IOException (java.io.IOException)4 ICPlayer (com.loohp.interactivechat.objectholders.ICPlayer)3 OfflineICPlayer (com.loohp.interactivechat.objectholders.OfflineICPlayer)3 ModelOverrideType (com.loohp.interactivechatdiscordsrvaddon.resources.models.ModelOverride.ModelOverrideType)3 LinkedHashMap (java.util.LinkedHashMap)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