Search in sources :

Example 1 with Cache

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

the class ImageGeneration method getPlayerInventoryImage.

public static BufferedImage getPlayerInventoryImage(Inventory inventory, OfflineICPlayer player) throws Exception {
    InteractiveChatDiscordSrvAddon.plugin.imageCounter.incrementAndGet();
    InteractiveChatDiscordSrvAddon.plugin.inventoryImageCounter.incrementAndGet();
    Debug.debug("ImageGeneration creating player inventory image of " + player.getName());
    BufferedImage background = resourceManager.get().getTextureManager().getTexture(ResourceRegistry.IC_GUI_TEXTURE_LOCATION + "player_inventory").getTexture(356, 336);
    Object playerInventoryData = player.getProperty("player_inventory");
    if (playerInventoryData != null && playerInventoryData instanceof BufferedImage) {
        BufferedImage playerBackground = ImageUtils.copyImage((BufferedImage) playerInventoryData);
        Object mask = player.getProperty("player_inventory_mask");
        if (mask == null) {
            background = playerBackground;
        } else {
            background = ImageUtils.combineWithBinMask(background, playerBackground, (byte[]) mask);
        }
    }
    String key = PLAYER_INVENTORY_CACHE_KEY + HashUtils.createSha1(player.getUniqueId().toString(), inventory) + ImageUtils.hash(background);
    if (!inventory.contains(XMaterial.COMPASS.parseMaterial()) && !inventory.contains(XMaterial.CLOCK.parseMaterial()) && Stream.of(inventory.getContents()).anyMatch(each -> each != null && NBTEditor.contains(each, "CustomModelData"))) {
        Cache<?> cache = Cache.getCache(key);
        if (cache != null) {
            return ImageUtils.copyImage((BufferedImage) cache.getObject());
        }
    }
    BufferedImage target = new BufferedImage(background.getWidth(), background.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = target.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
    g.drawImage(background, 0, 0, null);
    int i = 0;
    // hotbar
    for (; i < 9; i++) {
        ItemStack item = inventory.getItem(i);
        if (item == null || item.getType().equals(Material.AIR)) {
            continue;
        }
        BufferedImage itemImage = getRawItemImage(item, player);
        if (itemImage != null) {
            g.drawImage(itemImage, 18 + (SPACING * (i % 9)), 286 + (SPACING * (i / 9)), null);
        }
    }
    // inv
    for (; i < 36; i++) {
        ItemStack item = inventory.getItem(i);
        if (item == null || item.getType().equals(Material.AIR)) {
            continue;
        }
        BufferedImage itemImage = getRawItemImage(item, player);
        if (itemImage != null) {
            g.drawImage(itemImage, 18 + (SPACING * (i % 9)), 170 + (SPACING * ((i - 9) / 9)), null);
        }
    }
    // boots
    ItemStack boots = inventory.getItem(i);
    if (boots == null || boots.getType().equals(Material.AIR)) {
        g.drawImage(resourceManager.get().getTextureManager().getTexture(ResourceRegistry.ITEM_TEXTURE_LOCATION + "empty_armor_slot_boots").getTexture(32, 32), 18, 126 - (SPACING * (i - 36)), 32, 32, null);
    } else {
        BufferedImage itemImage = getRawItemImage(boots, player);
        if (itemImage != null) {
            g.drawImage(itemImage, 18, 126 - (SPACING * (i - 36)), null);
        }
    }
    i++;
    // leggings
    ItemStack leggings = inventory.getItem(i);
    if (leggings == null || leggings.getType().equals(Material.AIR)) {
        g.drawImage(resourceManager.get().getTextureManager().getTexture(ResourceRegistry.ITEM_TEXTURE_LOCATION + "empty_armor_slot_leggings").getTexture(32, 32), 18, 126 - (SPACING * (i - 36)), 32, 32, null);
    } else {
        BufferedImage itemImage = getRawItemImage(leggings, player);
        if (itemImage != null) {
            g.drawImage(itemImage, 18, 126 - (SPACING * (i - 36)), null);
        }
    }
    i++;
    // chestplate
    ItemStack chestplate = inventory.getItem(i);
    if (chestplate == null || chestplate.getType().equals(Material.AIR)) {
        g.drawImage(resourceManager.get().getTextureManager().getTexture(ResourceRegistry.ITEM_TEXTURE_LOCATION + "empty_armor_slot_chestplate").getTexture(32, 32), 18, 126 - (SPACING * (i - 36)), 32, 32, null);
    } else {
        BufferedImage itemImage = getRawItemImage(chestplate, player);
        if (itemImage != null) {
            g.drawImage(itemImage, 18, 126 - (SPACING * (i - 36)), null);
        }
    }
    i++;
    // helmet
    ItemStack helmet = inventory.getItem(i);
    if (helmet == null || helmet.getType().equals(Material.AIR)) {
        g.drawImage(resourceManager.get().getTextureManager().getTexture(ResourceRegistry.ITEM_TEXTURE_LOCATION + "empty_armor_slot_helmet").getTexture(32, 32), 18, 126 - (SPACING * (i - 36)), 32, 32, null);
    } else {
        BufferedImage itemImage = getRawItemImage(helmet, player);
        if (itemImage != null) {
            g.drawImage(itemImage, 18, 126 - (SPACING * (i - 36)), null);
        }
    }
    i++;
    // offhand
    if (!version.get().isOld()) {
        ItemStack offhand = inventory.getItem(i);
        if (offhand == null || offhand.getType().equals(Material.AIR)) {
            g.drawImage(resourceManager.get().getTextureManager().getTexture(ResourceRegistry.ITEM_TEXTURE_LOCATION + "empty_armor_slot_shield").getTexture(32, 32), 162, 126, 32, 32, null);
        } else {
            BufferedImage itemImage = getRawItemImage(offhand, player);
            if (itemImage != null) {
                g.drawImage(itemImage, 162, 126, null);
            }
        }
    }
    // puppet
    EntityEquipment equipment = player.getEquipment();
    BufferedImage puppet = getFullBodyImage(player, equipment.getHelmet(), equipment.getChestplate(), equipment.getLeggings(), equipment.getBoots());
    g.drawImage(puppet, 45, -10, null);
    g.dispose();
    Cache.putCache(key, target, InteractiveChatDiscordSrvAddon.plugin.cacheTimeout);
    return target;
}
Also used : Color(java.awt.Color) Arrays(java.util.Arrays) ValuePairs(com.loohp.interactivechat.objectholders.ValuePairs) RenderResult(com.loohp.interactivechatdiscordsrvaddon.resources.ModelRenderer.RenderResult) RenderingHints(java.awt.RenderingHints) JSONParser(com.loohp.interactivechat.libs.org.json.simple.parser.JSONParser) Player(org.bukkit.entity.Player) TextureAnimation(com.loohp.interactivechatdiscordsrvaddon.resources.textures.TextureAnimation) Inventory(org.bukkit.inventory.Inventory) PlayerModelItem(com.loohp.interactivechatdiscordsrvaddon.resources.ModelRenderer.PlayerModelItem) ICPlayer(com.loohp.interactivechat.objectholders.ICPlayer) TextureManager(com.loohp.interactivechatdiscordsrvaddon.resources.textures.TextureManager) InteractiveChatComponentSerializer(com.loohp.interactivechat.utils.InteractiveChatComponentSerializer) Map(java.util.Map) TextColor(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextColor) TextureResource(com.loohp.interactivechatdiscordsrvaddon.resources.textures.TextureResource) XMaterial(com.loohp.interactivechat.libs.com.cryptomorin.xseries.XMaterial) Material(org.bukkit.Material) Debug(com.loohp.interactivechatdiscordsrvaddon.debug.Debug) Bukkit(org.bukkit.Bukkit) ItemNBTUtils(com.loohp.interactivechat.utils.ItemNBTUtils) ItemMapWrapper(com.loohp.interactivechatdiscordsrvaddon.wrappers.ItemMapWrapper) BufferedImage(java.awt.image.BufferedImage) XMaterialUtils(com.loohp.interactivechat.utils.XMaterialUtils) UUID(java.util.UUID) AffineTransform(java.awt.geom.AffineTransform) ContainerTitlePrintingFunction(com.loohp.interactivechatdiscordsrvaddon.utils.ContainerTitlePrintingFunction) ItemStack(org.bukkit.inventory.ItemStack) InteractiveChat(com.loohp.interactivechat.InteractiveChat) MapCursor(org.bukkit.map.MapCursor) Damageable(org.bukkit.inventory.meta.Damageable) Base64(java.util.Base64) List(java.util.List) Stream(java.util.stream.Stream) HTTPRequestUtils(com.loohp.interactivechat.utils.HTTPRequestUtils) PlayerModelItemPosition(com.loohp.interactivechatdiscordsrvaddon.resources.ModelRenderer.PlayerModelItemPosition) LeatherArmorMeta(org.bukkit.inventory.meta.LeatherArmorMeta) CustomStringUtils(com.loohp.interactivechat.utils.CustomStringUtils) JSONArray(com.loohp.interactivechat.libs.org.json.simple.JSONArray) NBTEditor(com.loohp.interactivechat.libs.io.github.bananapuncher714.nbteditor.NBTEditor) ItemRenderUtils(com.loohp.interactivechatdiscordsrvaddon.utils.ItemRenderUtils) ItemStackProcessResult(com.loohp.interactivechatdiscordsrvaddon.utils.ItemRenderUtils.ItemStackProcessResult) Cache(com.loohp.interactivechatdiscordsrvaddon.Cache) FilledMapUtils(com.loohp.interactivechat.utils.FilledMapUtils) MCVersion(com.loohp.interactivechat.utils.MCVersion) ResourceManager(com.loohp.interactivechatdiscordsrvaddon.resources.ResourceManager) HashMap(java.util.HashMap) GeneratedTextureResource(com.loohp.interactivechatdiscordsrvaddon.resources.textures.GeneratedTextureResource) InteractiveChatDiscordSrvAddon(com.loohp.interactivechatdiscordsrvaddon.InteractiveChatDiscordSrvAddon) Supplier(java.util.function.Supplier) SkinUtils(com.loohp.interactivechat.utils.SkinUtils) ComponentStyling(com.loohp.interactivechat.utils.ComponentStyling) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) AdvancementType(com.loohp.interactivechatdiscordsrvaddon.objectholders.AdvancementType) Graphics2D(java.awt.Graphics2D) ModelOverrideType(com.loohp.interactivechatdiscordsrvaddon.resources.models.ModelOverride.ModelOverrideType) OfflineICPlayer(com.loohp.interactivechat.objectholders.OfflineICPlayer) JSONObject(com.loohp.interactivechat.libs.org.json.simple.JSONObject) ItemStackUtils(com.loohp.interactivechat.utils.ItemStackUtils) Component(com.loohp.interactivechat.libs.net.kyori.adventure.text.Component) ResourceRegistry(com.loohp.interactivechatdiscordsrvaddon.registry.ResourceRegistry) ModelUtils(com.loohp.interactivechatdiscordsrvaddon.utils.ModelUtils) TextureMeta(com.loohp.interactivechatdiscordsrvaddon.resources.textures.TextureMeta) IOException(java.io.IOException) TintIndexData(com.loohp.interactivechatdiscordsrvaddon.utils.TintUtils.TintIndexData) ValueTrios(com.loohp.interactivechat.objectholders.ValueTrios) TextureProperties(com.loohp.interactivechatdiscordsrvaddon.resources.textures.TextureProperties) TranslationKeyUtils(com.loohp.interactivechatdiscordsrvaddon.utils.TranslationKeyUtils) MapPalette(org.bukkit.map.MapPalette) EntityEquipment(org.bukkit.inventory.EntityEquipment) NamedTextColor(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.NamedTextColor) HashUtils(com.loohp.interactivechat.utils.HashUtils) ModelDisplayPosition(com.loohp.interactivechatdiscordsrvaddon.resources.models.ModelDisplay.ModelDisplayPosition) LegacyComponentSerializer(com.loohp.interactivechat.libs.net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer) EntityEquipment(org.bukkit.inventory.EntityEquipment) JSONObject(com.loohp.interactivechat.libs.org.json.simple.JSONObject) ItemStack(org.bukkit.inventory.ItemStack) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 2 with Cache

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

