Skip to content

Make operators a literal instead of expression #7439

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

Open
wants to merge 2 commits into
base: dev/feature
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
68 changes: 34 additions & 34 deletions src/main/java/ch/njol/skript/conditions/CondDamageCause.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,58 +15,58 @@
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;

/**
* @author Peter Güttinger
*/
@Name("Damage Cause")
@Description("Tests what kind of damage caused a <a href='events.html#damage'>damage event</a>. Refer to the <a href='classes.html#damagecause'>Damage Cause</a> type for a list of all possible causes.")
@Examples({"# make players use their potions of fire resistance whenever they take any kind of fire damage",
"on damage:",
"\tdamage was caused by lava, fire or burning",
"\tvictim is a player",
"\tvictim has a potion of fire resistance",
"\tcancel event",
"\tapply fire resistance to the victim for 30 seconds",
"\tremove 1 potion of fire resistance from the victim",
"# prevent mobs from dropping items under certain circumstances",
"on death:",
"\tentity is not a player",
"\tdamage wasn't caused by a block explosion, an attack, a projectile, a potion, fire, burning, thorns or poison",
"\tclear drops"})
@Description({
"Tests what kind of damage caused a <a href='events.html#damage'>damage event</a>.",
"Refer to the <a href='classes.html#damagecause'>Damage Cause</a> type for a list of all possible causes."
})
@Examples({
"# make players use their potions of fire resistance whenever they take any kind of fire damage",
"on damage:",
"\tdamage was caused by lava, fire or burning",
"\tvictim is a player",
"\tvictim has a potion of fire resistance",
"\tcancel event",
"\tapply fire resistance to the victim for 30 seconds",
"\tremove 1 potion of fire resistance from the victim",
"# prevent mobs from dropping items under certain circumstances",
"on death:",
"\tentity is not a player",
"\tdamage wasn't caused by a block explosion, an attack, a projectile, a potion, fire, burning, thorns or poison",
"\tclear drops"
})
@Since("2.0")
public class CondDamageCause extends Condition {

static {
Skript.registerCondition(CondDamageCause.class, "[the] damage (was|is|has)(0¦|1¦n('|o)t) [been] (caused|done|made) by %damagecause%");
}

@SuppressWarnings("null")
private Expression<DamageCause> cause;
@SuppressWarnings("null")

private Expression<DamageCause> expected;

@SuppressWarnings({"unchecked", "null"})
private Expression<DamageCause> cause;

@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
cause = new EventValueExpression<>(DamageCause.class);
expected = (Expression<DamageCause>) exprs[0];
setNegated(parseResult.mark == 1);
return ((EventValueExpression<DamageCause>) cause).init();
return ((EventValueExpression<DamageCause>) cause).init(matchedPattern, isDelayed, parseResult);
}

