Skip to content

Commit

Permalink
Fix Link/Mention/Spoiler Parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Apr 17, 2023
1 parent d7c216a commit c7e5e1f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fabric_version=0.75.1+1.19.4

# Mod Properties

mod_version = 2.1.5+1.19.4
mod_version = 2.1.6+1.19.4
maven_group = eu.pb4
archives_base_name = styled-chat

Expand Down
17 changes: 13 additions & 4 deletions src/main/java/eu/pb4/styledchat/parser/LinkParser.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package eu.pb4.styledchat.parser;

import eu.pb4.placeholders.api.ParserContext;
import eu.pb4.placeholders.api.PlaceholderContext;
import eu.pb4.placeholders.api.Placeholders;
import eu.pb4.placeholders.api.node.DirectTextNode;
import eu.pb4.placeholders.api.node.LiteralNode;
import eu.pb4.placeholders.api.node.TextNode;
import eu.pb4.placeholders.api.node.parent.ClickActionNode;
import eu.pb4.placeholders.api.node.parent.ParentNode;
import eu.pb4.placeholders.api.node.parent.ParentTextNode;
import eu.pb4.placeholders.api.parsers.NodeParser;
import eu.pb4.styledchat.config.ConfigManager;
import net.minecraft.text.ClickEvent;
import net.minecraft.text.MutableText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;

import java.util.ArrayList;
Expand Down Expand Up @@ -41,7 +46,11 @@ public TextNode[] parseNodes(TextNode node) {
list.add(new LiteralNode(betweenText));
}

list.add(new ClickActionNode(Placeholders.parseNodes(style, Placeholders.PREDEFINED_PLACEHOLDER_PATTERN, Map.of("link", Text.literal(matcher.group()))).getChildren(), ClickEvent.Action.OPEN_URL, new LiteralNode(matcher.group())));
var link = matcher.group();

var text = style.toText(ParserContext.of(DynamicNode.NODES, Map.of("url", Text.literal(link), "link", Text.literal(link))));

list.add(new DirectTextNode(Text.empty().append(text).setStyle(Style.EMPTY.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, link)))));

currentPos = matcher.end();
}
Expand All @@ -54,14 +63,14 @@ public TextNode[] parseNodes(TextNode node) {
}

return list.toArray(new TextNode[0]);
} else if (node instanceof ParentTextNode parentTextNode) {
} else if (node instanceof ParentNode parentNode) {
var list = new ArrayList<TextNode>();

for (var child : parentTextNode.getChildren()) {
for (var child : parentNode.getChildren()) {
list.addAll(List.of(this.parseNodes(child)));
}

return new TextNode[] { parentTextNode.copyWith(list.toArray(new TextNode[0])) };
return new TextNode[] { parentNode.copyWith(list.toArray(new TextNode[0])) };
}

return new TextNode[] { node };
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/eu/pb4/styledchat/parser/MentionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import eu.pb4.placeholders.api.PlaceholderContext;
import eu.pb4.placeholders.api.Placeholders;
import eu.pb4.placeholders.api.node.DirectTextNode;
import eu.pb4.placeholders.api.node.LiteralNode;
import eu.pb4.placeholders.api.node.TextNode;
import eu.pb4.placeholders.api.node.parent.ParentTextNode;
Expand Down Expand Up @@ -43,7 +44,7 @@ public TextNode[] parseInput(String input) {
if (startPos != -1) {
int endPos = startPos + player.getEntityName().length();
TextNode[] before = parseInput(input.substring(0, startPos));
TextNode mention = TextNode.convert(Placeholders.parseText(style, PlaceholderContext.of(player)));
TextNode mention = new DirectTextNode(style.toText(PlaceholderContext.of(player)));
TextNode[] after = parseInput(input.substring(Math.min(endPos, input.length())));
return Stream.of(before, new TextNode[]{mention}, after).flatMap(Stream::of).toArray(TextNode[]::new);
}
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/eu/pb4/styledchat/parser/SpoilerNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@ public SpoilerNode(TextNode[] children) {
protected Text applyFormatting(MutableText out, ParserContext context) {
var config = ConfigManager.getConfig();
var ctx = context.get(PlaceholderContext.KEY);
var obj = ((MutableText) Placeholders.parseText(config.getSpoilerStyle(ctx),
Placeholders.PREDEFINED_PLACEHOLDER_PATTERN,
Map.of("spoiler", Text.literal(config.getSpoilerSymbole(ctx).repeat(out.getString().length())))
));

return obj.setStyle(obj.getStyle().withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, out)));
var obj = config.getSpoilerStyle(ctx).toText(ctx.asParserContext()
.with(DynamicNode.NODES, Map.of("spoiler", Text.literal(config.getSpoilerSymbole(ctx).repeat(out.getString().length())))));
return Text.empty().append(obj).setStyle(obj.getStyle().withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, out)));
}

@Override
Expand Down

0 comments on commit c7e5e1f

Please sign in to comment.