Skip to content

Commit

Permalink
Fix ExprSets conflicting
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLimeGlass committed Oct 15, 2023
1 parent 8e1fd28 commit 0146ee2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
2 changes: 1 addition & 1 deletion skript-aliases
42 changes: 20 additions & 22 deletions src/main/java/ch/njol/skript/expressions/ExprSets.java
Expand Up @@ -18,6 +18,14 @@
*/
package ch.njol.skript.expressions;

import java.util.Iterator;
import java.util.function.Supplier;

import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

import com.google.common.collect.Lists;

import ch.njol.skript.Skript;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.doc.Description;
Expand All @@ -31,33 +39,26 @@
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.util.Utils;
import ch.njol.util.Kleenean;
import com.google.common.collect.Lists;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

import java.util.Iterator;
import java.util.List;
import java.util.function.Supplier;

@Name("Sets")
@Description("Returns a list of all the values of a type; useful for looping.")
@Description("Returns a list of all the values of a type. Useful for looping.")
@Examples({
"loop all attribute types:",
"\tset loop-value attribute of player to 10",
"\tmessage \"Set attribute %loop-value% to 10!\""
"\tset loop-value attribute of player to 10",
"\tmessage \"Set attribute %loop-value% to 10!\""
})
@Since("<i>unknown</i> (before 1.4.2), 2.7 (colors)")
// Class history rename order: LoopItems.class -> ExprItems.class (renamed in 2.3-alpha1) -> ExprSets.class (renamed in 2.7.0)
@Since("1.0 pre-5, 2.7 (classinfo)")
public class ExprSets extends SimpleExpression<Object> {

static {
Skript.registerExpression(ExprSets.class, Object.class, ExpressionType.COMBINED,
"[all [[of] the]|the|every] %*classinfo%"
);
"[all [[of] the]|the|every] %*classinfo%");
}

private ClassInfo<?> classInfo;
@Nullable
private Supplier<? extends Iterator<?>> supplier;
private ClassInfo<?> classInfo;

@Override
@SuppressWarnings("unchecked")
Expand All @@ -68,20 +69,17 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
return false;

classInfo = ((Literal<ClassInfo<?>>) exprs[0]).getSingle();
supplier = classInfo.getSupplier();
if (supplier == null) {
Skript.error("You cannot get all values of type '" + classInfo.getName().getSingular() + "'");
return false;
}
return true;
// Silently ignores and moves on to next syntax if no supplier is provided.
// This is to avoid very similar matching syntax conflicts like 'all players'
// So do not include a Skript.error here.
return (supplier = classInfo.getSupplier()) != null;
}

@Override
protected Object[] get(Event event) {
assert supplier != null;
Iterator<?> iterator = supplier.get();
List<?> elements = Lists.newArrayList(iterator);
return elements.toArray(new Object[0]);
return Lists.newArrayList(iterator).toArray(new Object[0]);
}

@Override
Expand Down

0 comments on commit 0146ee2

Please sign in to comment.