Skip to content

Commit

Permalink
Fix some tag's arguments not running through parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Jul 15, 2024
1 parent accf2f2 commit d855290
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ org.gradle.jvmargs=-Xmx1G
fabric_version=0.100.1+1.21

# Mod Properties
mod_version = 2.4.0+1.21
mod_version = 2.4.1+1.21
maven_group = eu.pb4
archives_base_name = placeholder-api

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package eu.pb4.placeholders.api.node.parent;

import eu.pb4.placeholders.api.ParserContext;
import eu.pb4.placeholders.api.node.TextNode;
import eu.pb4.placeholders.api.parsers.NodeParser;
import net.minecraft.text.Style;
import net.minecraft.text.TextColor;

import java.util.Arrays;

public final class DynamicColorNode extends SimpleStylingNode {
private final TextNode color;

public DynamicColorNode(TextNode[] children, TextNode color) {
super(children);
this.color = color;
}

@Override
public boolean isDynamicNoChildren() {
return this.color.isDynamic();
}

@Override
protected Style style(ParserContext context) {
var c = TextColor.parse(color.toText(context).getString());
return c.result().map(Style.EMPTY::withColor).orElse(Style.EMPTY);
}

@Override
public ParentTextNode copyWith(TextNode[] children) {
return new DynamicColorNode(children, this.color);
}

@Override
public ParentTextNode copyWith(TextNode[] children, NodeParser parser) {
return new DynamicColorNode(children, parser.parseNode(color));
}

@Override
public String toString() {
return "ColorNode{" +
"color=" + color +
", children=" + Arrays.toString(children) +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static void register() {
"color",
true,
(nodes, data, parser) -> {
return new ColorNode(nodes, TextColor.parse(data.get("value", 0, "white")).result().orElse(DEFAULT_COLOR));
return new DynamicColorNode(nodes, parser.parseNode(data.get("value", 0, "white")));
})
);
}
Expand Down Expand Up @@ -199,7 +199,7 @@ public static void register() {
var value = data.getNext("value", "");
for (ClickEvent.Action action : ClickEvent.Action.values()) {
if (action.asString().equals(type)) {
return new ClickActionNode(nodes, action, new LiteralNode(value));
return new ClickActionNode(nodes, action, parser.parseNode(value));
}
}
}
Expand All @@ -216,7 +216,7 @@ public static void register() {
false,
(nodes, data, parser) -> {
if (!data.isEmpty()) {
return new ClickActionNode(nodes, ClickEvent.Action.RUN_COMMAND, new LiteralNode(data.get("value", 0)));
return new ClickActionNode(nodes, ClickEvent.Action.RUN_COMMAND, parser.parseNode(data.get("value", 0)));
}
return new ParentNode(nodes);
}
Expand All @@ -234,7 +234,7 @@ public static void register() {
(nodes, data, parser) -> {

if (!data.isEmpty()) {
return new ClickActionNode(nodes, ClickEvent.Action.SUGGEST_COMMAND, new LiteralNode(data.getNext("value", "")));
return new ClickActionNode(nodes, ClickEvent.Action.SUGGEST_COMMAND, parser.parseNode(data.getNext("value", "")));
}
return new ParentNode(nodes);
}
Expand All @@ -251,7 +251,7 @@ public static void register() {
false, (nodes, data, parser) -> {

if (!data.isEmpty()) {
return new ClickActionNode(nodes, ClickEvent.Action.OPEN_URL, new LiteralNode(data.get("value", 0)));
return new ClickActionNode(nodes, ClickEvent.Action.OPEN_URL, parser.parseNode(data.get("value", 0)));
}
return new ParentNode(nodes);
}
Expand All @@ -269,7 +269,7 @@ public static void register() {
(nodes, data, parser) -> {

if (!data.isEmpty()) {
return new ClickActionNode(nodes, ClickEvent.Action.COPY_TO_CLIPBOARD, new LiteralNode(data.get("value", 0)));
return new ClickActionNode(nodes, ClickEvent.Action.COPY_TO_CLIPBOARD, parser.parseNode(data.get("value", 0)));
}
return new ParentNode(nodes);
}
Expand All @@ -285,7 +285,7 @@ public static void register() {
"click_action",
true, (nodes, data, parser) -> {
if (!data.isEmpty()) {
return new ClickActionNode(nodes, ClickEvent.Action.CHANGE_PAGE, new LiteralNode(data.get("value", 0)));
return new ClickActionNode(nodes, ClickEvent.Action.CHANGE_PAGE, parser.parseNode(data.get("value", 0)));
}
return new ParentNode(nodes);
}));
Expand Down Expand Up @@ -358,7 +358,7 @@ public static void register() {
List.of("insertion"),
"click_action",
false,
(nodes, data, parser) -> new InsertNode(nodes, new LiteralNode(data.get("value", 0)))));
(nodes, data, parser) -> new InsertNode(nodes, parser.parseNode(data.get("value", 0)))));
}

{
Expand Down
15 changes: 15 additions & 0 deletions src/testmod/java/eu/pb4/placeholderstest/TestMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,16 @@ private static int test7(CommandContext<ServerCommandSource> context) {
return 0;
}

private static int test8(CommandContext<ServerCommandSource> context) {
try {
var parser = NodeParser.builder().quickText().globalPlaceholders().build();
context.getSource().sendMessage(parser.parseText(StringArgumentType.getString(context, "text"), PlaceholderContext.of(context.getSource()).asParserContext()));
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}

public void onInitialize() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, dedicated) -> {
dispatcher.register(
Expand Down Expand Up @@ -319,6 +329,11 @@ public void onInitialize() {
dispatcher.register(
literal("test7").executes(TestMod::test7)
);

dispatcher.register(
literal("test8").then(argument("text", StringArgumentType.greedyString()).executes(TestMod::test8))
);

});
}

Expand Down

0 comments on commit d855290

Please sign in to comment.