From b813fc63c4a939c85c305fff77a21567faea161e Mon Sep 17 00:00:00 2001 From: _tud <98935832+UnderscoreTud@users.noreply.github.com> Date: Thu, 24 Oct 2024 22:23:40 +0300 Subject: [PATCH 1/3] Fix the REMOVE changer of variables --- .../java/ch/njol/skript/lang/Variable.java | 21 +++++++------------ ...removing from variable skips duplicates.sk | 6 ++++++ 2 files changed, 14 insertions(+), 13 deletions(-) create mode 100644 src/test/skript/tests/regressions/7162-removing from variable skips duplicates.sk diff --git a/src/main/java/ch/njol/skript/lang/Variable.java b/src/main/java/ch/njol/skript/lang/Variable.java index d85d6140aeb..0bed6421926 100644 --- a/src/main/java/ch/njol/skript/lang/Variable.java +++ b/src/main/java/ch/njol/skript/lang/Variable.java @@ -19,14 +19,8 @@ package ch.njol.skript.lang; import java.lang.reflect.Array; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Iterator; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.Map.Entry; -import java.util.NoSuchElementException; -import java.util.TreeMap; import ch.njol.skript.Skript; import ch.njol.skript.SkriptAPIException; @@ -578,14 +572,15 @@ public void change(Event event, @Nullable Object[] delta, ChangeMode mode) throw if (mode == ChangeMode.REMOVE) { if (map == null) return; - ArrayList toRemove = new ArrayList<>(); // prevents CMEs + Set toRemove = new HashSet<>(); // prevents CMEs for (Object value : delta) { for (Entry entry : map.entrySet()) { + String key = entry.getKey(); + if (key == null) + continue; // This is NOT a part of list variable + if (toRemove.contains(key)) + continue; // Skip if we've already marked this key to be removed if (Relation.EQUAL.isImpliedBy(Comparators.compare(entry.getValue(), value))) { - String key = entry.getKey(); - if (key == null) - continue; // This is NOT a part of list variable - // Otherwise, we'll mark that key to be set to null toRemove.add(key); break; @@ -599,7 +594,7 @@ public void change(Event event, @Nullable Object[] delta, ChangeMode mode) throw } else if (mode == ChangeMode.REMOVE_ALL) { if (map == null) return; - ArrayList toRemove = new ArrayList<>(); // prevents CMEs + Set toRemove = new HashSet<>(); // prevents CMEs for (Entry i : map.entrySet()) { for (Object value : delta) { if (Relation.EQUAL.isImpliedBy(Comparators.compare(i.getValue(), value))) diff --git a/src/test/skript/tests/regressions/7162-removing from variable skips duplicates.sk b/src/test/skript/tests/regressions/7162-removing from variable skips duplicates.sk new file mode 100644 index 00000000000..328a4a75832 --- /dev/null +++ b/src/test/skript/tests/regressions/7162-removing from variable skips duplicates.sk @@ -0,0 +1,6 @@ +test "removing from variables skips duplicates": + set {_list::*} to 1, 1, 2, 3, 4, 5, 6 and 6 + remove 1 and 1 from {_list::*} + assert {_list::*} is 2, 3, 4, 5, 6 and 6 with "didn't 1 and 1 from list" + remove first 6 elements out of {_list::*} from {_list::*} + assert {_list::*} is empty with "didn't remove all elements" From 8101cded133b40a2a6eb75a369a4f0851cfebe43 Mon Sep 17 00:00:00 2001 From: _tud <98935832+UnderscoreTud@users.noreply.github.com> Date: Thu, 24 Oct 2024 22:30:51 +0300 Subject: [PATCH 2/3] Fix wording --- .../regressions/7162-removing from variable skips duplicates.sk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/skript/tests/regressions/7162-removing from variable skips duplicates.sk b/src/test/skript/tests/regressions/7162-removing from variable skips duplicates.sk index 328a4a75832..c55f4402683 100644 --- a/src/test/skript/tests/regressions/7162-removing from variable skips duplicates.sk +++ b/src/test/skript/tests/regressions/7162-removing from variable skips duplicates.sk @@ -1,6 +1,6 @@ test "removing from variables skips duplicates": set {_list::*} to 1, 1, 2, 3, 4, 5, 6 and 6 remove 1 and 1 from {_list::*} - assert {_list::*} is 2, 3, 4, 5, 6 and 6 with "didn't 1 and 1 from list" + assert {_list::*} is 2, 3, 4, 5, 6 and 6 with "didn't remove all instances of 1 from the list" remove first 6 elements out of {_list::*} from {_list::*} assert {_list::*} is empty with "didn't remove all elements" From 4920c22c69d46add8d5d9e0ad74bec3b9123cdd7 Mon Sep 17 00:00:00 2001 From: _tud <98935832+UnderscoreTud@users.noreply.github.com> Date: Fri, 25 Oct 2024 17:55:15 +0300 Subject: [PATCH 3/3] Fix tests --- .../regressions/7162-removing from variable skips duplicates.sk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/skript/tests/regressions/7162-removing from variable skips duplicates.sk b/src/test/skript/tests/regressions/7162-removing from variable skips duplicates.sk index c55f4402683..c4fadc84163 100644 --- a/src/test/skript/tests/regressions/7162-removing from variable skips duplicates.sk +++ b/src/test/skript/tests/regressions/7162-removing from variable skips duplicates.sk @@ -3,4 +3,4 @@ test "removing from variables skips duplicates": remove 1 and 1 from {_list::*} assert {_list::*} is 2, 3, 4, 5, 6 and 6 with "didn't remove all instances of 1 from the list" remove first 6 elements out of {_list::*} from {_list::*} - assert {_list::*} is empty with "didn't remove all elements" + assert {_list::*} is not set with "didn't remove all elements"