Skip to content

Commit

Permalink
refactor: use merge parser to parse nodes.
Browse files Browse the repository at this point in the history
  • Loading branch information
sakurawald committed Jul 14, 2024
1 parent c2c74df commit f4cc2bf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ public class Chat {
See: https://docs.advntr.dev/minimessage/format.html
""")
public String format = "<#B1B2FF>[%fuji:player_playtime%\uD83D\uDD25 %fuji:player_mined%⛏ %fuji:player_placed%\uD83D\uDD33 %fuji:player_killed%\uD83D\uDDE1 %fuji:player_moved%\uD83C\uDF0D]<reset> <<dark_green><click:suggest_command:/msg %player% ><hover:show_text:\"Time: <date:'yyyy-MM-dd HH:mm:ss'><newline><italic>Click to Message\">%player%</hover></click></dark_green>> %message%";
public String format = "<#B1B2FF>[%fuji:player_playtime%\uD83D\uDD25 %fuji:player_mined%⛏ %fuji:player_placed%\uD83D\uDD33 %fuji:player_killed%\uD83D\uDDE1 %fuji:player_moved%\uD83C\uDF0D]<reset> <<dark_green><click:suggest_command:/msg %player% ><hover:show_text:\"Time: %fuji:date%<newline><italic>Click to Message\">%player%</hover></click></dark_green>> %message%";

public MentionPlayer mention_player = new MentionPlayer();
public History history = new History();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public abstract class ServerPlayerMixin implements AfkStateAccessor {

if (accessor.fuji$isAfk()) {
cir.setReturnValue(Text.literal("afk " + player.getGameProfile().getName()));
net.kyori.adventure.text.@NotNull Component component = ofComponent(Configs.configHandler.model().modules.afk.format)
net.kyori.adventure.text.@NotNull Component component = ofComponent(null, false,Configs.configHandler.model().modules.afk.format)
.replaceText(TextReplacementConfig.builder().match("%player_display_name%").replacement(player.getDisplayName()).build());
cir.setReturnValue(toVomponent(component));
} else {
Expand Down
47 changes: 29 additions & 18 deletions src/main/java/io/github/sakurawald/util/MessageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import eu.pb4.placeholders.api.ParserContext;
import eu.pb4.placeholders.api.PlaceholderContext;
import eu.pb4.placeholders.api.Placeholders;
import eu.pb4.placeholders.api.node.LiteralNode;
import eu.pb4.placeholders.api.node.TextNode;
import eu.pb4.placeholders.api.parsers.NodeParser;
import eu.pb4.placeholders.api.parsers.TagParser;
import eu.pb4.placeholders.api.parsers.tag.TagRegistry;
import eu.pb4.placeholders.api.parsers.tag.TextTag;
import eu.pb4.placeholders.impl.textparser.MergedParser;
import io.github.sakurawald.Fuji;
import io.github.sakurawald.config.Configs;
import io.github.sakurawald.config.handler.ResourceConfigHandler;
Expand All @@ -30,17 +36,33 @@
import java.util.List;
import java.util.Map;

import static eu.pb4.placeholders.api.Placeholders.DEFAULT_PLACEHOLDER_PARSER;

@UtilityClass
public class MessageUtil {
private static final MergedParser MERGED_PARSER = new MergedParser(
new NodeParser[]{
DEFAULT_PLACEHOLDER_PARSER
, TagParser.QUICK_TEXT_WITH_STF
});
private static final FabricServerAudiences adventure = FabricServerAudiences.of(Fuji.SERVER);
@Getter
private static final Map<String, String> player2lang = new HashMap<>();
@Getter
private static final Map<String, JsonObject> lang2json = new HashMap<>();
private static final MiniMessage miniMessageParser = MiniMessage.builder().build();

static {
writeDefaultLanguageFiles();

TagRegistry.registerDefault(
TextTag.self(
"newline",
"formatting",
true,
(nodes, data, parser) -> new LiteralNode("\n")
)

);
}

private static void writeDefaultLanguageFiles() {
Expand Down Expand Up @@ -118,10 +140,11 @@ public static void sendMessageToPlayerEntity(PlayerEntity player, String key, Ob
player.sendMessage(adventure.toNative(ofComponent(null, false, getString(player, key), args)));
}


/* This is the core method to map `String` into `Component`.
* All methods that return `Vomponent` are converted from this method.
* */
public static Component ofComponent(@Nullable Audience audience, boolean isKey, String keyOrString, MiniMessage serializer, Object... args) {
public static Component ofComponent(@Nullable Audience audience, boolean isKey, String keyOrString, Object... args) {
String string = isKey ? getString(audience, keyOrString, args) : keyOrString;

PlaceholderContext placeholderContext;
Expand All @@ -130,24 +153,13 @@ public static Component ofComponent(@Nullable Audience audience, boolean isKey,
} else {
placeholderContext = PlaceholderContext.of(Fuji.SERVER);
}
ParserContext parserContext = ParserContext.of(PlaceholderContext.KEY, placeholderContext);

// placeholder parser
Component component = Placeholders.parseText(TextNode.of(string), placeholderContext).asComponent();
string = PlainTextComponentSerializer.plainText().serialize(component);

// minimessage parser
if (serializer == null) {
serializer = miniMessageParser;
}
return serializer.deserialize(string);
}

public static Component ofComponent(Audience audience, boolean isKey, String keyOrString, Object... args) {
return ofComponent(audience, isKey, keyOrString, miniMessageParser, args);
return MERGED_PARSER.parseText(TextNode.of(string), parserContext).asComponent();
}

public static Component ofComponent(Audience audience, String key, Object... args) {
return ofComponent(audience, true, key, null, args);
return ofComponent(audience, true, key, args);
}

public static Text ofVomponent(Audience audience, String key, Object... args) {
Expand Down Expand Up @@ -188,5 +200,4 @@ public static void sendBroadcast(String key, Object... args) {
sendMessage(player, key, args);
}
}

}

0 comments on commit f4cc2bf

Please sign in to comment.