diff --git a/src/main/java/ch/njol/skript/conditions/CondIsTamed.java b/src/main/java/ch/njol/skript/conditions/CondIsTamed.java new file mode 100644 index 00000000000..a3e342deef6 --- /dev/null +++ b/src/main/java/ch/njol/skript/conditions/CondIsTamed.java @@ -0,0 +1,34 @@ +package ch.njol.skript.conditions; + +import ch.njol.skript.conditions.base.PropertyCondition; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.Since; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Tameable; + +@Name("Is Tamed") +@Description("Check if a tameable entity is tamed (horse, parrot, cat, etc.).") +@Examples({ + "send true if {_horse} is tamed", + "tame {_horse} if {_horse} is untamed" +}) +@Since("INSERT VERSION") +public class CondIsTamed extends PropertyCondition { + + static { + register(CondIsTamed.class, "(tamed|domesticated)", "entities"); + } + + @Override + public boolean check(Entity entity) { + return (entity instanceof Tameable) && ((Tameable) entity).isTamed(); + } + + @Override + protected String getPropertyName() { + return "tamed"; + } + +} diff --git a/src/main/java/ch/njol/skript/effects/EffTame.java b/src/main/java/ch/njol/skript/effects/EffTame.java new file mode 100644 index 00000000000..400f9e27d39 --- /dev/null +++ b/src/main/java/ch/njol/skript/effects/EffTame.java @@ -0,0 +1,54 @@ +package ch.njol.skript.effects; + +import ch.njol.skript.Skript; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.Since; +import ch.njol.skript.lang.Effect; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.util.Kleenean; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Tameable; +import org.bukkit.event.Event; +import org.jetbrains.annotations.Nullable; + +@Name("Tame / Untame") +@Description("Tame a tameable entity (horse, parrot, cat, etc.).") +@Examples({ + "tame {_horse}", + "untame {_horse}" +}) +@Since("INSERT VERSION") +public class EffTame extends Effect { + + static { + Skript.registerEffect(EffTame.class, "[:un](tame|domesticate) %entities%"); + } + + private boolean tame; + private Expression entities; + + @Override + @SuppressWarnings("unchecked") + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + tame = !parseResult.hasTag("un"); + entities = (Expression) exprs[0]; + return true; + } + + @Override + protected void execute(Event event) { + for (Entity entity : entities.getArray(event)) { + if (entity instanceof Tameable) + ((Tameable) entity).setTamed(tame); + } + } + + @Override + public String toString(@Nullable Event event, boolean debug) { + return (tame ? "tame " : "untame ") + entities.toString(event, debug); + } + +} diff --git a/src/test/skript/tests/syntaxes/conditions/CondIsTamed.sk b/src/test/skript/tests/syntaxes/conditions/CondIsTamed.sk new file mode 100644 index 00000000000..82f092e5cc4 --- /dev/null +++ b/src/test/skript/tests/syntaxes/conditions/CondIsTamed.sk @@ -0,0 +1,21 @@ +test "is tamed": + spawn a horse at (spawn of world "world"): + set {_e} to entity + spawn an adult zombie at (spawn of world "world"): + set {_z} to entity + + assert {_e} is not tamed with "a normally spawned horse should not be tamed" + + tame {_e} + assert {_e} is tamed with "taming a horse should do exactly that" + + untame {_e} + assert {_e} is not tamed with "untaming a horse should do exactly that" + + tame {_} + tame {_z} + assert {_} is not tamed with "taming null should do nothing" + assert {_z} is not tamed with "taming an invalid entity should do nothing" + + delete entity within {_e} + delete entity within {_z}