@Override
public boolean check(final Event e) {
final DamageCause cause = this.cause.getSingle(e);
public boolean check(Event event) {
DamageCause cause = this.cause.getSingle(event);
if (cause == null)
return false;
return expected.check(e,
return expected.check(event,
other -> cause == other,
isNegated());
}

@Override
public String toString(final @Nullable Event e, final boolean debug) {
return "damage was" + (isNegated() ? " not" : "") + " caused by " + expected.toString(e, debug);
public String toString(@Nullable Event event, boolean debug) {
return "damage was" + (isNegated() ? " not" : "") + " caused by " + expected.toString(event, debug);
}

}
48 changes: 25 additions & 23 deletions src/main/java/ch/njol/skript/expressions/ExprBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,51 @@
import ch.njol.skript.util.Direction;
import ch.njol.util.Kleenean;

/**
* @author Peter Güttinger
*/
@Name("Block")
@Description({"The block involved in the event, e.g. the clicked block or the placed block.",
"Can optionally include a direction as well, e.g. 'block above' or 'block in front of the player'."})
@Examples({"block is ore",
"set block below to air",
"spawn a creeper above the block",
"loop blocks in radius 4:",
" loop-block is obsidian",
" set loop-block to water",
"block is a chest:",
" clear the inventory of the block"})
@Description({
"The block involved in the event, e.g. the clicked block or the placed block.",
"Can optionally include a direction as well, e.g. 'block above' or 'block in front of the player'."
})
@Examples({
"block is ore",
"set block below to air",
"spawn a creeper above the block",
"loop blocks in radius 4:",
" loop-block is obsidian",
" set loop-block to water",
"block is a chest:",
" clear the inventory of the block"
})
@Since("1.0")
public class ExprBlock extends WrapperExpression<Block> {

static {
Skript.registerExpression(ExprBlock.class, Block.class, ExpressionType.SIMPLE, "[the] [event-]block");
Skript.registerExpression(ExprBlock.class, Block.class, ExpressionType.COMBINED, "[the] block %direction% [%location%]");
}

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

@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parser) {
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parser) {
if (exprs.length > 0) {
setExpr(new ConvertedExpression<>(Direction.combine((Expression<? extends Direction>) exprs[0],
(Expression<? extends Location>) exprs[1]), Block.class,
new ConverterInfo<>(Location.class, Block.class, new Converter<Location, Block>() {
@Override
public Block convert(final Location l) {
return l.getBlock();
public Block convert(Location location) {
return location.getBlock();
}
}, 0)));
return true;
} else {
setExpr(new EventValueExpression<>(Block.class));
return ((EventValueExpression<Block>) getExpr()).init();
return ((EventValueExpression<Block>) getExpr()).init(matchedPattern, isDelayed, parser);
}
}

@Override
public String toString(final @Nullable Event e, final boolean debug) {
return getExpr() instanceof EventValueExpression ? "the block" : "the block " + getExpr().toString(e, debug);
public String toString(@Nullable Event event, boolean debug) {
return getExpr() instanceof EventValueExpression ? "the block" : "the block " + getExpr().toString(event, debug);
}

}
87 changes: 40 additions & 47 deletions src/main/java/ch/njol/skript/expressions/ExprEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,49 +23,43 @@
import ch.njol.util.Kleenean;
import ch.njol.util.StringUtils;

/**
* @author Peter Güttinger
*/
@Name("Creature/Entity/Player/Projectile/Villager/Powered Creeper/etc.")
@Description({"The entity involved in an event (an entity is a player, a creature or an inanimate object like ignited TNT, a dropped item or an arrow).",
"You can use the specific type of the entity that's involved in the event, e.g. in a 'death of a creeper' event you can use 'the creeper' instead of 'the entity'."})
@Examples({"give a diamond sword of sharpness 3 to the player",
"kill the creeper",
"kill all powered creepers in the wolf's world",
"projectile is an arrow"})
@Description({
"The entity involved in an event (an entity is a player, a creature or an inanimate object like ignited TNT, a dropped item or an arrow).",
"You can use the specific type of the entity that's involved in the event, e.g. in a 'death of a creeper' event you can use 'the creeper' instead of 'the entity'."
})
@Examples({
"give a diamond sword of sharpness 3 to the player",
"kill the creeper",
"kill all powered creepers in the wolf's world",
"projectile is an arrow"
})
@Since("1.0")
public class ExprEntity extends SimpleExpression<Entity> {

static {
Skript.registerExpression(ExprEntity.class, Entity.class, ExpressionType.PATTERN_MATCHES_EVERYTHING, "[the] [event-]<.+>");
}

@SuppressWarnings("null")
private EntityData<?> type;

@SuppressWarnings("null")

private EventValueExpression<Entity> entity;

private EntityData<?> type;

@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
final RetainingLogHandler log = SkriptLogger.startRetainingLog();
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
RetainingLogHandler log = SkriptLogger.startRetainingLog();
try {
if (!StringUtils.startsWithIgnoreCase(parseResult.expr, "the ") && !StringUtils.startsWithIgnoreCase(parseResult.expr, "event-")) {

String s = parseResult.regexes.get(0).group();
final ItemType item = Aliases.parseItemType("" + s);
String input = parseResult.regexes.get(0).group();
ItemType item = Aliases.parseItemType("" + input);
log.clear();
if (item != null) {
log.printLog();
return false;
}

//if(!StringUtils.startsWithIgnoreCase(parseResult.expr, "the event-") && !s.equalsIgnoreCase("player") && !s.equalsIgnoreCase("entity")){
// return false;
//}

}

final EntityData<?> type = EntityData.parseWithoutIndefiniteArticle("" + parseResult.regexes.get(0).group());
EntityData<?> type = EntityData.parseWithoutIndefiniteArticle("" + parseResult.regexes.get(0).group());
log.clear();
log.printLog();
if (type == null || type.isPlural().isTrue())
Expand All @@ -75,31 +69,20 @@ public boolean init(final Expression<?>[] exprs, final int matchedPattern, final
log.stop();
}
entity = new EventValueExpression<>(type.getType());
return entity.init();
}

@Override
public boolean isSingle() {
return true;
}

@Override
public Class<? extends Entity> getReturnType() {
return type.getType();
return entity.init(matchedPattern, isDelayed, parseResult);
}

@Override
@Nullable
protected Entity[] get(final Event e) {
final Entity[] es = entity.getArray(e);
if (es.length == 0 || type.isInstance(es[0]))
return es;
protected Entity @Nullable [] get(Event event) {
Entity[] entities = entity.getArray(event);
if (entities.length == 0 || type.isInstance(entities[0]))
return entities;
return null;
}

@SuppressWarnings("unchecked")

@Override
@Nullable
@SuppressWarnings("unchecked")
public <R> Expression<? extends R> getConvertedExpression(Class<R>... to) {
for (Class<R> t : to) {
if (t.equals(EntityData.class)) {
Expand All @@ -108,10 +91,20 @@ public <R> Expression<? extends R> getConvertedExpression(Class<R>... to) {
}
return super.getConvertedExpression(to);
}


@Override
public boolean isSingle() {
return true;
}

@Override
public Class<? extends Entity> getReturnType() {
return type.getType();
}

@Override
public String toString(final @Nullable Event e, final boolean debug) {
public String toString(@Nullable Event event, boolean debug) {
return "the " + type;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,14 @@
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.localization.Noun;
import ch.njol.skript.registrations.Classes;
import ch.njol.skript.util.Utils;
import ch.njol.util.Kleenean;
import ch.njol.util.NonNullPair;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

/**
* Provided for convenience: one can write 'event-world' instead of only 'world' to distinguish between the event-world and the loop-world.
*
* @author Peter Güttinger
*/
@NoDoc
public class ExprEventExpression extends WrapperExpression<Object> {
Expand All @@ -39,7 +34,7 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
boolean plural = Utils.getEnglishPlural(parser.expr).getSecond();
EventValueExpression<?> eventValue = new EventValueExpression<>(plural ? CollectionUtils.arrayType(c) : c);
setExpr(eventValue);
return eventValue.init();
return eventValue.init(matchedPattern, isDelayed, parser);
}

@Override
Expand Down
26 changes: 13 additions & 13 deletions src/main/java/ch/njol/skript/expressions/ExprLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,35 @@
import ch.njol.skript.util.Direction;
import ch.njol.util.Kleenean;

/**
* @author Peter Güttinger
*/
@Name("Location")
@Description("The location where an event happened (e.g. at an entity or block), or a location <a href='#ExprDirection'>relative</a> to another (e.g. 1 meter above another location).")
@Examples({"drop 5 apples at the event-location # exactly the same as writing 'drop 5 apples'",
"set {_loc} to the location 1 meter above the player"})
@Examples({
"drop 5 apples at the event-location # exactly the same as writing 'drop 5 apples'",
"set {_loc} to the location 1 meter above the player"
})
@Since("2.0")
public class ExprLocation extends WrapperExpression<Location> {

static {
Skript.registerExpression(ExprLocation.class, Location.class, ExpressionType.SIMPLE, "[the] [event-](location|position)");
Skript.registerExpression(ExprLocation.class, Location.class, ExpressionType.COMBINED, "[the] (location|position) %directions% [%location%]");
}

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

@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
if (exprs.length > 0) {
super.setExpr(Direction.combine((Expression<? extends Direction>) exprs[0], (Expression<? extends Location>) exprs[1]));
return true;
} else {
setExpr(new EventValueExpression<>(Location.class));
return ((EventValueExpression<Location>) getExpr()).init();
return ((EventValueExpression<Location>) getExpr()).init(matchedPattern, isDelayed, parseResult);
}
}

@Override
public String toString(final @Nullable Event e, final boolean debug) {
return getExpr() instanceof EventValueExpression ? "the location" : "the location " + getExpr().toString(e, debug);
public String toString(@Nullable Event event, boolean debug) {
return getExpr() instanceof EventValueExpression ? "location" : "location " + getExpr().toString(event, debug);
}

}
Loading
Loading