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);
}
Aggregations