Skip to content

Commit 5934b65

Browse files
Adjust SecConditional behavior
1 parent deebb9c commit 5934b65

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

src/main/java/ch/njol/skript/sections/SecConditional.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public boolean init(Expression<?>[] exprs,
111111
* set {_uh oh} to true
112112
*/
113113
SecConditional precedingConditional = getPrecedingConditional(triggerItems, null);
114-
if (precedingConditional == null || !precedingConditional.multiline) {
114+
if (precedingConditional == null || !precedingConditional.multiline || precedingConditional.type == ConditionalType.THEN) {
115115
Skript.error("'then' has to placed just after a multiline 'if' or 'else if' section");
116116
return false;
117117
}
@@ -123,8 +123,6 @@ public boolean init(Expression<?>[] exprs,
123123
Skript.error("'else if' has to be placed just after another 'if' or 'else if' section");
124124
} else if (type == ConditionalType.ELSE) {
125125
Skript.error("'else' has to be placed just after another 'if' or 'else if' section");
126-
} else if (type == ConditionalType.THEN) {
127-
Skript.error("'then' has to placed just after a multiline 'if' or 'else if' section");
128126
}
129127
return false;
130128
}
@@ -386,27 +384,28 @@ private static List<SecConditional> getPrecedingConditionals(List<TriggerItem> t
386384
TriggerItem triggerItem = triggerItems.get(i);
387385
if (!(triggerItem instanceof SecConditional conditional))
388386
break;
389-
if (conditional.type == ConditionalType.ELSE)
390-
// if the conditional is an else, break because it belongs to a different condition and ends
391-
// this one
392-
break;
393387
conditionals.add(conditional);
388+
if (conditional.type == ConditionalType.IF)
389+
// if the conditional is an if, break because it is the root of our conditional
390+
break;
394391
}
395392
return conditionals;
396393
}
397394

398-
private static @Nullable Kleenean getPrecedingShouldDelayAfter(List<TriggerItem> triggerItems) {
395+
private @Nullable Kleenean getPrecedingShouldDelayAfter(List<TriggerItem> triggerItems) {
396+
if (this.type == ConditionalType.IF)
397+
// this is the head, meaning there is no preceding conditional
398+
return null;
399399
// loop through the triggerItems in reverse order so that we find the most recent items first
400400
for (int i = triggerItems.size() - 1; i >= 0; i--) {
401401
TriggerItem triggerItem = triggerItems.get(i);
402-
if (!(triggerItem instanceof SecConditional conditional))
402+
if (!(triggerItem instanceof SecConditional precedingSecConditional))
403403
break;
404-
if (conditional.type == ConditionalType.ELSE)
405-
// if the conditional is an else, break because it belongs to a different condition and ends
406-
// this one
404+
if (precedingSecConditional.shouldDelayAfter != null)
405+
return precedingSecConditional.shouldDelayAfter;
406+
if (precedingSecConditional.type == ConditionalType.IF)
407+
// this is the head of the conditional, meaning the search failed
407408
break;
408-
if (conditional.shouldDelayAfter != null)
409-
return conditional.shouldDelayAfter;
410409
}
411410
return null;
412411
}

src/test/skript/tests/syntaxes/sections/SecConditional.sk

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,17 @@ test "SecConditional - non-multiline conditional at the end":
179179
set {_a} to true
180180
assert {_a} is set with "non-multiline 'if' didn't run used at the end of a multiline 'if'"
181181

182+
test "SecConditional - repeating then sections":
183+
parse:
184+
if:
185+
1 is 1
186+
2 is 2
187+
then:
188+
stop
189+
then:
190+
stop
191+
assert last parse logs is "'then' has to placed just after a multiline 'if' or 'else if' section"
192+
182193
test "SecConditional - delay logic 1":
183194
assert has delay before is false with "should not have delay at start of test"
184195

0 commit comments

Comments
 (0)