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