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; 030 031import java.util.ArrayList; 032import java.util.List; 033 034public class BukkitRootCommand extends Command implements RootCommand { 035 036 private final BukkitCommandManager manager; 037 private final String name; 038 private BaseCommand defCommand; 039 private SetMultimap<String, RegisteredCommand> subCommands = HashMultimap.create(); 040 private List<BaseCommand> children = new ArrayList<>(); 041 boolean isRegistered = false; 042 043 BukkitRootCommand(BukkitCommandManager manager, String name) { 044 super(name); 045 this.manager = manager; 046 this.name = name; 047 } 048 049 @Override 050 public String getDescription() { 051 RegisteredCommand command = getDefaultRegisteredCommand(); 052 053 if (command != null && !command.getHelpText().isEmpty()) { 054 return command.getHelpText(); 055 } 056 if (command != null && command.scope.description != null) { 057 return command.scope.description; 058 } 059 if (defCommand.description != null) { 060 return defCommand.description; 061 } 062 return super.getDescription(); 063 } 064 065 @Override 066 public String getCommandName() { 067 return name; 068 } 069 070 @Override 071 public List<String> tabComplete(CommandSender sender, String commandLabel, String[] args) throws IllegalArgumentException { 072 if (commandLabel.contains(":")) commandLabel = ACFPatterns.COLON.split(commandLabel, 2)[1]; 073 return getTabCompletions(manager.getCommandIssuer(sender), commandLabel, args); 074 } 075 076 @Override 077 public boolean execute(CommandSender sender, String commandLabel, String[] args) { 078 if (commandLabel.contains(":")) commandLabel = ACFPatterns.COLON.split(commandLabel, 2)[1]; 079 execute(manager.getCommandIssuer(sender), commandLabel, args); 080 return true; 081 } 082 083 @Override 084 public boolean testPermissionSilent(CommandSender target) { 085 return hasAnyPermission(manager.getCommandIssuer(target)); 086 } 087 088 public void addChild(BaseCommand command) { 089 if (this.defCommand == null || !command.subCommands.get(BaseCommand.DEFAULT).isEmpty()) { 090 this.defCommand = command; 091 } 092 addChildShared(this.children, this.subCommands, command); 093 setPermission(getUniquePermission()); 094 } 095 096 @Override 097 public CommandManager getManager() { 098 return manager; 099 } 100 101 @Override 102 public SetMultimap<String, RegisteredCommand> getSubCommands() { 103 return this.subCommands; 104 } 105 106 @Override 107 public List<BaseCommand> getChildren() { 108 return children; 109 } 110 111 @Override 112 public BaseCommand getDefCommand() { 113 return defCommand; 114 } 115 116}