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 jdk.nashorn.internal.ir.ReturnNode; 031import org.bukkit.command.Command; 032import org.bukkit.command.CommandSender; 033 034import java.util.ArrayList; 035import java.util.HashSet; 036import java.util.List; 037import java.util.Set; 038 039public class BukkitRootCommand extends Command implements RootCommand { 040 041 private final BukkitCommandManager manager; 042 private final String name; 043 private BaseCommand defCommand; 044 private SetMultimap<String, RegisteredCommand> subCommands = HashMultimap.create(); 045 private List<BaseCommand> children = new ArrayList<>(); 046 boolean isRegistered = false; 047 048 BukkitRootCommand(BukkitCommandManager manager, String name) { 049 super(name); 050 this.manager = manager; 051 this.name = name; 052 } 053 054 @Override 055 public String getCommandName() { 056 return name; 057 } 058 059 @Override 060 public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException { 061 return tabComplete(manager.getCommandIssuer(sender), alias, args); 062 } 063 064 @Override 065 public boolean execute(CommandSender sender, String commandLabel, String[] args) { 066 execute(manager.getCommandIssuer(sender), commandLabel, args); 067 return true; 068 } 069 070 private List<String> tabComplete(CommandIssuer sender, String alias, String[] args) throws IllegalArgumentException { 071 Set<String> completions = new HashSet<>(); 072 this.children.forEach(child -> completions.addAll(child.tabComplete(sender, alias, args))); 073 return new ArrayList<>(completions); 074 } 075 076 077 078 public void addChild(BaseCommand command) { 079 if (this.defCommand == null || !command.subCommands.get("__default").isEmpty()) { 080 this.defCommand = command; 081 this.setPermission(command.permission); 082 //this.setDescription(command.getDescription()); 083 //this.setUsage(command.getUsage()); 084 } 085 addChildShared(this.children, this.subCommands, command); 086 } 087 088 @Override 089 public CommandManager getManager() { 090 return manager; 091 } 092 093 @Override 094 public SetMultimap<String, RegisteredCommand> getSubCommands() { 095 return this.subCommands; 096 } 097 098 @Override 099 public BaseCommand getDefCommand(){ 100 return defCommand; 101 } 102 103 @Override 104 public String getDescription() { 105 final RegisteredCommand cmd = this.getDefaultRegisteredCommand(); 106 if (cmd != null) { 107 return cmd.helpText; 108 } 109 BaseCommand defCommand = getDefCommand(); 110 if (defCommand != null) { 111 Description descAnno = defCommand.getClass().getAnnotation(Description.class); 112 if (descAnno != null) { 113 return descAnno.value(); 114 } 115 } 116 return ""; 117 } 118 119 @Override 120 public String getUsage() { 121 final RegisteredCommand cmd = this.getDefaultRegisteredCommand(); 122 if (cmd != null) { 123 return cmd.syntaxText; 124 } 125 BaseCommand defCommand = getDefCommand(); 126 if (defCommand != null) { 127 Syntax syntaxAnno = defCommand.getClass().getAnnotation(Syntax.class); 128 if (syntaxAnno != null) { 129 return syntaxAnno.value(); 130 } 131 } 132 return ""; 133 } 134}