-
-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy pathArgument.java
More file actions
153 lines (131 loc) · 4.43 KB
/
Copy pathArgument.java
File metadata and controls
153 lines (131 loc) · 4.43 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package ch.njol.skript.command;
import java.util.WeakHashMap;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;
import ch.njol.skript.Skript;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.Variable;
import ch.njol.skript.lang.VariableString;
import ch.njol.skript.lang.util.SimpleLiteral;
import ch.njol.skript.log.RetainingLogHandler;
import ch.njol.skript.log.SkriptLogger;
import ch.njol.skript.util.Utils;
import ch.njol.skript.variables.Variables;
/**
* Represents an argument of a command
*
* @author Peter Güttinger
* @deprecated There is no direct replacement for this class.
* The closest alternative is {@link org.skriptlang.skript.bukkit.command.custom.ArgumentData}.
*/
@Deprecated(since = "INSERT VERSION", forRemoval = true)
public class Argument<T> {
@Nullable
private final String name;
@Nullable
private final Expression<? extends T> def;
private final ClassInfo<T> type;
private final boolean single;
private final int index;
private final boolean optional;
private transient WeakHashMap<Event, T[]> current = new WeakHashMap<>();
private Argument(@Nullable final String name, final @Nullable Expression<? extends T> def, final ClassInfo<T> type, final boolean single, final int index, final boolean optional) {
this.name = name;
this.def = def;
this.type = type;
this.single = single;
this.index = index;
this.optional = optional;
}
@SuppressWarnings("unchecked")
@Nullable
public static <T> Argument<T> newInstance(@Nullable final String name, final ClassInfo<T> type, final @Nullable String def, final int index, final boolean single, final boolean forceOptional) {
if (name != null && !Variable.isValidVariableName(name, false, false)) {
Skript.error("An argument's name must be a valid variable name, and cannot be a list variable.");
return null;
}
Expression<? extends T> d = null;
if (def != null) {
if (def.startsWith("%") && def.endsWith("%")) {
final RetainingLogHandler log = SkriptLogger.startRetainingLog();
try {
d = new SkriptParser("" + def.substring(1, def.length() - 1), SkriptParser.PARSE_EXPRESSIONS, ParseContext.COMMAND).parseExpression(type.getC());
if (d == null) {
log.printErrors("Can't understand this expression: " + def + "");
return null;
}
log.printLog();
} finally {
log.stop();
}
} else {
final RetainingLogHandler log = SkriptLogger.startRetainingLog();
try {
if (type.getC() == String.class) {
if (def.startsWith("\"") && def.endsWith("\""))
d = (Expression<? extends T>) VariableString.newInstance("" + def.substring(1, def.length() - 1));
else
d = (Expression<? extends T>) new SimpleLiteral<>(def, false);
} else {
d = new SkriptParser(def, SkriptParser.PARSE_LITERALS, ParseContext.DEFAULT).parseExpression(type.getC());
}
if (d == null) {
log.printErrors("Can't understand this expression: '" + def + "'");
return null;
}
log.printLog();
} finally {
log.stop();
}
}
}
return new Argument<>(name, d, type, single, index, def != null || forceOptional);
}
@Override
public String toString() {
final Expression<? extends T> def = this.def;
return "<" + (name != null ? name + ": " : "") + Utils.toEnglishPlural(type.getCodeName(), !single) + (def == null ? "" : " = " + def.toString()) + ">";
}
public boolean isOptional() {
return optional;
}
public void setToDefault(final ScriptCommandEvent event) {
if (def != null)
set(event, def.getArray(event));
}
@SuppressWarnings("unchecked")
public void set(final ScriptCommandEvent e, final Object[] o) {
if (!(type.getC().isAssignableFrom(o.getClass().getComponentType())))
throw new IllegalArgumentException();
current.put(e, (T[]) o);
final String name = this.name;
if (name != null) {
if (single) {
if (o.length > 0)
Variables.setVariable(name, o[0], e, true);
} else {
for (int i = 0; i < o.length; i++)
Variables.setVariable(name + "::" + (i + 1), o[i], e, true);
}
}
}
@Nullable
public T[] getCurrent(final Event e) {
return current.get(e);
}
public @Nullable String getName() {
return name;
}
public Class<T> getType() {
return type.getC();
}
public int getIndex() {
return index;
}
public boolean isSingle() {
return single;
}
}