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 net.md_5.bungee.api.CommandSender;
029import net.md_5.bungee.api.plugin.Command;
030import net.md_5.bungee.api.plugin.TabExecutor;
031
032import java.util.ArrayList;
033import java.util.List;
034
035public class BungeeRootCommand extends Command implements RootCommand, TabExecutor {
036
037    private final BungeeCommandManager manager;
038    private final String name;
039    private BaseCommand defCommand;
040    private SetMultimap<String, RegisteredCommand> subCommands = HashMultimap.create();
041    private List<BaseCommand> children = new ArrayList<>();
042    boolean isRegistered = false;
043
044    BungeeRootCommand(BungeeCommandManager manager, String name) {
045        super(name);
046        this.manager = manager;
047        this.name = name;
048    }
049
050    @Override
051    public String getCommandName() {
052        return name;
053    }
054
055    @Override
056    public void addChild(BaseCommand command) {
057        if (this.defCommand == null || !command.subCommands.get(BaseCommand.DEFAULT).isEmpty()) {
058            this.defCommand = command;
059
060        }
061        this.addChildShared(this.children, this.subCommands, command);
062    }
063
064    @Override
065    public CommandManager getManager() {
066        return manager;
067    }
068
069    @Override
070    public SetMultimap<String, RegisteredCommand> getSubCommands() {
071        return subCommands;
072    }
073
074    @Override
075    public List<BaseCommand> getChildren() {
076        return children;
077    }
078
079    @Override
080    public void execute(CommandSender sender, String[] args) {
081        execute(manager.getCommandIssuer(sender), getName(), args);
082    }
083
084    @Override
085    public Iterable<String> onTabComplete(CommandSender commandSender, String[] strings) {
086        return getTabCompletions(manager.getCommandIssuer(commandSender), getName(), strings);
087    }
088
089    @Override
090    public BaseCommand getDefCommand() {
091        return defCommand;
092    }
093}