Search in sources :

Example 1 with TextureAnimationFrames

use of com.loohp.interactivechatdiscordsrvaddon.resources.textures.TextureAnimation.TextureAnimationFrames in project InteractiveChat-DiscordSRV-Addon by LOOHP.

the class TextureManager method loadDirectory.

@SuppressWarnings("unchecked")
@Override
protected void loadDirectory(String namespace, ResourcePackFile root) {
    if (!root.exists() || !root.isDirectory()) {
        throw new IllegalArgumentException(root.getAbsolutePath() + " is not a directory.");
    }
    JSONParser parser = new JSONParser();
    Map<String, TextureResource> textures = new HashMap<>();
    Collection<ResourcePackFile> files = root.listFilesRecursively();
    for (ResourcePackFile file : files) {
        try {
            String key = namespace + ":" + file.getRelativePathFrom(root);
            String extension = "";
            if (key.lastIndexOf(".") >= 0) {
                extension = key.substring(key.lastIndexOf(".") + 1);
                key = key.substring(0, key.lastIndexOf("."));
            }
            if (extension.equalsIgnoreCase("png")) {
                textures.put(key, new TextureResource(this, key, file, true));
            } else if (extension.equalsIgnoreCase("mcmeta")) {
                InputStreamReader reader = new InputStreamReader(new BOMInputStream(file.getInputStream()), StandardCharsets.UTF_8);
                JSONObject rootJson = (JSONObject) parser.parse(reader);
                reader.close();
                TextureAnimation animation = null;
                if (rootJson.containsKey("animation")) {
                    JSONObject animationJson = (JSONObject) rootJson.get("animation");
                    boolean interpolate = (boolean) animationJson.getOrDefault("interpolate", false);
                    int width = ((Number) animationJson.getOrDefault("width", -1)).intValue();
                    int height = ((Number) animationJson.getOrDefault("height", -1)).intValue();
                    int frametime = ((Number) animationJson.getOrDefault("frametime", -1)).intValue();
                    JSONArray framesArray = ((JSONArray) animationJson.getOrDefault("frames", new JSONArray()));
                    List<TextureAnimationFrames> frames = new ArrayList<>();
                    for (Object obj : framesArray) {
                        if (obj instanceof Number) {
                            frames.add(new TextureAnimationFrames(((Number) obj).intValue(), frametime));
                        } else if (obj instanceof JSONObject) {
                            JSONObject frameJson = (JSONObject) obj;
                            frames.add(new TextureAnimationFrames(((Number) frameJson.get("index")).intValue(), ((Number) frameJson.get("time")).intValue()));
                        }
                    }
                    animation = new TextureAnimation(interpolate, width, height, frametime, frames);
                }
                TextureProperties properties = null;
                if (rootJson.containsKey("texture")) {
                    JSONObject propertiesJson = (JSONObject) rootJson.get("texture");
                    boolean blur = (boolean) propertiesJson.getOrDefault("blur", false);
                    boolean clamp = (boolean) propertiesJson.getOrDefault("clamp", false);
                    int[] mipmaps = ((JSONArray) propertiesJson.getOrDefault("mipmaps", new JSONArray())).stream().mapToInt(each -> ((Number) each).intValue()).toArray();
                    properties = new TextureProperties(blur, clamp, mipmaps);
                }
                textures.put(key + "." + extension, new TextureMeta(this, key + "." + extension, file, animation, properties));
            } else {
                textures.put(key + "." + extension, new TextureResource(this, key, file));
            }
        } catch (Exception e) {
            new ResourceLoadingException("Unable to load block model " + file.getAbsolutePath(), e).printStackTrace();
        }
    }
    this.textures.putAll(textures);
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) JSONArray(com.loohp.interactivechat.libs.org.json.simple.JSONArray) TextureAnimationFrames(com.loohp.interactivechatdiscordsrvaddon.resources.textures.TextureAnimation.TextureAnimationFrames) ResourceLoadingException(com.loohp.interactivechatdiscordsrvaddon.resources.ResourceLoadingException) BOMInputStream(com.loohp.interactivechat.libs.org.apache.commons.io.input.BOMInputStream) JSONObject(com.loohp.interactivechat.libs.org.json.simple.JSONObject) ResourceLoadingException(com.loohp.interactivechatdiscordsrvaddon.resources.ResourceLoadingException) JSONParser(com.loohp.interactivechat.libs.org.json.simple.parser.JSONParser) ResourcePackFile(com.loohp.interactivechatdiscordsrvaddon.resources.ResourcePackFile) ArrayList(java.util.ArrayList) List(java.util.List) JSONObject(com.loohp.interactivechat.libs.org.json.simple.JSONObject)

Aggregations

BOMInputStream (com.loohp.interactivechat.libs.org.apache.commons.io.input.BOMInputStream)1 JSONArray (com.loohp.interactivechat.libs.org.json.simple.JSONArray)1 JSONObject (com.loohp.interactivechat.libs.org.json.simple.JSONObject)1 JSONParser (com.loohp.interactivechat.libs.org.json.simple.parser.JSONParser)1 ResourceLoadingException (com.loohp.interactivechatdiscordsrvaddon.resources.ResourceLoadingException)1 ResourcePackFile (com.loohp.interactivechatdiscordsrvaddon.resources.ResourcePackFile)1 TextureAnimationFrames (com.loohp.interactivechatdiscordsrvaddon.resources.textures.TextureAnimation.TextureAnimationFrames)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1