From 69a65b4d2e418ee605d21ad6412f2445cf81c3f5 Mon Sep 17 00:00:00 2001 From: gravityfox Date: Sat, 24 Dec 2016 21:46:30 -0800 Subject: [PATCH] Added Who and What commands. --- .../sponge/foxcore/plugin/FoxCoreMain.java | 6 +- .../plugin/command/misc/CommandPWD.java | 18 ++ .../plugin/command/misc/CommandWhat.java | 166 ++++++++++++++++++ .../plugin/command/misc/CommandWho.java | 126 +++++++++++++ 4 files changed, 315 insertions(+), 1 deletion(-) create mode 100644 src/main/java/net/foxdenstudio/sponge/foxcore/plugin/command/misc/CommandWhat.java create mode 100644 src/main/java/net/foxdenstudio/sponge/foxcore/plugin/command/misc/CommandWho.java diff --git a/src/main/java/net/foxdenstudio/sponge/foxcore/plugin/FoxCoreMain.java b/src/main/java/net/foxdenstudio/sponge/foxcore/plugin/FoxCoreMain.java index 9c00299..a417365 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxcore/plugin/FoxCoreMain.java +++ b/src/main/java/net/foxdenstudio/sponge/foxcore/plugin/FoxCoreMain.java @@ -30,6 +30,8 @@ import net.foxdenstudio.sponge.foxcore.common.network.server.packet.ServerPrintStringPacket; import net.foxdenstudio.sponge.foxcore.plugin.command.*; import net.foxdenstudio.sponge.foxcore.plugin.command.misc.CommandPWD; +import net.foxdenstudio.sponge.foxcore.plugin.command.misc.CommandWhat; +import net.foxdenstudio.sponge.foxcore.plugin.command.misc.CommandWho; import net.foxdenstudio.sponge.foxcore.plugin.listener.WandBlockListener; import net.foxdenstudio.sponge.foxcore.plugin.listener.WandEntityListener; import net.foxdenstudio.sponge.foxcore.plugin.state.FCStateManager; @@ -180,7 +182,9 @@ private void configureCommands() { fcDispatcher.register(new CommandAbout(builder.build()), "about", "info"); FCCommandDispatcher miscDispatcher = new FCCommandDispatcher("/foxcore misc", "Misc commands that may be helpful."); - miscDispatcher.register(new CommandPWD(), "pwd", "directory"); + miscDispatcher.register(new CommandPWD(), "pwd", "directory", "dir"); + miscDispatcher.register(new CommandWho(), "who", "plugin"); + miscDispatcher.register(new CommandWhat(), "what", "command"); fcDispatcher.register(miscDispatcher, "misc", "miscellaneous", "util"); } diff --git a/src/main/java/net/foxdenstudio/sponge/foxcore/plugin/command/misc/CommandPWD.java b/src/main/java/net/foxdenstudio/sponge/foxcore/plugin/command/misc/CommandPWD.java index 00f61b6..f5bbc6b 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxcore/plugin/command/misc/CommandPWD.java +++ b/src/main/java/net/foxdenstudio/sponge/foxcore/plugin/command/misc/CommandPWD.java @@ -7,6 +7,8 @@ import org.spongepowered.api.text.Text; import org.spongepowered.api.text.format.TextColors; +import java.util.Optional; + /** * Created by Fox on 11/5/2016. */ @@ -22,4 +24,20 @@ public CommandResult process(CommandSource source, String arguments) throws Comm public boolean testPermission(CommandSource source) { return source.hasPermission("foxcore.command.misc.pwd"); } + + @Override + public Optional getShortDescription(CommandSource source) { + return Optional.of(Text.of("Prints the working directory of the game.")); + } + + @Override + public Optional getHelp(CommandSource source) { + return Optional.of(Text.of("This command prints the directory from which the game was started.\n" + + "On servers, this corresponds to the folder the server was started from, usually the folder with the server jar.")); + } + + @Override + public Text getUsage(CommandSource source) { + return Text.of("/foxcore misc pwd"); + } } diff --git a/src/main/java/net/foxdenstudio/sponge/foxcore/plugin/command/misc/CommandWhat.java b/src/main/java/net/foxdenstudio/sponge/foxcore/plugin/command/misc/CommandWhat.java new file mode 100644 index 0000000..c4f3b74 --- /dev/null +++ b/src/main/java/net/foxdenstudio/sponge/foxcore/plugin/command/misc/CommandWhat.java @@ -0,0 +1,166 @@ +package net.foxdenstudio.sponge.foxcore.plugin.command.misc; + +import com.google.common.collect.ImmutableList; +import net.foxdenstudio.sponge.foxcore.plugin.command.FCCommandBase; +import net.foxdenstudio.sponge.foxcore.plugin.command.util.AdvCmdParser; +import org.spongepowered.api.Sponge; +import org.spongepowered.api.command.*; +import org.spongepowered.api.plugin.PluginContainer; +import org.spongepowered.api.text.Text; +import org.spongepowered.api.text.action.TextActions; +import org.spongepowered.api.text.format.TextColors; +import org.spongepowered.api.text.format.TextStyles; +import org.spongepowered.api.world.Location; +import org.spongepowered.api.world.World; + +import javax.annotation.Nullable; +import java.util.*; + +/** + * Created by Fox on 12/22/2016. + */ +public class CommandWhat extends FCCommandBase { + + @Override + public CommandResult process(CommandSource source, String arguments) throws CommandException { + AdvCmdParser.ParseResult parse = AdvCmdParser.builder() + .arguments(arguments) + .parse(); + CommandManager manager = Sponge.getCommandManager(); + + if (parse.args.length == 0) { + source.sendMessage(Text.of(TextColors.GREEN, "Usage: ", TextColors.RESET, "/foxcore misc what ")); + } else { + String commandName = parse.args[0]; + Set mappings = manager.getAll(commandName); + if (mappings.size() > 0) { + Text.Builder builder = Text.builder(); + builder.append(Text.of(TextColors.GOLD, "\n-----------------------------------------------------\n")); + if (mappings.size() == 1) { + CommandMapping mapping = mappings.iterator().next(); + + generateText(mapping, builder, source, manager); + + source.sendMessage(builder.build()); + } else { + Optional primaryMappingOpt = manager.get(commandName); + Set secondaryMappings = new HashSet<>(mappings); + if (primaryMappingOpt.isPresent()) { + CommandMapping primaryMapping = primaryMappingOpt.get(); + secondaryMappings.remove(primaryMapping); + + builder.append(Text.of(TextColors.GREEN, "------- Primary -------\n")); + + generateText(primaryMapping, builder, source, manager); + + builder.append(Text.of(TextColors.GREEN, "\n------- Secondary -------\n")); + + Iterator mappingIterator = secondaryMappings.iterator(); + + while (mappingIterator.hasNext()) { + CommandMapping mapping = mappingIterator.next(); + generateText(mapping, builder, source, manager); + if (mappingIterator.hasNext()) { + builder.append(Text.of("\n\n")); + } + } + } else { + source.sendMessage(Text.of(TextColors.RED, "Something very strange happened. What the heck did you do?")); + } + } + } else { + source.sendMessage(Text.of(TextColors.RED, "No command with this name: ", TextColors.RESET, commandName)); + } + } + return CommandResult.empty(); + } + + @Override + public List getSuggestions(CommandSource source, String arguments, @Nullable Location targetPosition) throws CommandException { + AdvCmdParser.ParseResult parse = AdvCmdParser.builder() + .arguments(arguments) + .autoCloseQuotes(true) + .excludeCurrent(true) + .parse(); + CommandManager manager = Sponge.getCommandManager(); + + if (parse.current.type == AdvCmdParser.CurrentElement.ElementType.ARGUMENT) { + if (parse.current.index == 0) { + return manager.getSuggestions(source, parse.current.token, targetPosition); + } + } else if (parse.current.type == AdvCmdParser.CurrentElement.ElementType.COMPLETE) { + return ImmutableList.of(parse.current.prefix + " "); + } + return ImmutableList.of(); + } + + @Override + public boolean testPermission(CommandSource source) { + return source.hasPermission("foxcore.command.misc.what"); + } + + @Override + public Optional getShortDescription(CommandSource source) { + return Optional.of(Text.of("Tells you information about any command.")); + } + + @Override + public Text getUsage(CommandSource source) { + return Text.of(""); + } + + private String colTS(Collection col) { + String str = ""; + Iterator it = col.iterator(); + while (it.hasNext()) { + str += it.next().toString(); + if (it.hasNext()) str += ", "; + } + return str; + } + + private void generateText(CommandMapping mapping, Text.Builder builder, CommandSource source, CommandManager manager) { + String primaryAlias = mapping.getPrimaryAlias(); + builder.append(Text.of(TextColors.GOLD, "Primary Alias: ", TextColors.RESET, primaryAlias, "\n")); + + Set secondaryAliases = new HashSet<>(mapping.getAllAliases()); + secondaryAliases.remove(primaryAlias); + + if (secondaryAliases.size() > 1) { + builder.append(Text.of(TextColors.GREEN, "Secondary Aliases: ")); + } else { + builder.append(Text.of(TextColors.GREEN, "Secondary Alias: ")); + } + builder.append(Text.of(TextColors.RESET, colTS(secondaryAliases), "\n")); + + CommandCallable callable = mapping.getCallable(); + + builder.append(Text.of(TextColors.AQUA, "Usage: ")); + builder.append(callable.getUsage(source)); + builder.append(Text.NEW_LINE); + + Optional descriptionOpt = callable.getShortDescription(source); + if (descriptionOpt.isPresent()) { + Text description = descriptionOpt.get(); + builder.append(Text.of(TextColors.AQUA, "Description: ")); + builder.append(description); + builder.append(Text.NEW_LINE); + } + + Optional containerOpt = manager.getOwner(mapping); + + if (containerOpt.isPresent()) { + PluginContainer container = containerOpt.get(); + String id = container.getId(); + builder.append(Text.of( + TextActions.showText(Text.of("Click to show plugin details")), + TextActions.runCommand("/foxcore misc who " + id), + TextColors.LIGHT_PURPLE, "Plugin: ", TextColors.RESET, id) + ); + } else { + builder.append(Text.of(TextColors.LIGHT_PURPLE, "Plugin: ", TextColors.GRAY, TextStyles.ITALIC, "unknown")); + } + } + + +} diff --git a/src/main/java/net/foxdenstudio/sponge/foxcore/plugin/command/misc/CommandWho.java b/src/main/java/net/foxdenstudio/sponge/foxcore/plugin/command/misc/CommandWho.java new file mode 100644 index 0000000..8830046 --- /dev/null +++ b/src/main/java/net/foxdenstudio/sponge/foxcore/plugin/command/misc/CommandWho.java @@ -0,0 +1,126 @@ +package net.foxdenstudio.sponge.foxcore.plugin.command.misc; + +import com.google.common.collect.ImmutableList; +import net.foxdenstudio.sponge.foxcore.plugin.command.FCCommandBase; +import net.foxdenstudio.sponge.foxcore.plugin.command.util.AdvCmdParser; +import org.spongepowered.api.Sponge; +import org.spongepowered.api.command.CommandException; +import org.spongepowered.api.command.CommandResult; +import org.spongepowered.api.command.CommandSource; +import org.spongepowered.api.plugin.PluginContainer; +import org.spongepowered.api.plugin.PluginManager; +import org.spongepowered.api.text.Text; +import org.spongepowered.api.text.format.TextColors; +import org.spongepowered.api.util.GuavaCollectors; +import org.spongepowered.api.util.StartsWithPredicate; +import org.spongepowered.api.world.Location; +import org.spongepowered.api.world.World; + +import javax.annotation.Nullable; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Optional; + +/** + * Created by Fox on 12/24/2016. + */ +public class CommandWho extends FCCommandBase { + + @Override + public CommandResult process(CommandSource source, String arguments) throws CommandException { + AdvCmdParser.ParseResult parse = AdvCmdParser.builder() + .arguments(arguments) + .parse(); + + PluginManager manager = Sponge.getPluginManager(); + + if (parse.args.length == 0) { + source.sendMessage(Text.of(TextColors.GREEN, "Usage: ", TextColors.RESET, "/foxcore misc who ")); + } else { + String pluginId = parse.args[0]; + + Optional containerOpt = manager.getPlugin(pluginId); + + if (containerOpt.isPresent()) { + PluginContainer container = containerOpt.get(); + Text.Builder builder = Text.builder(); + builder.append(Text.of(TextColors.GOLD, "\n-----------------------------------------------------\n")); + builder.append(Text.of(TextColors.GREEN, "Id: ", TextColors.RESET, container.getId(), "\n")); + builder.append(Text.of(TextColors.GREEN, "Name: ", TextColors.RESET, container.getName())); + + container.getVersion().ifPresent(s -> builder.append(Text.of(TextColors.GREEN, "\nVersion: ", TextColors.RESET, s))); + + List authors = container.getAuthors(); + if (!authors.isEmpty()) { + if (authors.size() > 1) { + builder.append(Text.of(TextColors.AQUA, "\nAuthors: ")); + } else { + builder.append(Text.of(TextColors.AQUA, "\nAuthor: ")); + } + builder.append(Text.of(TextColors.RESET, colTS(authors))); + } + + container.getDescription().ifPresent(s -> builder.append(Text.of(TextColors.AQUA, "\nDescription: ", TextColors.RESET, s))); + container.getUrl().ifPresent(s -> builder.append(Text.of(TextColors.AQUA, "\nURL: ", TextColors.RESET, s))); + container.getSource().ifPresent(p -> builder.append(Text.of(TextColors.LIGHT_PURPLE, "\nFile: ", TextColors.RESET, p.getFileName()))); + container.getInstance().ifPresent(i -> builder.append(Text.of(TextColors.LIGHT_PURPLE, "\nMain Class: ", TextColors.RESET, i.getClass().getName()))); + + source.sendMessage(builder.build()); + } else { + source.sendMessage(Text.of(TextColors.RED, "No plugin with this id: ", TextColors.RESET, pluginId)); + } + + } + return CommandResult.empty(); + } + + @Override + public List getSuggestions(CommandSource source, String arguments, @Nullable Location targetPosition) throws CommandException { + AdvCmdParser.ParseResult parse = AdvCmdParser.builder() + .arguments(arguments) + .autoCloseQuotes(true) + .excludeCurrent(true) + .parse(); + PluginManager manager = Sponge.getPluginManager(); + + if (parse.current.type == AdvCmdParser.CurrentElement.ElementType.ARGUMENT) { + if (parse.current.index == 0) { + return manager.getPlugins().stream() + .map(PluginContainer::getId) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } + } else if (parse.current.type == AdvCmdParser.CurrentElement.ElementType.COMPLETE) { + return ImmutableList.of(parse.current.prefix + " "); + } + return ImmutableList.of(); + } + + @Override + public boolean testPermission(CommandSource source) { + return source.hasPermission("foxcore.command.misc.who"); + } + + @Override + public Optional getShortDescription(CommandSource source) { + return Optional.of(Text.of("Tells you information about any plugin.")); + } + + @Override + public Text getUsage(CommandSource source) { + return Text.of(""); + } + + private String colTS(Collection col) { + String str = ""; + Iterator it = col.iterator(); + while (it.hasNext()) { + str += it.next().toString(); + if (it.hasNext()) str += ", "; + } + return str; + } + +}