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 ExprXOf CCE #4261

Merged
merged 5 commits into from
Aug 16, 2021
Merged
Changes from 1 commit
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
98 changes: 37 additions & 61 deletions src/main/java/ch/njol/skript/expressions/ExprXOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@
*/
package ch.njol.skript.expressions;

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

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.classes.Converter;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
Expand All @@ -36,6 +31,9 @@
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;
import org.eclipse.jdt.annotation.Nullable;

/**
* @author Peter Güttinger
Expand All @@ -45,75 +43,53 @@
@Examples("give level of player of pickaxes to the player")
@Since("1.2")
public class ExprXOf extends PropertyExpression<Object, Object> {

static {
Skript.registerExpression(ExprXOf.class, Object.class, ExpressionType.PATTERN_MATCHES_EVERYTHING, "%number% of %itemstacks/entitytype%");
Skript.registerExpression(ExprXOf.class, Object.class, ExpressionType.PATTERN_MATCHES_EVERYTHING, "%number% of %itemstacks/itemtypes/entitytype%");
}

@SuppressWarnings("null")
Expression<Number> amount;

@SuppressWarnings({"unchecked", "null"})

private Expression<Number> amount;

@SuppressWarnings("unchecked")
TPGamesNL marked this conversation as resolved.
Show resolved Hide resolved
@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
setExpr(exprs[1]);
amount = (Expression<Number>) exprs[0];
if (amount instanceof Literal && getExpr() instanceof Literal)// "x of y" is also an ItemType syntax
return false;
return true;
// "x of y" is also an ItemType syntax
return !(amount instanceof Literal) || !(getExpr() instanceof Literal);
}

@Override
public Class<? extends Object> getReturnType() {
return getExpr().getReturnType();
}


@Override
protected Object[] get(final Event e, final Object[] source) {
return get(source, new Converter<Object, Object>() {
@Override
@Nullable
public Object convert(final Object o) {
final Number a = amount.getSingle(e);
if (a == null)
return null;
if (o instanceof ItemStack) {
final ItemStack is = ((ItemStack) o).clone();
is.setAmount(a.intValue());
return is;
} else if (o instanceof ItemType) {
ItemType type = ((ItemType) o).clone();
type.setAmount(a.intValue());
return type;
} else {
final EntityType t = ((EntityType) o).clone();
t.amount = a.intValue();
return t;
}
protected Object[] get(Event e, Object[] source) {
Number a = amount.getSingle(e);

return get(source, o -> {
if (a == null)
return null;
TPGamesNL marked this conversation as resolved.
Show resolved Hide resolved
if (o instanceof ItemStack) {
ItemStack is = ((ItemStack) o).clone();
is.setAmount(a.intValue());
return is;
} else if (o instanceof ItemType) {
ItemType type = ((ItemType) o).clone();
type.setAmount(a.intValue());
return type;
} else {
EntityType t = ((EntityType) o).clone();
t.amount = a.intValue();
return t;
}
});
}

@SuppressWarnings("unchecked")

@Override
@Nullable
public <R> Expression<? extends R> getConvertedExpression(Class<R>... to) {
// Make sure we get converted expression from Variables etc. correctly
// Then, wrap it so that our 'X' is properly applied
// See #1747 for issue that was caused by failure to do this

Expression<? extends R> converted = getExpr().getConvertedExpression(to);
if (converted == null) // Can't create converted expression
return null;

ExprXOf wrapped = new ExprXOf();
wrapped.setExpr(converted);
wrapped.amount = amount;
return (Expression<? extends R>) wrapped;
public Class<?> getReturnType() {
return getExpr().getReturnType();
}

@Override
public String toString(final @Nullable Event e, final boolean debug) {
public String toString(@Nullable Event e, boolean debug) {
return amount.toString(e, debug) + " of " + getExpr().toString(e, debug);
}

}