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 java.lang.annotation.Annotation; 027import java.lang.reflect.Parameter; 028import java.util.List; 029import java.util.Map; 030 031@SuppressWarnings({"WeakerAccess", "unused"}) 032public class CommandExecutionContext <CEC extends CommandExecutionContext, I extends CommandIssuer> { 033 private final RegisteredCommand cmd; 034 private final CommandParameter param; 035 protected final I issuer; 036 private final List<String> args; 037 private final int index; 038 private final Map<String, Object> passedArgs; 039 private final Map<String, String> flags; 040 private final CommandManager manager; 041 042 CommandExecutionContext(RegisteredCommand cmd, CommandParameter param, I sender, List<String> args, 043 int index, Map<String, Object> passedArgs) { 044 this.cmd = cmd; 045 this.manager = cmd.scope.manager; 046 this.param = param; 047 this.issuer = sender; 048 this.args = args; 049 this.index = index; 050 this.passedArgs = passedArgs; 051 this.flags = param.getFlags(); 052 053 } 054 055 public String popFirstArg() { 056 return !args.isEmpty() ? args.remove(0) : null; 057 } 058 059 public String popLastArg() { 060 return !args.isEmpty() ? args.remove(args.size() - 1) : null; 061 } 062 063 public String getFirstArg() { 064 return !args.isEmpty() ? args.get(0) : null; 065 } 066 067 public String getLastArg() { 068 return !args.isEmpty() ? args.get(args.size() - 1) : null; 069 } 070 071 public boolean isLastArg() { 072 return cmd.parameters.length -1 == index; 073 } 074 075 public int getNumParams() { 076 return cmd.parameters.length; 077 } 078 079 public boolean canOverridePlayerContext() { 080 return cmd.requiredResolvers >= args.size(); 081 } 082 083 public Object getResolvedArg(String arg) { 084 return passedArgs.get(arg); 085 } 086 087 public Object getResolvedArg(Class<?>... classes) { 088 for (Class<?> clazz : classes) { 089 for (Object passedArg : passedArgs.values()) { 090 if (clazz.isInstance(passedArg)) { 091 return passedArg; 092 } 093 } 094 } 095 096 return null; 097 } 098 099 public <T> T getResolvedArg(String key, Class<?>... classes) { 100 final Object o = passedArgs.get(key); 101 for (Class<?> clazz : classes) { 102 if (clazz.isInstance(o)) { 103 //noinspection unchecked 104 return (T) o; 105 } 106 } 107 108 return null; 109 } 110 111 public boolean isOptional() { 112 return param.isOptional(); 113 } 114 115 public boolean hasFlag(String flag) { 116 return flags.containsKey(flag); 117 } 118 119 public String getFlagValue(String flag, String def) { 120 return flags.getOrDefault(flag, def); 121 } 122 123 public Integer getFlagValue(String flag, Integer def) { 124 return ACFUtil.parseInt(this.flags.get(flag), def); 125 } 126 127 public Long getFlagValue(String flag, Long def) { 128 return ACFUtil.parseLong(this.flags.get(flag), def); 129 } 130 131 public Float getFlagValue(String flag, Float def) { 132 return ACFUtil.parseFloat(this.flags.get(flag), def); 133 } 134 135 public Double getFlagValue(String flag, Double def) { 136 return ACFUtil.parseDouble(this.flags.get(flag), def); 137 } 138 139 public Integer getIntFlagValue(String flag, Number def) { 140 return ACFUtil.parseInt(this.flags.get(flag), def != null ? def.intValue() : null); 141 } 142 143 public Long getLongFlagValue(String flag, Number def) { 144 return ACFUtil.parseLong(this.flags.get(flag), def != null ? def.longValue() : null); 145 } 146 147 public Float getFloatFlagValue(String flag, Number def) { 148 return ACFUtil.parseFloat(this.flags.get(flag), def != null ? def.floatValue() : null); 149 } 150 151 public Double getDoubleFlagValue(String flag, Number def) { 152 return ACFUtil.parseDouble(this.flags.get(flag), def != null ? def.doubleValue() : null); 153 } 154 155 public Boolean getBooleanFlagValue(String flag) { 156 return getBooleanFlagValue(flag, false); 157 } 158 159 public Boolean getBooleanFlagValue(String flag, Boolean def) { 160 String val = this.flags.get(flag); 161 if (val == null) { 162 return def; 163 } 164 return ACFUtil.isTruthy(val); 165 } 166 167 public Double getFlagValue(String flag, Number def) { 168 return ACFUtil.parseDouble(this.flags.get(flag), def != null ? def.doubleValue() : null); 169 } 170 171 /** 172 * This method will not support annotation processors!! use getAnnotationValue or hasAnnotation 173 * @deprecated Use {@link #getAnnotationValue(Class)} 174 */ 175 @Deprecated 176 public <T extends Annotation> T getAnnotation(Class<T> cls) { 177 return param.getParameter().getAnnotation(cls); 178 } 179 180 public <T extends Annotation> String getAnnotationValue(Class<T> cls) { 181 return manager.getAnnotations().getAnnotationValue(param.getParameter(), cls); 182 } 183 184 public <T extends Annotation> String getAnnotationValue(Class<T> cls, int options) { 185 return manager.getAnnotations().getAnnotationValue(param.getParameter(), cls, options); 186 } 187 188 public <T extends Annotation> boolean hasAnnotation(Class<T> cls) { 189 return manager.getAnnotations().hasAnnotation(param.getParameter(), cls); 190 } 191 192 public RegisteredCommand getCmd() { 193 return this.cmd; 194 } 195 196 @UnstableAPI 197 CommandParameter getCommandParameter() { 198 return this.param; 199 } 200 201 @Deprecated 202 public Parameter getParam() { 203 return this.param.getParameter(); 204 } 205 206 public I getIssuer() { 207 return this.issuer; 208 } 209 210 public List<String> getArgs() { 211 return this.args; 212 } 213 214 public int getIndex() { 215 return this.index; 216 } 217 218 public Map<String, Object> getPassedArgs() { 219 return this.passedArgs; 220 } 221 222 public Map<String, String> getFlags() { 223 return this.flags; 224 } 225 226 public String joinArgs() { 227 return ACFUtil.join(args, " "); 228 } 229 public String joinArgs(String sep) { 230 return ACFUtil.join(args, sep); 231 } 232}