Skip to content
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
62 changes: 37 additions & 25 deletions src/main/java/ch/njol/skript/bukkitutil/PaperEntityUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,32 @@ private static void mobLookAt(Object target, @Nullable Float headRotationSpeed,
/**
* Instruct a Mob (1.17+) to look at a specific vector/location/entity.
* Object can be a {@link org.bukkit.util.Vector}, {@link org.bukkit.Location} or {@link org.bukkit.entity.Entity}
*
* @param target The vector/location/entity to make the livingentity look at.
* @param entities The living entities to make look at something.
*
* @param target The vector/location/entity to make the entity look at.
* @param entities The entities to make look at something.
*/
public static void lookAt(Object target, LivingEntity... entities) {
public static void lookAt(Object target, Entity... entities) {
lookAt(target, null, null, entities);
}

/**
* Instruct a Mob (1.17+) to look at a specific vector/location/entity.
* Object can be a {@link org.bukkit.util.Vector}, {@link org.bukkit.Location} or {@link org.bukkit.entity.Entity}
*
* @param target The vector/location/entity to make the livingentity look at.
* @param headRotationSpeed The rotation speed at which the living entities will rotate their head to the target. Vanilla default values range from 10-50. Doesn't apply to players.
*
* @param target The vector/location/entity to make the entity look at.
* @param headRotationSpeed The rotation speed at which the entities will rotate their head to the target. Vanilla default values range from 10-50. Doesn't apply to players.
* @param maxHeadPitch The maximum pitch at which the eyes/feet can go to. Doesn't apply to players.
* @param entities The living entities to make look at something.
* @param entities The entities to make look at something.
*/
public static void lookAt(Object target, @Nullable Float headRotationSpeed, @Nullable Float maxHeadPitch, LivingEntity... entities) {
public static void lookAt(Object target, @Nullable Float headRotationSpeed, @Nullable Float maxHeadPitch, Entity... entities) {
if (target == null || !LOOK_AT)
return;
// Use support for players if using Paper 1.19.1+
if (LOOK_ANCHORS) {
lookAt(LookAnchor.EYES, headRotationSpeed, maxHeadPitch, entities);
lookAt(LookAnchor.EYES, target, headRotationSpeed, maxHeadPitch, entities);
return;
}
for (LivingEntity entity : entities) {
for (Entity entity : entities) {
if (!(entity instanceof Mob))
continue;
mobLookAt(target, headRotationSpeed, maxHeadPitch, (Mob) entity);
Expand All @@ -76,26 +76,24 @@ public static void lookAt(Object target, @Nullable Float headRotationSpeed, @Nul
/**
* Instruct a Mob (1.17+) or Players (1.19.1+) to look at a specific vector/location/entity.
* Object can be a {@link org.bukkit.util.Vector}, {@link org.bukkit.Location} or {@link org.bukkit.entity.Entity}
* THIS METHOD IS FOR 1.19.1+ ONLY. Use {@link lookAt(Object, Float, Float, LivingEntity...)} otherwise.
*
* @param entityAnchor What part of the entity the player should face assuming the LivingEntity argument contains a player. Only for players.
* @param target The vector/location/entity to make the livingentity or player look at.
* @param headRotationSpeed The rotation speed at which the living entities will rotate their head to the target. Vanilla default values range from 10-50. Doesn't apply to players.
* THIS METHOD IS FOR 1.19.1+ ONLY. Use {@link lookAt(Object, Float, Float, Entity...)} otherwise.
*
* @param entityAnchor What part of the entity the player should face assuming the entities argument contains a player. Only for players.
* @param target The vector/location/entity to make the entity or player look at.
* @param headRotationSpeed The rotation speed at which the entities will rotate their head to the target. Vanilla default values range from 10-50. Doesn't apply to players.
* @param maxHeadPitch The maximum pitch at which the eyes/feet can go to. Doesn't apply to players.
* @param entities The living entities to make look at something. Players can be involved in 1.19.1+
* @param entities The entities to make look at something.
*/
public static void lookAt(LookAnchor entityAnchor, Object target, @Nullable Float headRotationSpeed, @Nullable Float maxHeadPitch, LivingEntity... entities) {
public static void lookAt(LookAnchor entityAnchor, Object target, @Nullable Float headRotationSpeed, @Nullable Float maxHeadPitch, Entity... entities) {
if (target == null || !LOOK_AT || !LOOK_ANCHORS)
return;
for (LivingEntity entity : entities) {
for (Entity entity : entities) {
if (target instanceof Location && !((Location) target).isWorldLoaded()) {
Location location = (Location) target;
target = new Location(entity.getWorld(), location.getX(), location.getY(), location.getZ());
}
if (entity instanceof Player) {
Player player = (Player) entity;
if (target instanceof Vector) {
Vector vector = (Vector) target;
if (entity instanceof Player player) {
if (target instanceof Vector vector) {
player.lookAt(player.getEyeLocation().add(vector), LookAnchor.EYES);
player.lookAt(player.getEyeLocation().add(vector), LookAnchor.FEET);
} else if (target instanceof Location) {
Expand All @@ -105,8 +103,22 @@ public static void lookAt(LookAnchor entityAnchor, Object target, @Nullable Floa
player.lookAt((Entity) target, LookAnchor.EYES, entityAnchor);
player.lookAt((Entity) target, LookAnchor.FEET, entityAnchor);
}
} else if (entity instanceof Mob) {
mobLookAt(target, headRotationSpeed, maxHeadPitch, (Mob) entity);
} else if (entity instanceof Mob mob) {
mobLookAt(target, headRotationSpeed, maxHeadPitch, mob);
} else {
if (target instanceof Vector vector) {
Location base = entity instanceof LivingEntity living ? living.getEyeLocation() : entity.getLocation();
Location loc = base.add(vector);
entity.lookAt(loc.getX(), loc.getY(), loc.getZ(), entityAnchor);
Comment on lines +110 to +112
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm a little suspicious of this. Can you explain + test this case to me and why living entities act different?

} else if (target instanceof LivingEntity targetEntity) {
Location loc = entityAnchor == LookAnchor.EYES ? targetEntity.getEyeLocation() : targetEntity.getLocation();
entity.lookAt(loc.getX(), loc.getY(), loc.getZ(), entityAnchor);
} else if (target instanceof Entity targetEntity) {
Location loc = targetEntity.getLocation();
entity.lookAt(loc.getX(), loc.getY(), loc.getZ(), entityAnchor);
} else if (target instanceof Location location) {
entity.lookAt(location.getX(), location.getY(), location.getZ(), entityAnchor);
}
}
}
}
Expand Down
38 changes: 23 additions & 15 deletions src/main/java/ch/njol/skript/effects/EffLook.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ch.njol.skript.effects;

import ch.njol.skript.Skript;
import ch.njol.skript.bukkitutil.PaperEntityUtils;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Example;
Expand All @@ -11,12 +10,14 @@
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import io.papermc.paper.entity.LookAnchor;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Entity;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.registration.SyntaxInfo;
import org.skriptlang.skript.registration.SyntaxRegistry;

@Name("Look At")
@Description("Forces the mob(s) or player(s) to look at an entity, vector or location. Vanilla max head pitches range from 10 to 50.")
@Description("Forces entities to look at an entity, vector or location. Vanilla max head pitches range from 10 to 50.")
@Example("force the player to look towards event-entity's feet")
@Example("""
on entity explosion:
Expand All @@ -29,22 +30,29 @@
@Since("2.7")
public class EffLook extends Effect {

static {
Skript.registerEffect(EffLook.class,
"(force|make) %livingentities% [to] (face [towards]|look [(at|towards)]) " +
"%entity%'s (feet:feet|eyes) [(at|with) [head] [rotation] speed %-number%] " +
"[[and] max[imum] [head] pitch %-number%]",
public static void register(SyntaxRegistry registry) {
registry.register(
SyntaxRegistry.EFFECT,
SyntaxInfo.builder(EffLook.class)
.addPatterns(
"(force|make) %entities% [to] (face [towards]|look [(at|towards)]) " +
"%entity%'s (feet:feet|eyes) [(at|with) [head] [rotation] speed %-number%] " +
"[[and] max[imum] [head] pitch %-number%]",

"(force|make) %livingentities% [to] (face [towards]|look [(at|towards)]) " +
"[the] (feet:feet|eyes) of %entity% [(at|with) [head] [rotation] speed %-number%] " +
"[[and] max[imum] [head] pitch %-number%]",
"(force|make) %entities% [to] (face [towards]|look [(at|towards)]) " +
"[the] (feet:feet|eyes) of %entity% [(at|with) [head] [rotation] speed %-number%] " +
"[[and] max[imum] [head] pitch %-number%]",

"(force|make) %livingentities% [to] (face [towards]|look [(at|towards)]) %vector/location/entity% " +
"[(at|with) [head] [rotation] speed %-number%] [[and] max[imum] [head] pitch %-number%]");
"(force|make) %entities% [to] (face [towards]|look [(at|towards)]) %vector/location/entity% " +
"[(at|with) [head] [rotation] speed %-number%] [[and] max[imum] [head] pitch %-number%]"
)
.supplier(EffLook::new)
.build()
);
}

private LookAnchor anchor = LookAnchor.EYES;
private Expression<LivingEntity> entities;
private Expression<Entity> entities;

@Nullable
private Expression<Number> speed, maxPitch;
Expand All @@ -57,7 +65,7 @@ public class EffLook extends Effect {
@SuppressWarnings("unchecked")
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
entities = (Expression<LivingEntity>) exprs[0];
entities = (Expression<Entity>) exprs[0];
target = exprs[1];
speed = (Expression<Number>) exprs[2];
maxPitch = (Expression<Number>) exprs[3];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.skriptlang.skript.bukkit.entity;

import ch.njol.skript.Skript;
import ch.njol.skript.effects.EffLook;
import ch.njol.skript.entity.SimpleEntityData;
import org.bukkit.entity.AbstractNautilus;
import org.skriptlang.skript.addon.AddonModule;
Expand Down Expand Up @@ -38,6 +39,7 @@ protected void loadSelf(SkriptAddon addon) {
}

register(addon,
EffLook::register,
ExprDeathMessage::register
);
}
Expand Down