diff --git a/src/main/java/ch/njol/skript/conditions/CondLeashed.java b/src/main/java/ch/njol/skript/conditions/CondLeashed.java index f2fb1fa887d..cd4a171d73b 100644 --- a/src/main/java/ch/njol/skript/conditions/CondLeashed.java +++ b/src/main/java/ch/njol/skript/conditions/CondLeashed.java @@ -5,21 +5,25 @@ import ch.njol.skript.doc.Examples; import ch.njol.skript.doc.Name; import ch.njol.skript.doc.Since; -import org.bukkit.entity.LivingEntity; +import io.papermc.paper.entity.Leashable; +import org.bukkit.entity.Entity; @Name("Is Leashed") @Description("Checks to see if an entity is currently leashed.") @Examples("target entity is leashed") @Since("2.5") -public class CondLeashed extends PropertyCondition { +public class CondLeashed extends PropertyCondition { static { - register(CondLeashed.class, PropertyType.BE, "leashed", "livingentities"); + register(CondLeashed.class, PropertyType.BE, "leashed", "entities"); } @Override - public boolean check(LivingEntity entity) { - return entity.isLeashed(); + public boolean check(Entity entity) { + if(entity instanceof Leashable leashable) { + return leashable.isLeashed(); + } + return false; } @Override diff --git a/src/main/java/ch/njol/skript/effects/EffLeash.java b/src/main/java/ch/njol/skript/effects/EffLeash.java index 4e1b0159b08..27006dc29ee 100644 --- a/src/main/java/ch/njol/skript/effects/EffLeash.java +++ b/src/main/java/ch/njol/skript/effects/EffLeash.java @@ -1,7 +1,7 @@ package ch.njol.skript.effects; +import io.papermc.paper.entity.Leashable; import org.bukkit.entity.Entity; -import org.bukkit.entity.LivingEntity; import org.bukkit.event.Event; import org.jetbrains.annotations.Nullable; @@ -17,8 +17,8 @@ @Name("Leash entities") @Description({ - "Leash living entities to other entities. When trying to leash an Ender Dragon, Wither, Player, or a Bat, this effect will not work.", - "See Spigot's Javadocs for more info." + "Leash entities to other entities. This works with all leashable entities including living entities and boats.", + "See Paper's Javadocs for more info." }) @Examples({ "on right click:", @@ -30,15 +30,15 @@ public class EffLeash extends Effect { static { Skript.registerEffect(EffLeash.class, - "(leash|lead) %livingentities% to %entity%", - "make %entity% (leash|lead) %livingentities%", - "un(leash|lead) [holder of] %livingentities%"); + "(leash|lead) %entities% to %entity%", + "make %entity% (leash|lead) %entities%", + "un(leash|lead) [holder of] %entities%"); } @SuppressWarnings("null") private Expression holder; @SuppressWarnings("null") - private Expression targets; + private Expression targets; private boolean leash; @Override @@ -47,9 +47,9 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye leash = matchedPattern != 2; if (leash) { holder = (Expression) exprs[1 - matchedPattern]; - targets = (Expression) exprs[matchedPattern]; + targets = (Expression) exprs[matchedPattern]; } else { - targets = (Expression) exprs[0]; + targets = (Expression) exprs[0]; } return true; } @@ -60,11 +60,15 @@ protected void execute(Event e) { Entity holder = this.holder.getSingle(e); if (holder == null) return; - for (LivingEntity target : targets.getArray(e)) - target.setLeashHolder(holder); + for (Entity target : targets.getArray(e)) { + if (target instanceof Leashable leashable) + leashable.setLeashHolder(holder); + } } else { - for (LivingEntity target : targets.getArray(e)) - target.setLeashHolder(null); + for (Entity target : targets.getArray(e)) { + if (target instanceof Leashable leashable) + leashable.setLeashHolder(null); + } } } diff --git a/src/main/java/ch/njol/skript/expressions/ExprLeashHolder.java b/src/main/java/ch/njol/skript/expressions/ExprLeashHolder.java index 7e223c2e3f7..e3bfa282bde 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprLeashHolder.java +++ b/src/main/java/ch/njol/skript/expressions/ExprLeashHolder.java @@ -1,7 +1,7 @@ package ch.njol.skript.expressions; +import io.papermc.paper.entity.Leashable; import org.bukkit.entity.Entity; -import org.bukkit.entity.LivingEntity; import org.jetbrains.annotations.Nullable; import ch.njol.skript.doc.Description; @@ -11,19 +11,22 @@ import ch.njol.skript.expressions.base.SimplePropertyExpression; @Name("Leash Holder") -@Description("The leash holder of a living entity.") -@Examples("set {_example} to the leash holder of the target mob") +@Description("The leash holder of a leashable entity.") +@Examples("set {_example} to the leash holder of the target entity") @Since("2.3") -public class ExprLeashHolder extends SimplePropertyExpression { +public class ExprLeashHolder extends SimplePropertyExpression { static { - register(ExprLeashHolder.class, Entity.class, "leash holder[s]", "livingentities"); + register(ExprLeashHolder.class, Entity.class, "leash holder[s]", "entities"); } @Override @Nullable - public Entity convert(LivingEntity entity) { - return entity.isLeashed() ? entity.getLeashHolder() : null; + public Entity convert(Entity entity) { + if (entity instanceof Leashable leashable && leashable.isLeashed()) { + return leashable.getLeashHolder(); + } + return null; } @Override diff --git a/src/test/skript/tests/syntaxes/effects/EffLeash.sk b/src/test/skript/tests/syntaxes/effects/EffLeash.sk new file mode 100644 index 00000000000..1273edc522e --- /dev/null +++ b/src/test/skript/tests/syntaxes/effects/EffLeash.sk @@ -0,0 +1,11 @@ +test "leash basic": + spawn a cow at test-location: + set {_cow} to entity + spawn a villager at test-location: + set {_holder} to entity + + leash {_cow} to {_holder} + unleash {_cow} + + delete entity within {_cow} + delete entity within {_holder}