use of com.loohp.interactivechat.objectholders.ValuePairs 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