Skip to content

Commit ec826f0

Browse files
committed
Event-values in custom events now have changer support
Added api-version to plugin.yml Added docs for `usable in` section in custom syntax
1 parent 148f04e commit ec826f0

File tree

11 files changed

+83
-26
lines changed

11 files changed

+83
-26
lines changed

docs/advanced/custom-syntax/conditions.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
{% code-tabs-item title="Syntax" %}
77
```text
88
[local] condition <pattern>:
9+
usable in:
10+
# events, optional
911
parse:
1012
# code, optional
1113
check:
@@ -20,6 +22,8 @@
2022
{% code-tabs-item title="Syntax" %}
2123
```text
2224
[local] condition:
25+
usable in:
26+
# events, optional
2327
patterns:
2428
# patterns, one per line
2529
parse:
@@ -36,6 +40,8 @@
3640
{% code-tabs-item title="Syntax" %}
3741
```text
3842
[local] <skript type> property condition <pattern>:
43+
usable in:
44+
# events, optional
3945
parse:
4046
# code, optional
4147
check:
@@ -54,11 +60,17 @@ Specifying that a condition is `local` makes the condition only usable from with
5460
Local conditions are guaranteed to be parsed before other custom conditions, but not necessarily before conditions from other addons.
5561
{% endhint %}
5662

63+
### Section `usable in`
64+
65+
Each entry in this section should be either an imported class or a custom event \(syntax: `custom event %string%`\).
66+
67+
This condition will error if it is used outside of all the given events.
68+
5769
### Section `parse`
5870

5971
Code in this section is executed whenever the condition is parsed. This section may be used to emit errors if the condition is used in an improper context.
6072

61-
If this section is included, you must also [`continue`](./#continue) if the effect was parsed successfully.
73+
If this section is included, you must also [`continue`](README.md#continue) if the effect was parsed successfully.
6274

6375
{% hint style="info" %}
6476
Local variables created in this section are copied by-value to other sections.
@@ -82,5 +94,5 @@ condition example:
8294

8395
### Section `check`
8496

85-
Code in this section is executed whenever the condition is checked. This section must [`continue`](./#continue) if the condition is met. The section may exit without continuing if the condition fails.
97+
Code in this section is executed whenever the condition is checked. This section must [`continue`](README.md#continue) if the condition is met. The section may exit without continuing if the condition fails.
8698

docs/advanced/custom-syntax/effects.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
{% code-tabs-item title="Syntax" %}
77
```text
88
[local] effect <pattern>:
9+
usable in:
10+
# events, optional
911
parse:
1012
# code, optional
1113
trigger:
@@ -20,6 +22,8 @@
2022
{% code-tabs-item title="Syntax" %}
2123
```text
2224
[local] effect:
25+
usable in:
26+
# events, optional
2327
patterns:
2428
# patterns, one per line
2529
parse:
@@ -40,11 +44,17 @@ Specifying that an effect is `local` makes the effect only usable from within th
4044
Local effects are guaranteed to be parsed before other custom effects, but not necessarily before effects from other addons.
4145
{% endhint %}
4246

47+
### Section `usable in`
48+
49+
Each entry in this section should be either an imported class or a custom event \(syntax: `custom event %string%`\).
50+
51+
This condition will error if it is used outside of all the given events.
52+
4353
### Section `parse`
4454

4555
Code in this section is executed whenever the effect is parsed. This section may be used to emit errors if the effect is used in an improper context.
4656

47-
If this section is included, you must also [`continue`](./#continue) if the effect was parsed successfully.
57+
If this section is included, you must also [`continue`](README.md#continue) if the effect was parsed successfully.
4858

4959
{% hint style="info" %}
5060
Local variables created in this section are copied by-value to other sections.

docs/advanced/custom-syntax/events.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,7 @@ In the event itself, you can get the extra data with the data expression:
121121
In the syntax above, `%strings%` is the index. This doesn't have to be plural, but can be.
122122

123123
{% hint style="info" %}
124-
It may look fancier to create a custom expression instead of using extra data. To do so, you need to call `event.getData(%index%)` to get the data value.
124+
It may look fancier to create a custom expression instead of using extra data.
125+
To do so, you need to call `event.getData(%index%)` to get the data value.
126+
See the `usable in` sections in [effects](effects.md#section-usable-in), [conditions](conditions.md#section-usable-in) and [expressions](expressions.md#section-usable-in).
125127
{% endhint %}

docs/advanced/custom-syntax/expressions.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
[local] [(plural|non(-|[ ])single))] expression <pattern>:
99
return type: <skript type (cannot be a java type)> # optional
1010
loop of: <text> # optional
11+
usable in:
12+
# events, optional
1113
parse:
1214
# code, optional
1315
get:
@@ -37,6 +39,8 @@
3739
patterns:
3840
# patterns, one per line
3941
return type: <skript type (cannot be a java type)> # optional
42+
usable in:
43+
# events, optional
4044
parse:
4145
# code, optional
4246
get:
@@ -64,6 +68,8 @@
6468
```text
6569
[local] <skript types> property <pattern>:
6670
return type: <skript type> # optional
71+
usable in:
72+
# events, optional
6773
parse:
6874
# code, optional
6975
get:
@@ -149,11 +155,17 @@ on script load:
149155
{% endcode-tabs-item %}
150156
{% endcode-tabs %}
151157

