use of com.loohp.interactivechatdiscordsrvaddon.objectholders.AttachmentData 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.AttachmentData in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class OutboundToDiscordEvents method onAdvancement.
// ===== Advancement
@Subscribe(priority = ListenerPriority.HIGHEST)
public void onAdvancement(AchievementMessagePreProcessEvent event) {
if (event.isCancelled()) {
return;
}
Debug.debug("Triggered onAdvancement");
MessageFormat messageFormat = event.getMessageFormat();
if (messageFormat == null) {
return;
}
String title = null;
String description = null;
ItemStack item = null;
AdvancementType advancementType = null;
boolean isMinecraft = true;
Event bukkitEvent = event.getTriggeringBukkitEvent();
if (bukkitEvent.getClass().getSimpleName().equals("PlayerAdvancementDoneEvent")) {
Debug.debug("onAdvancement getting advancement");
Object bukkitAdvancement = AdvancementUtils.getAdvancementFromEvent(bukkitEvent);
AdvancementData data = AdvancementUtils.getAdvancementData(bukkitAdvancement);
if (data == null) {
return;
}
title = InteractiveChatComponentSerializer.bungeecordApiLegacy().serialize(data.getTitle(), InteractiveChatDiscordSrvAddon.plugin.language);
description = InteractiveChatComponentSerializer.bungeecordApiLegacy().serialize(data.getDescription(), InteractiveChatDiscordSrvAddon.plugin.language);
item = data.getItem();
advancementType = data.getAdvancementType();
isMinecraft = data.isMinecraft();
} else if (bukkitEvent.getClass().getSimpleName().equals("PlayerAchievementAwardedEvent")) {
Debug.debug("onAdvancement getting achievement");
Object bukkitAchievement = AchievementUtils.getAdvancementFromEvent(bukkitEvent);
if (bukkitAchievement == null) {
return;
}
AdvancementData data = AchievementUtils.getAdvancementData(bukkitAchievement);
if (data == null) {
return;
}
title = InteractiveChatComponentSerializer.bungeecordApiLegacy().serialize(data.getTitle(), InteractiveChatDiscordSrvAddon.plugin.language);
description = InteractiveChatComponentSerializer.bungeecordApiLegacy().serialize(data.getDescription(), InteractiveChatDiscordSrvAddon.plugin.language);
item = data.getItem();
advancementType = data.getAdvancementType();
isMinecraft = data.isMinecraft();
} else {
return;
}
Debug.debug("onAdvancement processing advancement");
if (InteractiveChatDiscordSrvAddon.plugin.advancementItem && item != null && advancementType != null) {
String content = messageFormat.getContent();
if (content == null) {
content = "";
}
try {
int id = DATA_ID_PROVIDER.getNext();
BufferedImage thumbnail = ImageGeneration.getAdvancementIcon(item, advancementType, true, ICPlayerFactory.getICPlayer(event.getPlayer()));
byte[] thumbnailData = ImageUtils.toArray(thumbnail);
content += "<ICA=" + id + ">";
messageFormat.setContent(content);
RESEND_WITH_ATTACHMENT.put(id, new AttachmentData("Thumbnail.png", thumbnailData));
messageFormat.setThumbnailUrl("attachment://Thumbnail.png");
} catch (IOException e) {
e.printStackTrace();
}
}
if (InteractiveChatDiscordSrvAddon.plugin.advancementName && title != null) {
event.setAchievementName(ChatColorUtils.stripColor(title));
messageFormat.setAuthorName(LanguageUtils.getTranslation(advancementType.getTranslationKey(), InteractiveChatDiscordSrvAddon.plugin.language).replaceFirst("%s", event.getPlayer().getName()).replaceFirst("%s", ChatColorUtils.stripColor(title)));
Color color;
if (isMinecraft) {
color = ColorUtils.getColor(advancementType.getColor());
} else {
String colorStr = ChatColorUtils.getFirstColors(title);
color = ColorUtils.getColor(ColorUtils.toChatColor(colorStr));
}
if (color.equals(Color.white)) {
color = new Color(0xFFFFFE);
}
messageFormat.setColorRaw(color.getRGB());
}
if (InteractiveChatDiscordSrvAddon.plugin.advancementDescription && description != null) {
messageFormat.setDescription(ChatColorUtils.stripColor(description));
}
event.setMessageFormat(messageFormat);
}
Aggregations