Skip to content

Commit

Permalink
Discord消息的用户名称显示其身份组的颜色
Browse files Browse the repository at this point in the history
  • Loading branch information
Xujiayao committed Nov 18, 2021
1 parent cb0b13c commit f5bb243
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 26 deletions.
2 changes: 1 addition & 1 deletion build.gradle
@@ -1,5 +1,5 @@
plugins {
id 'fabric-loom' version '0.10.50'
id 'fabric-loom' version '0.10.57'
}

sourceCompatibility = JavaVersion.VERSION_17
Expand Down
8 changes: 4 additions & 4 deletions gradle.properties
Expand Up @@ -3,12 +3,12 @@ org.gradle.jvmargs=-Xmx4G
# Fabric Properties
# check these on https://fabricmc.net/use
minecraft_version=1.17.1
yarn_mappings=1.17.1+build.63
loader_version=0.12.4
yarn_mappings=1.17.1+build.64
loader_version=0.12.5
# Mod Properties
mod_version=1.17-1.10.6
mod_version=1.17-1.10.7
maven_group=top.xujiayao
archives_base_name=MCDiscordChat
# Dependencies
# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api
fabric_version=0.41.3+1.17
fabric_version=0.42.1+1.17
38 changes: 29 additions & 9 deletions src/main/java/top/xujiayao/mcdiscordchat/Config.java
Expand Up @@ -180,15 +180,25 @@ public static class TextsZH {
public String advancementGoal = "**%playername% 达成了目标 [%advancement%]**";

// Discord -> Minecraft
// Colored part of the message
// This part of the message will receive the same color as the role in the discord, comes before the colorless part
// Blue colored part of the message
// This part of the message comes before the role colored and colorless part
// ---
// Available placeholders:
// %servername% | 'Discord' or 'server name when using multi-server'
// %name% | Nickname of the user in the discord server (becomes player name when using multi-server)
// %message% | The message
@Expose
public String coloredText = "[%servername%] ";
public String blueColoredText = "[%servername%] ";

// Discord -> Minecraft
// Role colored part of the message
// This part of the message will receive the same color as the role in the discord, comes before the colorless part
// ---
// Available placeholders:
// %name% | Nickname of the user in the discord server (becomes player name when using multi-server)
// %message% | The message
@Expose
public String roleColoredText = "<%name%>";

// Discord -> Minecraft
// Colorless (white) part of the message
Expand All @@ -198,7 +208,7 @@ public static class TextsZH {
// %name% | Nickname of the user in the discord server (becomes player name when using multi-server)
// %message% | The message
@Expose
public String colorlessText = "<%name%> %message%";
public String colorlessText = " %message%";

// Replaces the § symbol with & in any discord message to avoid formatted messages
@Expose
Expand Down Expand Up @@ -275,25 +285,35 @@ public static class TextsEN {
public String advancementGoal = "**%playername% has reached the goal [%advancement%]**";

// Discord -> Minecraft
// Colored part of the message
// This part of the message will receive the same color as the role in the discord, comes before the colorless part
// Blue colored part of the message
// This part of the message comes before the role colored and colorless part
// ---
// Available placeholders:
// %servername% | 'Discord' or 'server name when using multi-server'
// %name% | Nickname of the user in the discord server (becomes player name when using multi-server)
// %message% | The message
@Expose
public String coloredText = "[%servername%] ";
public String blueColoredText = "[%servername%] ";

// Discord -> Minecraft
// Colorless (white) part of the message
// Role colored part of the message
// This part of the message will receive the same color as the role in the discord, comes before the colorless part
// ---
// Available placeholders:
// %name% | Nickname of the user in the discord server (becomes player name when using multi-server)
// %message% | The message
@Expose
public String roleColoredText = "<%name%>";

// Discord -> Minecraft
// Colorless (gray) part of the message
// I think you already know what it is by the other comment
// ---
// Available placeholders:
// %name% | Nickname of the user in the discord server (becomes player name when using multi-server)
// %message% | The message
@Expose
public String colorlessText = "<%name%> %message%";
public String colorlessText = " %message%";

// Replaces the § symbol with & in any discord message to avoid formatted messages
@Expose
Expand Down
Expand Up @@ -252,7 +252,8 @@ public void onMessageReceived(MessageReceivedEvent e) {
Utils.checkUpdate(true);
}

LiteralText coloredText;
LiteralText blueColoredText;
LiteralText roleColoredText;
LiteralText colorlessText;

StringBuilder message = new StringBuilder(e.getMessage().getContentDisplay()
Expand All @@ -275,22 +276,32 @@ public void onMessageReceived(MessageReceivedEvent e) {
}

if (e.isWebhookMessage() || e.getAuthor().isBot()) {
coloredText = new LiteralText(Main.texts.coloredText()
blueColoredText = new LiteralText(Main.texts.blueColoredText()
.replace("%servername%", e.getAuthor().getName().substring(1, e.getAuthor().getName().indexOf("] ")))
.replace("%message%", EmojiParser.parseToAliases(message.toString())));
coloredText.setStyle(coloredText.getStyle().withColor(TextColor.fromFormatting(Formatting.BLUE)));
coloredText.setStyle(coloredText.getStyle().withBold(true));
blueColoredText.setStyle(blueColoredText.getStyle().withColor(TextColor.fromFormatting(Formatting.BLUE)));
blueColoredText.setStyle(blueColoredText.getStyle().withBold(true));

roleColoredText = new LiteralText(Main.texts.roleColoredText()
.replace("%name%", e.getAuthor().getName().substring(e.getAuthor().getName().indexOf("] ") + 2))
.replace("%message%", MarkdownParser.parseMarkdown(EmojiParser.parseToAliases(message.toString()))));
roleColoredText.setStyle(roleColoredText.getStyle().withColor(TextColor.fromFormatting(Formatting.GRAY)));

colorlessText = new LiteralText(Main.texts.colorlessText()
.replace("%name%", e.getAuthor().getName().substring(e.getAuthor().getName().indexOf("] ") + 2))
.replace("%message%", MarkdownParser.parseMarkdown(EmojiParser.parseToAliases(message.toString()))));
} else {
coloredText = new LiteralText(Main.texts.coloredText()
blueColoredText = new LiteralText(Main.texts.blueColoredText()
.replace("%servername%", "Discord")
.replace("%name%", Objects.requireNonNull(e.getMember()).getEffectiveName())
.replace("%message%", EmojiParser.parseToAliases(message.toString())));
coloredText.setStyle(coloredText.getStyle().withColor(TextColor.fromFormatting(Formatting.BLUE)));
coloredText.setStyle(coloredText.getStyle().withBold(true));
blueColoredText.setStyle(blueColoredText.getStyle().withColor(TextColor.fromFormatting(Formatting.BLUE)));
blueColoredText.setStyle(blueColoredText.getStyle().withBold(true));

roleColoredText = new LiteralText(Main.texts.roleColoredText()
.replace("%name%", Objects.requireNonNull(e.getMember()).getEffectiveName())
.replace("%message%", MarkdownParser.parseMarkdown(EmojiParser.parseToAliases(message.toString()))));
roleColoredText.setStyle(roleColoredText.getStyle().withColor(TextColor.fromRgb(Objects.requireNonNull(e.getMember()).getColorRaw())));

colorlessText = new LiteralText(Main.texts.colorlessText()
.replace("%name%", Objects.requireNonNull(e.getMember()).getEffectiveName())
Expand All @@ -300,7 +311,7 @@ public void onMessageReceived(MessageReceivedEvent e) {
colorlessText.setStyle(colorlessText.getStyle().withColor(TextColor.fromFormatting(Formatting.GRAY)));

server.getPlayerManager().getPlayerList().forEach(
serverPlayerEntity -> serverPlayerEntity.sendMessage(new LiteralText("").append(coloredText).append(colorlessText), false));
serverPlayerEntity -> serverPlayerEntity.sendMessage(new LiteralText("").append(blueColoredText).append(roleColoredText).append(colorlessText), false));
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/top/xujiayao/mcdiscordchat/objects/Texts.java
Expand Up @@ -11,7 +11,8 @@ public record Texts(String serverStarted,
String advancementTask,
String advancementChallenge,
String advancementGoal,
String coloredText,
String blueColoredText,
String roleColoredText,
String colorlessText,
boolean removeVanillaFormattingFromDiscord,
boolean removeLineBreakFromDiscord) {
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/top/xujiayao/mcdiscordchat/utils/Utils.java
Expand Up @@ -72,7 +72,8 @@ public static void reloadTextsConfig() {
Main.config.textsEN.advancementTask,
Main.config.textsEN.advancementChallenge,
Main.config.textsEN.advancementGoal,
Main.config.textsEN.coloredText,
Main.config.textsZH.blueColoredText,
Main.config.textsZH.roleColoredText,
Main.config.textsEN.colorlessText,
Main.config.textsEN.removeVanillaFormattingFromDiscord,
Main.config.textsEN.removeLineBreakFromDiscord);
Expand All @@ -85,7 +86,8 @@ public static void reloadTextsConfig() {
Main.config.textsZH.advancementTask,
Main.config.textsZH.advancementChallenge,
Main.config.textsZH.advancementGoal,
Main.config.textsZH.coloredText,
Main.config.textsZH.blueColoredText,
Main.config.textsZH.roleColoredText,
Main.config.textsZH.colorlessText,
Main.config.textsZH.removeVanillaFormattingFromDiscord,
Main.config.textsZH.removeLineBreakFromDiscord);
Expand Down
2 changes: 1 addition & 1 deletion update/version.json
@@ -1 +1 @@
{"version":"1.17-1.10.6"}
{"version":"1.17-1.10.7"}

0 comments on commit f5bb243

Please sign in to comment.