Skip to content

Commit 8a6dce7

Browse files
committed
Fix TextNode#convert and legacy text parsing not working
1 parent 7619d67 commit 8a6dce7

11 files changed

Lines changed: 164 additions & 113 deletions

File tree

gradle.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ org.gradle.jvmargs=-Xmx1G
33

44
# Fabric Properties
55
# check these on https://fabricmc.net/use
6-
minecraft_version=1.21.5-rc1
7-
yarn_mappings=1.21.5-rc1+build.1
6+
minecraft_version=1.21.5
7+
yarn_mappings=1.21.5+build.1
88
loader_version=0.16.10
99

1010
#Fabric api
1111
fabric_version=0.119.1+1.21.5
1212

1313
# Mod Properties
14-
mod_version = 2.6.1+1.21.5
14+
mod_version = 2.6.2+1.21.5
1515
maven_group = eu.pb4
1616
archives_base_name = placeholder-api

src/main/java/eu/pb4/placeholders/api/ParserContext.java

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@
1414
public final class ParserContext {
1515
private Map<Key<?>, Object> map;
1616
private boolean copyOnWrite;
17+
private boolean hasNodeContext;
1718

18-
private ParserContext(Map<Key<?>, Object> map, boolean copyOnWrite) {
19+
private ParserContext(Map<Key<?>, Object> map, boolean copyOnWrite, boolean hasNodeContext) {
1920
this.map = map;
2021
this.copyOnWrite = copyOnWrite;
22+
this.hasNodeContext = hasNodeContext;
2123
}
2224

2325
public static ParserContext of() {
24-
return new ParserContext(new HashMap<>(), false);
26+
return new ParserContext(new HashMap<>(), false, false);
2527
}
2628

2729
public static <T> ParserContext of(Key<T> key, T object) {
@@ -34,6 +36,7 @@ public <T> ParserContext with(Key<T> key, T object) {
3436
this.copyOnWrite = false;
3537
}
3638
this.map.put(key, object);
39+
this.hasNodeContext |= key.nodeContext();
3740
return this;
3841
}
3942

@@ -68,24 +71,52 @@ public boolean contains(Key<?> key) {
6871

6972
public ParserContext copy() {
7073
this.copyOnWrite = true;
71-
return new ParserContext(this.map, true);
74+
return new ParserContext(this.map, true, this.hasNodeContext);
7275
}
7376

77+
public ParserContext copyWithoutNodeContext() {
78+
if (this.hasNodeContext) {
79+
var map = new HashMap<Key<?>, Object>();
80+
for (var key : this.map.keySet()) {
81+
if (!key.nodeContext()) {
82+
map.put(key, this.map.get(key));
83+
}
84+
}
85+
86+
return new ParserContext(map, false, false);
87+
}
88+
return this.copy();
89+
}
90+
91+
public record Key<T>(String key, @Nullable Class<T> type, boolean nodeContext) {
92+
public Key(String key, @Nullable Class<T> type) {
93+
this(key, type, false);
94+
}
95+
7496

75-
public record Key<T>(String key, @Nullable Class<T> type) {
7697
public static final Key<Boolean> COMPACT_TEXT = new Key<>("compact_text", Boolean.class);
7798
public static final Key<RegistryWrapper.WrapperLookup> WRAPPER_LOOKUP = new Key<>("wrapper_lookup", RegistryWrapper.WrapperLookup.class);
78-
public static final Key<DynamicShadowNode.Transformer> DEFAULT_SHADOW_STYLER = Key.of("default_shadow_styler");
99+
public static final Key<DynamicShadowNode.Transformer> DEFAULT_SHADOW_STYLER = Key.ofNode("default_shadow_styler");
79100

80101
public static <T> Key<T> of(String key, T type) {
81102
//noinspection unchecked
82-
return new Key<T>(key, (Class<T>) type.getClass());
103+
return new Key<T>(key, (Class<T>) type.getClass(), false);
83104
}
84105

85-
86106
public static <T> Key<T> of(String key) {
87107
//noinspection unchecked
88-
return new Key<T>(key, null);
108+
return new Key<T>(key, null, false);
109+
}
110+
111+
public static <T> Key<T> ofNode(String key, T type) {
112+
//noinspection unchecked
113+
return new Key<T>(key, (Class<T>) type.getClass(), true);
114+
}
115+
116+
117+
public static <T> Key<T> ofNode(String key) {
118+
//noinspection unchecked
119+
return new Key<T>(key, null, true);
89120
}
90121
};
91122
}

src/main/java/eu/pb4/placeholders/api/Placeholders.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,31 +89,31 @@ public static Text parseText(TextNode textNode, PlaceholderContext context) {
8989
}
9090

9191

92-
@Deprecated
92+
@Deprecated(forRemoval = true)
9393
public static ParentNode parseNodes(TextNode node, Pattern pattern) {
9494
return parseNodes(node, pattern, PlaceholderContext.KEY);
9595
}
96-
@Deprecated
96+
@Deprecated(forRemoval = true)
9797
public static ParentNode parseNodes(TextNode node, Pattern pattern, ParserContext.Key<PlaceholderContext> contextKey) {
9898
return asSingleParent(PatternPlaceholderParser.of(pattern, contextKey, DEFAULT_PLACEHOLDER_GETTER).parseNodes(node));
9999
}
100-
@Deprecated
100+
@Deprecated(forRemoval = true)
101101
public static ParentNode parseNodes(TextNode node, Pattern pattern, PlaceholderGetter placeholderGetter) {
102102
return parseNodes(node, pattern, placeholderGetter, PlaceholderContext.KEY);
103103
}
104-
@Deprecated
104+
@Deprecated(forRemoval = true)
105105
public static ParentNode parseNodes(TextNode node, Pattern pattern, PlaceholderGetter placeholderGetter, ParserContext.Key<PlaceholderContext> contextKey) {
106106
return asSingleParent(PatternPlaceholderParser.of(pattern, contextKey, placeholderGetter).parseNodes(node));
107107
}
108-
@Deprecated
108+
@Deprecated(forRemoval = true)
109109
public static ParentNode parseNodes(TextNode node, Pattern pattern, Map<String, Text> placeholders) {
110110
return asSingleParent(PatternPlaceholderParser.ofTextMap(pattern, placeholders).parseNodes(node));
111111
}
112-
@Deprecated
112+
@Deprecated(forRemoval = true)
113113
public static ParentNode parseNodes(TextNode node, Pattern pattern, Set<String> placeholders, ParserContext.Key<PlaceholderGetter> key) {
114114
return parseNodes(node, pattern, placeholders, key, PlaceholderContext.KEY);
115115
}
116-
@Deprecated
116+
@Deprecated(forRemoval = true)
117117
public static ParentNode parseNodes(TextNode node, Pattern pattern, Set<String> placeholders, ParserContext.Key<PlaceholderGetter> key, ParserContext.Key<PlaceholderContext> contextKey) {
118118
return asSingleParent(PatternPlaceholderParser.of(pattern, contextKey, new PlaceholderGetter() {
119119
@Override
@@ -133,43 +133,43 @@ public boolean isContextOptional() {
133133
}
134134
}).parseNodes(node));
135135
}
136-
@Deprecated
136+
@Deprecated(forRemoval = true)
137137
public static Text parseText(Text text, PlaceholderContext context, Pattern pattern) {
138138
return parseNodes(TextNode.convert(text), pattern).toText(ParserContext.of(PlaceholderContext.KEY, context));
139139
}
140-
@Deprecated
140+
@Deprecated(forRemoval = true)
141141
public static Text parseText(Text text, PlaceholderContext context, Pattern pattern, PlaceholderGetter placeholderGetter) {
142142
return parseNodes(TextNode.convert(text), pattern, placeholderGetter).toText(ParserContext.of(PlaceholderContext.KEY, context));
143143
}
144-
@Deprecated
144+
@Deprecated(forRemoval = true)
145145
public static Text parseText(Text text, Pattern pattern, Map<String, Text> placeholders) {
146146
return parseNodes(TextNode.convert(text), pattern, placeholders).toText(ParserContext.of());
147147
}
148-
@Deprecated
148+
@Deprecated(forRemoval = true)
149149
public static Text parseText(Text text, Pattern pattern, Set<String> placeholders, ParserContext.Key<PlaceholderGetter> key) {
150150
return parseNodes(TextNode.convert(text), pattern, placeholders, key).toText(ParserContext.of());
151151
}
152152

153-
@Deprecated
153+
@Deprecated(forRemoval = true)
154154
public static Text parseText(TextNode textNode, PlaceholderContext context, Pattern pattern) {
155155
return parseNodes(textNode, pattern).toText(ParserContext.of(PlaceholderContext.KEY, context));
156156
}
157157

158-
@Deprecated
158+
@Deprecated(forRemoval = true)
159159
public static Text parseText(TextNode textNode, PlaceholderContext context, Pattern pattern, PlaceholderGetter placeholderGetter) {
160160
return parseNodes(textNode, pattern, placeholderGetter).toText(ParserContext.of(PlaceholderContext.KEY, context));
161161
}
162162

163-
@Deprecated
163+
@Deprecated(forRemoval = true)
164164
public static Text parseText(TextNode textNode, PlaceholderContext context, Pattern pattern, Map<String, Text> placeholders) {
165165
return parseNodes(textNode, pattern, placeholders).toText(ParserContext.of(PlaceholderContext.KEY, context));
166166
}
167-
@Deprecated
167+
@Deprecated(forRemoval = true)
168168
public static Text parseText(TextNode textNode, Pattern pattern, Map<String, Text> placeholders) {
169169
return parseNodes(textNode, pattern, placeholders).toText();
170170
}
171171

172-
@Deprecated
172+
@Deprecated(forRemoval = true)
173173
public static Text parseText(TextNode textNode, Pattern pattern, Set<String> placeholders, ParserContext.Key<PlaceholderGetter> key) {
174174
return parseNodes(textNode, pattern, placeholders, key).toText();
175175
}

src/main/java/eu/pb4/placeholders/api/TextParserUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
* You should use {@link eu.pb4.placeholders.api.parsers.ParserBuilder} for stacked parsing
1212
* or {@link eu.pb4.placeholders.api.parsers.TagParser} for only tags to text.
1313
*/
14-
@Deprecated
14+
@Deprecated(forRemoval = true)
1515
public final class TextParserUtils {
1616
private TextParserUtils() {}
1717

1818
public static Text formatText(String text) {
19-
return formatNodes(text).toText(null, true);
19+
return formatNodes(text).toText(ParserContext.of(), true);
2020
}
2121

2222
public static Text formatTextSafe(String text) {
23-
return formatNodesSafe(text).toText(null, true);
23+
return formatNodesSafe(text).toText(ParserContext.of(), true);
2424
}
2525

2626
public static Text formatText(String text, TextParserV1.TagParserGetter getter) {

src/main/java/eu/pb4/placeholders/api/node/parent/HoverNode.java

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,7 @@ public T value() {
4141
@SuppressWarnings("unchecked")
4242
@Override
4343
protected Style style(ParserContext context) {
44-
if (this.action == Action.TEXT_NODE) {
45-
return Style.EMPTY.withHoverEvent(new HoverEvent.ShowText(((TextNode) this.value).toText(context, true)));
46-
} else if (this.action == Action.ENTITY_NODE) {
47-
return Style.EMPTY.withHoverEvent(new HoverEvent.ShowEntity(((EntityNodeContent) this.value).toVanilla(context)));
48-
} else if (this.action == Action.LAZY_ITEM_STACK) {
49-
RegistryWrapper.WrapperLookup wrapper;
50-
if (context.contains(ParserContext.Key.WRAPPER_LOOKUP)) {
51-
wrapper = context.getOrThrow(ParserContext.Key.WRAPPER_LOOKUP);
52-
} else if (context.contains(PlaceholderContext.KEY)) {
53-
wrapper = context.getOrThrow(PlaceholderContext.KEY).server().getRegistryManager();
54-
} else {
55-
return Style.EMPTY;
56-
}
57-
58-
return Style.EMPTY.withHoverEvent(new HoverEvent.ShowItem(((LazyItemStackNodeContent<T>) this.value).toVanilla(wrapper)));
59-
} else if (this.action == Action.VANILLA_ITEM_STACK) {
60-
return Style.EMPTY.withHoverEvent(new HoverEvent.ShowItem(((HoverEvent.ShowItem) this.value).item()));
61-
} else if (this.action == Action.VANILLA_ENTITY) {
62-
return Style.EMPTY.withHoverEvent(new HoverEvent.ShowEntity(((HoverEvent.ShowEntity) this.value).entity()));
63-
} else {
64-
return Style.EMPTY;
65-
}
44+
return Style.EMPTY.withHoverEvent(toVanilla(this.action, this.value, context));
6645
}
6746

6847
@Override
@@ -111,6 +90,33 @@ public ParentTextNode copyWith(TextNode[] children, NodeParser parser) {
11190
} return this.copyWith(children);
11291
}
11392

93+
94+
@Nullable
95+
public static <T> HoverEvent toVanilla(HoverNode.Action<T, ?> action, T value, ParserContext context) {
96+
if (action == Action.TEXT_NODE) {
97+
return new HoverEvent.ShowText(((TextNode) value).toText(context.copyWithoutNodeContext(), true));
98+
} else if (action == Action.ENTITY_NODE) {
99+
return new HoverEvent.ShowEntity(((EntityNodeContent) value).toVanilla(context.copyWithoutNodeContext()));
100+
} else if (action == Action.LAZY_ITEM_STACK) {
101+
RegistryWrapper.WrapperLookup wrapper;
102+
if (context.contains(ParserContext.Key.WRAPPER_LOOKUP)) {
103+
wrapper = context.getOrThrow(ParserContext.Key.WRAPPER_LOOKUP);
104+
} else if (context.contains(PlaceholderContext.KEY)) {
105+
wrapper = context.getOrThrow(PlaceholderContext.KEY).server().getRegistryManager();
106+
} else {
107+
return null;
108+
}
109+
110+
return new HoverEvent.ShowItem(((LazyItemStackNodeContent<T>) value).toVanilla(wrapper));
111+
} else if (action == Action.VANILLA_ITEM_STACK) {
112+
return new HoverEvent.ShowItem(((HoverEvent.ShowItem) value).item());
113+
} else if (action == Action.VANILLA_ENTITY) {
114+
return new HoverEvent.ShowEntity(((HoverEvent.ShowEntity) value).entity());
115+
} else {
116+
return null;
117+
}
118+
}
119+
114120
@Override
115121
public String toString() {
116122
return "HoverNode{" +
@@ -139,14 +145,11 @@ public String toString()
139145
}
140146
}
141147

142-
public record EntityNodeContent(EntityType<?>entityType, UUID uuid, @Nullable TextNode name) implements HoverEvent {
143-
public EntityContent toVanilla(ParserContext context) {
144-
return new EntityContent(this.entityType, this.uuid, Optional.ofNullable(this.name != null ? this.name.toText(context, true) : null));
148+
public record EntityNodeContent(EntityType<?>entityType, UUID uuid, @Nullable TextNode name) {
149+
public HoverEvent.EntityContent toVanilla(ParserContext context) {
150+
return new HoverEvent.EntityContent(this.entityType, this.uuid, Optional.ofNullable(this.name != null ? this.name.toText(context, true) : null));
145151
}
146152

147-
@Override
148-
public Action getAction() { return Action.SHOW_ENTITY; }
149-
150153
@Override
151154
public String toString() {
152155
return "HoverNode$EntityNodeContent{id="+
@@ -159,7 +162,7 @@ public String toString() {
159162
}
160163
}
161164

162-
public record LazyItemStackNodeContent<T>(Identifier identifier, int count, DynamicOps<T> ops, T componentMap) implements HoverEvent {
165+
public record LazyItemStackNodeContent<T>(Identifier identifier, int count, DynamicOps<T> ops, T componentMap) {
163166
public ItemStack toVanilla(RegistryWrapper.WrapperLookup lookup) {
164167
var stack = new ItemStack(lookup.getOrThrow(RegistryKeys.ITEM).getOrThrow(RegistryKey.of(RegistryKeys.ITEM, identifier)));
165168
stack.setCount(count);
@@ -169,9 +172,6 @@ public ItemStack toVanilla(RegistryWrapper.WrapperLookup lookup) {
169172
return stack;
170173
}
171174

172-
@Override
173-
public Action getAction() { return Action.SHOW_ITEM; }
174-
175175
@Override
176176
public String toString() {
177177
return "HoverNode$LazyItemStackNodeContent{id="

0 commit comments

Comments
 (0)