Skip to content
Draft
14 changes: 9 additions & 5 deletions src/main/java/ch/njol/skript/conditions/CondLeashed.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<LivingEntity> {
public class CondLeashed extends PropertyCondition<Entity> {

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(entity instanceof Leashable leashable) {
if (entity instanceof Leashable leashable) {

return leashable.isLeashed();
}
return false;
}

@Override
Expand Down
30 changes: 17 additions & 13 deletions src/main/java/ch/njol/skript/effects/EffLeash.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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 <a href=\"https://hub.spigotmc.org/javadocs/spigot/org/bukkit/entity/LivingEntity.html#setLeashHolder(org.bukkit.entity.Entity)\">Spigot's Javadocs for more info</a>."
"Leash entities to other entities. This works with all leashable entities including living entities and boats.",
"See <a href=\"https://jd.papermc.io/paper/1.21.10/io/papermc/paper/entity/Leashable.html\">Paper's Javadocs for more info</a>."
})
@Examples({
"on right click:",
Expand All @@ -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<Entity> holder;
@SuppressWarnings("null")
private Expression<LivingEntity> targets;
private Expression<Entity> targets;
private boolean leash;

@Override
Expand All @@ -47,9 +47,9 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
leash = matchedPattern != 2;
if (leash) {
holder = (Expression<Entity>) exprs[1 - matchedPattern];
targets = (Expression<LivingEntity>) exprs[matchedPattern];
targets = (Expression<Entity>) exprs[matchedPattern];
} else {
targets = (Expression<LivingEntity>) exprs[0];
targets = (Expression<Entity>) exprs[0];
}
return true;
}
Expand All @@ -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);
}
}
}

Expand Down
17 changes: 10 additions & 7 deletions src/main/java/ch/njol/skript/expressions/ExprLeashHolder.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<LivingEntity, Entity> {
public class ExprLeashHolder extends SimplePropertyExpression<Entity, Entity> {

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
Expand Down
11 changes: 11 additions & 0 deletions src/test/skript/tests/syntaxes/effects/EffLeash.sk
Original file line number Diff line number Diff line change
@@ -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}
Comment on lines +7 to +8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should assert that the leash holder of cow is properly set after each line

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes this is just a basic template, I was having some trouble with unrelated tests failing after adding this test. Probably some issue with cleanup and state restoring. I'll be adding assertions soon


delete entity within {_cow}
delete entity within {_holder}
Loading