Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the REMOVE changer of variables #7163

Open
wants to merge 4 commits into
base: dev/patch
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions src/main/java/ch/njol/skript/lang/Variable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -578,14 +572,15 @@ public void change(Event event, @Nullable Object[] delta, ChangeMode mode) throw
if (mode == ChangeMode.REMOVE) {
if (map == null)
return;
ArrayList<String> toRemove = new ArrayList<>(); // prevents CMEs
Set<String> toRemove = new HashSet<>(); // prevents CMEs
for (Object value : delta) {
for (Entry<String, Object> 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))) {
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
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;
Expand All @@ -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<String> toRemove = new ArrayList<>(); // prevents CMEs
Set<String> toRemove = new HashSet<>(); // prevents CMEs
for (Entry<String, Object> i : map.entrySet()) {
for (Object value : delta) {
if (Relation.EQUAL.isImpliedBy(Comparators.compare(i.getValue(), value)))
Expand Down
Original file line number Diff line number Diff line change
@@ -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 remove all instances of 1 from the list"
remove first 6 elements out of {_list::*} from {_list::*}
assert {_list::*} is not set with "didn't remove all elements"