Skip to content

Commit

Permalink
Fix the REMOVE changer of variables (#7163)
Browse files Browse the repository at this point in the history
* Fix the REMOVE changer of variables

* Fix wording

* Fix tests

* Update Variable.java

---------

Co-authored-by: sovdee <[email protected]>
  • Loading branch information
UnderscoreTud and sovdeeth authored Dec 15, 2024
1 parent 7b5a8a6 commit aa71764
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 35 deletions.
47 changes: 12 additions & 35 deletions src/main/java/ch/njol/skript/lang/Variable.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.lang;

import java.lang.reflect.Array;
import java.util.*;
import java.util.Map.Entry;
import java.util.function.Function;

import ch.njol.skript.Skript;
import ch.njol.skript.SkriptAPIException;
import ch.njol.skript.SkriptConfig;
Expand Down Expand Up @@ -55,17 +42,6 @@
import org.skriptlang.skript.lang.script.Script;
import org.skriptlang.skript.lang.script.ScriptWarning;

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.Map.Entry;
import java.util.NoSuchElementException;
import java.util.TreeMap;
import java.util.function.Function;

public class Variable<T> implements Expression<T> {

private final static String SINGLE_SEPARATOR_CHAR = ":";
Expand Down Expand Up @@ -579,14 +555,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))) {
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 @@ -600,7 +577,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"

0 comments on commit aa71764

Please sign in to comment.