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