Skip to content

Commit 8f8d7d9

Browse files
committed
Store origin on custom syntaxes
1 parent 41e3383 commit 8f8d7d9

6 files changed

Lines changed: 110 additions & 32 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.skriptlang.reflect.registration;
2+
3+
import org.jetbrains.annotations.ApiStatus;
4+
import org.jetbrains.annotations.Unmodifiable;
5+
import org.skriptlang.skript.docs.Origin;
6+
import org.skriptlang.skript.registration.SyntaxInfo;
7+
import org.skriptlang.skript.registration.SyntaxRegistry;
8+
9+
import java.util.Collection;
10+
11+
// TODO Replace with Skript's when 2.15 is released
12+
@ApiStatus.Internal
13+
public final class OriginApplyingSyntaxRegistry implements SyntaxRegistry {
14+
15+
private final SyntaxRegistry syntaxRegistry;
16+
private final Origin origin;
17+
18+
public OriginApplyingSyntaxRegistry(SyntaxRegistry syntaxRegistry, Origin origin) {
19+
this.syntaxRegistry = syntaxRegistry;
20+
this.origin = origin;
21+
}
22+
23+
@Override
24+
public @Unmodifiable <I extends SyntaxInfo<?>> Collection<I> syntaxes(Key<I> key) {
25+
return syntaxRegistry.syntaxes(key);
26+
}
27+
28+
@Override
29+
public <I extends SyntaxInfo<?>> void register(Key<I> key, I info) {
30+
if (info.origin() == Origin.UNKNOWN) { // when origin is unspecified, add one
31+
//noinspection unchecked
32+
info = (I) info.toBuilder().origin(origin).build();
33+
}
34+
syntaxRegistry.register(key, info);
35+
}
36+
37+
@Override
38+
public void unregister(SyntaxInfo<?> info) {
39+
syntaxRegistry.unregister(info);
40+
}
41+
42+
@Override
43+
public <I extends SyntaxInfo<?>> void unregister(Key<I> key, I info) {
44+
syntaxRegistry.unregister(key, info);
45+
}
46+
47+
@Override
48+
public @Unmodifiable Collection<SyntaxInfo<?>> elements() {
49+
return syntaxRegistry.elements();
50+
}
51+
52+
}
Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.skriptlang.reflect.syntax.custom;
22

3+
import com.btk5h.skriptmirror.SkriptMirror;
4+
import org.skriptlang.reflect.registration.OriginApplyingSyntaxRegistry;
35
import org.skriptlang.reflect.syntax.custom.computedoption.elements.StructComputedOption;
46
import org.skriptlang.reflect.syntax.custom.condition.elements.EffNegateCondition;
57
import org.skriptlang.reflect.syntax.custom.condition.elements.StructCustomCondition;
@@ -12,48 +14,64 @@
1214
import org.skriptlang.reflect.syntax.custom.shared.elements.expressions.*;
1315
import org.skriptlang.skript.addon.AddonModule;
1416
import org.skriptlang.skript.addon.SkriptAddon;
17+
import org.skriptlang.skript.registration.SyntaxRegistry;
18+
19+
import java.util.function.BiConsumer;
20+
import java.util.function.Consumer;
1521

1622
public class CustomSyntaxModule implements AddonModule {
1723

24+
public static ModuleOrigin ORIGIN = AddonModule.origin(SkriptMirror.getAddonInstance(), new CustomSyntaxModule());
25+
1826
@Override
1927
public void load(SkriptAddon addon) {
20-
// ==Computed Option==
21-
StructComputedOption.register(addon.syntaxRegistry());
22-
23-
// ==Custom Condition==
24-
EffNegateCondition.register(addon.syntaxRegistry());
25-
StructCustomCondition.register(addon.syntaxRegistry());
26-
27-
// ==Custom Effect==
28-
EffDelayEffect.register(addon.syntaxRegistry());
29-
StructCustomEffect.register(addon.syntaxRegistry());
30-
31-
// ==Custom Event==
32-
CondEventCancelled.register(addon.syntaxRegistry());
33-
EffCallEvent.register(addon.syntaxRegistry());
34-
ExprCustomEvent.register(addon.syntaxRegistry());
35-
ExprEventData.register(addon.syntaxRegistry());
36-
StructCustomEvent.register(addon.syntaxRegistry());
37-
38-
// ==Custom Expression==
39-
StructCustomExpression.register(addon.syntaxRegistry());
40-
ExprChangeValue.register(addon.syntaxRegistry());
41-
42-
// ==Shared==
43-
EffContinue.register(addon.syntaxRegistry());
44-
ExprEventClasses.register(addon.syntaxRegistry());
45-
ExprExpression.register(addon.syntaxRegistry());
46-
ExprMatchedPattern.register(addon.syntaxRegistry());
47-
ExprParseMark.register(addon.syntaxRegistry());
48-
ExprParseRegexes.register(addon.syntaxRegistry());
49-
ExprParseTags.register(addon.syntaxRegistry());
50-
ExprRawExpression.register(addon.syntaxRegistry());
51-
ExprSelf.register(addon.syntaxRegistry());
28+
SyntaxRegistry registry = new OriginApplyingSyntaxRegistry(addon.syntaxRegistry(), ORIGIN);
29+
register(
30+
registry,
31+
32+
// ==Computed Option==
33+
StructComputedOption::register,
34+
35+
// ==Custom Condition==
36+
EffNegateCondition::register,
37+
StructCustomCondition::register,
38+
39+
// ==Custom Effect==
40+
EffDelayEffect::register,
41+
StructCustomEffect::register,
42+
43+
// ==Custom Event==
44+
CondEventCancelled::register,
45+
EffCallEvent::register,
46+
ExprCustomEvent::register,
47+
ExprEventData::register,
48+
StructCustomEvent::register,
49+
50+
// ==Custom Expression==
51+
StructCustomExpression::register,
52+
ExprChangeValue::register,
53+
54+
// ==Shared==
55+
EffContinue::register,
56+
ExprEventClasses::register,
57+
ExprExpression::register,
58+
ExprMatchedPattern::register,
59+
ExprParseMark::register,
60+
ExprParseRegexes::register,
61+
ExprParseTags::register,
62+
ExprRawExpression::register,
63+
ExprSelf::register
64+
);
5265
}
5366

5467
@Override
5568
public String name() {
5669
return "custom syntax";
5770
}
5871

72+
@SafeVarargs
73+
private static void register(SyntaxRegistry registry, Consumer<SyntaxRegistry>... consumers) {
74+
for (Consumer<SyntaxRegistry> consumer : consumers)
75+
consumer.accept(registry);
76+
}
5977
}

