From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Parker Hawke Date: Sat, 27 Jun 2020 18:43:37 -0400 Subject: [PATCH] Spigot - Improve output of plugins command diff --git a/src/main/java/org/bukkit/command/defaults/PluginsCommand.java b/src/main/java/org/bukkit/command/defaults/PluginsCommand.java index 1aa58c59e1e8738bbdc77752885ff3b18b29de42..4974fc518c3645e6e060ff52e71a47a86d52ec5c 100644 --- a/src/main/java/org/bukkit/command/defaults/PluginsCommand.java +++ b/src/main/java/org/bukkit/command/defaults/PluginsCommand.java @@ -11,6 +11,15 @@ import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; +// Spigot start +import net.md_5.bungee.api.chat.BaseComponent; +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.ComponentBuilder; +import net.md_5.bungee.api.chat.HoverEvent; +import net.md_5.bungee.api.chat.ComponentBuilder.FormatRetention; +import org.bukkit.entity.Player; +import org.bukkit.plugin.PluginDescriptionFile; +// Spigot end public class PluginsCommand extends BukkitCommand { public PluginsCommand(@NotNull String name) { @@ -25,7 +34,13 @@ public class PluginsCommand extends BukkitCommand { public boolean execute(@NotNull CommandSender sender, @NotNull String currentAlias, @NotNull String[] args) { if (!testPermission(sender)) return true; - sender.sendMessage("Plugins " + getPluginList()); + // Spigot start + if (sender instanceof Player && sender.hasPermission("bukkit.command.version")) { + sender.spigot().sendMessage(getPluginListSpigot()); + } else { + sender.sendMessage("Plugins " + getPluginList()); + } + // Spigot end return true; } @@ -71,4 +86,72 @@ public class PluginsCommand extends BukkitCommand { // Paper end } + // Spigot start + @NotNull + private BaseComponent[] getPluginListSpigot() { + Plugin[] plugins = Bukkit.getPluginManager().getPlugins(); + ComponentBuilder pluginList = new ComponentBuilder("Plugins (" + plugins.length + "): "); + + int index = 0; + for (Plugin plugin : plugins) { + if (index++ > 0) { + pluginList.append(", ", FormatRetention.NONE).color(net.md_5.bungee.api.ChatColor.WHITE); + } + + // Event components + PluginDescriptionFile description = plugin.getDescription(); + ComponentBuilder hoverEventComponents = new ComponentBuilder(); + hoverEventComponents.append("Version: ").color(net.md_5.bungee.api.ChatColor.WHITE).append(description.getVersion()).color(net.md_5.bungee.api.ChatColor.GREEN); + + if (description.getDescription() != null) { + hoverEventComponents.append("\nDescription: ").color(net.md_5.bungee.api.ChatColor.WHITE).append(description.getDescription()).color(net.md_5.bungee.api.ChatColor.GREEN); + } + + if (description.getWebsite() != null) { + hoverEventComponents.append("\nWebsite: ").color(net.md_5.bungee.api.ChatColor.WHITE).append(description.getWebsite()).color(net.md_5.bungee.api.ChatColor.GREEN); + } + + if (!description.getAuthors().isEmpty()) { + if (description.getAuthors().size() == 1) { + hoverEventComponents.append("\nAuthor: "); + } else { + hoverEventComponents.append("\nAuthors: "); + } + + hoverEventComponents.color(net.md_5.bungee.api.ChatColor.WHITE).append(getAuthors(description)); + } + + HoverEvent hoverEvent = new HoverEvent(HoverEvent.Action.SHOW_TEXT, hoverEventComponents.create()); + ClickEvent clickEvent = new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/version " + description.getName()); + + // Plugin list entry + pluginList.append(plugin.getDescription().getName()); + pluginList.color(plugin.isEnabled() ? net.md_5.bungee.api.ChatColor.GREEN : net.md_5.bungee.api.ChatColor.RED); + pluginList.event(hoverEvent).event(clickEvent); + + if (plugin.getDescription().getProvides().size() > 0) { + pluginList.append("( ", FormatRetention.NONE).color(net.md_5.bungee.api.ChatColor.WHITE).append(String.join(", ", plugin.getDescription().getProvides())).append(")"); + } + } + + return pluginList.create(); + } + + @NotNull + private BaseComponent[] getAuthors(@NotNull final PluginDescriptionFile description) { + ComponentBuilder result = new ComponentBuilder(); + List authors = description.getAuthors(); + + for (int i = 0; i < authors.size(); i++) { + if (i > 0) { + result.append(i < authors.size() - 1 ? ", " : " and ", FormatRetention.NONE); + result.color(net.md_5.bungee.api.ChatColor.WHITE); + } + + result.append(authors.get(i)).color(net.md_5.bungee.api.ChatColor.GREEN); + } + + return result.create(); + } + // Spigot end }