158+
### Section `usable in`
159+
160+
Each entry in this section should be either an imported class or a custom event \(syntax: `custom event %string%`\).
161+
162+
This condition will error if it is used outside of all the given events.
163+
152164
### Section `parse`
153165

154166
Code in this section is executed whenever the effect is parsed. This section may be used to emit errors if the effect is used in an improper context.
155167

156-
If this section is included, you must also [`continue`](./#continue) if the effect was parsed successfully.
168+
If this section is included, you must also [`continue`](README.md#continue) if the effect was parsed successfully.
157169

158170
{% hint style="info" %}
159171
Local variables created in this section are copied by-value to other sections.
@@ -177,7 +189,7 @@ expression example:
177189

178190
### Section `get`
179191

180-
Code in this section is executed whenever the expression's value is read. This section must [return](expressions.md#return) a value and must not contain delays.
192+
Code in this section is executed whenever the expression's value is read. This section must [return](#return) a value and must not contain delays.
181193

182194
#### Return
183195

src/main/java/com/btk5h/skriptmirror/skript/custom/CustomSyntaxSection.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ protected boolean handleUsableSection(SectionNode sectionNode, Map<T, List<Suppl
246246
String usableKey = usableNode.getKey();
247247
assert usableKey != null;
248248

249-
249+
Supplier<Boolean> supplier;
250250
if (usableKey.startsWith("custom event ")) {
251251
String customEventString = usableKey.substring("custom event ".length());
252252
VariableString variableString = VariableString.newInstance(
@@ -256,15 +256,12 @@ protected boolean handleUsableSection(SectionNode sectionNode, Map<T, List<Suppl
256256
return false;
257257
} else {
258258
String identifier = variableString.toString(null);
259-
whichInfo.forEach(which ->
260-
usableSuppliers.computeIfAbsent(which, (whichIndex) -> new ArrayList<>())
261-
.add(() -> {
262-
if (!ScriptLoader.isCurrentEvent(BukkitCustomEvent.class))
263-
return false;
264-
EventSyntaxInfo eventWhich = CustomEvent.lastWhich;
265-
return CustomEventUtils.getName(eventWhich).equalsIgnoreCase(identifier);
266-
})
267-
);
259+
supplier = () -> {
260+
if (!ScriptLoader.isCurrentEvent(BukkitCustomEvent.class))
261+
return false;
262+
EventSyntaxInfo eventWhich = CustomEvent.lastWhich;
263+
return CustomEventUtils.getName(eventWhich).equalsIgnoreCase(identifier);
264+
};
268265
}
269266
} else {
270267
JavaType javaType = CustomImport.lookup(currentScript, usableKey);
@@ -275,11 +272,12 @@ protected boolean handleUsableSection(SectionNode sectionNode, Map<T, List<Suppl
275272
}
276273
Class<? extends Event> eventClass = (Class<? extends Event>) javaClass;
277274

278-
whichInfo.forEach(which ->
279-
usableSuppliers.computeIfAbsent(which, (whichIndex) -> new ArrayList<>())
280-
.add(() -> ScriptLoader.isCurrentEvent(eventClass))
281-
);
275+
supplier = () -> ScriptLoader.isCurrentEvent(eventClass);
282276
}
277+
whichInfo.forEach(which ->
278+
usableSuppliers.computeIfAbsent(which, (whichIndex) -> new ArrayList<>())
279+
.add(supplier)
280+
);
283281
}
284282
return true;
285283
}

src/main/java/com/btk5h/skriptmirror/skript/custom/EffContinue.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ protected TriggerItem walk(Event e) {
3030
TriggerItem.walk(((EffectTriggerEvent) e).getNext(),
3131
((EffectTriggerEvent) e).getDirectEvent());
3232
}
33+
// TODO make ContinuableEvent interface / abstract class or sth alike
3334
} else if (e instanceof ConditionCheckEvent) {
3435
((ConditionCheckEvent) e).markContinue();
3536
} else if (e instanceof SyntaxParseEvent) {

src/main/java/com/btk5h/skriptmirror/skript/custom/event/BukkitCustomEvent.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*
1717
* If extra data is needed, {@link #setData(String, Object)} and {@link #getData(String)} can be used.
1818
*/
19+
// TODO generics for getters / setters
20+
// TODO do more checks for init return true vs SkriptUtil#canInitSafely
1921
public class BukkitCustomEvent extends Event implements Cancellable {
2022

2123
private static final HandlerList HANDLERS = new HandlerList();

src/main/java/com/btk5h/skriptmirror/skript/custom/event/EffCallEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class EffCallEvent extends Effect {
2121
@Override
2222
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
2323
eventExpr = SkriptUtil.defendExpression(exprs[0]);
24-
return true;
24+
return SkriptUtil.canInitSafely(eventExpr);
2525
}
2626

2727
@Override

src/main/java/com/btk5h/skriptmirror/skript/custom/event/ExprCustomEvent.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,16 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
5050
}
5151
}
5252

53-
return true;
53+
return SkriptUtil.canInitSafely(customEventName, eventValueVarList, dataVarList);
5454
}
5555

5656
@Nullable
5757
@Override
5858
protected Event[] get(Event e) {
59-
BukkitCustomEvent bukkitCustomEvent = new BukkitCustomEvent(this.customEventName.getSingle(e));
59+
String name = this.customEventName.getSingle(e);
60+
if (name == null)
61+
return null;
62+
BukkitCustomEvent bukkitCustomEvent = new BukkitCustomEvent(name);
6063

6164
if (eventValueVarList != null)
6265
eventValueVarList.variablesIterator(e).forEachRemaining(pair -> {

src/main/java/com/btk5h/skriptmirror/skript/custom/event/ExprCustomEventValue.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import ch.njol.skript.ScriptLoader;
44
import ch.njol.skript.Skript;
5+
import ch.njol.skript.classes.Changer;
56
import ch.njol.skript.classes.ClassInfo;
67
import ch.njol.skript.expressions.base.EventValueExpression;
78
import ch.njol.skript.lang.Expression;
@@ -22,13 +23,15 @@ public class ExprCustomEventValue<T> extends EventValueExpression<T> {
2223
Skript.registerExpression(ExprCustomEventValue.class, Object.class, ExpressionType.PATTERN_MATCHES_EVERYTHING, "[the] [event-]<.+>");
2324
}
2425

25-
private ClassInfo<?> classInfo;
26+
private ClassInfo<? super T> classInfo;
27+
private Changer<? super T> changer;
2628

2729
@SuppressWarnings("unchecked")
2830
public ExprCustomEventValue() {
2931
super((Class<? extends T>) Object.class);
3032
}
3133

34+
@SuppressWarnings("unchecked")
3235
@Override
3336
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final SkriptParser.ParseResult parseResult) {
3437
if (!ScriptLoader.isCurrentEvent(BukkitCustomEvent.class, EventTriggerEvent.class))
@@ -38,7 +41,7 @@ public boolean init(final Expression<?>[] exprs, final int matchedPattern, final
3841
return false;
3942

4043
String stringClass = parseResult.regexes.get(0).group();
41-
classInfo = Classes.getClassInfoFromUserInput(stringClass);
44+
classInfo = (ClassInfo<? super T>) Classes.getClassInfoFromUserInput(stringClass);
4245
if (classInfo == null) {
4346
return false;
4447
}
@@ -69,9 +72,22 @@ public T[] get(Event event) {
6972
return tArray;
7073
}
7174

75+
@Override
76+
public Class<?>[] acceptChange(Changer.ChangeMode mode) {
77+
changer = classInfo.getChanger();
78+
return changer == null ? null : changer.acceptChange(mode);
79+
}
80+
81+
@Override
82+
public void change(Event e, Object[] delta, Changer.ChangeMode mode) {
83+
if (changer == null)
84+
throw new UnsupportedOperationException();
85+
Changer.ChangerUtils.change(changer, getArray(e), delta, mode);
86+
}
87+
7288
@Override
7389
public String toString(final @Nullable Event e, final boolean debug) {
74-
return "event-" + this.getReturnType();
90+
return "event-" + classInfo.getCodeName();
7591
}
7692

7793
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)