Merge pull request #43 from FrankHeijden/fix/disabled-plugins-format

Fix disabled plugins format + cleanup
This commit is contained in:
Frank van der Heijden 2021-12-20 19:12:38 +01:00 committed by GitHub
commit 2a80055b10
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 73 deletions

View file

@ -5,16 +5,12 @@ import cloud.commandframework.CommandManager;
import cloud.commandframework.context.CommandContext; import cloud.commandframework.context.CommandContext;
import net.frankheijden.serverutils.bukkit.entities.BukkitAudience; import net.frankheijden.serverutils.bukkit.entities.BukkitAudience;
import net.frankheijden.serverutils.bukkit.entities.BukkitPlugin; import net.frankheijden.serverutils.bukkit.entities.BukkitPlugin;
import net.frankheijden.serverutils.bukkit.entities.BukkitPluginDescription;
import net.frankheijden.serverutils.common.commands.CommandPlugins; import net.frankheijden.serverutils.common.commands.CommandPlugins;
import net.frankheijden.serverutils.common.config.MessageKey;
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;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
public class BukkitCommandPlugins extends CommandPlugins<BukkitPlugin, Plugin, BukkitAudience> { @SuppressWarnings("LineLength")
public class BukkitCommandPlugins extends CommandPlugins<BukkitPlugin, Plugin, BukkitAudience, BukkitPluginDescription> {
public BukkitCommandPlugins(BukkitPlugin plugin) { public BukkitCommandPlugins(BukkitPlugin plugin) {
super(plugin); super(plugin);
@ -35,21 +31,6 @@ public class BukkitCommandPlugins extends CommandPlugins<BukkitPlugin, Plugin, B
BukkitAudience sender = context.getSender(); BukkitAudience sender = context.getSender();
boolean hasVersionFlag = context.flags().contains("version"); boolean hasVersionFlag = context.flags().contains("version");
MessagesResource messages = plugin.getMessagesResource(); handlePlugins(sender, plugin.getPluginManager().getPluginsSorted(), hasVersionFlag);
handlePlugins(sender, plugin.getPluginManager().getPluginsSorted(), bukkitPlugin -> {
PluginDescriptionFile description = bukkitPlugin.getDescription();
TextComponent.Builder builder = Component.text();
builder.append(messages.get(MessageKey.PLUGINS_FORMAT).toComponent(
Template.of("plugin", description.getName())
));
if (hasVersionFlag) {
builder.append(messages.get(MessageKey.PLUGINS_VERSION).toComponent(
Template.of("version", description.getVersion())
));
}
return builder.build();
});
} }
} }

View file

@ -5,16 +5,12 @@ import cloud.commandframework.CommandManager;
import cloud.commandframework.context.CommandContext; import cloud.commandframework.context.CommandContext;
import net.frankheijden.serverutils.bungee.entities.BungeeAudience; import net.frankheijden.serverutils.bungee.entities.BungeeAudience;
import net.frankheijden.serverutils.bungee.entities.BungeePlugin; import net.frankheijden.serverutils.bungee.entities.BungeePlugin;
import net.frankheijden.serverutils.bungee.entities.BungeePluginDescription;
import net.frankheijden.serverutils.common.commands.CommandPlugins; import net.frankheijden.serverutils.common.commands.CommandPlugins;
import net.frankheijden.serverutils.common.config.MessageKey;
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;
import net.md_5.bungee.api.plugin.Plugin; import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.api.plugin.PluginDescription;
public class BungeeCommandPlugins extends CommandPlugins<BungeePlugin, Plugin, BungeeAudience> { @SuppressWarnings("LineLength")
public class BungeeCommandPlugins extends CommandPlugins<BungeePlugin, Plugin, BungeeAudience, BungeePluginDescription> {
public BungeeCommandPlugins(BungeePlugin plugin) { public BungeeCommandPlugins(BungeePlugin plugin) {
super(plugin); super(plugin);
@ -37,21 +33,6 @@ public class BungeeCommandPlugins extends CommandPlugins<BungeePlugin, Plugin, B
boolean hasVersionFlag = context.flags().contains("version"); boolean hasVersionFlag = context.flags().contains("version");
boolean hasModulesFlag = context.flags().contains("modules"); boolean hasModulesFlag = context.flags().contains("modules");
MessagesResource messages = plugin.getMessagesResource(); handlePlugins(sender, plugin.getPluginManager().getPluginsSorted(hasModulesFlag), hasVersionFlag);
handlePlugins(sender, plugin.getPluginManager().getPluginsSorted(hasModulesFlag), bungeePlugin -> {
PluginDescription description = bungeePlugin.getDescription();
TextComponent.Builder builder = Component.text();
builder.append(messages.get(MessageKey.PLUGINS_FORMAT).toComponent(
Template.of("plugin", description.getName())
));
if (hasVersionFlag) {
builder.append(messages.get(MessageKey.PLUGINS_VERSION).toComponent(
Template.of("version", description.getVersion())
));
}
return builder.build();
});
} }
} }

