use of com.loohp.interactivechatdiscordsrvaddon.objectholders.AdvancementData in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class AchievementUtils method getAdvancementData.
public static AdvancementData getAdvancementData(Object achievementObject) {
try {
Object nmsAchievement = craftStatisticClassGetNMSAchievementMethod.invoke(null, achievementObject);
nmsAdvancementClassNameField.setAccessible(true);
String name = nmsAdvancementClassNameField.get(nmsAchievement).toString();
Component title = Component.translatable(name).color(NamedTextColor.GREEN);
Component description = Component.translatable(name + ".desc");
AdvancementType advancementType = AdvancementType.LEGACY;
nmsAdvancementClassItemStackField.setAccessible(true);
Object nmsItemStack = nmsAdvancementClassItemStackField.get(nmsAchievement);
ItemStack itemStack = ItemStackUtils.toBukkitCopy(nmsItemStack);
return new AdvancementData(title, description, itemStack, advancementType, true);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
use of com.loohp.interactivechatdiscordsrvaddon.objectholders.AdvancementData 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);
}
use of com.loohp.interactivechatdiscordsrvaddon.objectholders.AdvancementData in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class AdvancementUtils method getAdvancementData.
public static AdvancementData getAdvancementData(Object advancementObject) {
try {
Advancement advancement = (Advancement) advancementObject;
boolean isMinecraft = advancement.getKey().getNamespace().equals(MINECRAFT_NAMESPACE);
Object craftAdvancement = craftAdvancementClass.cast(advancement);
Object nmsAdvancement = craftAdvancementClassGetHandleMethod.invoke(craftAdvancement);
Object nmsAdvancementDisplay = nmsAdvancementClassGetDisplayMethod.invoke(nmsAdvancement);
Object nmsTitle = nmsAdvancementDisplayClassGetTitleMethod.invoke(nmsAdvancementDisplay);
Component title = ChatComponentType.IChatBaseComponent.convertFrom(nmsTitle);
Object nmsDescription = nmsAdvancementDisplayClassGetDescriptionMethod.invoke(nmsAdvancementDisplay);
Component description = ChatComponentType.IChatBaseComponent.convertFrom(nmsDescription);
Object nmsAdvancementType = nmsAdvancementDisplayClassGetTypeMethod.invoke(nmsAdvancementDisplay);
AdvancementType advancementType = AdvancementType.fromHandle(nmsAdvancementType);
nmsAdvancementDisplayClassItemStackField.setAccessible(true);
Object nmsItemStack = nmsAdvancementDisplayClassItemStackField.get(nmsAdvancementDisplay);
ItemStack itemStack = ItemStackUtils.toBukkitCopy(nmsItemStack);
return new AdvancementData(title, description, itemStack, advancementType, isMinecraft);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
Aggregations