Search in sources :

Example 1 with ItemMapWrapper

use of com.loohp.interactivechatdiscordsrvaddon.wrappers.ItemMapWrapper in project InteractiveChat-DiscordSRV-Addon by LOOHP.

the class ImageGeneration method getMapImage.

public static BufferedImage getMapImage(ItemStack item, Player player) throws Exception {
    if (!FilledMapUtils.isFilledMap(item)) {
        throw new IllegalArgumentException("Provided item is not a filled map");
    }
    InteractiveChatDiscordSrvAddon.plugin.imageCounter.incrementAndGet();
    Debug.debug("ImageGeneration creating map image");
    BufferedImage background = resourceManager.get().getTextureManager().getTexture(ResourceRegistry.MAP_TEXTURE_LOCATION + "map_background").getTexture();
    BufferedImage image = new BufferedImage(1120, 1120, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = image.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
    g.drawImage(background, 0, 0, 1120, 1120, null);
    g.dispose();
    int borderOffset = (int) (image.getWidth() / 23.3333333333333333333);
    int ratio = (image.getWidth() - borderOffset * 2) / 128;
    ItemMapWrapper data = new ItemMapWrapper(item, player);
    for (int widthOffset = 0; widthOffset < 128; widthOffset++) {
        for (int heightOffset = 0; heightOffset < 128; heightOffset++) {
            byte index = data.getColors()[widthOffset + heightOffset * 128];
            if (MapPalette.TRANSPARENT != index) {
                Color color = MapPalette.getColor(index);
                for (int x = 0; x < ratio; x++) {
                    for (int y = 0; y < ratio; y++) {
                        image.setRGB(widthOffset * ratio + borderOffset + x, heightOffset * ratio + borderOffset + y, color.getRGB());
                    }
                }
            }
        }
    }
    Graphics2D g2 = image.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
    BufferedImage asset = resourceManager.get().getTextureManager().getTexture(ResourceRegistry.MAP_TEXTURE_LOCATION + "map_icons").getTexture();
    int iconWidth = asset.getWidth() / MAP_ICON_PER_ROLE;
    for (MapCursor icon : data.getMapCursors()) {
        int x = icon.getX() + 128;
        int y = icon.getY() + 128;
        double rotation = (360.0 / 16.0 * (double) icon.getDirection()) + 180.0;
        int type = icon.getType().ordinal();
        Component component;
        try {
            component = LegacyComponentSerializer.legacySection().deserializeOrNull(icon.getCaption());
        } catch (Throwable e) {
            component = null;
        }
        // String name
        BufferedImage iconImage = ImageUtils.copyAndGetSubImage(asset, type % MAP_ICON_PER_ROLE * iconWidth, type / MAP_ICON_PER_ROLE * iconWidth, iconWidth, iconWidth);
        BufferedImage iconImageBig = new BufferedImage(96, 96, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g3 = iconImageBig.createGraphics();
        g3.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
        g3.drawImage(iconImage, iconImageBig.getWidth() / 6, iconImageBig.getHeight() / 6, 64, 64, null);
        g3.dispose();
        iconImage = iconImageBig;
        BufferedImage iconCan = new BufferedImage(96, 96, BufferedImage.TYPE_INT_ARGB);
        AffineTransform at = new AffineTransform();
        at.rotate(Math.toRadians(rotation), iconImage.getWidth() / 2.0, iconImage.getHeight() / 2.0);
        Graphics2D g2d = iconCan.createGraphics();
        g2d.drawImage(iconImage, at, null);
        g2d.dispose();
        int imageX = x * ratio / 2 + borderOffset;
        int imageY = y * ratio / 2 + borderOffset;
        g2.drawImage(iconCan, imageX - (iconCan.getWidth() / 2), imageY - (iconCan.getHeight() / 2), 96, 96, null);
        if (component != null) {
            ImageUtils.printComponentShadowlessDynamicSize(resourceManager.get(), image, component, InteractiveChatDiscordSrvAddon.plugin.language, version.get().isLegacyRGB(), imageX, imageY + 32, 30, true);
        }
    }
    g2.dispose();
    return image;
}
Also used : ItemMapWrapper(com.loohp.interactivechatdiscordsrvaddon.wrappers.ItemMapWrapper) Color(java.awt.Color) TextColor(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextColor) NamedTextColor(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.NamedTextColor) AffineTransform(java.awt.geom.AffineTransform) Component(com.loohp.interactivechat.libs.net.kyori.adventure.text.Component) BufferedImage(java.awt.image.BufferedImage) MapCursor(org.bukkit.map.MapCursor) Graphics2D(java.awt.Graphics2D)

Aggregations

Component (com.loohp.interactivechat.libs.net.kyori.adventure.text.Component)1 NamedTextColor (com.loohp.interactivechat.libs.net.kyori.adventure.text.format.NamedTextColor)1 TextColor (com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextColor)1 ItemMapWrapper (com.loohp.interactivechatdiscordsrvaddon.wrappers.ItemMapWrapper)1 Color (java.awt.Color)1 Graphics2D (java.awt.Graphics2D)1 AffineTransform (java.awt.geom.AffineTransform)1 BufferedImage (java.awt.image.BufferedImage)1 MapCursor (org.bukkit.map.MapCursor)1