View file

@ -9,12 +9,15 @@ import net.frankheijden.serverutils.common.config.MessageKey;
import net.frankheijden.serverutils.common.config.MessagesResource; import net.frankheijden.serverutils.common.config.MessagesResource;
import net.frankheijden.serverutils.common.entities.ServerUtilsAudience; import net.frankheijden.serverutils.common.entities.ServerUtilsAudience;
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin; import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
import net.frankheijden.serverutils.common.entities.ServerUtilsPluginDescription;
import net.frankheijden.serverutils.common.managers.AbstractPluginManager;
import net.frankheijden.serverutils.common.utils.ListComponentBuilder; import net.frankheijden.serverutils.common.utils.ListComponentBuilder;
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.Template;
public abstract class CommandPlugins<U extends ServerUtilsPlugin<P, ?, C, ?, ?>, P, C extends ServerUtilsAudience<?>> @SuppressWarnings("LineLength")
public abstract class CommandPlugins<U extends ServerUtilsPlugin<P, ?, C, ?, D>, P, C extends ServerUtilsAudience<?>, D extends ServerUtilsPluginDescription>
extends ServerUtilsCommand<U, C> { extends ServerUtilsCommand<U, C> {
protected CommandPlugins(U plugin) { protected CommandPlugins(U plugin) {
@ -29,13 +32,14 @@ public abstract class CommandPlugins<U extends ServerUtilsPlugin<P, ?, C, ?, ?>,
* @param plugins The plugins to be sent. * @param plugins The plugins to be sent.
* @param pluginFormat The format of the plugins to be sent. * @param pluginFormat The format of the plugins to be sent.
*/ */
protected void handlePlugins(C sender, List<P> plugins, ListComponentBuilder.Format<P> pluginFormat) { protected void handlePlugins(C sender, List<P> plugins, boolean hasVersionFlag) {
List<P> filteredPlugins = new ArrayList<>(plugins.size()); List<P> filteredPlugins = new ArrayList<>(plugins.size());
Set<String> hiddenPlugins = new HashSet<>(plugin.getConfigResource().getConfig().getStringList( Set<String> hiddenPlugins = new HashSet<>(plugin.getConfigResource().getConfig().getStringList(
"hide-plugins-from-plugins-command" "hide-plugins-from-plugins-command"
)); ));
AbstractPluginManager<P, D> pluginManager = plugin.getPluginManager();
for (P plugin : plugins) { for (P plugin : plugins) {
if (!hiddenPlugins.contains(this.plugin.getPluginManager().getPluginId(plugin))) { if (!hiddenPlugins.contains(pluginManager.getPluginId(plugin))) {
filteredPlugins.add(plugin); filteredPlugins.add(plugin);
} }
} }
@ -50,7 +54,24 @@ public abstract class CommandPlugins<U extends ServerUtilsPlugin<P, ?, C, ?, ?>,
builder.append(ListComponentBuilder.create(filteredPlugins) builder.append(ListComponentBuilder.create(filteredPlugins)
.separator(messages.get(MessageKey.PLUGINS_SEPARATOR).toComponent()) .separator(messages.get(MessageKey.PLUGINS_SEPARATOR).toComponent())
.lastSeparator(messages.get(MessageKey.PLUGINS_LAST_SEPARATOR).toComponent()) .lastSeparator(messages.get(MessageKey.PLUGINS_LAST_SEPARATOR).toComponent())
.format(pluginFormat) .format(plugin -> {
D description = pluginManager.getLoadedPluginDescription(plugin);
TextComponent.Builder formatBuilder = Component.text();
MessageKey formatKey = pluginManager.isPluginEnabled(plugin)
? MessageKey.PLUGINS_FORMAT
: MessageKey.PLUGINS_FORMAT_DISABLED;
formatBuilder.append(messages.get(formatKey).toComponent(
Template.of("plugin", description.getName())
));
if (hasVersionFlag) {
formatBuilder.append(messages.get(MessageKey.PLUGINS_VERSION).toComponent(
Template.of("version", description.getVersion())
));
}
return formatBuilder.build();
})
.build()); .build());
sender.sendMessage(builder.build()); sender.sendMessage(builder.build());
sender.sendMessage(messages.get(MessageKey.PLUGININFO_FOOTER).toComponent()); sender.sendMessage(messages.get(MessageKey.PLUGININFO_FOOTER).toComponent());

View file

@ -4,17 +4,13 @@ import cloud.commandframework.Command;
import cloud.commandframework.CommandManager; import cloud.commandframework.CommandManager;
import cloud.commandframework.context.CommandContext; import cloud.commandframework.context.CommandContext;
import com.velocitypowered.api.plugin.PluginContainer; import com.velocitypowered.api.plugin.PluginContainer;
import com.velocitypowered.api.plugin.PluginDescription;
import net.frankheijden.serverutils.common.commands.CommandPlugins; import net.frankheijden.serverutils.common.commands.CommandPlugins;
import net.frankheijden.serverutils.common.config.MessageKey;
import net.frankheijden.serverutils.common.config.MessagesResource;
import net.frankheijden.serverutils.velocity.entities.VelocityAudience; import net.frankheijden.serverutils.velocity.entities.VelocityAudience;
import net.frankheijden.serverutils.velocity.entities.VelocityPlugin; import net.frankheijden.serverutils.velocity.entities.VelocityPlugin;
import net.kyori.adventure.text.Component; import net.frankheijden.serverutils.velocity.entities.VelocityPluginDescription;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.minimessage.Template;
public class VelocityCommandPlugins extends CommandPlugins<VelocityPlugin, PluginContainer, VelocityAudience> { @SuppressWarnings("LineLength")
public class VelocityCommandPlugins extends CommandPlugins<VelocityPlugin, PluginContainer, VelocityAudience, VelocityPluginDescription> {
public VelocityCommandPlugins(VelocityPlugin plugin) { public VelocityCommandPlugins(VelocityPlugin plugin) {
super(plugin); super(plugin);
@ -35,21 +31,6 @@ public class VelocityCommandPlugins extends CommandPlugins<VelocityPlugin, Plugi
VelocityAudience sender = context.getSender(); VelocityAudience sender = context.getSender();
boolean hasVersionFlag = context.flags().contains("version"); boolean hasVersionFlag = context.flags().contains("version");
MessagesResource messages = plugin.getMessagesResource(); handlePlugins(sender, plugin.getPluginManager().getPluginsSorted(), hasVersionFlag);
handlePlugins(sender, plugin.getPluginManager().getPluginsSorted(), container -> {
PluginDescription description = container.getDescription();
TextComponent.Builder builder = Component.text();
builder.append(messages.get(MessageKey.PLUGINS_FORMAT).toComponent(
Template.of("plugin", description.getId())
));
if (hasVersionFlag) {
builder.append(messages.get(MessageKey.PLUGINS_VERSION).toComponent(
Template.of("version", description.getVersion().orElse("<UNKNOWN>"))
));
}
return builder.build();
});
} }
} }