src/main/java/org/skriptlang/reflect/syntax/custom/condition/CustomConditionInfo.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import ch.njol.skript.lang.Trigger;
44
import ch.njol.skript.lang.parser.ParserInstance;
55
import org.jetbrains.annotations.Nullable;
6+
import org.skriptlang.reflect.syntax.custom.CustomSyntaxModule;
67
import org.skriptlang.reflect.syntax.custom.shared.CustomSyntaxInfo;
78
import org.skriptlang.skript.lang.script.Script;
89
import org.skriptlang.skript.registration.SyntaxInfo;
@@ -26,6 +27,7 @@ public CustomConditionInfo(
2627
super(patterns, hasParseSection, script, usableInPredicate);
2728
this.property = property;
2829
this.info = SyntaxInfo.builder(CustomCondition.class)
30+
.origin(CustomSyntaxModule.ORIGIN)
2931
.supplier(this::newInstance)
3032
.addPatterns(patterns)
3133
.priority(priority())

src/main/java/org/skriptlang/reflect/syntax/custom/effect/CustomEffectInfo.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import ch.njol.skript.lang.Trigger;
44
import ch.njol.skript.lang.parser.ParserInstance;
55
import org.jetbrains.annotations.Nullable;
6+
import org.skriptlang.reflect.syntax.custom.CustomSyntaxModule;
67
import org.skriptlang.reflect.syntax.custom.shared.CustomSyntaxInfo;
78
import org.skriptlang.skript.lang.script.Script;
89
import org.skriptlang.skript.registration.SyntaxInfo;
@@ -18,6 +19,7 @@ public class CustomEffectInfo extends CustomSyntaxInfo<CustomEffect> {
1819
public CustomEffectInfo(String[] patterns, boolean hasParseSection, @Nullable Script script, Predicate<ParserInstance> usableInPredicate) {
1920
super(patterns, hasParseSection, script, usableInPredicate);
2021
this.info = SyntaxInfo.builder(CustomEffect.class)
22+
.origin(CustomSyntaxModule.ORIGIN)
2123
.addPatterns(patterns)
2224
.supplier(this::newInstance)
2325
.priority(priority())

src/main/java/org/skriptlang/reflect/syntax/custom/event/CustomEventInfo.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import ch.njol.skript.registrations.EventValues;
66
import com.btk5h.skriptmirror.util.SkriptReflection;
77
import org.jetbrains.annotations.Nullable;
8+
import org.skriptlang.reflect.syntax.custom.CustomSyntaxModule;
89
import org.skriptlang.reflect.syntax.custom.event.CustomEventManager.RegisteredEvent;
910
import org.skriptlang.reflect.syntax.custom.shared.CustomSyntaxInfo;
1011
import org.skriptlang.skript.bukkit.registration.BukkitSyntaxInfos;
@@ -40,6 +41,7 @@ public CustomEventInfo(
4041
this.eventValueTypes = eventValueTypes;
4142
this.registeredEventRef = new WeakReference<>(registeredEvent);
4243
this.info = BukkitSyntaxInfos.Event.builder(CustomEvent.class, identifier)
44+
.origin(CustomSyntaxModule.ORIGIN)
4345
.supplier(this::newInstance)
4446
.addEvent(registeredEvent.eventClass())
4547
.addPatterns(patterns)

src/main/java/org/skriptlang/reflect/syntax/custom/expression/CustomExpressionInfo.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import ch.njol.skript.lang.parser.ParserInstance;
88
import org.bukkit.event.Event;
99
import org.jetbrains.annotations.Nullable;
10+
import org.skriptlang.reflect.syntax.custom.CustomSyntaxModule;
1011
import org.skriptlang.reflect.syntax.custom.shared.CustomSyntaxInfo;
1112
import org.skriptlang.skript.lang.script.Script;
1213
import org.skriptlang.skript.registration.SyntaxInfo;
@@ -33,6 +34,7 @@ public CustomExpressionInfo(String[] patterns, boolean hasParseSection, @Nullabl
3334
this.changeModes = changeModes;
3435
//noinspection unchecked,rawtypes
3536
this.info = (SyntaxInfo.Expression) SyntaxInfo.Expression.builder(CustomExpression.class, returnType)
37+
.origin(CustomSyntaxModule.ORIGIN)
3638
.addPatterns(patterns)
3739
.supplier(this::newInstance)
3840
.priority(priority())

0 commit comments

Comments
 (0)