diff --git a/docs/scarpet/api/Entities.md b/docs/scarpet/api/Entities.md index 9d8c4473..0c38a2ca 100644 --- a/docs/scarpet/api/Entities.md +++ b/docs/scarpet/api/Entities.md @@ -589,6 +589,12 @@ Changes entity's location in the specified direction. Changes entity's pitch or yaw angle. +### `modify(e, 'look', x, y, z), modify(e, 'look', [x,y,z] )` + +Sets entity's 3d vector where the entity is looking. +For cases where the vector has a length of 0, yaw and pitch won't get changed. +When pointing straight up or down, yaw will stay the same. + ### `modify(e, 'head_yaw', angle)`, `modify(e, 'body_yaw', angle)` For living entities, controls their head and body yaw angle. diff --git a/src/main/java/carpet/script/value/EntityValue.java b/src/main/java/carpet/script/value/EntityValue.java index 0bce0ae6..bf125311 100644 --- a/src/main/java/carpet/script/value/EntityValue.java +++ b/src/main/java/carpet/script/value/EntityValue.java @@ -1034,7 +1034,22 @@ public class EntityValue extends Value updatePosition(e, e.getX(), e.getY(), e.getZ(), e.yaw, MathHelper.clamp((float)NumericValue.asNumber(v).getDouble(), -90, 90)); }); - //"look" + put("look", (e, v) -> { + if (!(v instanceof ListValue)) throw new InternalExpressionException("Expected a list of 3 parameters as a second argument"); + List vec = ((ListValue)v).getItems(); + float x = NumericValue.asNumber(vec.get(0)).getFloat(); + float y = NumericValue.asNumber(vec.get(1)).getFloat(); + float z = NumericValue.asNumber(vec.get(2)).getFloat(); + float l = MathHelper.sqrt(x*x + y*y + z*z); + if(l==0) return; + x /= l; + y /= l; + z /= l; + float pitch = (float) -Math.asin(y) / 0.017453292F; + float yaw = (float) (x==0 && z==0 ? e.yaw : MathHelper.atan2(-x,z) / 0.017453292F); + updatePosition(e, e.getX(), e.getY(), e.getZ(), yaw, pitch); + }); + //"turn" //"nod"