001/*
002 * Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License
003 *
004 *  Permission is hereby granted, free of charge, to any person obtaining
005 *  a copy of this software and associated documentation files (the
006 *  "Software"), to deal in the Software without restriction, including
007 *  without limitation the rights to use, copy, modify, merge, publish,
008 *  distribute, sublicense, and/or sell copies of the Software, and to
009 *  permit persons to whom the Software is furnished to do so, subject to
010 *  the following conditions:
011 *
012 *  The above copyright notice and this permission notice shall be
013 *  included in all copies or substantial portions of the Software.
014 *
015 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
016 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
017 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
018 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
019 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
020 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
021 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
022 */
023
024package co.aikar.commands;
025
026import com.google.common.collect.HashMultimap;
027import com.google.common.collect.SetMultimap;
028import org.bukkit.command.Command;
029import org.bukkit.command.CommandSender;
030import org.bukkit.command.PluginIdentifiableCommand;
031import org.bukkit.plugin.Plugin;
032
033import java.util.ArrayList;
034import java.util.List;
035
036public class BukkitRootCommand extends Command implements RootCommand, PluginIdentifiableCommand {
037
038    private final BukkitCommandManager manager;
039    private final String name;
040    private BaseCommand defCommand;
041    private SetMultimap<String, RegisteredCommand> subCommands = HashMultimap.create();
042    private List<BaseCommand> children = new ArrayList<>();
043    boolean isRegistered = false;
044
045    BukkitRootCommand(BukkitCommandManager manager, String name) {
046        super(name);
047        this.manager = manager;
048        this.name = name;
049    }
050
051    @Override
052    public String getDescription() {
053        RegisteredCommand command = getDefaultRegisteredCommand();
054        String description = null;
055
056        if (command != null && !command.getHelpText().isEmpty()) {
057            description = command.getHelpText();
058        } else if (command != null && command.scope.description != null) {
059            description = command.scope.description;
060        } else if (defCommand.description != null) {
061            description = defCommand.description;
062        }
063
064        if (description != null) {
065            return manager.getLocales().replaceI18NStrings(description);
066        }
067        return super.getDescription();
068    }
069
070    @Override
071    public String getCommandName() {
072        return name;
073    }
074
075    @Override
076    public List<String> tabComplete(CommandSender sender, String commandLabel, String[] args) throws IllegalArgumentException {
077        if (commandLabel.contains(":")) commandLabel = ACFPatterns.COLON.split(commandLabel, 2)[1];
078        return getTabCompletions(manager.getCommandIssuer(sender), commandLabel, args);
079    }
080
081    @Override
082    public boolean execute(CommandSender sender, String commandLabel, String[] args) {
083        if (commandLabel.contains(":")) commandLabel = ACFPatterns.COLON.split(commandLabel, 2)[1];
084        execute(manager.getCommandIssuer(sender), commandLabel, args);
085        return true;
086    }
087
088    @Override
089    public boolean testPermissionSilent(CommandSender target) {
090        return hasAnyPermission(manager.getCommandIssuer(target));
091    }
092
093    public void addChild(BaseCommand command) {
094        if (this.defCommand == null || !command.subCommands.get(BaseCommand.DEFAULT).isEmpty()) {
095            this.defCommand = command;
096        }
097        addChildShared(this.children, this.subCommands, command);
098        setPermission(getUniquePermission());
099    }
100
101    @Override
102    public CommandManager getManager() {
103        return manager;
104    }
105
106    @Override
107    public SetMultimap<String, RegisteredCommand> getSubCommands() {
108        return this.subCommands;
109    }
110
111    @Override
112    public List<BaseCommand> getChildren() {
113        return children;
114    }
115
116    @Override
117    public BaseCommand getDefCommand() {
118        return defCommand;
119    }
120
121    @Override
122    public Plugin getPlugin() {
123        return manager.getPlugin();
124    }
125}