Skip to content

Commit

Permalink
统一参数只放placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
Xujiayao committed Jul 9, 2024
1 parent c10a9e3 commit a8cf687
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,19 @@ public void onSlashCommandInteraction(SlashCommandInteractionEvent e) {

Objects.requireNonNull(e.getMember());

LOGGER.info(PlaceholderParser.parseOtherMessage(PlaceholderParser.parseCommandNotice(e)).getString());
Component commandNoticeText = PlaceholderParser.parseOtherMessage(
CONFIG.multiServer.enable ? CONFIG.multiServer.name : "Discord",
PlaceholderParser.parseCommandNotice(
CONFIG.generic.useServerNickname ? e.getMember().getEffectiveName() : e.getMember().getUser().getName(),
e.getMember().getRoles().stream().map(Role::getName).findFirst().orElse("null"),
String.format("#%06X", (0xFFFFFF & e.getMember().getColorRaw())),
e.getCommandString()
)
);

LOGGER.info(commandNoticeText.getString());

if (CONFIG.generic.broadcastSlashCommandExecution) {
Component commandNoticeText = PlaceholderParser.parseOtherMessage(PlaceholderParser.parseCommandNotice(e));

SERVER.getPlayerList().getPlayers().forEach(
player -> player.displayClientMessage(commandNoticeText, false));

Expand Down Expand Up @@ -301,7 +309,12 @@ public void onMessageReceived(MessageReceivedEvent e) {
referencedMemberRoleName = "null";
}

LOGGER.info(PlaceholderParser.parseResponseMessage(referencedMember, webhookName, referencedMemberRoleName, referencedMessageTemp).getString());
LOGGER.info(PlaceholderParser.parseResponseMessage(
"Discord",
(referencedMember != null) ? (CONFIG.generic.useServerNickname ? referencedMember.getEffectiveName() : referencedMember.getUser().getName()) : webhookName,
referencedMemberRoleName,
EmojiManager.replaceAllEmojis(referencedMessageTemp, emoji -> emoji.getDiscordAliases().getFirst())
).getString());
}

if (StringUtils.countMatches(messageTemp, "\n") > CONFIG.generic.discordNewlineLimit) {
Expand All @@ -314,7 +327,12 @@ public void onMessageReceived(MessageReceivedEvent e) {
memberRoleName = "null";
}

LOGGER.info(PlaceholderParser.parseChatMessage(e, memberRoleName, messageTemp).getString());
LOGGER.info(PlaceholderParser.parseChatMessage(
"Discord",
CONFIG.generic.useServerNickname ? e.getMember().getEffectiveName() : e.getMember().getUser().getName(),
memberRoleName,
EmojiManager.replaceAllEmojis(messageTemp, emoji -> emoji.getDiscordAliases().getFirst())
).getString());

if (SERVER == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@
import eu.pb4.placeholders.api.node.TextNode;
import eu.pb4.placeholders.api.node.parent.ClickActionNode;
import eu.pb4.placeholders.api.node.parent.FormattingNode;
import eu.pb4.placeholders.api.node.parent.HoverNode;
import eu.pb4.placeholders.api.parsers.MarkdownLiteParserV1;
import eu.pb4.placeholders.api.parsers.NodeParser;
import eu.pb4.placeholders.api.parsers.ParserBuilder;
import eu.pb4.placeholders.api.parsers.TagLikeParser;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.fellbaum.jemoji.EmojiManager;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
Expand All @@ -22,9 +16,7 @@
//#endif

import java.util.Map;
import java.util.Objects;

import static com.xujiayao.discord_mc_chat.Main.CONFIG;
import static com.xujiayao.discord_mc_chat.Main.SERVER;

/**
Expand All @@ -48,13 +40,13 @@ private static TextNode customUrlFormatting(TextNode[] textNodes, TextNode url)
return new ClickActionNode(TextNode.array(new FormattingNode(textNodes, ChatFormatting.YELLOW, ChatFormatting.UNDERLINE)), ClickEvent.Action.OPEN_URL, url);
}

public static Component parseOtherMessage(Component message) {
public static Component parseOtherMessage(String server, Component message) {
Map<String, Component> placeholders = Map.of(
//#if MC >= 11900
"server", Component.literal(CONFIG.multiServer.enable ? CONFIG.multiServer.name : "Discord"),
"server", Component.literal(server),
"message", message
//#else
//$$ "server", new TextComponent(CONFIG.multiServer.enable ? CONFIG.multiServer.name : "Discord"),
//$$ "server", new TextComponent(server),
//$$ "message", message
//#endif
);
Expand All @@ -67,20 +59,18 @@ public static Component parseOtherMessage(Component message) {
return parser.parseText(TextNode.of(Translations.translateMessage("message.otherMessage")), PlaceholderContext.of(SERVER).asParserContext());
}

public static Component parseCommandNotice(SlashCommandInteractionEvent e) {
Objects.requireNonNull(e.getMember());

public static Component parseCommandNotice(String name, String roleName, String roleColor, String command) {
Map<String, Component> placeholders = Map.of(
//#if MC >= 11900
"name", Component.literal(CONFIG.generic.useServerNickname ? e.getMember().getEffectiveName() : e.getMember().getUser().getName()),
"roleName", Component.literal(e.getMember().getRoles().stream().map(Role::getName).findFirst().orElse("null")),
"roleColor", Component.literal(String.format("#%06X", (0xFFFFFF & e.getMember().getColorRaw()))),
"command", Component.literal(e.getCommandString())
"name", Component.literal(name),
"roleName", Component.literal(roleName),
"roleColor", Component.literal(roleColor),
"command", Component.literal(command)
//#else
//$$ "name", new TextComponent(CONFIG.generic.useServerNickname ? e.getMember().getEffectiveName() : e.getMember().getUser().getName()),
//$$ "roleName", new TextComponent(e.getMember().getRoles().stream().map(Role::getName).findFirst().orElse("null")),
//$$ "roleColor", new TextComponent(String.format("#%06X", (0xFFFFFF & e.getMember().getColorRaw()))),
//$$ "command", new TextComponent(e.getCommandString())
//$$ "name", new TextComponent(name),
//$$ "roleName", new TextComponent(roleName),
//$$ "roleColor", new TextComponent(roleColor),
//$$ "command", new TextComponent(command)
//#endif
);

Expand All @@ -92,18 +82,18 @@ public static Component parseCommandNotice(SlashCommandInteractionEvent e) {
return parser.parseText(TextNode.of(Translations.translateMessage("message.commandNotice")), PlaceholderContext.of(SERVER).asParserContext());
}

public static Component parseResponseMessage(Member referencedMember, String webhookName, String referencedMemberRoleName, String referencedMessageTemp) {
public static Component parseResponseMessage(String server, String name, String roleName, String message) {
Map<String, Component> placeholders = Map.of(
//#if MC >= 11900
"server", Component.literal("Discord"),
"name", Component.literal((referencedMember != null) ? (CONFIG.generic.useServerNickname ? referencedMember.getEffectiveName() : referencedMember.getUser().getName()) : webhookName),
"roleName", Component.literal(referencedMemberRoleName),
"message", Component.literal(EmojiManager.replaceAllEmojis(referencedMessageTemp, emoji -> emoji.getDiscordAliases().getFirst()))
"server", Component.literal(server),
"name", Component.literal(name),
"roleName", Component.literal(roleName),
"message", Component.literal(message)
//#else
//$$ "server", new TextComponent("Discord"),
//$$ "name", new TextComponent((referencedMember != null) ? (CONFIG.generic.useServerNickname ? referencedMember.getEffectiveName() : referencedMember.getUser().getName()) : webhookName),
//$$ "roleName", new TextComponent(referencedMemberRoleName),
//$$ "message", new TextComponent(EmojiManager.replaceAllEmojis(referencedMessageTemp, emoji -> emoji.getDiscordAliases().getFirst()))
//$$ "server", new TextComponent(server),
//$$ "name", new TextComponent(name),
//$$ "roleName", new TextComponent(roleName),
//$$ "message", new TextComponent(message)
//#endif
);

Expand All @@ -115,20 +105,18 @@ public static Component parseResponseMessage(Member referencedMember, String web
return parser.parseText(TextNode.of(Translations.translateMessage("message.responseMessage")), PlaceholderContext.of(SERVER).asParserContext());
}

public static Component parseChatMessage(MessageReceivedEvent e, String memberRoleName, String messageTemp) {
Objects.requireNonNull(e.getMember());

public static Component parseChatMessage(String server, String name, String roleName, String message) {
Map<String, Component> placeholders = Map.of(
//#if MC >= 11900
"server", Component.literal("Discord"),
"name", Component.literal(CONFIG.generic.useServerNickname ? e.getMember().getEffectiveName() : e.getMember().getUser().getName()),
"roleName", Component.literal(memberRoleName),
"message", Component.literal(EmojiManager.replaceAllEmojis(messageTemp, emoji -> emoji.getDiscordAliases().getFirst()))
"server", Component.literal(server),
"name", Component.literal(name),
"roleName", Component.literal(roleName),
"message", Component.literal(message)
//#else
//$$ "server", new TextComponent("Discord"),
//$$ "name", new TextComponent(CONFIG.generic.useServerNickname ? e.getMember().getEffectiveName() : e.getMember().getUser().getName()),
//$$ "roleName", new TextComponent(memberRoleName),
//$$ "message", new TextComponent(EmojiManager.replaceAllEmojis(messageTemp, emoji -> emoji.getDiscordAliases().getFirst()))
//$$ "server", new TextComponent(server),
//$$ "name", new TextComponent(name),
//$$ "roleName", new TextComponent(roleName),
//$$ "message", new TextComponent(message)
//#endif
);

Expand Down

0 comments on commit a8cf687

Please sign in to comment.