the class ImageGeneration method getInventoryImage.

public static BufferedImage getInventoryImage(Inventory inventory, Component title, OfflineICPlayer player) throws Exception {
    InteractiveChatDiscordSrvAddon.plugin.imageCounter.incrementAndGet();
    InteractiveChatDiscordSrvAddon.plugin.inventoryImageCounter.incrementAndGet();
    Debug.debug("ImageGeneration creating inventory image of " + player.getName());
    String key = INVENTORY_CACHE_KEY + HashUtils.createSha1("Inventory", inventory);
    if (!inventory.contains(XMaterial.COMPASS.parseMaterial()) && !inventory.contains(XMaterial.CLOCK.parseMaterial()) && Stream.of(inventory.getContents()).anyMatch(each -> each != null && NBTEditor.contains(each, "CustomModelData"))) {
        Cache<?> cache = Cache.getCache(key);
        if (cache != null) {
            return ImageUtils.copyImage((BufferedImage) cache.getObject());
        }
    }
    int rows = inventory.getSize() / 9;
    GenericContainerBackgroundResult result = getGenericContainerBackground(rows, (image, x, y, fontSize, defaultTextColor) -> {
        Component defaultColorTitle = title == null ? Component.translatable(TranslationKeyUtils.getDefaultContainerTitle()).color(defaultTextColor) : title.colorIfAbsent(defaultTextColor);
        return ImageUtils.printComponentShadowless(resourceManager.get(), image, defaultColorTitle, InteractiveChatDiscordSrvAddon.plugin.language, version.get().isLegacyRGB(), x, y, fontSize);
    });
    BufferedImage background = result.getBackgroundImage();
    BufferedImage target = new BufferedImage(background.getWidth(), background.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = target.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
    g.drawImage(background, 0, 0, null);
    for (int i = 0; i < inventory.getSize(); i++) {
        ItemStack item = inventory.getItem(i);
        if (item == null || item.getType().equals(Material.AIR)) {
            continue;
        }
        BufferedImage itemImage = getRawItemImage(item, player);
        if (itemImage != null) {
            g.drawImage(itemImage, result.getExpandedX() + 18 + (SPACING * (i % 9)), result.getExpandedY() + 38 + (SPACING * (i / 9)), null);
        }
    }
    g.dispose();
    Cache.putCache(key, target, InteractiveChatDiscordSrvAddon.plugin.cacheTimeout);
    return target;
}
Also used : Color(java.awt.Color) Arrays(java.util.Arrays) ValuePairs(com.loohp.interactivechat.objectholders.ValuePairs) RenderResult(com.loohp.interactivechatdiscordsrvaddon.resources.ModelRenderer.RenderResult) RenderingHints(java.awt.RenderingHints) JSONParser(com.loohp.interactivechat.libs.org.json.simple.parser.JSONParser) Player(org.bukkit.entity.Player) TextureAnimation(com.loohp.interactivechatdiscordsrvaddon.resources.textures.TextureAnimation) Inventory(org.bukkit.inventory.Inventory) PlayerModelItem(com.loohp.interactivechatdiscordsrvaddon.resources.ModelRenderer.PlayerModelItem) ICPlayer(com.loohp.interactivechat.objectholders.ICPlayer) TextureManager(com.loohp.interactivechatdiscordsrvaddon.resources.textures.TextureManager) InteractiveChatComponentSerializer(com.loohp.interactivechat.utils.InteractiveChatComponentSerializer) Map(java.util.Map) TextColor(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextColor) TextureResource(com.loohp.interactivechatdiscordsrvaddon.resources.textures.TextureResource) XMaterial(com.loohp.interactivechat.libs.com.cryptomorin.xseries.XMaterial) Material(org.bukkit.Material) Debug(com.loohp.interactivechatdiscordsrvaddon.debug.Debug) Bukkit(org.bukkit.Bukkit) ItemNBTUtils(com.loohp.interactivechat.utils.ItemNBTUtils) ItemMapWrapper(com.loohp.interactivechatdiscordsrvaddon.wrappers.ItemMapWrapper) BufferedImage(java.awt.image.BufferedImage) XMaterialUtils(com.loohp.interactivechat.utils.XMaterialUtils) UUID(java.util.UUID) AffineTransform(java.awt.geom.AffineTransform) ContainerTitlePrintingFunction(com.loohp.interactivechatdiscordsrvaddon.utils.ContainerTitlePrintingFunction) ItemStack(org.bukkit.inventory.ItemStack) InteractiveChat(com.loohp.interactivechat.InteractiveChat) MapCursor(org.bukkit.map.MapCursor) Damageable(org.bukkit.inventory.meta.Damageable) Base64(java.util.Base64) List(java.util.List) Stream(java.util.stream.Stream) HTTPRequestUtils(com.loohp.interactivechat.utils.HTTPRequestUtils) PlayerModelItemPosition(com.loohp.interactivechatdiscordsrvaddon.resources.ModelRenderer.PlayerModelItemPosition) LeatherArmorMeta(org.bukkit.inventory.meta.LeatherArmorMeta) CustomStringUtils(com.loohp.interactivechat.utils.CustomStringUtils) JSONArray(com.loohp.interactivechat.libs.org.json.simple.JSONArray) NBTEditor(com.loohp.interactivechat.libs.io.github.bananapuncher714.nbteditor.NBTEditor) ItemRenderUtils(com.loohp.interactivechatdiscordsrvaddon.utils.ItemRenderUtils) ItemStackProcessResult(com.loohp.interactivechatdiscordsrvaddon.utils.ItemRenderUtils.ItemStackProcessResult) Cache(com.loohp.interactivechatdiscordsrvaddon.Cache) FilledMapUtils(com.loohp.interactivechat.utils.FilledMapUtils) MCVersion(com.loohp.interactivechat.utils.MCVersion) ResourceManager(com.loohp.interactivechatdiscordsrvaddon.resources.ResourceManager) HashMap(java.util.HashMap) GeneratedTextureResource(com.loohp.interactivechatdiscordsrvaddon.resources.textures.GeneratedTextureResource) InteractiveChatDiscordSrvAddon(com.loohp.interactivechatdiscordsrvaddon.InteractiveChatDiscordSrvAddon) Supplier(java.util.function.Supplier) SkinUtils(com.loohp.interactivechat.utils.SkinUtils) ComponentStyling(com.loohp.interactivechat.utils.ComponentStyling) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) AdvancementType(com.loohp.interactivechatdiscordsrvaddon.objectholders.AdvancementType) Graphics2D(java.awt.Graphics2D) ModelOverrideType(com.loohp.interactivechatdiscordsrvaddon.resources.models.ModelOverride.ModelOverrideType) OfflineICPlayer(com.loohp.interactivechat.objectholders.OfflineICPlayer) JSONObject(com.loohp.interactivechat.libs.org.json.simple.JSONObject) ItemStackUtils(com.loohp.interactivechat.utils.ItemStackUtils) Component(com.loohp.interactivechat.libs.net.kyori.adventure.text.Component) ResourceRegistry(com.loohp.interactivechatdiscordsrvaddon.registry.ResourceRegistry) ModelUtils(com.loohp.interactivechatdiscordsrvaddon.utils.ModelUtils) TextureMeta(com.loohp.interactivechatdiscordsrvaddon.resources.textures.TextureMeta) IOException(java.io.IOException) TintIndexData(com.loohp.interactivechatdiscordsrvaddon.utils.TintUtils.TintIndexData) ValueTrios(com.loohp.interactivechat.objectholders.ValueTrios) TextureProperties(com.loohp.interactivechatdiscordsrvaddon.resources.textures.TextureProperties) TranslationKeyUtils(com.loohp.interactivechatdiscordsrvaddon.utils.TranslationKeyUtils) MapPalette(org.bukkit.map.MapPalette) EntityEquipment(org.bukkit.inventory.EntityEquipment) NamedTextColor(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.NamedTextColor) HashUtils(com.loohp.interactivechat.utils.HashUtils) ModelDisplayPosition(com.loohp.interactivechatdiscordsrvaddon.resources.models.ModelDisplay.ModelDisplayPosition) LegacyComponentSerializer(com.loohp.interactivechat.libs.net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer) Component(com.loohp.interactivechat.libs.net.kyori.adventure.text.Component) ItemStack(org.bukkit.inventory.ItemStack) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Aggregations

