Skip to content

Commit

Permalink
Advancement description and color + Formatted color and timestamp in …
Browse files Browse the repository at this point in the history
…embeds (#16)

* Allow NamedLogger for Fabric modules

* Support misused translation in datapacks or other mods

* Allow adjusting embed color via context

* Add support for advancement description and color

* Support formattable color and timestamp in DiscordMessageEmbed

* Support for unformatted color and timestamp in embeds

* Add placeholders for current date and time

* Clean up and style code

* Rename FabricAdvancementModule logger and use NamedLogger for FabricBanModule

* Remove default color for embeds
  • Loading branch information
IxPrumxI authored Feb 11, 2025
1 parent 0925aba commit af84312
Show file tree
Hide file tree
Showing 18 changed files with 231 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ public static Builder builder() {
}

private final Color color;
private final String unformattedColor;
private final String authorName, authorUrl, authorImageUrl;
private final String title, titleUrl;
private final String description;
private final List<Field> fields;
private final String thumbnailUrl;
private final String imageUrl;
private final OffsetDateTime timestamp;
private final String unformattedTimestamp;
private final String footer, footerImageUrl;

public DiscordMessageEmbed(MessageEmbed embed) {
Expand All @@ -69,6 +71,7 @@ public DiscordMessageEmbed(MessageEmbed embed) {
}

this.color = new Color(embed.getColorRaw());
this.unformattedColor = String.valueOf(embed.getColorRaw());
this.authorName = author != null ? author.getName(): null;
this.authorUrl = author != null ? author.getUrl() : null;
this.authorImageUrl = author != null ? author.getIconUrl() : null;
Expand All @@ -79,14 +82,16 @@ public DiscordMessageEmbed(MessageEmbed embed) {
this.thumbnailUrl = thumbnail != null ? thumbnail.getUrl() : null;
this.imageUrl = image != null ? image.getUrl() : null;
this.timestamp = embed.getTimestamp();
this.unformattedTimestamp = embed.getTimestamp() != null ? embed.getTimestamp().toString() : null;
this.footer = footer != null ? footer.getText() : null;
this.footerImageUrl = footer != null ? footer.getIconUrl() : null;
}

public DiscordMessageEmbed(Color color, String authorName, String authorUrl, String authorAvatarUrl, String title,
public DiscordMessageEmbed(Color color, String unformattedColor, String authorName, String authorUrl, String authorAvatarUrl, String title,
String titleUrl, String description, List<Field> fields, String thumbnailUrl,
String imageUrl, OffsetDateTime timestamp, String footer, String footerImageUrl) {
String imageUrl, OffsetDateTime timestamp, String unformattedTimestamp, String footer, String footerImageUrl) {
this.color = color;
this.unformattedColor = unformattedColor;
this.authorName = authorName;
this.authorUrl = authorUrl;
this.authorImageUrl = authorAvatarUrl;
Expand All @@ -97,6 +102,7 @@ public DiscordMessageEmbed(Color color, String authorName, String authorUrl, Str
this.thumbnailUrl = thumbnailUrl;
this.imageUrl = imageUrl;
this.timestamp = timestamp;
this.unformattedTimestamp = unformattedTimestamp;
this.footer = footer;
this.footerImageUrl = footerImageUrl;
}
Expand All @@ -106,6 +112,11 @@ public Color getColor() {
return color;
}

@Nullable
public String getUnformattedColor() {
return unformattedColor;
}

@Nullable
public String getAuthorName() {
return authorName;
Expand Down Expand Up @@ -156,6 +167,11 @@ public OffsetDateTime getTimestamp() {
return timestamp;
}

@Nullable
public String getUnformattedTimestamp() {
return unformattedTimestamp;
}

@Nullable
public String getFooter() {
return footer;
Expand Down Expand Up @@ -221,23 +237,26 @@ public boolean isInline() {
public static class Builder {

private Color color;
private String unformattedColor;
private String authorName, authorUrl, authorImageUrl;
private String title, titleUrl;
private String description;
private final List<Field> fields;
private String thumbnailUrl;
private String imageUrl;
private OffsetDateTime timestamp;
private String unformattedTimestamp;
private String footer, footerImageUrl;

protected Builder() {
this.fields = new ArrayList<>();
}

protected Builder(Color color, String authorName, String authorUrl, String authorImageUrl, String title,
protected Builder(Color color, String unformattedColor, String authorName, String authorUrl, String authorImageUrl, String title,
String titleUrl, String description, List<Field> fields, String thumbnailUrl, String imageUrl,
OffsetDateTime timestamp, String footer, String footerImageUrl) {
OffsetDateTime timestamp, String unformattedTimestamp, String footer, String footerImageUrl) {
this.color = color;
this.unformattedColor = unformattedColor;
this.authorName = authorName;
this.authorUrl = authorUrl;
this.authorImageUrl = authorImageUrl;
Expand All @@ -248,12 +267,14 @@ protected Builder(Color color, String authorName, String authorUrl, String autho
this.thumbnailUrl = thumbnailUrl;
this.imageUrl = imageUrl;
this.timestamp = timestamp;
this.unformattedTimestamp = unformattedTimestamp;
this.footer = footer;
this.footerImageUrl = footerImageUrl;
}

protected Builder(DiscordMessageEmbed embed) {
this.color = embed.getColor();
this.unformattedColor = embed.getUnformattedColor();
this.authorName = embed.getAuthorName();
this.authorUrl = embed.getAuthorUrl();
this.authorImageUrl = embed.getAuthorImageUrl();
Expand All @@ -264,6 +285,7 @@ protected Builder(DiscordMessageEmbed embed) {
this.thumbnailUrl = embed.getThumbnailUrl();
this.imageUrl = embed.getImageUrl();
this.timestamp = embed.getTimestamp();
this.unformattedTimestamp = embed.getUnformattedTimestamp();
this.footer = embed.getFooter();
this.footerImageUrl = embed.getFooterImageUrl();
}
Expand All @@ -284,6 +306,17 @@ public Builder setColor(int rgb) {
return setColor(new Color(rgb));
}

@Nullable
public String getUnformattedColor() {
return unformattedColor;
}

@NotNull
public Builder setUnformattedColor(String unformattedColor) {
this.unformattedColor = unformattedColor;
return this;
}

@NotNull
public Builder setAuthor(@Nullable CharSequence authorName, @Nullable CharSequence authorUrl) {
return setAuthor(authorName, authorUrl, null);
Expand Down Expand Up @@ -425,6 +458,17 @@ public Builder setTimestamp(@Nullable OffsetDateTime timestamp) {
return this;
}

@Nullable
public String getUnformattedTimestamp() {
return unformattedTimestamp;
}

@NotNull
public Builder setUnformattedTimestamp(String unformattedTimestamp) {
this.unformattedTimestamp = unformattedTimestamp;
return this;
}

@NotNull
public Builder setFooter(@Nullable CharSequence footer, @Nullable CharSequence footerImageUrl) {
this.footer = footer != null ? footer.toString() : null;
Expand Down Expand Up @@ -456,16 +500,16 @@ public Builder setFooterImageUrl(@Nullable CharSequence footerImageUrl) {

@NotNull
public DiscordMessageEmbed build() {
return new DiscordMessageEmbed(color, authorName, authorUrl, authorImageUrl, title, titleUrl, description,
fields, thumbnailUrl, imageUrl, timestamp, footer, footerImageUrl);
return new DiscordMessageEmbed(color, unformattedColor, authorName, authorUrl, authorImageUrl, title, titleUrl, description,
fields, thumbnailUrl, imageUrl, timestamp, unformattedTimestamp, footer, footerImageUrl);
}

@SuppressWarnings({"MethodDoesntCallSuperMethod"})
@Override
@NotNull
public Builder clone() {
return new Builder(color, authorName, authorUrl, authorImageUrl, title, titleUrl, description,
fields, thumbnailUrl, imageUrl, timestamp, footer, footerImageUrl);
return new Builder(color, unformattedColor, authorName, authorUrl, authorImageUrl, title, titleUrl, description,
fields, thumbnailUrl, imageUrl, timestamp, unformattedTimestamp, footer, footerImageUrl);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.discordsrv.api.discord.entity.message.impl;

import com.discordsrv.api.DiscordSRVApi;
import com.discordsrv.api.color.Color;
import com.discordsrv.api.discord.entity.interaction.component.actionrow.MessageActionRow;
import com.discordsrv.api.discord.entity.message.AllowedMention;
import com.discordsrv.api.discord.entity.message.DiscordMessageEmbed;
Expand All @@ -38,6 +39,8 @@
import org.jetbrains.annotations.Nullable;

import java.io.InputStream;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -420,6 +423,13 @@ private Function<Matcher, Object> wrapFunction(Function<Matcher, Object> functio
for (DiscordMessageEmbed embed : embeds) {
DiscordMessageEmbed.Builder embedBuilder = embed.toBuilder();

Color color = embedBuilder.getColor();
String formattedColorHex = placeholders.apply(embedBuilder.getUnformattedColor());
try {
color = new Color(formattedColorHex);
} catch (Throwable ignored) {}
embedBuilder.setColor(color);

embedBuilder.setAuthor(
cutToLength(
placeholders.apply(embedBuilder.getAuthorName()),
Expand Down Expand Up @@ -452,6 +462,12 @@ private Function<Matcher, Object> wrapFunction(Function<Matcher, Object> functio
placeholders.apply(embedBuilder.getFooterImageUrl())
);

embedBuilder.setTimestamp(
parseTimestamp(
placeholders.apply(embedBuilder.getUnformattedTimestamp())
)
);

PlainPlaceholderFormat.with(PlainPlaceholderFormat.Formatting.DISCORD, () -> embedBuilder.setDescription(
cutToLength(
markdownPlaceholders.apply(embedBuilder.getDescription()),
Expand Down Expand Up @@ -494,5 +510,17 @@ private String cutToLength(String input, int maxLength) {
}
return input;
}

private OffsetDateTime parseTimestamp(String timestamp) {
try {
return OffsetDateTime.parse(timestamp);
} catch (Throwable ignored) {
try {
return ZonedDateTime.parse(timestamp).toOffsetDateTime();
} catch (Throwable ignoredAgain) {
return null;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.discordsrv.api.events.message.receive.game;

import com.discordsrv.api.channel.GameChannel;
import com.discordsrv.api.color.Color;
import com.discordsrv.api.component.MinecraftComponent;
import com.discordsrv.api.eventbus.EventPriorities;
import com.discordsrv.api.events.PlayerEvent;
Expand All @@ -40,20 +41,26 @@ public class AwardMessageReceiveEvent extends AbstractGameMessageReceiveEvent im
private final DiscordSRVPlayer player;
private MinecraftComponent name;
private MinecraftComponent title;
private MinecraftComponent description;
private AdvancementFrame frame;
private GameChannel gameChannel;

public AwardMessageReceiveEvent(
@Nullable Object triggeringEvent,
@NotNull DiscordSRVPlayer player,
@Nullable MinecraftComponent name,
@Nullable MinecraftComponent title,
@Nullable MinecraftComponent description,
@Nullable AdvancementFrame frame,
@Nullable GameChannel gameChannel,
boolean cancelled
) {
super(triggeringEvent, cancelled);
this.player = player;
this.name = name;
this.title = title;
this.description = description;
this.frame = frame;
this.gameChannel = gameChannel;
}

Expand Down Expand Up @@ -81,6 +88,23 @@ public void setTitle(@Nullable MinecraftComponent title) {
this.title = title;
}

@Nullable
public MinecraftComponent getDescription() {
return description;
}

public void setDescription(@Nullable MinecraftComponent description) {
this.description = description;
}

@Nullable public AdvancementFrame getFrame() {
return frame;
}

public void setFrame(@Nullable AdvancementFrame frame) {
this.frame = frame;
}

public GameChannel getGameChannel() {
return gameChannel;
}
Expand All @@ -96,4 +120,26 @@ public String toString() {
+ "gameChannel=" + GameChannel.toString(gameChannel)
+ '}';
}

public enum AdvancementFrame {
TASK("task", new Color(0x55FF55)), // Green
GOAL("goal", new Color(0x55FF55)), // Green
CHALLENGE("challenge", new Color(0xAA00AA)); // Dark Purple

private final String name;
private final Color color;

AdvancementFrame(String id, Color color) {
this.name = id;
this.color = color;
}

public String id() {
return name;
}

public Color color() {
return color;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ protected void handleEvent(@NotNull PlayerAdvancementDoneEvent event, Void __) {
title,
null,
null,
null,
null,
false
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class PaperAdvancementListener extends AbstractBukkitListener<PlayerAdvan
= PaperComponentHandle.get(PlayerAdvancementDoneEvent.class, "message");
private static final PaperComponentHandle.Get<Advancement> ADVANCEMENT_DISPLAY_NAME_HANDLE
= PaperComponentHandle.get(Advancement.class, "displayName");
private static final PaperComponentHandle.Get<AdvancementDisplay> ADVANCEMENT_DESCRIPTION_HANDLE
= PaperComponentHandle.get(AdvancementDisplay.class, "description");

public PaperAdvancementListener(BukkitDiscordSRV discordSRV) {
super(discordSRV, new NamedLogger(discordSRV, "ADVANCEMENT_LISTENER"));
Expand Down Expand Up @@ -70,15 +72,18 @@ protected void handleEvent(@NotNull PlayerAdvancementDoneEvent event, Void __) {
}

MinecraftComponent message = MESSAGE_HANDLE.getAPI(event);
MinecraftComponent displayName = ADVANCEMENT_DISPLAY_NAME_HANDLE.getAPI(advancement);
MinecraftComponent name = ADVANCEMENT_DISPLAY_NAME_HANDLE.getAPI(advancement);
MinecraftComponent description = ADVANCEMENT_DESCRIPTION_HANDLE.getAPI(display);

IPlayer player = discordSRV.playerProvider().player(event.getPlayer());
discordSRV.eventBus().publish(
new AwardMessageReceiveEvent(
event,
player,
name,
message,
displayName,
description,
AwardMessageReceiveEvent.AdvancementFrame.valueOf(display.frame().toString()),
null,
message == null
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,17 @@ protected void handleEvent(@NotNull PlayerAdvancementDoneEvent event, Void __) {
return;
}

MinecraftComponent title = ComponentUtil.toAPI(BukkitComponentSerializer.legacy().deserialize(display.getTitle())) ;
MinecraftComponent title = ComponentUtil.toAPI(BukkitComponentSerializer.legacy().deserialize(display.getTitle()));
MinecraftComponent description = ComponentUtil.toAPI(BukkitComponentSerializer.legacy().deserialize(display.getDescription()));
IPlayer srvPlayer = discordSRV.playerProvider().player(event.getPlayer());
discordSRV.eventBus().publish(
new AwardMessageReceiveEvent(
event,
srvPlayer,
null,
title,
description,
AwardMessageReceiveEvent.AdvancementFrame.valueOf(display.getType().toString()),
null,
false
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ protected void handleEvent(@NotNull PlayerAchievementAwardedEvent event, Void __
achievementName,
null,
null,
null,
null,
event.isCancelled()
)
);
Expand Down
Loading

0 comments on commit af84312

Please sign in to comment.