use of com.loohp.interactivechatdiscordsrvaddon.utils.DiscordItemStackUtils.DiscordToolTip in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class OutboundToDiscordEvents method onDeathMessageSend.
@Subscribe(priority = ListenerPriority.HIGHEST)
public void onDeathMessageSend(DeathMessagePostProcessEvent event) {
if (event.isCancelled()) {
return;
}
if (!InteractiveChatDiscordSrvAddon.plugin.deathMessageItem) {
return;
}
Debug.debug("Triggered onDeathMessageSend");
ItemStack item = DEATH_BY.remove(event.getPlayer().getUniqueId());
if (item == null || item.getType().equals(Material.AIR)) {
return;
}
if (!item.hasItemMeta()) {
return;
}
ItemMeta meta = item.getItemMeta();
if (meta == null || !meta.hasDisplayName() || meta.getDisplayName().length() <= 0) {
return;
}
Color color = null;
if (!event.getDiscordMessage().getEmbeds().isEmpty()) {
color = event.getDiscordMessage().getEmbeds().get(0).getColor();
}
if (color == null) {
color = Color.black;
}
Player player = event.getPlayer();
DiscordMessageContent content = new DiscordMessageContent(ChatColorUtils.stripColor(meta.getDisplayName()), "attachment://Item.png", color);
try {
BufferedImage image = ImageGeneration.getItemStackImage(item, ICPlayerFactory.getICPlayer(player), InteractiveChatDiscordSrvAddon.plugin.itemAltAir);
byte[] itemData = ImageUtils.toArray(image);
DiscordDescription description = DiscordItemStackUtils.getDiscordDescription(item, player);
content.addAttachment("Item.png", itemData);
if (InteractiveChatDiscordSrvAddon.plugin.itemUseTooltipImage) {
DiscordToolTip discordToolTip = DiscordItemStackUtils.getToolTip(item, player);
if (!discordToolTip.isBaseItem() || InteractiveChatDiscordSrvAddon.plugin.itemUseTooltipImageOnBaseItem) {
BufferedImage tooltip = ImageGeneration.getToolTipImage(discordToolTip.getComponents());
byte[] tooltipData = ImageUtils.toArray(tooltip);
content.addAttachment("ToolTip.png", tooltipData);
content.addImageUrl("attachment://ToolTip.png");
} else {
content.addDescription(description.getDescription().orElse(null));
}
} else {
content.addDescription(description.getDescription().orElse(null));
}
} catch (Exception e) {
e.printStackTrace();
}
Bukkit.getScheduler().runTaskLaterAsynchronously(InteractiveChat.plugin, () -> {
Debug.debug("onDeathMessageSend sending item to discord");
TextChannel destinationChannel = DiscordSRV.getPlugin().getDestinationTextChannelForGameChannelName(event.getChannel());
if (event.isUsingWebhooks()) {
String webHookUrl = WebhookUtil.getWebhookUrlToUseForChannel(destinationChannel);
WebhookClient client = WebhookClient.withUrl(webHookUrl);
if (client == null) {
throw new NullPointerException("Unable to get the Webhook client URL for the TextChannel " + destinationChannel.getName());
}
client.send(content.toWebhookMessageBuilder().setUsername(event.getWebhookName()).setAvatarUrl(event.getWebhookAvatarUrl()).build());
client.close();
} else {
content.toJDAMessageRestAction(destinationChannel).queue();
}
}, 5);
}
use of com.loohp.interactivechatdiscordsrvaddon.utils.DiscordItemStackUtils.DiscordToolTip in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class DiscordCommands method onSlashCommand.
@Override
public void onSlashCommand(SlashCommandEvent event) {
if (!(event.getChannel() instanceof TextChannel)) {
return;
}
TextChannel channel = (TextChannel) event.getChannel();
String label = event.getName();
if (InteractiveChatDiscordSrvAddon.plugin.resourcepackCommandEnabled && label.equalsIgnoreCase(RESOURCEPACK_LABEL)) {
if (InteractiveChatDiscordSrvAddon.plugin.resourcepackCommandIsMainServer) {
event.deferReply().setEphemeral(true).queue();
List<MessageEmbed> messageEmbeds = new ArrayList<>();
Map<String, byte[]> attachments = new HashMap<>();
int i = 0;
for (ResourcePackInfo packInfo : InteractiveChatDiscordSrvAddon.plugin.resourceManager.getResourcePackInfo()) {
i++;
String packName = packInfo.getName();
Component description = packInfo.getDescription();
EmbedBuilder builder = new EmbedBuilder().setAuthor(packName).setThumbnail("attachment://" + i + ".png");
if (packInfo.getStatus()) {
builder.setDescription(PlainTextComponentSerializer.plainText().serialize(description));
ChatColor firstColor = ChatColorUtils.getColor(LegacyComponentSerializer.builder().useUnusualXRepeatedCharacterHexFormat().character(ChatColorUtils.COLOR_CHAR).build().serialize(description));
Color color = ColorUtils.getColor(firstColor);
if (color == null) {
color = new Color(0xAAAAAA);
} else if (color.equals(Color.WHITE)) {
color = new Color(0xFFFFFE);
}
builder.setColor(color);
if (packInfo.comparePackFormat() > 0) {
builder.setFooter(LanguageUtils.getTranslation(TranslationKeyUtils.getNewIncompatiblePack(), InteractiveChatDiscordSrvAddon.plugin.language));
} else if (packInfo.comparePackFormat() < 0) {
builder.setFooter(LanguageUtils.getTranslation(TranslationKeyUtils.getOldIncompatiblePack(), InteractiveChatDiscordSrvAddon.plugin.language));
}
} else {
builder.setColor(0xFF0000).setDescription(packInfo.getRejectedReason());
}
messageEmbeds.add(builder.build());
try {
attachments.put(i + ".png", ImageUtils.toArray(ImageUtils.resizeImageAbs(packInfo.getIcon(), 128, 128)));
} catch (IOException e) {
e.printStackTrace();
}
}
WebhookMessageUpdateAction<Message> action = event.getHook().setEphemeral(true).editOriginal("**" + LanguageUtils.getTranslation(TranslationKeyUtils.getServerResourcePack(), InteractiveChatDiscordSrvAddon.plugin.language) + "**").setEmbeds(messageEmbeds);
for (Entry<String, byte[]> entry : attachments.entrySet()) {
action = action.addFile(entry.getValue(), entry.getKey());
}
action.queue();
}
} else if (InteractiveChatDiscordSrvAddon.plugin.playerlistCommandEnabled && label.equalsIgnoreCase(PLAYERLIST_LABEL)) {
String minecraftChannel = discordsrv.getChannels().entrySet().stream().filter(entry -> channel.getId().equals(entry.getValue())).map(Map.Entry::getKey).findFirst().orElse(null);
if (minecraftChannel == null) {
if (InteractiveChatDiscordSrvAddon.plugin.respondToCommandsInInvalidChannels && InteractiveChatDiscordSrvAddon.plugin.playerlistCommandIsMainServer) {
event.reply(ChatColorUtils.stripColor(InteractiveChatDiscordSrvAddon.plugin.invalidDiscordChannel)).setEphemeral(true).queue();
}
return;
}
AtomicBoolean deleted = new AtomicBoolean(false);
event.deferReply().queue(hook -> {
if (InteractiveChatDiscordSrvAddon.plugin.playerlistCommandDeleteAfter > 0) {
Bukkit.getScheduler().runTaskLaterAsynchronously(InteractiveChatDiscordSrvAddon.plugin, () -> {
if (!deleted.get()) {
hook.deleteOriginal().queue();
}
}, InteractiveChatDiscordSrvAddon.plugin.playerlistCommandDeleteAfter * 20L);
}
});
Map<OfflinePlayer, Integer> players;
if (InteractiveChat.bungeecordMode && InteractiveChatDiscordSrvAddon.plugin.playerlistCommandBungeecord && !Bukkit.getOnlinePlayers().isEmpty()) {
try {
List<ValueTrios<UUID, String, Integer>> bungeePlayers = InteractiveChatAPI.getBungeecordPlayerList().get();
players = new LinkedHashMap<>(bungeePlayers.size());
for (ValueTrios<UUID, String, Integer> playerinfo : bungeePlayers) {
if (!VanishUtils.isVanished(playerinfo.getFirst())) {
players.put(Bukkit.getOfflinePlayer(playerinfo.getFirst()), playerinfo.getThird());
}
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
event.getHook().editOriginal(ChatColorUtils.stripColor(InteractiveChatDiscordSrvAddon.plugin.unableToRetrieveData) + " (-1)").queue();
return;
}
} else {
players = Bukkit.getOnlinePlayers().stream().filter(each -> !VanishUtils.isVanished(each.getUniqueId())).collect(Collectors.toMap(each -> each, each -> PlayerUtils.getPing(each)));
}
if (players.isEmpty()) {
event.getHook().editOriginal(ChatColorUtils.stripColor(InteractiveChatDiscordSrvAddon.plugin.playerlistCommandEmptyServer)).queue();
} else {
int errorCode = -2;
try {
String[] groups = getGroups();
List<ValueTrios<UUID, Component, Integer>> player = new ArrayList<>();
Map<UUID, Integer> playerGroups = new HashMap<>();
for (Entry<OfflinePlayer, Integer> entry : players.entrySet()) {
OfflinePlayer bukkitOfflinePlayer = entry.getKey();
@SuppressWarnings("deprecation") OfflineICPlayer offlinePlayer = ICPlayerFactory.getUnsafe().getOfflineICPPlayerWithoutInitialization(bukkitOfflinePlayer.getUniqueId());
playerGroups.put(offlinePlayer.getUniqueId(), getPlayerGroupOrder(groups, bukkitOfflinePlayer));
player.add(new ValueTrios<>(offlinePlayer.getUniqueId(), LegacyComponentSerializer.legacySection().deserialize(ChatColorUtils.translateAlternateColorCodes('&', PlaceholderParser.parse(offlinePlayer, InteractiveChatDiscordSrvAddon.plugin.playerlistCommandPlayerFormat))), entry.getValue()));
}
errorCode--;
player.sort(Comparator.comparing(each -> playerGroups.getOrDefault(((ValueTrios<UUID, Component, Integer>) each).getFirst(), Integer.MIN_VALUE)).thenComparing(each -> ((ValueTrios<UUID, Component, Integer>) each).getFirst()));
errorCode--;
@SuppressWarnings("deprecation") OfflineICPlayer firstPlayer = ICPlayerFactory.getUnsafe().getOfflineICPPlayerWithoutInitialization(players.keySet().iterator().next().getUniqueId());
List<Component> header = new ArrayList<>();
if (!InteractiveChatDiscordSrvAddon.plugin.playerlistCommandHeader.isEmpty()) {
header = ComponentStyling.splitAtLineBreaks(LegacyComponentSerializer.legacySection().deserialize(ChatColorUtils.translateAlternateColorCodes('&', PlaceholderParser.parse(firstPlayer, InteractiveChatDiscordSrvAddon.plugin.playerlistCommandHeader.replace("{OnlinePlayers}", players.size() + "")))));
}
errorCode--;
List<Component> footer = new ArrayList<>();
if (!InteractiveChatDiscordSrvAddon.plugin.playerlistCommandFooter.isEmpty()) {
footer = ComponentStyling.splitAtLineBreaks(LegacyComponentSerializer.legacySection().deserialize(ChatColorUtils.translateAlternateColorCodes('&', PlaceholderParser.parse(firstPlayer, InteractiveChatDiscordSrvAddon.plugin.playerlistCommandFooter.replace("{OnlinePlayers}", players.size() + "")))));
}
errorCode--;
BufferedImage image = ImageGeneration.getTabListImage(header, footer, player, InteractiveChatDiscordSrvAddon.plugin.playerlistCommandAvatar, InteractiveChatDiscordSrvAddon.plugin.playerlistCommandPing);
errorCode--;
byte[] data = ImageUtils.toArray(image);
errorCode--;
event.getHook().editOriginalEmbeds(new EmbedBuilder().setImage("attachment://Tablist.png").setColor(InteractiveChatDiscordSrvAddon.plugin.playerlistCommandColor).build()).addFile(data, "Tablist.png").queue(success -> {
if (InteractiveChatDiscordSrvAddon.plugin.playerlistCommandDeleteAfter > 0) {
deleted.set(true);
success.delete().queueAfter(InteractiveChatDiscordSrvAddon.plugin.playerlistCommandDeleteAfter, TimeUnit.SECONDS);
}
});
} catch (Exception e) {
e.printStackTrace();
event.getHook().editOriginal(ChatColorUtils.stripColor(InteractiveChatDiscordSrvAddon.plugin.unableToRetrieveData) + " (" + errorCode + ")").queue();
return;
}
}
} else if (InteractiveChatDiscordSrvAddon.plugin.shareItemCommandEnabled && (label.equalsIgnoreCase(ITEM_LABEL) || label.equalsIgnoreCase(ITEM_OTHER_LABEL))) {
String minecraftChannel = discordsrv.getChannels().entrySet().stream().filter(entry -> channel.getId().equals(entry.getValue())).map(Map.Entry::getKey).findFirst().orElse(null);
if (minecraftChannel == null) {
if (InteractiveChatDiscordSrvAddon.plugin.respondToCommandsInInvalidChannels && InteractiveChatDiscordSrvAddon.plugin.shareInvCommandIsMainServer) {
event.reply(ChatColorUtils.stripColor(InteractiveChatDiscordSrvAddon.plugin.invalidDiscordChannel)).setEphemeral(true).queue();
}
return;
}
String discordUserId = event.getUser().getId();
List<OptionMapping> options = event.getOptionsByType(OptionType.USER);
if (options.size() > 0) {
discordUserId = options.get(0).getAsUser().getId();
}
UUID uuid = discordsrv.getAccountLinkManager().getUuid(discordUserId);
if (uuid == null) {
if (InteractiveChatDiscordSrvAddon.plugin.shareItemCommandIsMainServer) {
event.reply(ChatColorUtils.stripColor(InteractiveChatDiscordSrvAddon.plugin.accountNotLinked)).setEphemeral(true).queue();
}
return;
}
int errorCode = -1;
try {
OfflineICPlayer offlineICPlayer = ICPlayerFactory.getOfflineICPlayer(uuid);
if (offlineICPlayer == null) {
if (InteractiveChatDiscordSrvAddon.plugin.shareItemCommandIsMainServer) {
event.reply(ChatColorUtils.stripColor(InteractiveChatDiscordSrvAddon.plugin.unableToRetrieveData) + " (" + errorCode + ")").setEphemeral(true).queue();
}
return;
}
errorCode--;
if (InteractiveChatDiscordSrvAddon.plugin.shareItemCommandIsMainServer) {
event.deferReply().queue();
}
errorCode--;
ICPlayer icplayer = offlineICPlayer.getPlayer();
if (InteractiveChat.bungeecordMode && icplayer != null) {
if (icplayer.isLocal()) {
ItemStack[] equipment;
if (InteractiveChat.version.isOld()) {
// noinspection deprecation
equipment = new ItemStack[] { icplayer.getEquipment().getHelmet(), icplayer.getEquipment().getChestplate(), icplayer.getEquipment().getLeggings(), icplayer.getEquipment().getBoots(), icplayer.getEquipment().getItemInHand() };
} else {
equipment = new ItemStack[] { icplayer.getEquipment().getHelmet(), icplayer.getEquipment().getChestplate(), icplayer.getEquipment().getLeggings(), icplayer.getEquipment().getBoots(), icplayer.getEquipment().getItemInMainHand(), icplayer.getEquipment().getItemInOffHand() };
}
try {
BungeeMessageSender.forwardEquipment(System.currentTimeMillis(), icplayer.getUniqueId(), icplayer.isRightHanded(), icplayer.getSelectedSlot(), icplayer.getExperienceLevel(), equipment);
} catch (IOException e) {
e.printStackTrace();
}
} else {
TimeUnit.MILLISECONDS.sleep(InteractiveChat.remoteDelay);
}
}
errorCode--;
ItemStack itemStack = resolveItemStack(event, offlineICPlayer);
if (itemStack == null) {
itemStack = new ItemStack(Material.AIR);
}
errorCode--;
BufferedImage itemImage = ImageGeneration.getItemStackImage(itemStack, offlineICPlayer, true);
errorCode--;
String title = ChatColorUtils.stripColor(InteractiveChatDiscordSrvAddon.plugin.shareItemCommandTitle.replace("{Player}", offlineICPlayer.getName()));
errorCode--;
Component itemTag = ItemDisplay.createItemDisplay(offlineICPlayer, itemStack, title, true, null);
Component resolvedItemTag = ComponentStringUtils.convertTranslatables(ComponentModernizing.modernize(itemTag), InteractiveChatDiscordSrvAddon.plugin.resourceManager.getLanguageManager().getTranslateFunction().ofLanguage(InteractiveChatDiscordSrvAddon.plugin.language));
Component component = LegacyComponentSerializer.legacySection().deserialize(InteractiveChatDiscordSrvAddon.plugin.shareItemCommandInGameMessageText.replace("{Player}", offlineICPlayer.getName())).replaceText(TextReplacementConfig.builder().matchLiteral("{ItemTag}").replacement(itemTag).build());
Component resolvedComponent = LegacyComponentSerializer.legacySection().deserialize(InteractiveChatDiscordSrvAddon.plugin.shareItemCommandInGameMessageText.replace("{Player}", offlineICPlayer.getName())).replaceText(TextReplacementConfig.builder().matchLiteral("{ItemTag}").replacement(resolvedItemTag).build());
errorCode--;
String key = "<DiscordShare=" + UUID.randomUUID() + ">";
components.put(key, component);
Bukkit.getScheduler().runTaskLater(InteractiveChatDiscordSrvAddon.plugin, () -> components.remove(key), 100);
errorCode--;
discordsrv.broadcastMessageToMinecraftServer(minecraftChannel, ComponentStringUtils.toDiscordSRVComponent(Component.text(key)), event.getUser());
if (InteractiveChatDiscordSrvAddon.plugin.shareItemCommandIsMainServer) {
errorCode--;
Player bukkitPlayer = icplayer == null || !icplayer.isLocal() ? null : icplayer.getLocalPlayer();
DiscordDescription description = DiscordItemStackUtils.getDiscordDescription(itemStack, bukkitPlayer);
errorCode--;
Color color = DiscordItemStackUtils.getDiscordColor(itemStack);
if (color == null || color.equals(Color.WHITE)) {
color = new Color(0xFFFFFE);
}
EmbedBuilder embedBuilder = new EmbedBuilder().setAuthor(description.getName(), null, "attachment://Item.png").setColor(color);
WebhookMessageUpdateAction<Message> action = event.getHook().editOriginal(PlainTextComponentSerializer.plainText().serialize(resolvedComponent));
errorCode--;
byte[] data = ImageUtils.toArray(itemImage);
action.addFile(data, "Item.png");
errorCode--;
if (InteractiveChatDiscordSrvAddon.plugin.itemUseTooltipImage) {
DiscordToolTip discordToolTip = DiscordItemStackUtils.getToolTip(itemStack, bukkitPlayer);
if (!discordToolTip.isBaseItem() || InteractiveChatDiscordSrvAddon.plugin.itemUseTooltipImageOnBaseItem) {
BufferedImage tooltip = ImageGeneration.getToolTipImage(discordToolTip.getComponents());
byte[] tooltipData = ImageUtils.toArray(tooltip);
action.addFile(tooltipData, "ToolTip.png");
embedBuilder.setImage("attachment://ToolTip.png");
} else {
embedBuilder.setDescription(description.getDescription().orElse(null));
}
} else {
embedBuilder.setDescription(description.getDescription().orElse(null));
}
errorCode--;
action.setEmbeds(embedBuilder.build()).queue();
}
} catch (Exception e) {
e.printStackTrace();
event.getHook().editOriginal(ChatColorUtils.stripColor(InteractiveChatDiscordSrvAddon.plugin.unableToRetrieveData) + " (" + errorCode + ")").queue();
return;
}
} else if (InteractiveChatDiscordSrvAddon.plugin.shareInvCommandEnabled && (label.equalsIgnoreCase(INVENTORY_LABEL) || label.equalsIgnoreCase(INVENTORY_OTHER_LABEL))) {
String minecraftChannel = discordsrv.getChannels().entrySet().stream().filter(entry -> channel.getId().equals(entry.getValue())).map(Map.Entry::getKey).findFirst().orElse(null);
if (minecraftChannel == null) {
if (InteractiveChatDiscordSrvAddon.plugin.respondToCommandsInInvalidChannels && InteractiveChatDiscordSrvAddon.plugin.shareInvCommandIsMainServer) {
event.reply(ChatColorUtils.stripColor(InteractiveChatDiscordSrvAddon.plugin.invalidDiscordChannel)).setEphemeral(true).queue();
}
return;
}
String discordUserId = event.getUser().getId();
List<OptionMapping> options = event.getOptionsByType(OptionType.USER);
if (options.size() > 0) {
discordUserId = options.get(0).getAsUser().getId();
}
UUID uuid = discordsrv.getAccountLinkManager().getUuid(discordUserId);
if (uuid == null) {
if (InteractiveChatDiscordSrvAddon.plugin.shareInvCommandIsMainServer) {
event.reply(ChatColorUtils.stripColor(InteractiveChatDiscordSrvAddon.plugin.accountNotLinked)).setEphemeral(true).queue();
}
return;
}
int errorCode = -1;
try {
OfflineICPlayer offlineICPlayer = ICPlayerFactory.getOfflineICPlayer(uuid);
if (offlineICPlayer == null) {
if (InteractiveChatDiscordSrvAddon.plugin.shareInvCommandIsMainServer) {
event.reply(ChatColorUtils.stripColor(InteractiveChatDiscordSrvAddon.plugin.unableToRetrieveData) + " (" + errorCode + ")").setEphemeral(true).queue();
}
return;
}
errorCode--;
if (InteractiveChatDiscordSrvAddon.plugin.shareInvCommandIsMainServer) {
event.deferReply().queue();
}
errorCode--;
ICPlayer icplayer = offlineICPlayer.getPlayer();
if (InteractiveChat.bungeecordMode && icplayer != null) {
if (icplayer.isLocal()) {
BungeeMessageSender.forwardInventory(System.currentTimeMillis(), uuid, icplayer.isRightHanded(), icplayer.getSelectedSlot(), icplayer.getExperienceLevel(), null, icplayer.getInventory());
} else {
TimeUnit.MILLISECONDS.sleep(InteractiveChat.remoteDelay);
}
}
errorCode--;
BufferedImage image = InteractiveChatDiscordSrvAddon.plugin.usePlayerInvView ? ImageGeneration.getPlayerInventoryImage(offlineICPlayer.getInventory(), offlineICPlayer) : ImageGeneration.getInventoryImage(offlineICPlayer.getInventory(), Component.translatable(TranslationKeyUtils.getDefaultContainerTitle()), offlineICPlayer);
errorCode--;
Component component = LegacyComponentSerializer.legacySection().deserialize(InteractiveChatDiscordSrvAddon.plugin.shareInvCommandInGameMessageText.replace("{Player}", offlineICPlayer.getName()));
errorCode--;
String title = ChatColorUtils.stripColor(InteractiveChatDiscordSrvAddon.plugin.shareInvCommandTitle.replace("{Player}", offlineICPlayer.getName()));
errorCode--;
String sha1 = HashUtils.createSha1(true, offlineICPlayer.getSelectedSlot(), offlineICPlayer.getExperienceLevel(), title, offlineICPlayer.getInventory());
errorCode--;
layout0(offlineICPlayer, sha1, title);
errorCode--;
layout1(offlineICPlayer, sha1, title);
errorCode--;
component = component.hoverEvent(HoverEvent.showText(LegacyComponentSerializer.legacySection().deserialize(InteractiveChatDiscordSrvAddon.plugin.shareInvCommandInGameMessageHover)));
component = component.clickEvent(ClickEvent.runCommand("/interactivechat viewinv " + sha1));
errorCode--;
String key = "<DiscordShare=" + UUID.randomUUID() + ">";
components.put(key, component);
Bukkit.getScheduler().runTaskLater(InteractiveChatDiscordSrvAddon.plugin, () -> components.remove(key), 100);
errorCode--;
discordsrv.broadcastMessageToMinecraftServer(minecraftChannel, ComponentStringUtils.toDiscordSRVComponent(Component.text(key)), event.getUser());
if (InteractiveChatDiscordSrvAddon.plugin.shareInvCommandIsMainServer) {
errorCode--;
EmbedBuilder embedBuilder = new EmbedBuilder().setAuthor(title).setImage("attachment://Inventory.png").setColor(InteractiveChatDiscordSrvAddon.plugin.invColor);
WebhookMessageUpdateAction<Message> action = event.getHook().editOriginal(PlainTextComponentSerializer.plainText().serialize(component));
errorCode--;
byte[] data = ImageUtils.toArray(image);
action.addFile(data, "Inventory.png");
errorCode--;
if (InteractiveChatDiscordSrvAddon.plugin.invShowLevel) {
int level = offlineICPlayer.getExperienceLevel();
byte[] bottleData = ImageUtils.toArray(InteractiveChatDiscordSrvAddon.plugin.modelRenderer.render(32, 32, InteractiveChatDiscordSrvAddon.plugin.resourceManager, InteractiveChat.version.isOld(), "minecraft:item/experience_bottle", ModelDisplayPosition.GUI, false).getImage());
embedBuilder.setFooter(LanguageUtils.getTranslation(TranslationKeyUtils.getLevelTranslation(level), InteractiveChatDiscordSrvAddon.plugin.language).replaceFirst("%s|%d", level + ""), "attachment://Level.png");
action.addFile(bottleData, "Level.png");
}
errorCode--;
action.setEmbeds(embedBuilder.build()).queue();
}
} catch (Exception e) {
e.printStackTrace();
event.getHook().editOriginal(ChatColorUtils.stripColor(InteractiveChatDiscordSrvAddon.plugin.unableToRetrieveData) + " (" + errorCode + ")").queue();
return;
}
} else if (InteractiveChatDiscordSrvAddon.plugin.shareEnderCommandEnabled && (label.equals(ENDERCHEST_LABEL) || label.equals(ENDERCHEST_OTHER_LABEL))) {
String minecraftChannel = discordsrv.getChannels().entrySet().stream().filter(entry -> channel.getId().equals(entry.getValue())).map(Map.Entry::getKey).findFirst().orElse(null);
if (minecraftChannel == null) {
if (InteractiveChatDiscordSrvAddon.plugin.respondToCommandsInInvalidChannels && InteractiveChatDiscordSrvAddon.plugin.shareEnderCommandIsMainServer) {
event.reply(ChatColorUtils.stripColor(InteractiveChatDiscordSrvAddon.plugin.invalidDiscordChannel)).setEphemeral(true).queue();
}
return;
}
String discordUserId = event.getUser().getId();
List<OptionMapping> options = event.getOptionsByType(OptionType.USER);
if (options.size() > 0) {
discordUserId = options.get(0).getAsUser().getId();
}
UUID uuid = discordsrv.getAccountLinkManager().getUuid(discordUserId);
if (uuid == null) {
if (InteractiveChatDiscordSrvAddon.plugin.shareEnderCommandIsMainServer) {
event.reply(ChatColorUtils.stripColor(InteractiveChatDiscordSrvAddon.plugin.accountNotLinked)).setEphemeral(true).queue();
}
return;
}
int errorCode = -1;
try {
OfflineICPlayer offlineICPlayer = ICPlayerFactory.getOfflineICPlayer(uuid);
if (offlineICPlayer == null) {
if (InteractiveChatDiscordSrvAddon.plugin.shareEnderCommandIsMainServer) {
event.reply(ChatColorUtils.stripColor(InteractiveChatDiscordSrvAddon.plugin.unableToRetrieveData) + " (" + errorCode + ")").setEphemeral(true).queue();
}
return;
}
errorCode--;
if (InteractiveChatDiscordSrvAddon.plugin.shareEnderCommandIsMainServer) {
event.deferReply().queue();
}
errorCode--;
ICPlayer icplayer = offlineICPlayer.getPlayer();
if (InteractiveChat.bungeecordMode && icplayer != null) {
if (icplayer.isLocal()) {
BungeeMessageSender.forwardEnderchest(System.currentTimeMillis(), uuid, icplayer.isRightHanded(), icplayer.getSelectedSlot(), icplayer.getExperienceLevel(), null, icplayer.getEnderChest());
} else {
TimeUnit.MILLISECONDS.sleep(InteractiveChat.remoteDelay);
}
}
errorCode--;
BufferedImage image = ImageGeneration.getInventoryImage(offlineICPlayer.getEnderChest(), Component.translatable(TranslationKeyUtils.getEnderChestContainerTitle()), offlineICPlayer);
errorCode--;
Component component = LegacyComponentSerializer.legacySection().deserialize(InteractiveChatDiscordSrvAddon.plugin.shareEnderCommandInGameMessageText.replace("{Player}", offlineICPlayer.getName()));
errorCode--;
String title = ChatColorUtils.stripColor(InteractiveChatDiscordSrvAddon.plugin.shareEnderCommandTitle.replace("{Player}", offlineICPlayer.getName()));
errorCode--;
String sha1 = HashUtils.createSha1(true, offlineICPlayer.getSelectedSlot(), offlineICPlayer.getExperienceLevel(), title, offlineICPlayer.getEnderChest());
errorCode--;
ender(offlineICPlayer, sha1, title);
errorCode--;
component = component.hoverEvent(HoverEvent.showText(LegacyComponentSerializer.legacySection().deserialize(InteractiveChatDiscordSrvAddon.plugin.shareEnderCommandInGameMessageHover)));
component = component.clickEvent(ClickEvent.runCommand("/interactivechat viewender " + sha1));
errorCode--;
String key = "<DiscordShare=" + UUID.randomUUID() + ">";
components.put(key, component);
Bukkit.getScheduler().runTaskLater(InteractiveChatDiscordSrvAddon.plugin, () -> components.remove(key), 100);
errorCode--;
discordsrv.broadcastMessageToMinecraftServer(minecraftChannel, ComponentStringUtils.toDiscordSRVComponent(Component.text(key)), event.getUser());
if (InteractiveChatDiscordSrvAddon.plugin.shareEnderCommandIsMainServer) {
errorCode--;
byte[] data = ImageUtils.toArray(image);
errorCode--;
event.getHook().editOriginal(PlainTextComponentSerializer.plainText().serialize(component)).setEmbeds(new EmbedBuilder().setAuthor(title).setImage("attachment://Inventory.png").setColor(InteractiveChatDiscordSrvAddon.plugin.enderColor).build()).addFile(data, "Inventory.png").queue();
}
} catch (Exception e) {
e.printStackTrace();
event.getHook().editOriginal(ChatColorUtils.stripColor(InteractiveChatDiscordSrvAddon.plugin.unableToRetrieveData) + " (" + errorCode + ")").queue();
return;
}
}
}
use of com.loohp.interactivechatdiscordsrvaddon.utils.DiscordItemStackUtils.DiscordToolTip in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class OutboundToDiscordEvents method createContents.
private static List<DiscordMessageContent> createContents(List<DiscordDisplayData> dataList, Player player) {
List<DiscordMessageContent> contents = new ArrayList<>();
for (DiscordDisplayData data : dataList) {
if (data instanceof ImageDisplayData) {
ImageDisplayData iData = (ImageDisplayData) data;
ImageDisplayType type = iData.getType();
String title = iData.getTitle();
if (iData.getItemStack().isPresent()) {
Debug.debug("createContents creating item discord content");
ItemStack item = iData.getItemStack().get();
Color color = DiscordItemStackUtils.getDiscordColor(item);
if (color == null || color.equals(Color.WHITE)) {
color = new Color(0xFFFFFE);
}
try {
BufferedImage image = ImageGeneration.getItemStackImage(item, data.getPlayer(), InteractiveChatDiscordSrvAddon.plugin.itemAltAir);
byte[] imageData = ImageUtils.toArray(image);
DiscordDescription description = DiscordItemStackUtils.getDiscordDescription(item, player);
DiscordMessageContent content = new DiscordMessageContent(description.getName(), "attachment://Item.png", color);
content.addAttachment("Item.png", imageData);
contents.add(content);
if (InteractiveChatDiscordSrvAddon.plugin.itemUseTooltipImage) {
DiscordToolTip discordToolTip = DiscordItemStackUtils.getToolTip(item, player);
if (!discordToolTip.isBaseItem() || InteractiveChatDiscordSrvAddon.plugin.itemUseTooltipImageOnBaseItem) {
BufferedImage tooltip = ImageGeneration.getToolTipImage(discordToolTip.getComponents());
byte[] tooltipData = ImageUtils.toArray(tooltip);
content.addAttachment("ToolTip.png", tooltipData);
content.addImageUrl("attachment://ToolTip.png");
} else {
content.addDescription(description.getDescription().orElse(null));
}
} else {
content.addDescription(description.getDescription().orElse(null));
}
if (type.equals(ImageDisplayType.ITEM_CONTAINER)) {
if (!description.getDescription().isPresent()) {
content.getImageUrls().remove("attachment://ToolTip.png");
content.getAttachments().remove("ToolTip.png");
}
TitledInventoryWrapper inv = iData.getInventory().get();
BufferedImage container = ImageGeneration.getInventoryImage(inv.getInventory(), inv.getTitle(), data.getPlayer());
byte[] containerData = ImageUtils.toArray(container);
content.addAttachment("Container.png", containerData);
content.addImageUrl("attachment://Container.png");
} else {
if (iData.isFilledMap() && iData.getPlayer().isLocal()) {
if (!description.getDescription().isPresent()) {
content.getImageUrls().remove("attachment://ToolTip.png");
content.getAttachments().remove("ToolTip.png");
}
BufferedImage map = ImageGeneration.getMapImage(item, iData.getPlayer().getLocalPlayer());
byte[] mapData = ImageUtils.toArray(map);
content.addAttachment("Map.png", mapData);
content.addImageUrl("attachment://Map.png");
}
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (iData.getInventory().isPresent()) {
Debug.debug("createContents creating inventory discord content");
TitledInventoryWrapper inv = iData.getInventory().get();
try {
BufferedImage image;
if (iData.isPlayerInventory()) {
if (InteractiveChatDiscordSrvAddon.plugin.usePlayerInvView) {
image = ImageGeneration.getPlayerInventoryImage(inv.getInventory(), iData.getPlayer());
} else {
image = ImageGeneration.getInventoryImage(inv.getInventory(), inv.getTitle(), data.getPlayer());
}
} else {
image = ImageGeneration.getInventoryImage(inv.getInventory(), inv.getTitle(), data.getPlayer());
}
Color color;
switch(type) {
case ENDERCHEST:
color = InteractiveChatDiscordSrvAddon.plugin.enderColor;
break;
case INVENTORY:
color = InteractiveChatDiscordSrvAddon.plugin.invColor;
break;
default:
color = Color.black;
break;
}
byte[] imageData = ImageUtils.toArray(image);
DiscordMessageContent content = new DiscordMessageContent(title, null, null, "attachment://Inventory.png", color);
content.addAttachment("Inventory.png", imageData);
if (type.equals(ImageDisplayType.INVENTORY) && InteractiveChatDiscordSrvAddon.plugin.invShowLevel) {
int level = iData.getPlayer().getExperienceLevel();
byte[] bottleData = ImageUtils.toArray(InteractiveChatDiscordSrvAddon.plugin.modelRenderer.render(32, 32, InteractiveChatDiscordSrvAddon.plugin.resourceManager, InteractiveChat.version.isOld(), "minecraft:item/experience_bottle", ModelDisplayPosition.GUI, false).getImage());
content.addAttachment("Level.png", bottleData);
content.setFooter(LanguageUtils.getTranslation(TranslationKeyUtils.getLevelTranslation(level), InteractiveChatDiscordSrvAddon.plugin.language).replaceFirst("%s", level + ""));
content.setFooterImageUrl("attachment://Level.png");
}
contents.add(content);
} catch (Exception e) {
e.printStackTrace();
}
}
} else if (data instanceof HoverClickDisplayData) {
Debug.debug("createContents creating hover event discord content");
try {
HoverClickDisplayData hData = (HoverClickDisplayData) data;
String title = hData.getDisplayText();
Color color = hData.getColor();
DiscordMessageContent content = new DiscordMessageContent(title, null, color);
String body = "";
String preview = null;
if (hData.hasHover()) {
if (InteractiveChatDiscordSrvAddon.plugin.hoverUseTooltipImage) {
Component print = hData.getHoverText();
BufferedImage tooltip = ImageGeneration.getToolTipImage(print, true);
byte[] tooltipData = ImageUtils.toArray(tooltip);
content.addAttachment("ToolTip.png", tooltipData);
content.addImageUrl("attachment://ToolTip.png");
content.addDescription(null);
} else {
body += ComponentStringUtils.stripColorAndConvertMagic(InteractiveChatComponentSerializer.bungeecordApiLegacy().serialize(hData.getHoverText()));
}
}
if (hData.hasClick()) {
switch(hData.getClickAction()) {
case COPY_TO_CLIPBOARD:
if (body.length() > 0) {
body += "\n\n";
}
body += LanguageUtils.getTranslation(TranslationKeyUtils.getCopyToClipboard(), InteractiveChatDiscordSrvAddon.plugin.language) + ": __" + hData.getClickValue() + "__";
break;
case OPEN_URL:
if (body.length() > 0) {
body += "\n\n";
}
String url = hData.getClickValue();
body += LanguageUtils.getTranslation(TranslationKeyUtils.getOpenUrl(), InteractiveChatDiscordSrvAddon.plugin.language) + ": __" + url + "__";
if (URLRequestUtils.IMAGE_URL_PATTERN.matcher(url).matches() && URLRequestUtils.isAllowed(url)) {
preview = url;
}
break;
default:
break;
}
}
if (!body.isEmpty()) {
content.addDescription(body);
}
if (InteractiveChatDiscordSrvAddon.plugin.hoverImage) {
BufferedImage image = InteractiveChatDiscordSrvAddon.plugin.resourceManager.getTextureManager().getTexture(ResourceRegistry.IC_MISC_TEXTURE_LOCATION + "hover_cursor").getTexture();
byte[] imageData = ImageUtils.toArray(image);
content.setAuthorIconUrl("attachment://Hover.png");
content.addAttachment("Hover.png", imageData);
}
if (preview != null) {
content.addImageUrl(preview);
}
contents.add(content);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return contents;
}
Aggregations