Use TagResolver instead of Template

This commit is contained in:
Roman Zhuravlev 2025-11-02 04:10:03 +05:00
parent 6597b2bade
commit d99f6fb11b

View file

@ -4,12 +4,13 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import net.frankheijden.serverutils.common.config.MessagesResource; import net.frankheijden.serverutils.common.config.MessagesResource;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.Template; import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
public class KeyValueComponentBuilder { public class KeyValueComponentBuilder {
private final MessagesResource.Message format; private final MessagesResource.Message format;
private final List<Template[]> templatesList; private final List<TagResolver[]> templatesList;
private final String keyPlaceholder; private final String keyPlaceholder;
private final String valuePlaceholder; private final String valuePlaceholder;
@ -43,8 +44,8 @@ public class KeyValueComponentBuilder {
return new KeyValuePair(key); return new KeyValuePair(key);
} }
private KeyValueComponentBuilder add(Template key, Template value) { private KeyValueComponentBuilder add(TagResolver key, TagResolver value) {
this.templatesList.add(new Template[]{ key, value }); this.templatesList.add(new TagResolver[]{ key, value });
return this; return this;
} }
@ -54,7 +55,7 @@ public class KeyValueComponentBuilder {
public List<Component> build() { public List<Component> build() {
List<Component> components = new ArrayList<>(templatesList.size()); List<Component> components = new ArrayList<>(templatesList.size());
for (Template[] templates : templatesList) { for (TagResolver[] templates : templatesList) {
components.add(format.toComponent(templates)); components.add(format.toComponent(templates));
} }
@ -63,24 +64,24 @@ public class KeyValueComponentBuilder {
public class KeyValuePair { public class KeyValuePair {
private final Template key; private final TagResolver key;
private KeyValuePair(String key) { private KeyValuePair(String key) {
this.key = Template.of(keyPlaceholder, key); this.key = TagResolver.resolver(keyPlaceholder, Tag.inserting(Component.text(key)));
} }
private KeyValuePair(Component key) { private KeyValuePair(Component key) {
this.key = Template.of(keyPlaceholder, key); this.key = TagResolver.resolver(keyPlaceholder, Tag.inserting(key));
} }
public KeyValueComponentBuilder value(String value) { public KeyValueComponentBuilder value(String value) {
if (value == null) return KeyValueComponentBuilder.this; if (value == null) return KeyValueComponentBuilder.this;
return add(key, Template.of(valuePlaceholder, value)); return add(key, TagResolver.resolver(valuePlaceholder, Tag.inserting(Component.text(value))));
} }
public KeyValueComponentBuilder value(Component value) { public KeyValueComponentBuilder value(Component value) {
if (value == null) return KeyValueComponentBuilder.this; if (value == null) return KeyValueComponentBuilder.this;
return add(key, Template.of(valuePlaceholder, value)); return add(key, TagResolver.resolver(valuePlaceholder, Tag.inserting(value)));
} }
} }
} }