use of com.loohp.interactivechatdiscordsrvaddon.objectholders.DiscordMessageContent in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class OutboundToDiscordEvents method onAdvancementSend.
@Subscribe(priority = ListenerPriority.HIGHEST)
public void onAdvancementSend(AchievementMessagePostProcessEvent event) {
if (event.isCancelled()) {
return;
}
Debug.debug("Triggered onAdvancementSend");
Message message = event.getDiscordMessage();
if (!message.getContentRaw().contains("<ICA=")) {
return;
}
String text = message.getContentRaw();
Set<Integer> matches = new LinkedHashSet<>();
synchronized (RESEND_WITH_ATTACHMENT) {
for (int key : RESEND_WITH_ATTACHMENT.keySet()) {
if (text.contains("<ICA=" + key + ">")) {
matches.add(key);
}
}
}
event.setCancelled(true);
DiscordMessageContent content = new DiscordMessageContent(message);
for (int key : matches) {
AttachmentData data = RESEND_WITH_ATTACHMENT.remove(key);
if (data != null) {
content.addAttachment(data.getName(), data.getData());
}
}
TextChannel destinationChannel = DiscordSRV.getPlugin().getDestinationTextChannelForGameChannelName(event.getChannel());
Debug.debug("onAdvancementSend sending message to discord");
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();
}
}
use of com.loohp.interactivechatdiscordsrvaddon.objectholders.DiscordMessageContent 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.objectholders.DiscordMessageContent in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class OutboundToDiscordEvents method discordMessageSent.
// =====
@Subscribe(priority = ListenerPriority.HIGHEST)
public void discordMessageSent(DiscordGuildMessageSentEvent event) {
Debug.debug("Triggered discordMessageSent");
Message message = event.getMessage();
String textOriginal = message.getContentRaw();
TextChannel channel = event.getChannel();
if (!InteractiveChatDiscordSrvAddon.plugin.isEnabled()) {
return;
}
Bukkit.getScheduler().runTaskAsynchronously(InteractiveChatDiscordSrvAddon.plugin, () -> {
String text = textOriginal;
if (!text.contains("<ICD=")) {
return;
}
Set<Integer> matches = new LinkedHashSet<>();
synchronized (DATA) {
for (int key : DATA.keySet()) {
if (text.contains("<ICD=" + key + ">")) {
text = text.replace("<ICD=" + key + ">", "");
matches.add(key);
}
}
}
if (matches.isEmpty()) {
Debug.debug("discordMessageSent keys empty");
return;
}
message.editMessage(text + " ...").queue();
ICPlayer player = DATA.get(matches.iterator().next()).getPlayer();
List<DiscordDisplayData> dataList = new ArrayList<>();
for (int key : matches) {
DiscordDisplayData data = DATA.remove(key);
if (data != null) {
dataList.add(data);
}
}
dataList.sort(DISPLAY_DATA_COMPARATOR);
Debug.debug("discordMessageSent creating contents");
List<DiscordMessageContent> contents = createContents(dataList, player.isLocal() ? player.getLocalPlayer() : (Bukkit.getOnlinePlayers().isEmpty() ? null : Bukkit.getOnlinePlayers().iterator().next()));
DiscordImageEvent discordImageEvent = new DiscordImageEvent(channel, textOriginal, text, contents, false, true);
Bukkit.getPluginManager().callEvent(discordImageEvent);
Debug.debug("discordMessageSent sending to discord, Cancelled: " + discordImageEvent.isCancelled());
if (discordImageEvent.isCancelled()) {
message.editMessage(discordImageEvent.getOriginalMessage()).queue();
} else {
text = discordImageEvent.getNewMessage();
MessageAction action = message.editMessage(text);
List<MessageEmbed> embeds = new ArrayList<>();
for (DiscordMessageContent content : contents) {
embeds.addAll(content.toJDAMessageEmbeds());
for (Entry<String, byte[]> attachment : content.getAttachments().entrySet()) {
action = action.addFile(attachment.getValue(), attachment.getKey());
}
}
action.setEmbeds(embeds).queue();
}
});
}
use of com.loohp.interactivechatdiscordsrvaddon.objectholders.DiscordMessageContent 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