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