use of com.loohp.interactivechat.libs.org.json.simple.JSONObject in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class LanguageManager method loadDirectory.
@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, Map<String, String>> translations = new HashMap<>();
for (ResourcePackFile file : root.listFilesRecursively()) {
String name = file.getName();
if (name.endsWith(".json")) {
try {
InputStreamReader reader = new InputStreamReader(new BOMInputStream(file.getInputStream()), StandardCharsets.UTF_8);
JSONObject json = (JSONObject) parser.parse(reader);
reader.close();
Map<String, String> mapping = new HashMap<>();
for (Object obj : json.keySet()) {
try {
String key = (String) obj;
mapping.put(key, (String) json.get(key));
} catch (Exception e) {
}
}
translations.put(file.getName().substring(0, file.getName().lastIndexOf(".")), mapping);
} catch (Exception e) {
new ResourceLoadingException("Unable to load language " + file.getAbsolutePath(), e).printStackTrace();
}
} else if (name.endsWith(".lang")) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new BOMInputStream(file.getInputStream()), StandardCharsets.UTF_8))) {
Map<String, String> mapping = new HashMap<>();
String line;
while ((line = reader.readLine()) != null) {
int separator = line.indexOf("=");
if (separator >= 0) {
mapping.put(line.substring(0, separator), line.substring(separator + 1));
}
}
translations.put(file.getName().substring(0, file.getName().lastIndexOf(".")), mapping);
} catch (Exception e) {
new ResourceLoadingException("Unable to load language " + file.getAbsolutePath(), e).printStackTrace();
}
}
}
for (Entry<String, Map<String, String>> entry : translations.entrySet()) {
String key = entry.getKey();
Map<String, String> mapping = this.translations.get(key);
if (mapping == null) {
this.translations.put(key, entry.getValue());
} else {
mapping.putAll(entry.getValue());
}
}
}
use of com.loohp.interactivechat.libs.org.json.simple.JSONObject in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class AssetsDownloader method loadLibraries.
public static void loadLibraries(File rootFolder) {
try {
File hashes = new File(rootFolder, "hashes.json");
if (!hashes.exists()) {
try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(hashes), StandardCharsets.UTF_8))) {
pw.println("{}");
pw.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
JSONObject json;
try (InputStreamReader hashReader = new InputStreamReader(new FileInputStream(hashes), StandardCharsets.UTF_8)) {
json = (JSONObject) new JSONParser().parse(hashReader);
} catch (Throwable e) {
new RuntimeException("Invalid hashes.json! It will be reset.", e).printStackTrace();
json = new JSONObject();
}
String oldHash = InteractiveChatDiscordSrvAddon.plugin.defaultResourceHash = json.containsKey("libs") ? json.get("libs").toString() : "EMPTY";
String oldVersion = json.containsKey("version") ? json.get("version").toString() : "EMPTY";
File libsFolder = new File(rootFolder, "libs");
libsFolder.mkdirs();
LibraryDownloadManager downloadManager = new LibraryDownloadManager(libsFolder);
String hash = downloadManager.getHash();
if (!hash.equals(oldHash) || !InteractiveChatDiscordSrvAddon.plugin.getDescription().getVersion().equals(oldVersion)) {
downloadManager.downloadLibraries((result, jarName) -> {
if (result) {
Bukkit.getConsoleSender().sendMessage("[ICDiscordSrvAddon] Downloaded library \"" + jarName + "\"");
} else {
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[ICDiscordSrvAddon] Unable to download library \"" + jarName + "\"");
}
});
}
LibraryLoader.loadLibraries(libsFolder, (file, e) -> {
String jarName = file.getName();
if (e == null) {
Bukkit.getConsoleSender().sendMessage("[ICDiscordSrvAddon] Loaded library \"" + jarName + "\"");
} else {
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[ICDiscordSrvAddon] Unable to load library \"" + jarName + "\"");
e.printStackTrace();
}
});
json.put("libs", hash);
json.put("version", InteractiveChatDiscordSrvAddon.plugin.getDescription().getVersion());
try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(hashes), StandardCharsets.UTF_8))) {
Gson g = new GsonBuilder().setPrettyPrinting().create();
pw.println(g.toJson(new JsonParser().parse(json.toString())));
pw.flush();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.loohp.interactivechat.libs.org.json.simple.JSONObject in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class InteractiveChatDiscordSrvAddon method cachePlayerSkin.
private void cachePlayerSkin(ICPlayer player) {
Debug.debug("Caching skin for player " + player.getName() + " (" + player.getUniqueId() + ")");
if (player.isLocal()) {
try {
UUID uuid = player.getUniqueId();
JSONObject json = (JSONObject) new JSONParser().parse(SkinUtils.getSkinJsonFromProfile(player.getLocalPlayer()));
String value = (String) ((JSONObject) ((JSONObject) json.get("textures")).get("SKIN")).get("url");
BufferedImage skin = ImageUtils.downloadImage(value);
Cache.putCache(uuid + value + ImageGeneration.PLAYER_SKIN_CACHE_KEY, skin, InteractiveChatDiscordSrvAddon.plugin.cacheTimeout);
} catch (Exception e) {
}
} else {
try {
UUID uuid = player.getUniqueId();
String value = SkinUtils.getSkinURLFromUUID(uuid);
BufferedImage skin = ImageUtils.downloadImage(value);
Cache.putCache(uuid + "null" + ImageGeneration.PLAYER_SKIN_CACHE_KEY, skin, InteractiveChatDiscordSrvAddon.plugin.cacheTimeout);
} catch (Exception e) {
}
}
}
use of com.loohp.interactivechat.libs.org.json.simple.JSONObject in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class LibraryDownloadManager method downloadLibraries.
public synchronized void downloadLibraries(BiConsumer<Boolean, String> progressListener) {
ensureData();
try {
JSONObject libs = (JSONObject) data.get("libs");
Set<String> jarNames = new HashSet<>();
for (Object key : libs.keySet()) {
String jarName = (String) key;
jarNames.add(jarName);
JSONObject details = (JSONObject) libs.get(jarName);
String url = (String) details.get("url");
File jarFile = new File(libsFolder, jarName);
if (HTTPRequestUtils.download(jarFile, url)) {
progressListener.accept(true, jarName);
} else {
progressListener.accept(false, jarName);
}
}
for (File jarFile : libsFolder.listFiles()) {
if (!jarNames.contains(jarFile.getName())) {
jarFile.delete();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.loohp.interactivechat.libs.org.json.simple.JSONObject in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class ImageGeneration method getTabListImage.
public static BufferedImage getTabListImage(List<Component> header, List<Component> footer, List<ValueTrios<UUID, Component, Integer>> players, boolean showAvatar, boolean showPing) {
List<ValuePairs<BufferedImage, Integer>> playerImages = new ArrayList<>(players.size());
int masterOffsetX = 0;
for (ValueTrios<UUID, Component, Integer> trio : players) {
UUID uuid = trio.getFirst();
Component name = trio.getSecond();
int ping = trio.getThird();
BufferedImage image = new BufferedImage(2048, TABLIST_INTERNAL_HEIGHT, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
int offsetX = 0;
if (showAvatar) {
BufferedImage skin;
try {
Player onlinePlayer = Bukkit.getPlayer(uuid);
if (onlinePlayer == null) {
Cache<?> cache = Cache.getCache(uuid + "null" + PLAYER_SKIN_CACHE_KEY);
if (cache == null) {
String value = SkinUtils.getSkinURLFromUUID(uuid);
skin = ImageUtils.downloadImage(value);
Cache.putCache(uuid + "null" + PLAYER_SKIN_CACHE_KEY, skin, InteractiveChatDiscordSrvAddon.plugin.cacheTimeout);
skin = ImageUtils.copyImage(skin);
} else {
skin = ImageUtils.copyImage((BufferedImage) cache.getObject());
}
} else {
try {
JSONObject json = (JSONObject) new JSONParser().parse(SkinUtils.getSkinJsonFromProfile(onlinePlayer));
String value = (String) ((JSONObject) ((JSONObject) json.get("textures")).get("SKIN")).get("url");
Cache<?> cache = Cache.getCache(uuid + value + PLAYER_SKIN_CACHE_KEY);
if (cache == null) {
skin = ImageUtils.downloadImage(value);
Cache.putCache(uuid + value + PLAYER_SKIN_CACHE_KEY, skin, InteractiveChatDiscordSrvAddon.plugin.cacheTimeout);
} else {
skin = (BufferedImage) cache.getObject();
}
skin = ImageUtils.copyImage(skin);
} catch (Exception e) {
String value = SkinUtils.getSkinURLFromUUID(uuid);
skin = ImageUtils.downloadImage(value);
}
}
} catch (Exception e) {
skin = resourceManager.get().getTextureManager().getTexture(ResourceRegistry.ENTITY_TEXTURE_LOCATION + "steve").getTexture(64, 64);
}
BufferedImage avatar = ImageUtils.copyAndGetSubImage(skin, 8, 8, 8, 8);
BufferedImage avatarOverlay = ImageUtils.copyAndGetSubImage(skin, 40, 8, 8, 8);
if (ModelUtils.isRenderedUpsideDown(name)) {
avatar = ImageUtils.rotateImageByDegrees(avatar, 180);
avatarOverlay = ImageUtils.rotateImageByDegrees(avatarOverlay, 180);
}
g.drawImage(avatar, offsetX, (TABLIST_INTERNAL_HEIGHT - 18) / 2, 16, 16, null);
g.drawImage(avatarOverlay, offsetX, (TABLIST_INTERNAL_HEIGHT - 18) / 2, 16, 16, null);
offsetX += 18;
} else {
offsetX += 2;
}
g.dispose();
ImageUtils.printComponent(resourceManager.get(), image, name, InteractiveChatDiscordSrvAddon.plugin.language, version.get().isLegacyRGB(), offsetX, (TABLIST_INTERNAL_HEIGHT - 18) / 2 - 1, 16);
int lastX = InteractiveChatDiscordSrvAddon.plugin.playerlistCommandMinWidth;
for (int x = InteractiveChatDiscordSrvAddon.plugin.playerlistCommandMinWidth; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
if (image.getRGB(x, y) != 0) {
lastX = x;
break;
}
}
}
if (lastX > masterOffsetX) {
masterOffsetX = lastX;
}
playerImages.add(new ValuePairs<>(image, ping));
}
List<BufferedImage> playerRows = new ArrayList<>(playerImages.size());
if (showPing) {
masterOffsetX += 26;
} else {
masterOffsetX += 2;
}
for (ValuePairs<BufferedImage, Integer> pair : playerImages) {
BufferedImage image = pair.getFirst();
if (showPing) {
BufferedImage ping = getPingIcon(pair.getSecond());
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
g.drawImage(ImageUtils.resizeImageAbs(ping, 20, 14), masterOffsetX - 22, (TABLIST_INTERNAL_HEIGHT - 18) / 2 + 2, null);
g.dispose();
}
BufferedImage cropped = new BufferedImage(masterOffsetX, TABLIST_INTERNAL_HEIGHT, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = cropped.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
g.setColor(TABLIST_PLAYER_BACKGROUND);
g.fillRect(0, (TABLIST_INTERNAL_HEIGHT - 18) / 2, cropped.getWidth(), 16);
g.drawImage(image, 0, 0, null);
g.dispose();
playerRows.add(cropped);
}
Map<BufferedImage, Integer> headerLines = new LinkedHashMap<>(header.size());
for (Component line : header) {
BufferedImage image = new BufferedImage(2048, TABLIST_INTERNAL_HEIGHT, BufferedImage.TYPE_INT_ARGB);
ImageUtils.printComponent(resourceManager.get(), image, line, InteractiveChatDiscordSrvAddon.plugin.language, version.get().isLegacyRGB(), 0, (TABLIST_INTERNAL_HEIGHT - 18) / 2 - 1, 16);
int lastX = 0;
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
if (image.getRGB(x, y) != 0) {
lastX = x;
break;
}
}
}
BufferedImage cropped = new BufferedImage(Math.max(1, lastX), TABLIST_INTERNAL_HEIGHT, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = cropped.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
g.drawImage(image, 0, 0, null);
g.dispose();
headerLines.put(cropped, lastX);
}
Map<BufferedImage, Integer> footerLines = new LinkedHashMap<>(footer.size());
for (Component line : footer) {
BufferedImage image = new BufferedImage(2048, TABLIST_INTERNAL_HEIGHT, BufferedImage.TYPE_INT_ARGB);
ImageUtils.printComponent(resourceManager.get(), image, line, InteractiveChatDiscordSrvAddon.plugin.language, version.get().isLegacyRGB(), 0, (TABLIST_INTERNAL_HEIGHT - 18) / 2 - 1, 16);
int lastX = 0;
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
if (image.getRGB(x, y) != 0) {
lastX = x;
break;
}
}
}
BufferedImage cropped = new BufferedImage(Math.max(1, lastX), TABLIST_INTERNAL_HEIGHT, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = cropped.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
g.drawImage(image, 0, 0, null);
g.dispose();
footerLines.put(cropped, lastX);
}
BufferedImage image;
if (playerRows.size() <= TABLIST_SINGLE_COLUMN_LIMIT) {
image = new BufferedImage(masterOffsetX + 4, playerRows.size() * 18 + 2, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
g.setColor(TABLIST_BACKGROUND);
g.fillRect(0, 0, image.getWidth(), image.getHeight());
int offsetY = -((TABLIST_INTERNAL_HEIGHT - 18) / 2);
for (BufferedImage each : playerRows) {
g.drawImage(each, 2, offsetY, null);
offsetY += 18;
}
g.dispose();
} else {
image = new BufferedImage(masterOffsetX * 2 + 6, (int) Math.ceil((double) playerRows.size() / 2) * 18 + 2, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
g.setColor(TABLIST_BACKGROUND);
g.fillRect(0, 0, image.getWidth(), image.getHeight());
int offsetY = -((TABLIST_INTERNAL_HEIGHT - 18) / 2 - 2);
int half = (int) Math.ceil((double) playerRows.size() / 2);
for (int i = 0; i < half; i++) {
g.drawImage(playerRows.get(i), 2, offsetY, null);
offsetY += 18;
}
offsetY = -((TABLIST_INTERNAL_HEIGHT - 18) / 2 - 2);
for (int i = half; i < playerRows.size(); i++) {
g.drawImage(playerRows.get(i), masterOffsetX + 4, offsetY, null);
offsetY += 18;
}
if (playerRows.size() % 2 == 1) {
g.setColor(TABLIST_PLAYER_BACKGROUND);
g.fillRect(masterOffsetX + 4, offsetY + ((TABLIST_INTERNAL_HEIGHT - 18) / 2), masterOffsetX, 16);
}
g.dispose();
}
int maxOffsetX = Stream.concat(headerLines.values().stream(), footerLines.values().stream()).mapToInt(each -> each).max().orElse(0);
if (maxOffsetX <= 0) {
return image;
} else {
BufferedImage decoration = new BufferedImage(Math.max(image.getWidth(), maxOffsetX + 4), (headerLines.isEmpty() ? 0 : headerLines.size() * 18 + 2) + image.getHeight() + (footerLines.isEmpty() ? 2 : footerLines.size() * 18 + 4), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = decoration.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
int offsetY = -((TABLIST_INTERNAL_HEIGHT - 18) / 2 - 2);
for (BufferedImage each : headerLines.keySet()) {
g.drawImage(each, (decoration.getWidth() / 2) - (each.getWidth() / 2), offsetY, null);
offsetY += 18;
}
g.drawImage(image, (decoration.getWidth() / 2) - (image.getWidth() / 2), offsetY + ((TABLIST_INTERNAL_HEIGHT - 18) / 2), null);
offsetY += image.getHeight() + 2;
for (BufferedImage each : footerLines.keySet()) {
g.drawImage(each, (decoration.getWidth() / 2) - (each.getWidth() / 2), offsetY, null);
offsetY += 18;
}
g.dispose();
int lastY = offsetY + ((TABLIST_INTERNAL_HEIGHT - 18) / 2 - 2);
for (int y = offsetY + ((TABLIST_INTERNAL_HEIGHT - 18) / 2 - 2); y < decoration.getHeight(); y++) {
for (int x = 0; x < decoration.getWidth(); x++) {
if (decoration.getRGB(x, y) != 0) {
lastY = y;
break;
}
}
}
BufferedImage finalDecoration = new BufferedImage(decoration.getWidth(), lastY + 3, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = finalDecoration.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
g2.setColor(TABLIST_BACKGROUND);
g2.fillRect(0, 0, finalDecoration.getWidth(), finalDecoration.getHeight());
g2.drawImage(decoration, 0, 0, null);
g2.dispose();
return finalDecoration;
}
}
Aggregations