forked from Mwexim/skript-parser
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExprContextValue.java
More file actions
166 lines (145 loc) · 7.01 KB
/
ExprContextValue.java
File metadata and controls
166 lines (145 loc) · 7.01 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
154
155
156
157
158
159
160
161
162
163
164
165
166
package io.github.syst3ms.skriptparser.expressions;
import io.github.syst3ms.skriptparser.Parser;
import io.github.syst3ms.skriptparser.lang.Expression;
import io.github.syst3ms.skriptparser.lang.TriggerContext;
import io.github.syst3ms.skriptparser.log.ErrorContext;
import io.github.syst3ms.skriptparser.log.ErrorType;
import io.github.syst3ms.skriptparser.log.SkriptLogger;
import io.github.syst3ms.skriptparser.parsing.MatchContext;
import io.github.syst3ms.skriptparser.parsing.ParseContext;
import io.github.syst3ms.skriptparser.parsing.ParserState;
import io.github.syst3ms.skriptparser.pattern.PatternElement;
import io.github.syst3ms.skriptparser.pattern.PatternParser;
import io.github.syst3ms.skriptparser.registration.context.ContextValue;
import io.github.syst3ms.skriptparser.registration.context.ContextValue.State;
import io.github.syst3ms.skriptparser.registration.context.ContextValues;
import io.github.syst3ms.skriptparser.types.changers.ChangeMode;
import org.jetbrains.annotations.Contract;
import java.lang.reflect.Array;
import java.util.Optional;
public class ExprContextValue<T, C extends TriggerContext> implements Expression<T> {
private static final String PATTERN = "[the] [(1:(past|previous)|2:(future|next))] [ctx:(context|event)-]<.+>";
private static final PatternElement CONTEXT_VALUE_PATTERN = PatternParser.parsePattern(PATTERN, new SkriptLogger()).orElseThrow();
static {
//noinspection unchecked
Parser.getMainRegistration().newExpression(ExprContextValue.class, Object.class, false,
PATTERN)
.name("Context/Event Value")
.description("Returns a value from the current context/event.")
.examples("set {_var} to context-player")
.since("1.0.0")
.register();
}
private ContextValue<C, T> info;
private String value;
private boolean alone;
@SuppressWarnings("unchecked")
@Override
@Contract("_, _, _ -> fail")
public boolean init(Expression<?>[] expressions, int matchedPattern, ParseContext parseContext) {
boolean alone = !parseContext.hasMark("ctx");
String toParse = parseContext.getMatches().getFirst().group();
String value = parseContext.getMatches().getFirst().group();
State state = switch (parseContext.getNumericMark()) {
case 0 -> State.PRESENT;
case 1 -> State.PAST;
case 2 -> State.FUTURE;
default -> throw new IllegalArgumentException("Invalid state index: " + parseContext.getNumericMark());
};
ParserState parserState = parseContext.getParserState();
SkriptLogger logger = parseContext.getLogger();
var matchContext = new MatchContext(CONTEXT_VALUE_PATTERN, parserState, logger);
for (Class<? extends TriggerContext> ctx : parserState.getCurrentContexts()) {
for (var info : ContextValues.getContextValues(ctx)) {
matchContext = new MatchContext(info.getPattern(), parserState, logger);
// Checking all conditions, so no false results slip through.
if (info.getPattern().match(value, 0, matchContext) != value.length()) {
continue;
} else if (!info.getUsage().isCorrect(alone)) {
if (alone) {
logger.error(
"The context value matching '" + toParse + "' cannot be used alone",
ErrorType.SEMANTIC_ERROR,
"Use 'context-" + toParse + "' instead of using just '" + toParse + "' alone"
);
} else {
logger.error(
"The context value matching '" + toParse + "' must be used alone",
ErrorType.SEMANTIC_ERROR,
"Instead of 'context-" + toParse + "', use this context value as '" + toParse + "'"
);
}
return false;
} else if (state != info.getState()) {
// TODO might find a way to re-implement this later
//logger.error("The time state of this context value (" + state.toString().toLowerCase() + ") is incorrect", ErrorType.SEMANTIC_ERROR);
continue;
} else if (parserState.isRestrictingExpressions() && parserState.forbidsSyntax(ExprContextValue.class)) {
logger.setContext(ErrorContext.RESTRICTED_SYNTAXES);
logger.error("The enclosing section does not allow the use of context expressions.", ErrorType.SEMANTIC_ERROR);
return false;
}
this.info = (ContextValue<C, T>) info;
this.value = value;
this.alone = alone;
return true;
}
}
if (!alone) {
logger.error("No context value matching '" + toParse + "' was found", ErrorType.SEMANTIC_ERROR);
}
return false;
}
@SuppressWarnings("unchecked")
@Override
public T[] getValues(TriggerContext ctx) {
if (!this.info.getContext().isAssignableFrom(ctx.getClass())) {
return (T[]) Array.newInstance(getReturnType(), 0);
}
assert this.info.getContext().isInstance(ctx);
if (this.info.isSingle()) {
T apply = this.info.getSingleFunction().apply((C) ctx);
T[] array = (T[]) Array.newInstance(getReturnType(), 1);
array[0] = apply;
return array;
} else {
return this.info.getListFunction().apply((C) ctx);
}
}
@Override
public Optional<Class<?>[]> acceptsChange(ChangeMode mode) {
if (mode == ChangeMode.SET && this.info.canBeSet()) {
if (this.info.getListSetterFunction() != null) {
return Optional.of(new Class<?>[]{getReturnType().arrayType()});
} else {
return Optional.of(new Class<?>[]{getReturnType()});
}
}
return Optional.empty();
}
@SuppressWarnings("unchecked")
@Override
public void change(TriggerContext ctx, ChangeMode changeMode, Object[] changeWith) {
if (changeMode != ChangeMode.SET) return;
if (this.info.isSingle()) {
this.info.getSingleSetterFunction().accept((C) ctx, (T) changeWith[0]);
} else {
T[] o = (T[])Array.newInstance(getReturnType(), changeWith.length);
System.arraycopy(changeWith, 0, o, 0, changeWith.length);
this.info.getListSetterFunction().accept((C) ctx, o);
}
}
@Override
public boolean isSingle() {
return this.info.isSingle();
}
@Override
public Class<? extends T> getReturnType() {
return this.info.getReturnType().getType().getTypeClass();
}
@Override
public String toString(TriggerContext ctx, boolean debug) {
return new String[]{"past ", "", "future "}[this.info.getState().ordinal()] +
(this.alone ? "" : "context-") + this.value;
}
}