Skip to content

Make has potion effect types optional #8010

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 6 commits into
base: dev/patch
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
51 changes: 30 additions & 21 deletions src/main/java/ch/njol/skript/conditions/CondHasPotion.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<LivingEntity> livingEntities;

private Expression<PotionEffectType> potionEffects;
private Expression<LivingEntity> livingEntities;

@Override
@SuppressWarnings({"unchecked", "null"})
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
livingEntities = (Expression<LivingEntity>) exprs[0];
potionEffects = (Expression<PotionEffectType>) exprs[1];
Expand All @@ -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);
}

}
8 changes: 8 additions & 0 deletions src/test/skript/tests/syntaxes/conditions/CondHasPotion.sk
Original file line number Diff line number Diff line change
@@ -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