diff --git a/src/main/java/ch/njol/skript/conditions/CondHasPotion.java b/src/main/java/ch/njol/skript/conditions/CondHasPotion.java index b71b6298d27..cae83eafc60 100644 --- a/src/main/java/ch/njol/skript/conditions/CondHasPotion.java +++ b/src/main/java/ch/njol/skript/conditions/CondHasPotion.java @@ -4,39 +4,46 @@ import ch.njol.skript.conditions.base.PropertyCondition; import ch.njol.skript.conditions.base.PropertyCondition.PropertyType; import ch.njol.skript.doc.Description; -import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Example; import ch.njol.skript.doc.Name; import ch.njol.skript.doc.Since; import ch.njol.skript.lang.Condition; import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.SkriptParser.ParseResult; import ch.njol.util.Kleenean; + import org.bukkit.entity.LivingEntity; import org.bukkit.event.Event; import org.bukkit.potion.PotionEffectType; import org.jetbrains.annotations.Nullable; @Name("Has Potion") -@Description("Checks whether the given living entities have specific potion effects.") -@Examples({"if player has potion speed:", - "\tsend \"You are sonic!\"", - "", - "if all players have potion effects speed and haste:", - "\tbroadcast \"You are ready to MINE!\""}) +@Description("Checks whether the given living entities have potion effects.") +@Example(""" + if player has potion speed: + send "You are sonic!" + if all players have potion effects speed and haste: + broadcast "You are ready to MINE!" +""") @Since("2.6.1") public class CondHasPotion extends Condition { static { - Skript.registerCondition(CondHasPotion.class, - "%livingentities% (has|have) potion[s] [effect[s]] %potioneffecttypes%", - "%livingentities% (doesn't|does not|do not|don't) have potion[s] [effect[s]] %potioneffecttypes%"); + Skript.registerCondition( + CondHasPotion.class, + PropertyCondition.getPatterns( + PropertyType.HAVE, + "([any] potion effect[s]|potion[s] [effect[s]] %-potioneffecttypes%)", + "livingentities" + ) + ); } - - private Expression livingEntities; + private Expression potionEffects; + private Expression livingEntities; @Override - @SuppressWarnings({"unchecked", "null"}) + @SuppressWarnings("unchecked") public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { livingEntities = (Expression) exprs[0]; potionEffects = (Expression) exprs[1]; @@ -45,17 +52,19 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye } @Override - public boolean check(Event e) { - return livingEntities.check(e, - livingEntity -> potionEffects.check(e, - livingEntity::hasPotionEffect - ), isNegated()); + public boolean check(Event event) { + if (potionEffects == null) { + return livingEntities.check(event, entity -> !entity.getActivePotionEffects().isEmpty(), isNegated()); + } + return livingEntities.check(event, + entity -> potionEffects.check(event, entity::hasPotionEffect), + isNegated()); } @Override - public String toString(@Nullable Event e, boolean debug) { - return PropertyCondition.toString(this, PropertyType.HAVE, e, debug, livingEntities, - "potion " + potionEffects.toString(e, debug)); + public String toString(@Nullable Event event, boolean debug) { + String effects = (potionEffects == null) ? "any potion effect" : "potion " + potionEffects.toString(event, debug); + return PropertyCondition.toString(this, PropertyType.HAVE, event, debug, livingEntities, effects); } } diff --git a/src/test/skript/tests/syntaxes/conditions/CondHasPotion.sk b/src/test/skript/tests/syntaxes/conditions/CondHasPotion.sk new file mode 100644 index 00000000000..eee7b58b5fc --- /dev/null +++ b/src/test/skript/tests/syntaxes/conditions/CondHasPotion.sk @@ -0,0 +1,8 @@ +test "has potion": + spawn a pig at event-location + apply ambient strength of tier 5 to last spawned pig for 10 seconds + + assert last spawned pig has any potion effect with "pig has no potion effect" + assert last spawned pig has potion effect strength with "pig has strength potion effect" + + clear last spawned pig