Skip to content

Commit 163957a

Browse files
Merge branch 'dev/patch' into dev/feature
2 parents 92ea365 + 1cf2b8f commit 163957a

File tree

6 files changed

+113
-11
lines changed

6 files changed

+113
-11
lines changed

src/main/java/ch/njol/skript/lang/EffectSection.java

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
*/
2424
public abstract class EffectSection extends Section {
2525

26+
static {
27+
ParserInstance.registerData(EffectSectionContext.class, EffectSectionContext::new);
28+
}
29+
2630
private boolean hasSection;
2731

2832
public boolean hasSection() {
@@ -34,10 +38,23 @@ public boolean hasSection() {
3438
*/
3539
@Override
3640
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
37-
SectionContext sectionContext = getParser().getData(SectionContext.class);
41+
ParserInstance parser = getParser();
42+
SectionContext sectionContext = parser.getData(SectionContext.class);
43+
EffectSectionContext effectSectionContext = parser.getData(EffectSectionContext.class);
44+
SectionNode sectionNode = sectionContext.sectionNode;
45+
if (!effectSectionContext.isNodeForEffectSection) {
46+
sectionContext.sectionNode = null;
47+
}
48+
3849
//noinspection ConstantConditions - For an EffectSection, it may be null
3950
hasSection = sectionContext.sectionNode != null;
40-
return super.init(expressions, matchedPattern, isDelayed, parseResult);
51+
boolean result = super.init(expressions, matchedPattern, isDelayed, parseResult);
52+
53+
if (!effectSectionContext.isNodeForEffectSection) {
54+
sectionContext.sectionNode = sectionNode;
55+
}
56+
57+
return result;
4158
}
4259

4360
@Override
@@ -52,15 +69,48 @@ public abstract boolean init(Expression<?>[] expressions,
5269
* Similar to {@link Section#parse(String, String, SectionNode, List)}, but will only attempt to parse from other {@link EffectSection}s.
5370
*/
5471
public static @Nullable EffectSection parse(String input, @Nullable String defaultError, @Nullable SectionNode sectionNode, @Nullable List<TriggerItem> triggerItems) {
55-
SectionContext sectionContext = ParserInstance.get().getData(SectionContext.class);
72+
return parse(input, defaultError, sectionNode, true, triggerItems);
73+
}
74+
75+
/**
76+
* Similar to {@link Section#parse(String, String, SectionNode, List)}, but will only attempt to parse from other {@link EffectSection}s.
77+
* @param isNodeForEffectSection Whether {@code sectionNode} can be {@link SectionContext#claim(SyntaxElement)}-ed by the parsed EffectSection.
78+
*/
79+
public static @Nullable EffectSection parse(String input, @Nullable String defaultError, SectionNode sectionNode, boolean isNodeForEffectSection, List<TriggerItem> triggerItems) {
80+
ParserInstance parser = ParserInstance.get();
81+
SectionContext sectionContext = parser.getData(SectionContext.class);
82+
EffectSectionContext effectSectionContext = parser.getData(EffectSectionContext.class);
83+
boolean wasNodeForEffectSection = effectSectionContext.isNodeForEffectSection;
84+
effectSectionContext.isNodeForEffectSection = isNodeForEffectSection;
5685

57-
return sectionContext.modify(sectionNode, triggerItems, () -> {
86+
EffectSection effectSection = sectionContext.modify(sectionNode, triggerItems, () -> {
5887
var iterator = Skript.instance().syntaxRegistry().syntaxes(org.skriptlang.skript.registration.SyntaxRegistry.SECTION).stream()
59-
.filter(info -> EffectSection.class.isAssignableFrom(info.type()))
60-
.iterator();
88+
.filter(info -> EffectSection.class.isAssignableFrom(info.type()))
89+
.iterator();
6190
//noinspection unchecked,rawtypes
62-
return (EffectSection) SkriptParser.parse(input, (Iterator) iterator, defaultError);
91+
EffectSection parsed = (EffectSection) SkriptParser.parse(input, (Iterator) iterator, defaultError);
92+
if (parsed != null && sectionNode != null && !sectionContext.claimed()) {
93+
Skript.error("The line '" + input + "' is a valid statement but cannot function as a section (:) because there is no syntax in the line to manage it.");
94+
return null;
95+
}
96+
return parsed;
6397
});
98+
99+
effectSectionContext.isNodeForEffectSection = wasNodeForEffectSection;
100+
return effectSection;
101+
}
102+
103+
private static class EffectSectionContext extends ParserInstance.Data {
104+
105+
/**
106+
* Whether the {@link SectionContext#sectionNode} can be used by the initializing {@link EffectSection}.
107+
*/
108+
public boolean isNodeForEffectSection = true;
109+
110+
public EffectSectionContext(ParserInstance parserInstance) {
111+
super(parserInstance);
112+
}
113+
64114
}
65115

66116
}

src/main/java/ch/njol/skript/lang/Statement.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ public abstract class Statement extends TriggerItem implements SyntaxElement {
3333
Section.SectionContext sectionContext = ParserInstance.get().getData(Section.SectionContext.class);
3434
EffFunctionCall functionCall;
3535
if (node != null) {
36-
functionCall = sectionContext.modify(node, items, () -> EffFunctionCall.parse(input));
36+
functionCall = sectionContext.modify(node, items, () -> {
37+
EffFunctionCall parsed = EffFunctionCall.parse(input);
38+
if (parsed != null && !sectionContext.claimed()) {
39+
Skript.error("The line '" + input + "' is a valid function call but cannot function as a section (:) because there is no parameter to manage it.");
40+
return null;
41+
}
42+
return parsed;
43+
});
3744
} else {
3845
functionCall = EffFunctionCall.parse(input);
3946
}
@@ -46,7 +53,7 @@ public abstract class Statement extends TriggerItem implements SyntaxElement {
4653
}
4754
log.clear();
4855

49-
EffectSection section = EffectSection.parse(input, null, null, items);
56+
EffectSection section = EffectSection.parse(input, null, node, false, items);
5057
if (section != null) {
5158
log.printLog();
5259
return new EffectSectionEffect(section);

src/main/java/org/skriptlang/skript/lang/arithmetic/OperationInfo.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.skriptlang.skript.lang.arithmetic;
22

33
import com.google.common.base.MoreObjects;
4+
import com.google.common.base.Preconditions;
5+
import org.jetbrains.annotations.NotNull;
46
import org.jetbrains.annotations.Nullable;
57
import org.skriptlang.skript.lang.converter.Converters;
68

@@ -18,6 +20,22 @@
1820
*/
1921
public record OperationInfo<L, R, T>(Class<L> left, Class<R> right, Class<T> returnType, Operation<L, R, T> operation) {
2022

23+
public OperationInfo(
24+
@NotNull Class<L> left,
25+
@NotNull Class<R> right,
26+
@NotNull Class<T> returnType,
27+
@NotNull Operation<L, R, T> operation
28+
) {
29+
Preconditions.checkNotNull(left, "Cannot do arithmetic with nothing and something! (left is null)");
30+
Preconditions.checkNotNull(right, "Cannot do arithmetic with something and nothing! (right is null)");
31+
Preconditions.checkNotNull(returnType, "Cannot have nothing as the result of arithmetic! (returnType is null)");
32+
Preconditions.checkNotNull(operation, "Cannot do arithmetic with a null operation!");
33+
this.left = left;
34+
this.right = right;
35+
this.returnType = returnType;
36+
this.operation = operation;
37+
}
38+
2139
@Deprecated(since = "2.13", forRemoval = true)
2240
public Class<L> getLeft() {
2341
return left;

src/main/java/org/skriptlang/skript/lang/comparator/ComparatorInfo.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.skriptlang.skript.lang.comparator;
22

3+
import com.google.common.base.Preconditions;
4+
import org.jetbrains.annotations.NotNull;
5+
36
/**
47
* Holds information about a Comparator.
58
*
@@ -12,7 +15,14 @@ public final class ComparatorInfo<T1, T2> {
1215
private final Class<T2> secondType;
1316
private final Comparator<T1, T2> comparator;
1417

15-
ComparatorInfo(Class<T1> firstType, Class<T2> secondType, Comparator<T1, T2> comparator) {
18+
ComparatorInfo(
19+
@NotNull Class<T1> firstType,
20+
@NotNull Class<T2> secondType,
21+
@NotNull Comparator<T1, T2> comparator
22+
) {
23+
Preconditions.checkNotNull(firstType, "Cannot create a comparison between nothing and something! (firstType is null)");
24+
Preconditions.checkNotNull(secondType, "Cannot create a comparison between something and nothing! (secondType is null)");
25+
Preconditions.checkNotNull(comparator, "Cannot create a comparison with a null comparator!");
1626
this.firstType = firstType;
1727
this.secondType = secondType;
1828
this.comparator = comparator;

src/main/java/org/skriptlang/skript/lang/converter/ConverterInfo.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.skriptlang.skript.lang.converter;
22

3+
import com.google.common.base.Preconditions;
4+
import org.jetbrains.annotations.NotNull;
5+
36
/**
47
* Holds information about a {@link Converter}.
58
*
@@ -13,7 +16,15 @@ public final class ConverterInfo<F, T> {
1316
private final Converter<F, T> converter;
1417
private final int flags;
1518

16-
public ConverterInfo(Class<F> from, Class<T> to, Converter<F, T> converter, int flags) {
19+
public ConverterInfo(
20+
@NotNull Class<F> from,
21+
@NotNull Class<T> to,
22+
@NotNull Converter<F, T> converter,
23+
int flags
24+
) {
25+
Preconditions.checkNotNull(from, "Cannot convert from nothing to something! (from is null)");
26+
Preconditions.checkNotNull(to, "Cannot convert from something to nothing! (to is null)");
27+
Preconditions.checkNotNull(converter, "Cannot convert using a null converter!");
1728
this.from = from;
1829
this.to = to;
1930
this.converter = converter;

src/test/skript/tests/regressions/8199-parse exprsecs in function args.sk

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@ local function f(x: worldborder):
44
test "load expr secs in functions":
55
f(a worldborder):
66
set worldborder warning time to 20 ticks
7+
8+
# ensure it fails for unclaimed sections
9+
parse:
10+
f({_null}):
11+
stop
12+
assert first element of last parse logs contains "is a valid function call but cannot function as a section" with "failed to correctly error for unclaimed section"

0 commit comments

Comments
 (0)