|
| 1 | +package net.foxdenstudio.sponge.foxcore.plugin.command.misc; |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableList; |
| 4 | +import net.foxdenstudio.sponge.foxcore.plugin.command.FCCommandBase; |
| 5 | +import net.foxdenstudio.sponge.foxcore.plugin.command.util.AdvCmdParser; |
| 6 | +import org.spongepowered.api.Sponge; |
| 7 | +import org.spongepowered.api.command.*; |
| 8 | +import org.spongepowered.api.plugin.PluginContainer; |
| 9 | +import org.spongepowered.api.text.Text; |
| 10 | +import org.spongepowered.api.text.action.TextActions; |
| 11 | +import org.spongepowered.api.text.format.TextColors; |
| 12 | +import org.spongepowered.api.text.format.TextStyles; |
| 13 | +import org.spongepowered.api.world.Location; |
| 14 | +import org.spongepowered.api.world.World; |
| 15 | + |
| 16 | +import javax.annotation.Nullable; |
| 17 | +import java.util.*; |
| 18 | + |
| 19 | +/** |
| 20 | + * Created by Fox on 12/22/2016. |
| 21 | + */ |
| 22 | +public class CommandWhat extends FCCommandBase { |
| 23 | + |
| 24 | + @Override |
| 25 | + public CommandResult process(CommandSource source, String arguments) throws CommandException { |
| 26 | + AdvCmdParser.ParseResult parse = AdvCmdParser.builder() |
| 27 | + .arguments(arguments) |
| 28 | + .parse(); |
| 29 | + CommandManager manager = Sponge.getCommandManager(); |
| 30 | + |
| 31 | + if (parse.args.length == 0) { |
| 32 | + source.sendMessage(Text.of(TextColors.GREEN, "Usage: ", TextColors.RESET, "/foxcore misc what <command>")); |
| 33 | + } else { |
| 34 | + String commandName = parse.args[0]; |
| 35 | + Set<? extends CommandMapping> mappings = manager.getAll(commandName); |
| 36 | + if (mappings.size() > 0) { |
| 37 | + Text.Builder builder = Text.builder(); |
| 38 | + builder.append(Text.of(TextColors.GOLD, "\n-----------------------------------------------------\n")); |
| 39 | + if (mappings.size() == 1) { |
| 40 | + CommandMapping mapping = mappings.iterator().next(); |
| 41 | + |
| 42 | + generateText(mapping, builder, source, manager); |
| 43 | + |
| 44 | + source.sendMessage(builder.build()); |
| 45 | + } else { |
| 46 | + Optional<? extends CommandMapping> primaryMappingOpt = manager.get(commandName); |
| 47 | + Set<? extends CommandMapping> secondaryMappings = new HashSet<>(mappings); |
| 48 | + if (primaryMappingOpt.isPresent()) { |
| 49 | + CommandMapping primaryMapping = primaryMappingOpt.get(); |
| 50 | + secondaryMappings.remove(primaryMapping); |
| 51 | + |
| 52 | + builder.append(Text.of(TextColors.GREEN, "------- Primary -------\n")); |
| 53 | + |
| 54 | + generateText(primaryMapping, builder, source, manager); |
| 55 | + |
| 56 | + builder.append(Text.of(TextColors.GREEN, "\n------- Secondary -------\n")); |
| 57 | + |
| 58 | + Iterator<? extends CommandMapping> mappingIterator = secondaryMappings.iterator(); |
| 59 | + |
| 60 | + while (mappingIterator.hasNext()) { |
| 61 | + CommandMapping mapping = mappingIterator.next(); |
| 62 | + generateText(mapping, builder, source, manager); |
| 63 | + if (mappingIterator.hasNext()) { |
| 64 | + builder.append(Text.of("\n\n")); |
| 65 | + } |
| 66 | + } |
| 67 | + } else { |
| 68 | + source.sendMessage(Text.of(TextColors.RED, "Something very strange happened. What the heck did you do?")); |
| 69 | + } |
| 70 | + } |
| 71 | + } else { |
| 72 | + source.sendMessage(Text.of(TextColors.RED, "No command with this name: ", TextColors.RESET, commandName)); |
| 73 | + } |
| 74 | + } |
| 75 | + return CommandResult.empty(); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<World> targetPosition) throws CommandException { |
| 80 | + AdvCmdParser.ParseResult parse = AdvCmdParser.builder() |
| 81 | + .arguments(arguments) |
| 82 | + .autoCloseQuotes(true) |
| 83 | + .excludeCurrent(true) |
| 84 | + .parse(); |
| 85 | + CommandManager manager = Sponge.getCommandManager(); |
| 86 | + |
| 87 | + if (parse.current.type == AdvCmdParser.CurrentElement.ElementType.ARGUMENT) { |
| 88 | + if (parse.current.index == 0) { |
| 89 | + return manager.getSuggestions(source, parse.current.token, targetPosition); |
| 90 | + } |
| 91 | + } else if (parse.current.type == AdvCmdParser.CurrentElement.ElementType.COMPLETE) { |
| 92 | + return ImmutableList.of(parse.current.prefix + " "); |
| 93 | + } |
| 94 | + return ImmutableList.of(); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public boolean testPermission(CommandSource source) { |
| 99 | + return source.hasPermission("foxcore.command.misc.what"); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public Optional<Text> getShortDescription(CommandSource source) { |
| 104 | + return Optional.of(Text.of("Tells you information about any command.")); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public Text getUsage(CommandSource source) { |
| 109 | + return Text.of("<command>"); |
| 110 | + } |
| 111 | + |
| 112 | + private String colTS(Collection col) { |
| 113 | + String str = ""; |
| 114 | + Iterator it = col.iterator(); |
| 115 | + while (it.hasNext()) { |
| 116 | + str += it.next().toString(); |
| 117 | + if (it.hasNext()) str += ", "; |
| 118 | + } |
| 119 | + return str; |
| 120 | + } |
| 121 | + |
| 122 | + private void generateText(CommandMapping mapping, Text.Builder builder, CommandSource source, CommandManager manager) { |
| 123 | + String primaryAlias = mapping.getPrimaryAlias(); |
| 124 | + builder.append(Text.of(TextColors.GOLD, "Primary Alias: ", TextColors.RESET, primaryAlias, "\n")); |
| 125 | + |
| 126 | + Set<String> secondaryAliases = new HashSet<>(mapping.getAllAliases()); |
| 127 | + secondaryAliases.remove(primaryAlias); |
| 128 | + |
| 129 | + if (secondaryAliases.size() > 1) { |
| 130 | + builder.append(Text.of(TextColors.GREEN, "Secondary Aliases: ")); |
| 131 | + } else { |
| 132 | + builder.append(Text.of(TextColors.GREEN, "Secondary Alias: ")); |
| 133 | + } |
| 134 | + builder.append(Text.of(TextColors.RESET, colTS(secondaryAliases), "\n")); |
| 135 | + |
| 136 | + CommandCallable callable = mapping.getCallable(); |
| 137 | + |
| 138 | + builder.append(Text.of(TextColors.AQUA, "Usage: ")); |
| 139 | + builder.append(callable.getUsage(source)); |
| 140 | + builder.append(Text.NEW_LINE); |
| 141 | + |
| 142 | + Optional<Text> descriptionOpt = callable.getShortDescription(source); |
| 143 | + if (descriptionOpt.isPresent()) { |
| 144 | + Text description = descriptionOpt.get(); |
| 145 | + builder.append(Text.of(TextColors.AQUA, "Description: ")); |
| 146 | + builder.append(description); |
| 147 | + builder.append(Text.NEW_LINE); |
| 148 | + } |
| 149 | + |
| 150 | + Optional<PluginContainer> containerOpt = manager.getOwner(mapping); |
| 151 | + |
| 152 | + if (containerOpt.isPresent()) { |
| 153 | + PluginContainer container = containerOpt.get(); |
| 154 | + String id = container.getId(); |
| 155 | + builder.append(Text.of( |
| 156 | + TextActions.showText(Text.of("Click to show plugin details")), |
| 157 | + TextActions.runCommand("/foxcore misc who " + id), |
| 158 | + TextColors.LIGHT_PURPLE, "Plugin: ", TextColors.RESET, id) |
| 159 | + ); |
| 160 | + } else { |
| 161 | + builder.append(Text.of(TextColors.LIGHT_PURPLE, "Plugin: ", TextColors.GRAY, TextStyles.ITALIC, "unknown")); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + |
| 166 | +} |
0 commit comments