summaryrefslogtreecommitdiff
path: root/dicore3/command/src/main/java/io/dico/dicore/command/parameter/Parameter.java
blob: 9ce111923d562255d1a86a861fe34bf1c436f58d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package io.dico.dicore.command.parameter;

import io.dico.dicore.command.CommandException;
import io.dico.dicore.command.ExecutionContext;
import io.dico.dicore.command.Validate;
import io.dico.dicore.command.annotation.Range;
import io.dico.dicore.command.parameter.type.ParameterType;
import org.bukkit.Location;

import java.util.List;
import java.util.Objects;

/**
 * IParameter object.
 *
 * @param <TResult>    the parameter's type
 * @param <TParamInfo> the parameter info object. Example: {@link Range.Memory}
 */
public class Parameter<TResult, TParamInfo> {
    private final String name;
    private final String description;
    private final ParameterType<TResult, TParamInfo> parameterType;
    private final TParamInfo paramInfo;
    private final boolean isPrimitive;

    private final boolean flag;
    private final String flagPermission;

    public Parameter(String name, String description, ParameterType<TResult, TParamInfo> parameterType, TParamInfo paramInfo) {
        this(name, description, parameterType, paramInfo, false, null);
    }

    public Parameter(String name, String description, ParameterType<TResult, TParamInfo> parameterType, TParamInfo paramInfo, boolean flag, String flagPermission) {
        this(name, description, parameterType, paramInfo, false, flag, flagPermission);
    }

    public Parameter(String name, String description, ParameterType<TResult, TParamInfo> parameterType, TParamInfo paramInfo, boolean isPrimitive, boolean flag, String flagPermission) {
        this.name = Objects.requireNonNull(name);
        this.description = description == null ? "" : description;
        this.parameterType = flag ? parameterType.asFlagParameter() : parameterType;
        /*
        if (paramInfo == null && parameterType.getParameterConfig() != null) {
            paramInfo = parameterType.getParameterConfig().getDefaultValue();
        }
        */
        this.paramInfo = paramInfo;
        this.isPrimitive = isPrimitive;

        this.flag = flag;
        this.flagPermission = flagPermission;

        if (flag && !name.startsWith("-")) {
            throw new IllegalArgumentException("Flag parameter's name must start with -");
        } else if (!flag && name.startsWith("-")) {
            throw new IllegalArgumentException("Non-flag parameter's name may not start with -");
        }
    }



    public TResult parse(ExecutionContext context, ArgumentBuffer buffer) throws CommandException {
        if (getFlagPermission() != null) {
            Validate.isAuthorized(context.getSender(), getFlagPermission(), "You do not have permission to use the flag " + name);
        }
        return checkAllowed(context, parameterType.parseForContext(this, context, buffer));
    }

    public TResult getDefaultValue(ExecutionContext context, ArgumentBuffer buffer) throws CommandException {
        return parameterType.getDefaultValueForContext(this, context, buffer);
    }

    public List<String> complete(ExecutionContext context, Location location, ArgumentBuffer buffer) {
        return parameterType.completeForContext(this, context, location, buffer);
    }

    public TResult checkAllowed(ExecutionContext context, TResult result) throws CommandException {
        return result;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public ParameterType<TResult, TParamInfo> getType() {
        return parameterType;
    }

    public TParamInfo getParamInfo() {
        return paramInfo;
    }

    public boolean isPrimitive() {
        return isPrimitive;
    }

    public boolean isFlag() {
        return flag;
    }

    // override with false for the flag parameter that simply must be present
    public boolean expectsInput() {
        return parameterType.getExpectedAmountOfConsumedArguments() > 0;
    }

    public String getFlagPermission() {
        return flag ? flagPermission : null;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Parameter)) return false;
        /*
        IParameter<?, ?> parameter = (IParameter<?, ?>) o;
        
        return name.equals(parameter.name);
        */
        return false;
    }

    @Override
    public int hashCode() {
        return name.hashCode();
    }

}