Avoid component newlines, instead send message per line

This commit is contained in:
Frank van der Heijden 2021-08-03 16:41:09 +02:00
parent 083012abfe
commit 12b3aa5720
No known key found for this signature in database
GPG key ID: B808721C2DD5B5B8
11 changed files with 133 additions and 160 deletions

View file

@ -10,7 +10,6 @@ import net.frankheijden.serverutils.common.entities.results.PluginResult;
import net.frankheijden.serverutils.common.entities.ServerUtilsAudience;
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
import net.frankheijden.serverutils.common.tasks.UpdateCheckerTask;
import net.kyori.adventure.text.Component;
public class ServerUtilsApp<U extends ServerUtilsPlugin<P, T, C, S, D>, P, T, C extends ServerUtilsAudience<S>, S, D extends ServerUtilsPluginDescription> {
@ -66,15 +65,13 @@ public class ServerUtilsApp<U extends ServerUtilsPlugin<P, T, C, S, D>, P, T, C
File file = plugin.getPluginManager().getPluginFile(updaterPlugin);
PluginResult<P> disableResult = plugin.getPluginManager().disablePlugin(updaterPlugin);
if (!disableResult.isSuccess()) {
Component component = disableResult.toComponent(null);
plugin.getChatProvider().getConsoleServerAudience().sendMessage(component);
disableResult.sendTo(plugin.getChatProvider().getConsoleServerAudience(), null);
return;
}
CloseablePluginResult<P> unloadResult = plugin.getPluginManager().unloadPlugin(disableResult.getPlugin());
if (!unloadResult.isSuccess()) {
Component component = unloadResult.toComponent(null);
plugin.getChatProvider().getConsoleServerAudience().sendMessage(component);
unloadResult.sendTo(plugin.getChatProvider().getConsoleServerAudience(), null);
return;
}

View file

@ -97,9 +97,7 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?,
C sender = context.getSender();
MessagesResource messages = plugin.getMessagesResource();
TextComponent.Builder builder = Component.text()
.append(messages.get(MessageKey.HELP_HEADER).toComponent())
.append(Component.newline());
sender.sendMessage(messages.get(MessageKey.HELP_HEADER).toComponent());
MessagesResource.Message helpFormatMessage = messages.get(MessageKey.HELP_FORMAT);
@ -110,10 +108,10 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?,
String shortestCommandAlias = determineShortestAlias(commandElement);
if (commandElement.shouldDisplayInHelp()) {
builder.append(helpFormatMessage.toComponent(
sender.sendMessage(helpFormatMessage.toComponent(
Template.of("command", shortestCommandAlias),
Template.of("help", commandElement.getDescription().getDescription())
)).append(Component.newline());
));
}
Object subcommandsObject = commandConfig.get("subcommands");
@ -125,10 +123,10 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?,
CommandElement subcommandElement = parseElement(subcommandConfig);
if (subcommandElement.shouldDisplayInHelp()) {
String shortestSubcommandAlias = determineShortestAlias(subcommandElement);
builder.append(helpFormatMessage.toComponent(
sender.sendMessage(helpFormatMessage.toComponent(
Template.of("command", shortestCommandAlias + ' ' + shortestSubcommandAlias),
Template.of("help", subcommandElement.getDescription().getDescription())
)).append(Component.newline());
));
}
}
}
@ -143,17 +141,16 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?,
if (flagElement.shouldDisplayInHelp()) {
String shortestFlagAlias = determineShortestAlias(flagElement);
String flagPrefix = "-" + (flagElement.getMain().equals(shortestFlagAlias) ? "_" : "");
builder.append(helpFormatMessage.toComponent(
sender.sendMessage(helpFormatMessage.toComponent(
Template.of("command", shortestCommandAlias + ' ' + flagPrefix + shortestFlagAlias),
Template.of("help", flagElement.getDescription().getDescription())
)).append(Component.newline());
Template.of("help", flagElement.getDescription().getDescription())
));
}
}
}
}
builder.append(messages.get(MessageKey.HELP_FOOTER).toComponent());
sender.sendMessage(builder.build());
sender.sendMessage(messages.get(MessageKey.HELP_FOOTER).toComponent());
}
private String determineShortestAlias(CommandElement element) {
@ -180,12 +177,12 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?,
PluginResults<P> loadResults = pluginManager.loadPlugins(jarFiles);
if (!loadResults.isSuccess()) {
PluginResult<P> failedResult = loadResults.last();
sender.sendMessage(failedResult.toComponent(null));
failedResult.sendTo(sender, null);
return;
}
PluginResults<P> enableResults = pluginManager.enablePlugins(loadResults.getPlugins());
sender.sendMessage(enableResults.toComponent(MessageKey.LOADPLUGIN));
enableResults.sendTo(sender, MessageKey.LOADPLUGIN);
}
private void handleUnloadPlugin(CommandContext<C> context) {
@ -199,14 +196,14 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?,
PluginResults<P> disableResults = plugin.getPluginManager().disablePlugins(plugins);
for (PluginResult<P> disableResult : disableResults.getResults()) {
if (!disableResult.isSuccess() && disableResult.getResult() != Result.ALREADY_DISABLED) {
sender.sendMessage(disableResult.toComponent(null));
disableResult.sendTo(sender, null);
return;
}
}
CloseablePluginResults<P> unloadResults = plugin.getPluginManager().unloadPlugins(plugins);
unloadResults.tryClose();
sender.sendMessage(unloadResults.toComponent(MessageKey.UNLOADPLUGIN));
unloadResults.sendTo(sender, MessageKey.UNLOADPLUGIN);
}
private void handleReloadPlugin(CommandContext<C> context) {
@ -218,7 +215,7 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?,
}
PluginResults<P> reloadResults = plugin.getPluginManager().reloadPlugins(plugins);
sender.sendMessage(reloadResults.toComponent(MessageKey.RELOADPLUGIN));
reloadResults.sendTo(sender, MessageKey.RELOADPLUGIN);
}
protected boolean checkDependingPlugins(CommandContext<C> context, C sender, List<P> plugins, String subcommand) {
@ -227,27 +224,24 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?,
AbstractPluginManager<P, ?> pluginManager = plugin.getPluginManager();
MessagesResource messages = plugin.getMessagesResource();
TextComponent.Builder builder = Component.text();
boolean hasDependingPlugins = false;
for (P plugin : plugins) {
String pluginId = pluginManager.getPluginId(plugin);
List<P> dependingPlugins = pluginManager.getPluginsDependingOn(pluginId);
if (!dependingPlugins.isEmpty()) {
TextComponent.Builder builder = Component.text();
builder.append(messages.get(MessageKey.DEPENDING_PLUGINS_PREFIX).toComponent(
Template.of("plugin", pluginId)
));
MessagesResource.Message dependingPluginsFormat = messages.get(MessageKey.DEPENDING_PLUGINS_FORMAT);
builder.append(ListComponentBuilder.create(dependingPlugins)
.format(p -> dependingPluginsFormat.toComponent(
.format(p -> messages.get(MessageKey.DEPENDING_PLUGINS_FORMAT).toComponent(
Template.of("plugin", pluginManager.getPluginId(p))
))
.separator(messages.get(MessageKey.DEPENDING_PLUGINS_SEPARATOR).toComponent())
.lastSeparator(messages.get(MessageKey.DEPENDING_PLUGINS_LAST_SEPARATOR).toComponent())
.build());
builder.append(Component.newline());
sender.sendMessage(builder.build());
hasDependingPlugins = true;
}
}
@ -258,13 +252,11 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?,
.min(Comparator.comparingInt(String::length))
.orElse("-f");
builder.append(messages.get(MessageKey.DEPENDING_PLUGINS_OVERRIDE).toComponent(
sender.sendMessage(messages.get(MessageKey.DEPENDING_PLUGINS_OVERRIDE).toComponent(
Template.of("command", context.getRawInputJoined() + " " + forceFlag)
));
}
sender.sendMessage(builder.build());
return hasDependingPlugins;
}
@ -323,31 +315,26 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?,
private <T> void createInfo(C sender, String command, T item, InfoCreator<T> creator) {
MessagesResource messages = plugin.getMessagesResource();
String messagePrefix = "messages." + command;
MessagesResource.Message formatMessage = messages.get(messagePrefix + ".format");
MessagesResource.Message listFormatMessage = messages.get(messagePrefix + ".list-format");
Component separator = messages.get(messagePrefix + ".list-separator").toComponent();
Component lastSeparator = messages.get(messagePrefix + ".list-last-separator").toComponent();
TextComponent.Builder builder = Component.text()
.append(messages.get(messagePrefix + ".header").toComponent())
.append(Component.newline());
MessagesResource.Message formatMessage = messages.get(command + ".format");
MessagesResource.Message listFormatMessage = messages.get(command + ".list-format");
Component separator = messages.get(command + ".list-separator").toComponent();
Component lastSeparator = messages.get(command + ".list-last-separator").toComponent();
sender.sendMessage(messages.get(command + ".header").toComponent());
creator.createInfo(
KeyValueComponentBuilder.create(formatMessage, "key", "value"),
listBuilderConsumer -> {
ListComponentBuilder<String> listBuilder = ListComponentBuilder.<String>create()
.format(str -> listFormatMessage.toComponent(Template.of("value", str)))
.separator(separator)
.lastSeparator(lastSeparator);
.lastSeparator(lastSeparator)
.emptyValue(null);
listBuilderConsumer.accept(listBuilder);
return listBuilder.build();
},
item
);
builder.append(messages.get(messagePrefix + ".footer").toComponent());
sender.sendMessage(builder.build());
).build().forEach(sender::sendMessage);
sender.sendMessage(messages.get(command + ".footer").toComponent());
}
private interface InfoCreator<T> {

View file

@ -2,7 +2,7 @@ package net.frankheijden.serverutils.common.entities.results;
import net.frankheijden.serverutils.common.ServerUtilsApp;
import net.frankheijden.serverutils.common.config.ConfigKey;
import net.kyori.adventure.text.Component;
import net.frankheijden.serverutils.common.entities.ServerUtilsAudience;
import net.kyori.adventure.text.minimessage.Template;
public class PluginResult<T> implements AbstractResult {
@ -48,9 +48,9 @@ public class PluginResult<T> implements AbstractResult {
return plugin != null && result == Result.SUCCESS;
}
public Component toComponent(ConfigKey successKey) {
public void sendTo(ServerUtilsAudience<?> sender, ConfigKey successKey) {
ConfigKey key = isSuccess() ? successKey : result.getKey();
return ServerUtilsApp.getPlugin().getMessagesResource().get(key).toComponent(templates);
sender.sendMessage(ServerUtilsApp.getPlugin().getMessagesResource().get(key).toComponent(templates));
}
@Override

View file

@ -4,8 +4,7 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import net.frankheijden.serverutils.common.config.ConfigKey;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.frankheijden.serverutils.common.entities.ServerUtilsAudience;
import net.kyori.adventure.text.minimessage.Template;
public class PluginResults<T> implements Iterable<PluginResult<T>> {
@ -68,14 +67,12 @@ public class PluginResults<T> implements Iterable<PluginResult<T>> {
}
/**
* Creates a {@link Component}.
* Sends the result(s) to the given sender.
*/
public Component toComponent(ConfigKey successKey) {
TextComponent.Builder builder = Component.text();
public void sendTo(ServerUtilsAudience<?> sender, ConfigKey successKey) {
for (PluginResult<T> result : results) {
builder.append(result.toComponent(successKey));
result.sendTo(sender, successKey);
}
return builder.build();
}
@Override

View file

@ -6,8 +6,6 @@ import java.util.List;
import net.frankheijden.serverutils.common.ServerUtilsApp;
import net.frankheijden.serverutils.common.config.MessagesResource;
import net.frankheijden.serverutils.common.entities.ServerUtilsAudience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.minimessage.Template;
public class PluginWatchResults implements Iterable<PluginWatchResult> {
@ -33,11 +31,9 @@ public class PluginWatchResults implements Iterable<PluginWatchResult> {
public void sendTo(ServerUtilsAudience<?> sender) {
MessagesResource messages = ServerUtilsApp.getPlugin().getMessagesResource();
TextComponent.Builder builder = Component.text();
for (PluginWatchResult watchResult : watchResults) {
builder.append(messages.get(watchResult.getKey()).toComponent(watchResult.getTemplates()));
sender.sendMessage(messages.get(watchResult.getKey()).toComponent(watchResult.getTemplates()));
}
sender.sendMessage(builder.build());
}
@Override

View file

@ -167,7 +167,7 @@ public class PluginWatcherTask<P, T> extends AbstractTask {
fileNameToWatchEntryMap.clear();
PluginResults<P> reloadResults = pluginManager.reloadPlugins(plugins);
sender.sendMessage(reloadResults.toComponent(MessageKey.RELOADPLUGIN));
reloadResults.sendTo(sender, MessageKey.RELOADPLUGIN);
for (PluginResult<P> reloadResult : reloadResults) {
if (!reloadResult.isSuccess()) continue;

View file

@ -4,7 +4,6 @@ import java.util.ArrayList;
import java.util.List;
import net.frankheijden.serverutils.common.config.MessagesResource;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.minimessage.Template;
public class KeyValueComponentBuilder {
@ -36,60 +35,52 @@ public class KeyValueComponentBuilder {
return new KeyValueComponentBuilder(format, keyPlaceholder, valuePlaceholder);
}
/**
* Adds an entry.
*/
public KeyValueComponentBuilder add(String key, String value) {
if (value != null) this.templatesList.add(new Template[]{
Template.of(keyPlaceholder, key),
Template.of(valuePlaceholder, value)
});
return this;
public KeyValueComponentBuilder.KeyValuePair key(String key) {
return new KeyValuePair(key);
}
/**
* Adds an entry.
*/
public KeyValueComponentBuilder add(String key, Component value) {
if (value != null) this.templatesList.add(new Template[]{
Template.of(keyPlaceholder, key),
Template.of(valuePlaceholder, value)
});
return this;
public KeyValueComponentBuilder.KeyValuePair key(Component key) {
return new KeyValuePair(key);
}
/**
* Adds an entry.
*/
public KeyValueComponentBuilder add(Component key, String value) {
if (value != null) this.templatesList.add(new Template[]{
Template.of(keyPlaceholder, key),
Template.of(valuePlaceholder, value)
});
return this;
}
/**
* Adds an entry.
*/
public KeyValueComponentBuilder add(Component key, Component value) {
if (value != null) this.templatesList.add(new Template[]{
Template.of(keyPlaceholder, key),
Template.of(valuePlaceholder, value)
});
private KeyValueComponentBuilder add(Template key, Template value) {
this.templatesList.add(new Template[]{ key, value });
return this;
}
/**
* Builds the current ListMessageBuilder instance into a Component.
*/
public Component build() {
TextComponent.Builder builder = Component.text();
public List<Component> build() {
List<Component> components = new ArrayList<>(templatesList.size());
for (Template[] templates : templatesList) {
builder.append(format.toComponent(templates));
components.add(format.toComponent(templates));
}
return builder.build();
return components;
}
public class KeyValuePair {
private final Template key;
private KeyValuePair(String key) {
this.key = Template.of(keyPlaceholder, key);
}
private KeyValuePair(Component key) {
this.key = Template.of(keyPlaceholder, key);
}
public KeyValueComponentBuilder value(String value) {
if (value == null) return KeyValueComponentBuilder.this;
return add(key, Template.of(valuePlaceholder, value));
}
public KeyValueComponentBuilder value(Component value) {
if (value == null) return KeyValueComponentBuilder.this;
return add(key, Template.of(valuePlaceholder, value));
}
}
}

View file

@ -13,9 +13,11 @@ public class ListComponentBuilder<T> {
private Format<T> format;
private Component separator;
private Component lastSeparator;
private Component emptyValue;
private ListComponentBuilder() {
this.elements = new ArrayList<>();
this.emptyValue = Component.empty();
}
public static <T> ListComponentBuilder<T> create(Collection<? extends T> list) {
@ -42,6 +44,11 @@ public class ListComponentBuilder<T> {
return this;
}
public ListComponentBuilder<T> emptyValue(Component emptyValue) {
this.emptyValue = emptyValue;
return this;
}
public ListComponentBuilder<T> addAll(Collection<? extends T> elements) {
this.elements.addAll(elements);
return this;
@ -51,10 +58,12 @@ public class ListComponentBuilder<T> {
* Builds the ListComponent.
*/
public Component build() {
if (elements.size() == 1) {
if (elements.isEmpty()) {
return emptyValue;
} else if (elements.size() == 1) {
return format.format(elements.iterator().next());
} else {
TextComponent.Builder builder = Component.empty().toBuilder();
TextComponent.Builder builder = Component.text();
int sizeMinusTwo = elements.size() - 2;
for (int i = 0; i < elements.size(); i++) {