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 co.aikar.commands.annotation.Description;
027import co.aikar.commands.annotation.Syntax;
028import com.google.common.collect.HashMultimap;
029import com.google.common.collect.SetMultimap;
030import org.bukkit.command.Command;
031import org.bukkit.command.CommandSender;
032
033import java.util.ArrayList;
034import java.util.List;
035
036public class BukkitRootCommand extends Command implements RootCommand {
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 getCommandName() {
053        return name;
054    }
055
056    @Override
057    public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
058        return getTabCompletions(manager.getCommandIssuer(sender), alias, args);
059    }
060
061    @Override
062    public boolean execute(CommandSender sender, String commandLabel, String[] args) {
063        execute(manager.getCommandIssuer(sender), commandLabel, args);
064        return true;
065    }
066
067    public void addChild(BaseCommand command) {
068        if (this.defCommand == null || !command.subCommands.get("__default").isEmpty()) {
069            this.defCommand = command;
070            this.setPermission(command.permission);
071            //this.setDescription(command.getDescription());
072            //this.setUsage(command.getUsage());
073        }
074        addChildShared(this.children, this.subCommands, command);
075    }
076
077    @Override
078    public CommandManager getManager() {
079        return manager;
080    }
081
082    @Override
083    public SetMultimap<String, RegisteredCommand> getSubCommands() {
084        return this.subCommands;
085    }
086
087    @Override
088    public List<BaseCommand> getChildren() {
089        return children;
090    }
091
092    @Override
093    public BaseCommand getDefCommand(){
094        return defCommand;
095    }
096
097    @Override
098    public String getDescription() {
099        final RegisteredCommand cmd = this.getDefaultRegisteredCommand();
100        if (cmd != null) {
101            return cmd.helpText;
102        }
103        BaseCommand defCommand = getDefCommand();
104        if (defCommand != null) {
105            Description descAnno = defCommand.getClass().getAnnotation(Description.class);
106            if (descAnno != null) {
107                return descAnno.value();
108            }
109        }
110        return "";
111    }
112
113    @Override
114    public String getUsage() {
115        final RegisteredCommand cmd = this.getDefaultRegisteredCommand();
116        if (cmd != null) {
117            return cmd.syntaxText;
118        }
119        BaseCommand defCommand = getDefCommand();
120        if (defCommand != null) {
121            Syntax syntaxAnno = defCommand.getClass().getAnnotation(Syntax.class);
122            if (syntaxAnno != null) {
123                return syntaxAnno.value();
124            }
125        }
126        return "";
127    }
128}