InteractiveChat (com.loohp.interactivechat.InteractiveChat)2 XMaterial (com.loohp.interactivechat.libs.com.cryptomorin.xseries.XMaterial)2 NBTEditor (com.loohp.interactivechat.libs.io.github.bananapuncher714.nbteditor.NBTEditor)2 Component (com.loohp.interactivechat.libs.net.kyori.adventure.text.Component)2 NamedTextColor (com.loohp.interactivechat.libs.net.kyori.adventure.text.format.NamedTextColor)2 TextColor (com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextColor)2 LegacyComponentSerializer (com.loohp.interactivechat.libs.net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer)2 JSONArray (com.loohp.interactivechat.libs.org.json.simple.JSONArray)2 JSONObject (com.loohp.interactivechat.libs.org.json.simple.JSONObject)2 JSONParser (com.loohp.interactivechat.libs.org.json.simple.parser.JSONParser)2 ICPlayer (com.loohp.interactivechat.objectholders.ICPlayer)2 OfflineICPlayer (com.loohp.interactivechat.objectholders.OfflineICPlayer)2 ValuePairs (com.loohp.interactivechat.objectholders.ValuePairs)2 ValueTrios (com.loohp.interactivechat.objectholders.ValueTrios)2 ComponentStyling (com.loohp.interactivechat.utils.ComponentStyling)2 CustomStringUtils (com.loohp.interactivechat.utils.CustomStringUtils)2 FilledMapUtils (com.loohp.interactivechat.utils.FilledMapUtils)2 HTTPRequestUtils (com.loohp.interactivechat.utils.HTTPRequestUtils)2 HashUtils (com.loohp.interactivechat.utils.HashUtils)2 InteractiveChatComponentSerializer (com.loohp.interactivechat.utils.InteractiveChatComponentSerializer)2