Skip to content

Commit

Permalink
Merge branch 'dev/patch' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Moderocky authored Dec 24, 2024
2 parents 9ad3ff1 + 48f931a commit f42f5d3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 36 deletions.
4 changes: 3 additions & 1 deletion src/main/java/ch/njol/skript/expressions/ExprXOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Array;

@Name("X of Item")
@Description("An expression to be able to use a certain amount of items where the amount can be any expression. Please note that this expression is not stable and might be replaced in the future.")
@Examples("give level of player of pickaxes to the player")
Expand All @@ -62,7 +64,7 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
protected Object[] get(Event e, Object[] source) {
Number a = amount.getSingle(e);
if (a == null)
return new Object[0];
return (Object[]) Array.newInstance(getReturnType(), 0);

return get(source, o -> {
if (o instanceof ItemStack) {
Expand Down
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,3 @@
test "null of x incorrect type":
# would cause an exception
spawn {_null} of pig above {_null}
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 f42f5d3

Please sign in to comment.