Use TagResolver instead of Template

This commit is contained in:
Roman Zhuravlev 2025-11-02 04:10:06 +05:00
parent d99f6fb11b
commit 400db548a0

View file

@ -36,7 +36,8 @@ import net.frankheijden.serverutils.common.utils.ListComponentBuilder;
import net.frankheijden.serverutils.common.utils.KeyValueComponentBuilder; import net.frankheijden.serverutils.common.utils.KeyValueComponentBuilder;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TextComponent;
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 abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?, ?>, P, C extends ServerUtilsAudience<?>> public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?, ?>, P, C extends ServerUtilsAudience<?>>
extends ServerUtilsCommand<U, C> { extends ServerUtilsCommand<U, C> {
@ -117,8 +118,9 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?,
if (commandElement.shouldDisplayInHelp()) { if (commandElement.shouldDisplayInHelp()) {
sender.sendMessage(helpFormatMessage.toComponent( sender.sendMessage(helpFormatMessage.toComponent(
Template.of("command", shortestCommandAlias), TagResolver.resolver("command", Tag.inserting(Component.text(shortestCommandAlias))),
Template.of("help", commandElement.getDescription().getDescription()) TagResolver.resolver("help",
Tag.inserting(Component.text(commandElement.getDescription().getDescription())))
)); ));
} }
@ -132,8 +134,12 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?,
if (subcommandElement.shouldDisplayInHelp()) { if (subcommandElement.shouldDisplayInHelp()) {
String shortestSubcommandAlias = determineShortestAlias(subcommandElement); String shortestSubcommandAlias = determineShortestAlias(subcommandElement);
sender.sendMessage(helpFormatMessage.toComponent( sender.sendMessage(helpFormatMessage.toComponent(
Template.of("command", shortestCommandAlias + ' ' + shortestSubcommandAlias), TagResolver.resolver("command",
Template.of("help", subcommandElement.getDescription().getDescription()) Tag.inserting(Component.text(shortestCommandAlias + ' '
+ shortestSubcommandAlias))),
TagResolver.resolver("help",
Tag.inserting(Component.text(subcommandElement.getDescription()
.getDescription())))
)); ));
} }
} }
@ -150,8 +156,11 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?,
String shortestFlagAlias = determineShortestAlias(flagElement); String shortestFlagAlias = determineShortestAlias(flagElement);
String flagPrefix = "-" + (flagElement.getMain().equals(shortestFlagAlias) ? "_" : ""); String flagPrefix = "-" + (flagElement.getMain().equals(shortestFlagAlias) ? "_" : "");
sender.sendMessage(helpFormatMessage.toComponent( sender.sendMessage(helpFormatMessage.toComponent(
Template.of("command", shortestCommandAlias + ' ' + flagPrefix + shortestFlagAlias), TagResolver.resolver("command",
Template.of("help", flagElement.getDescription().getDescription()) Tag.inserting(Component.text(shortestCommandAlias + ' ' + flagPrefix
+ shortestFlagAlias))),
TagResolver.resolver("help",
Tag.inserting(Component.text(flagElement.getDescription().getDescription())))
)); ));
} }
} }
@ -319,11 +328,12 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?,
if (!recursive) { if (!recursive) {
TextComponent.Builder builder = Component.text(); TextComponent.Builder builder = Component.text();
builder.append(messages.get(MessageKey.DEPENDING_PLUGINS_PREFIX).toComponent( builder.append(messages.get(MessageKey.DEPENDING_PLUGINS_PREFIX).toComponent(
Template.of("plugin", pluginId) TagResolver.resolver("plugin", Tag.inserting(Component.text(pluginId)))
)); ));
builder.append(ListComponentBuilder.create(dependingPlugins) builder.append(ListComponentBuilder.create(dependingPlugins)
.format(p -> messages.get(MessageKey.DEPENDING_PLUGINS_FORMAT).toComponent( .format(p -> messages.get(MessageKey.DEPENDING_PLUGINS_FORMAT).toComponent(
Template.of("plugin", pluginManager.getPluginId(p)) TagResolver.resolver("plugin",
Tag.inserting(Component.text(pluginManager.getPluginId(p))))
)) ))
.separator(messages.get(MessageKey.DEPENDING_PLUGINS_SEPARATOR).toComponent()) .separator(messages.get(MessageKey.DEPENDING_PLUGINS_SEPARATOR).toComponent())
.lastSeparator(messages.get(MessageKey.DEPENDING_PLUGINS_LAST_SEPARATOR).toComponent()) .lastSeparator(messages.get(MessageKey.DEPENDING_PLUGINS_LAST_SEPARATOR).toComponent())
@ -349,10 +359,12 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?,
.orElse("-r"); .orElse("-r");
sender.sendMessage(messages.get(MessageKey.DEPENDING_PLUGINS_OVERRIDE).toComponent( sender.sendMessage(messages.get(MessageKey.DEPENDING_PLUGINS_OVERRIDE).toComponent(
Template.of("command", context.getRawInputJoined() + " " + forceFlag) TagResolver.resolver("command", Tag.inserting(Component.text(context.getRawInputJoined() + " "
+ forceFlag)))
)); ));
sender.sendMessage(messages.get(MessageKey.DEPENDING_PLUGINS_RECURSIVELY).toComponent( sender.sendMessage(messages.get(MessageKey.DEPENDING_PLUGINS_RECURSIVELY).toComponent(
Template.of("command", context.getRawInputJoined() + " " + rescuriveFlag) TagResolver.resolver("command", Tag.inserting(Component.text(context.getRawInputJoined() + " "
+ rescuriveFlag)))
)); ));
} }
@ -365,8 +377,10 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?,
String restartCommand = plugin.getCommandsResource().getAllAliases(getRawPath("restart")).stream() String restartCommand = plugin.getCommandsResource().getAllAliases(getRawPath("restart")).stream()
.min(Comparator.comparingInt(String::length)) .min(Comparator.comparingInt(String::length))
.orElse("restart"); .orElse("restart");
Component component = plugin.getMessagesResource().get(MessageKey.RELOADPLUGIN_SERVERUTILS).toComponent( Component component = plugin.getMessagesResource().get(MessageKey.RELOADPLUGIN_SERVERUTILS)
Template.of("command", context.getRawInput().peekFirst() + " " + restartCommand) .toComponent(TagResolver.resolver("command",
Tag.inserting(Component.text(context.getRawInput().peekFirst() + " "
+ restartCommand)))
); );
sender.sendMessage(component); sender.sendMessage(component);
return true; return true;
@ -384,7 +398,7 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?,
String pluginId = pluginManager.getPluginId(plugin); String pluginId = pluginManager.getPluginId(plugin);
if (protectedPlugins.contains(pluginId)) { if (protectedPlugins.contains(pluginId)) {
sender.sendMessage(messagesResource.get(MessageKey.GENERIC_PROTECTED_PLUGIN).toComponent( sender.sendMessage(messagesResource.get(MessageKey.GENERIC_PROTECTED_PLUGIN).toComponent(
Template.of("plugin", pluginId) TagResolver.resolver("plugin", Tag.inserting(Component.text(pluginId)))
)); ));
return true; return true;
} }
@ -461,7 +475,8 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?,
KeyValueComponentBuilder.create(formatMessage, "key", "value"), KeyValueComponentBuilder.create(formatMessage, "key", "value"),
listBuilderConsumer -> { listBuilderConsumer -> {
ListComponentBuilder<String> listBuilder = ListComponentBuilder.<String>create() ListComponentBuilder<String> listBuilder = ListComponentBuilder.<String>create()
.format(str -> listFormatMessage.toComponent(Template.of("value", str))) .format(str -> listFormatMessage.toComponent(TagResolver.resolver("value",
Tag.inserting(Component.text(str)))))
.separator(separator) .separator(separator)
.lastSeparator(lastSeparator) .lastSeparator(lastSeparator)
.emptyValue(null); .emptyValue(null);