Add PluginInfo command
- /su plugininfo will show info about the plugin - Added dynamic messages.yml for better updating
This commit is contained in:
parent
d60c34ddf1
commit
58a8e9304c
7 changed files with 219 additions and 28 deletions
|
|
@ -6,14 +6,16 @@ import net.frankheijden.serverutils.ServerUtils;
|
|||
import net.frankheijden.serverutils.config.Messenger;
|
||||
import net.frankheijden.serverutils.managers.PluginManager;
|
||||
import net.frankheijden.serverutils.reflection.*;
|
||||
import net.frankheijden.serverutils.utils.ForwardFilter;
|
||||
import net.frankheijden.serverutils.utils.ReloadHandler;
|
||||
import net.frankheijden.serverutils.utils.*;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.*;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.PluginDescriptionFile;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static net.frankheijden.serverutils.config.Messenger.sendMessage;
|
||||
import static net.frankheijden.serverutils.reflection.ReflectionUtils.MINOR;
|
||||
|
||||
@CommandAlias("serverutils|su")
|
||||
public class CommandServerUtils extends BaseCommand {
|
||||
|
|
@ -46,22 +48,19 @@ public class CommandServerUtils extends BaseCommand {
|
|||
@Description("Shows a help page with a few commands.")
|
||||
public void onHelp(CommandSender sender) {
|
||||
Messenger.sendMessage(sender, "serverutils.help.header");
|
||||
|
||||
FormatBuilder builder = FormatBuilder.create(Messenger.getMessage("serverutils.help.format"))
|
||||
.orderedKeys("%command%", "%subcommand%", "%help%");
|
||||
plugin.getCommandManager().getRegisteredRootCommands().stream()
|
||||
.filter(c -> !ALIASES.contains(c.getCommandName().toLowerCase()))
|
||||
.forEach(rootCommand -> {
|
||||
Messenger.sendMessage(sender, "serverutils.help.format",
|
||||
"%command%", rootCommand.getCommandName(),
|
||||
"%subcommand%", "",
|
||||
"%help%", rootCommand.getDescription());
|
||||
|
||||
builder.add(rootCommand.getCommandName(), "", rootCommand.getDescription());
|
||||
rootCommand.getSubCommands().forEach((str, cmd) -> {
|
||||
if (cmd.getPrefSubCommand().isEmpty()) return;
|
||||
Messenger.sendMessage(sender, "serverutils.help.format",
|
||||
"%command%", rootCommand.getCommandName(),
|
||||
"%subcommand%", " " + cmd.getPrefSubCommand(),
|
||||
"%help%", cmd.getHelpText());
|
||||
builder.add(rootCommand.getCommandName(), " " + cmd.getPrefSubCommand(), cmd.getHelpText());
|
||||
});
|
||||
});
|
||||
builder.sendTo(sender);
|
||||
Messenger.sendMessage(sender, "serverutils.help.footer");
|
||||
}
|
||||
|
||||
|
|
@ -136,4 +135,58 @@ public class CommandServerUtils extends BaseCommand {
|
|||
PluginManager.Result result = PluginManager.reloadPlugin(pluginName);
|
||||
result.sendTo(sender, "reload", pluginName);
|
||||
}
|
||||
|
||||
@Subcommand("plugininfo")
|
||||
@CommandCompletion("@plugins")
|
||||
@CommandPermission("serverutils.plugininfo")
|
||||
@Description("Shows information about the specified plugin.")
|
||||
public void onPluginInfo(CommandSender sender, String pluginName) {
|
||||
Plugin plugin = Bukkit.getPluginManager().getPlugin(pluginName);
|
||||
if (plugin == null) {
|
||||
PluginManager.Result.NOT_EXISTS.sendTo(sender, "fetch", pluginName);
|
||||
return;
|
||||
}
|
||||
|
||||
PluginDescriptionFile description = plugin.getDescription();
|
||||
String format = Messenger.getMessage("serverutils.plugininfo.format");
|
||||
String seperator = Messenger.getMessage("serverutils.plugininfo.seperator");
|
||||
String lastSeperator = Messenger.getMessage("serverutils.plugininfo.last_seperator");
|
||||
|
||||
Messenger.sendMessage(sender, "serverutils.plugininfo.header");
|
||||
|
||||
FormatBuilder builder = FormatBuilder.create(format)
|
||||
.orderedKeys("%key%", "%value%")
|
||||
.add("Name", plugin.getName())
|
||||
.add("Full Name", description.getFullName())
|
||||
.add("Version", description.getVersion());
|
||||
if (MINOR >= 13) builder.add( "API Version", description.getAPIVersion());
|
||||
builder.add("Website", description.getWebsite())
|
||||
.add("Authors", ListBuilder.createStrings(description.getAuthors())
|
||||
.seperator(seperator)
|
||||
.lastSeperator(lastSeperator)
|
||||
.toString())
|
||||
.add("Description", description.getDescription())
|
||||
.add("Main", description.getMain())
|
||||
.add("Prefix", description.getPrefix())
|
||||
.add("Load Order", description.getLoad().name())
|
||||
.add("Load Before", ListBuilder.createStrings(description.getLoadBefore())
|
||||
.seperator(seperator)
|
||||
.lastSeperator(lastSeperator)
|
||||
.toString())
|
||||
.add("Depend", ListBuilder.createStrings(description.getDepend())
|
||||
.seperator(seperator)
|
||||
.lastSeperator(lastSeperator)
|
||||
.toString())
|
||||
.add("Soft Depend", ListBuilder.createStrings(description.getSoftDepend())
|
||||
.seperator(seperator)
|
||||
.lastSeperator(lastSeperator)
|
||||
.toString());
|
||||
if (MINOR >= 15) builder.add("Provides", ListBuilder.createStrings(description.getProvides())
|
||||
.seperator(seperator)
|
||||
.lastSeperator(lastSeperator)
|
||||
.toString());
|
||||
|
||||
builder.sendTo(sender);
|
||||
Messenger.sendMessage(sender, "serverutils.plugininfo.footer");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,38 @@
|
|||
package net.frankheijden.serverutils.config;
|
||||
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Defaults {
|
||||
|
||||
private final Map<String, Object> rootMap;
|
||||
|
||||
private Defaults(Object... objects) {
|
||||
this.rootMap = new LinkedHashMap<>();
|
||||
for (int i = 0; i < objects.length; i += 2) {
|
||||
this.rootMap.put(String.valueOf(objects[i]), objects[i + 1]);
|
||||
}
|
||||
}
|
||||
|
||||
public static Defaults of(Object... objects) {
|
||||
return new Defaults(objects);
|
||||
}
|
||||
|
||||
public static void addDefaults(Defaults defaults, YamlConfiguration yml) {
|
||||
addDefaults(defaults, yml, "");
|
||||
}
|
||||
|
||||
private static void addDefaults(Defaults defaults, YamlConfiguration yml, String root) {
|
||||
for (Map.Entry<String, Object> entry : defaults.rootMap.entrySet()) {
|
||||
String key = (root.isEmpty() ? "" : root + ".") + entry.getKey();
|
||||
Object value = entry.getValue();
|
||||
if (value instanceof Defaults) {
|
||||
addDefaults((Defaults) value, yml, key);
|
||||
} else if (yml.get(key) == null) {
|
||||
yml.set(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -6,9 +6,43 @@ import org.bukkit.command.CommandSender;
|
|||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Messenger {
|
||||
|
||||
private static final Defaults DEFAULT_MESSAGES;
|
||||
static {
|
||||
DEFAULT_MESSAGES = Defaults.of(
|
||||
"serverutils", Defaults.of(
|
||||
"success", "&3Successfully %action%ed &b%what%&3!",
|
||||
"warning", "&3Successfully %action%ed &b%what%&3, but with warnings.",
|
||||
"error", "&cAn error occurred while %action%ing &4%what%&c, please check the console!",
|
||||
"not_exists", "&cAn error occurred while %action%ing &4%what%&c, plugin does not exist!",
|
||||
"already_enabled", "&cAn error occurred while %action%ing &4%what%&c, plugin is already enabled!",
|
||||
"help", Defaults.of(
|
||||
"header", "&8&m-------------=&r&8[ &b&lServerUtils Help&r &8]&m=---------------",
|
||||
"format", "&8/&3%command%&b%subcommand% &f(&7%help%&f)",
|
||||
"footer", "&8&m-------------------------------------------------"
|
||||
),
|
||||
"plugins", Defaults.of(
|
||||
"header", "&8&m------------=&r&8[ &b&lServerUtils Plugins&r &8]&m=-------------",
|
||||
"format", "&3%plugin%",
|
||||
"seperator", "&b, ",
|
||||
"last_seperator", " &band ",
|
||||
"version", " &8(&a%version%&8)",
|
||||
"footer", "&8&m-------------------------------------------------"
|
||||
),
|
||||
"plugininfo", Defaults.of(
|
||||
"header", "&8&m-----------=&r&8[ &b&lServerUtils PluginInfo&r &8]&m=-----------",
|
||||
"format", " &3%key%&8: &b%value%",
|
||||
"seperator", "&8, ",
|
||||
"last_seperator", " &8and ",
|
||||
"footer", "&8&m-------------------------------------------------"
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private static final ServerUtils plugin = ServerUtils.getInstance();
|
||||
private static Messenger instance;
|
||||
private final YamlConfiguration messages;
|
||||
|
|
@ -16,6 +50,18 @@ public class Messenger {
|
|||
public Messenger(File file) {
|
||||
instance = this;
|
||||
messages = YamlConfiguration.loadConfiguration(file);
|
||||
Defaults.addDefaults(DEFAULT_MESSAGES, messages);
|
||||
|
||||
try {
|
||||
// Idk somehow the order messes up
|
||||
// of the messages if we don't do this
|
||||
file.delete();
|
||||
file.createNewFile();
|
||||
|
||||
messages.save(file);
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static String getMessage(String path, String... replacements) {
|
||||
|
|
@ -36,6 +82,13 @@ public class Messenger {
|
|||
return message;
|
||||
}
|
||||
|
||||
public static void sendRawMessage(CommandSender sender, String msg, String... replacements) {
|
||||
String message = apply(msg, replacements);
|
||||
if (message != null) {
|
||||
sender.sendMessage(color(message));
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendMessage(CommandSender sender, String path, String... replacements) {
|
||||
String message = getMessage(path, replacements);
|
||||
if (message != null) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,54 @@
|
|||
package net.frankheijden.serverutils.utils;
|
||||
|
||||
import net.frankheijden.serverutils.config.Messenger;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class FormatBuilder {
|
||||
|
||||
private final String format;
|
||||
private final List<String[]> valueList;
|
||||
private String[] orderedKeys;
|
||||
private boolean alwaysSend;
|
||||
|
||||
private FormatBuilder(String format) {
|
||||
this.format = format;
|
||||
this.valueList = new ArrayList<>();
|
||||
this.orderedKeys = new String[0];
|
||||
this.alwaysSend = false;
|
||||
}
|
||||
|
||||
public static FormatBuilder create(String format) {
|
||||
return new FormatBuilder(format);
|
||||
}
|
||||
|
||||
public FormatBuilder orderedKeys(String... orderedKeys) {
|
||||
this.orderedKeys = orderedKeys;
|
||||
return this;
|
||||
}
|
||||
|
||||
public FormatBuilder add(String... values) {
|
||||
this.valueList.add(values);
|
||||
return this;
|
||||
}
|
||||
|
||||
public FormatBuilder alwaysSend(boolean alwaysSend) {
|
||||
this.alwaysSend = alwaysSend;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void sendTo(CommandSender sender) {
|
||||
valueList.forEach(values -> {
|
||||
int length = Math.min(values.length, orderedKeys.length);
|
||||
String message = format;
|
||||
for (int i = 0; i < length; i++) {
|
||||
String value = values[i];
|
||||
if ((value == null || value.isEmpty()) && !alwaysSend) return;
|
||||
message = message.replace(orderedKeys[i], String.valueOf(value));
|
||||
}
|
||||
Messenger.sendRawMessage(sender, message);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -17,6 +17,13 @@ public class ListBuilder<T> {
|
|||
return new ListBuilder<>(collection);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static ListBuilder<String> createStrings(Collection<? extends String> collection) {
|
||||
ListBuilder<String> builder = create((Collection<String>) collection);
|
||||
builder.format(ListFormat.stringFormat);
|
||||
return builder;
|
||||
}
|
||||
|
||||
public ListBuilder<T> format(ListFormat<T> formatter) {
|
||||
this.formatter = formatter;
|
||||
return this;
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@ package net.frankheijden.serverutils.utils;
|
|||
|
||||
public interface ListFormat<T> {
|
||||
|
||||
ListFormat<String> stringFormat = String::toString;
|
||||
|
||||
String format(T t);
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue