Skip to content

Commit

Permalink
Fix current section update issues (#6845)
Browse files Browse the repository at this point in the history
  • Loading branch information
APickledWalrus authored Jul 1, 2024
1 parent d4ccff7 commit b48f66d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
12 changes: 9 additions & 3 deletions src/main/java/ch/njol/skript/lang/Section.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Supplier;
Expand Down Expand Up @@ -81,12 +82,17 @@ public abstract boolean init(Expression<?>[] expressions,
* (although the loaded code may change it), the calling code must deal with this.
*/
protected void loadCode(SectionNode sectionNode) {
List<TriggerSection> currentSections = getParser().getCurrentSections();
currentSections.add(this);
ParserInstance parser = getParser();
List<TriggerSection> previousSections = parser.getCurrentSections();

List<TriggerSection> sections = new ArrayList<>(previousSections);
sections.add(this);
parser.setCurrentSections(sections);

try {
setTriggerItems(ScriptLoader.loadItems(sectionNode));
} finally {
currentSections.remove(currentSections.size() - 1);
parser.setCurrentSections(previousSections);
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/main/java/ch/njol/skript/lang/TriggerSection.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
Expand All @@ -42,12 +43,17 @@ protected TriggerSection(List<TriggerItem> items) {
}

protected TriggerSection(SectionNode node) {
List<TriggerSection> currentSections = ParserInstance.get().getCurrentSections();
currentSections.add(this);
ParserInstance parser = ParserInstance.get();
List<TriggerSection> previousSections = parser.getCurrentSections();

List<TriggerSection> sections = new ArrayList<>(previousSections);
sections.add(this);
parser.setCurrentSections(sections);

try {
setTriggerItems(ScriptLoader.loadItems(node));
} finally {
currentSections.remove(currentSections.size() - 1);
parser.setCurrentSections(previousSections);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
test "outdated current section references":
parse:
loop {_list::*}:
spawn a sheep at {_loc}:
set {_e} to event-entity
loop {_list::*}:
set {_var} to loop-value
set {_var} to loop-value-1
assert last parse logs is not set with "loop-value and loop-value-1 should've worked"

0 comments on commit b48f66d

Please sign in to comment.