From b48f66d83760be7ebdfcc1dfcc833d3760709ba2 Mon Sep 17 00:00:00 2001 From: Patrick Miller Date: Mon, 1 Jul 2024 17:19:22 -0400 Subject: [PATCH] Fix current section update issues (#6845) --- src/main/java/ch/njol/skript/lang/Section.java | 12 +++++++++--- .../java/ch/njol/skript/lang/TriggerSection.java | 12 +++++++++--- .../6843-current section reference issue.sk | 9 +++++++++ 3 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 src/test/skript/tests/regressions/6843-current section reference issue.sk diff --git a/src/main/java/ch/njol/skript/lang/Section.java b/src/main/java/ch/njol/skript/lang/Section.java index c511e88b2a2..5a665165eff 100644 --- a/src/main/java/ch/njol/skript/lang/Section.java +++ b/src/main/java/ch/njol/skript/lang/Section.java @@ -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; @@ -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 currentSections = getParser().getCurrentSections(); - currentSections.add(this); + ParserInstance parser = getParser(); + List previousSections = parser.getCurrentSections(); + + List sections = new ArrayList<>(previousSections); + sections.add(this); + parser.setCurrentSections(sections); + try { setTriggerItems(ScriptLoader.loadItems(sectionNode)); } finally { - currentSections.remove(currentSections.size() - 1); + parser.setCurrentSections(previousSections); } } diff --git a/src/main/java/ch/njol/skript/lang/TriggerSection.java b/src/main/java/ch/njol/skript/lang/TriggerSection.java index d1343d43605..e7945784752 100644 --- a/src/main/java/ch/njol/skript/lang/TriggerSection.java +++ b/src/main/java/ch/njol/skript/lang/TriggerSection.java @@ -24,6 +24,7 @@ import org.bukkit.event.Event; import org.eclipse.jdt.annotation.Nullable; +import java.util.ArrayList; import java.util.List; /** @@ -42,12 +43,17 @@ protected TriggerSection(List items) { } protected TriggerSection(SectionNode node) { - List currentSections = ParserInstance.get().getCurrentSections(); - currentSections.add(this); + ParserInstance parser = ParserInstance.get(); + List previousSections = parser.getCurrentSections(); + + List sections = new ArrayList<>(previousSections); + sections.add(this); + parser.setCurrentSections(sections); + try { setTriggerItems(ScriptLoader.loadItems(node)); } finally { - currentSections.remove(currentSections.size() - 1); + parser.setCurrentSections(previousSections); } } diff --git a/src/test/skript/tests/regressions/6843-current section reference issue.sk b/src/test/skript/tests/regressions/6843-current section reference issue.sk new file mode 100644 index 00000000000..20e8beb20ef --- /dev/null +++ b/src/test/skript/tests/regressions/6843-current section reference issue.sk @@ -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"