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.Lists; 027import com.google.common.collect.Maps; 028 029import java.lang.reflect.Parameter; 030import java.util.List; 031import java.util.Map; 032 033public class CommandCompletionContext { 034 private final RegisteredCommand command; 035 protected final CommandIssuer issuer; 036 private final String input; 037 private final String config; 038 private final Map<String, String> configs = Maps.newHashMap(); 039 private final List<String> args; 040 041 CommandCompletionContext(RegisteredCommand command, CommandIssuer issuer, String input, String config, String[] args) { 042 this.command = command; 043 this.issuer = issuer; 044 this.input = input; 045 if (config != null) { 046 String[] configs = ACFPatterns.COMMA.split(config); 047 for (String conf : configs) { 048 String[] confsplit = ACFPatterns.EQUALS.split(conf, 2); 049 this.configs.put(confsplit[0].toLowerCase(), confsplit.length > 1 ? confsplit[1] : null); 050 } 051 this.config = configs[0]; 052 } else { 053 this.config = null; 054 } 055 056 this.args = Lists.newArrayList(args); 057 } 058 059 public Map<String, String> getConfigs() { 060 return configs; 061 } 062 063 public String getConfig(String key) { 064 return getConfig(key, null); 065 } 066 067 public String getConfig(String key, String def) { 068 return this.configs.getOrDefault(key.toLowerCase(), def); 069 } 070 071 public boolean hasConfig(String key) { 072 return this.configs.containsKey(key.toLowerCase()); 073 } 074 075 public <T> T getContextValue(Class<? extends T> clazz) throws InvalidCommandArgument { 076 return getContextValue(clazz, null); 077 } 078 079 public <T> T getContextValue(Class<? extends T> clazz, Integer paramIdx) throws InvalidCommandArgument { 080 String name = null; 081 if (paramIdx != null) { 082 if (paramIdx >= command.parameters.length) { 083 throw new IllegalArgumentException("Param index is higher than number of parameters"); 084 } 085 Parameter param = command.parameters[paramIdx]; 086 Class<?> paramType = param.getType(); 087 if (!clazz.isAssignableFrom(paramType)) { 088 throw new IllegalArgumentException(param.getName() +":" + paramType.getName() + " can not satisfy " + clazz.getName()); 089 } 090 name = param.getName(); 091 } else { 092 Parameter[] parameters = command.parameters; 093 for (int i = 0; i < parameters.length; i++) { 094 Parameter param = parameters[i]; 095 if (clazz.isAssignableFrom(param.getType())) { 096 paramIdx = i; 097 name = param.getName(); 098 break; 099 } 100 } 101 if (paramIdx == null) { 102 throw new IllegalStateException("Can not find any parameter that can satisfy " + clazz.getName()); 103 } 104 } 105 //noinspection unchecked 106 Map<String, Object> resolved = command.resolveContexts(issuer, args, args.size()); 107 if (resolved == null || paramIdx > resolved.size()) { 108 this.command.scope.manager.log(LogLevel.ERROR, "resolved: " + resolved + " paramIdx: " + paramIdx + " - size: " + (resolved != null ? resolved.size() : null )); 109 ACFUtil.sneaky(new CommandCompletionTextLookupException()); 110 } 111 112 //noinspection unchecked 113 return (T) resolved.get(name); 114 } 115 116 public CommandIssuer getIssuer() { 117 return issuer; 118 } 119 120 public String getInput() { 121 return input; 122 } 123 124 public String getConfig() { 125 return config; 126 } 127}