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