From 3a26dae51da9397619a0938a91b986b1d8e51d39 Mon Sep 17 00:00:00 2001 From: gnembon Date: Mon, 26 Oct 2020 01:53:48 -0300 Subject: [PATCH] Moar control over angles, fixes #510 --- docs/scarpet/api/Entities.md | 14 ++++-- .../java/carpet/script/value/EntityValue.java | 47 ++++++++++++++++++- 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/docs/scarpet/api/Entities.md b/docs/scarpet/api/Entities.md index e09e97a1..db662ce2 100644 --- a/docs/scarpet/api/Entities.md +++ b/docs/scarpet/api/Entities.md @@ -127,10 +127,14 @@ Quin-tuple of the entity's position (x, y, and z coords), and rotation (yaw, pit Respective component of entity's coordinates -### `query(e, 'pitch'), query(e, 'yaw')` +### `query(e, 'pitch')`, `query(e, 'yaw')` Pitch and Yaw or where entity is looking. +### `query(e, 'head_yaw')`, `query(e, 'body_yaw')` + +Applies to living entites. Sets their individual head and body facing angle. + ### `query(e, 'look')` Returns a 3d vector where the entity is looking. @@ -522,9 +526,13 @@ Changes full location vector all at once. Changes entity's location in the specified direction. -### `modify(e, 'pitch', pitch), modify(e, 'yaw', yaw)` +### `modify(e, 'pitch', angle), modify(e, 'yaw', angle)` -Changes entity's pitch or yaw. +Changes entity's pitch or yaw angle. + +### `modify(e, 'head_yaw', angle)`, `modify(e, 'body_yaw', angle)` + +For living entities, controls their head and body yaw angle. ### `modify(e, 'move', x, y, z), modify(e, 'move', l(x,y,z) )` diff --git a/src/main/java/carpet/script/value/EntityValue.java b/src/main/java/carpet/script/value/EntityValue.java index 693c1cb5..6a83c83d 100644 --- a/src/main/java/carpet/script/value/EntityValue.java +++ b/src/main/java/carpet/script/value/EntityValue.java @@ -295,6 +295,21 @@ public class EntityValue extends Value put("tags", (e, a) -> ListValue.wrap(e.getScoreboardTags().stream().map(StringValue::new).collect(Collectors.toList()))); put("has_tag", (e, a) -> new NumericValue(e.getScoreboardTags().contains(a.getString()))); put("yaw", (e, a)-> new NumericValue(e.yaw)); + put("head_yaw", (e, a)-> { + if (e instanceof LivingEntity) + { + return new NumericValue(e.getHeadYaw()); + } + return Value.NULL; + }); + put("body_yaw", (e, a)-> { + if (e instanceof LivingEntity) + { + return new NumericValue(((LivingEntity) e).bodyYaw); + } + return Value.NULL; + }); + put("pitch", (e, a)-> new NumericValue(e.pitch)); put("look", (e, a) -> { Vec3d look = e.getRotationVector(); @@ -661,9 +676,22 @@ public class EntityValue extends Value else { e.refreshPositionAndAngles(x, y, z, yaw, pitch); - ((ServerWorld) e.getEntityWorld()).getChunkManager().sendToNearbyPlayers(e, new EntityPositionS2CPacket(e)); + // we were sending to players for not-living entites, that were untracked. Living entities should be tracked. + //((ServerWorld) e.getEntityWorld()).getChunkManager().sendToNearbyPlayers(e, new EntityS2CPacket.(e)); + if (e instanceof LivingEntity) + { + LivingEntity le = (LivingEntity)e; + le.prevBodyYaw = le.prevYaw = yaw; + le.prevHeadYaw = le.headYaw = yaw; + // seems universal for: + //e.setHeadYaw(yaw); + //e.setYaw(yaw); + } + else + { + ((ServerWorld) e.getEntityWorld()).getChunkManager().sendToNearbyPlayers(e, new EntityPositionS2CPacket(e)); + } } - } private static void updateVelocity(Entity e) @@ -753,6 +781,21 @@ public class EntityValue extends Value { updatePosition(e, e.getX(), e.getY(), e.getZ(), ((float)NumericValue.asNumber(v).getDouble()) % 360, e.pitch); }); + put("head_yaw", (e, v) -> + { + if (e instanceof LivingEntity) + { + e.setHeadYaw((float)NumericValue.asNumber(v).getDouble() % 360); + } + }); + put("body_yaw", (e, v) -> + { + if (e instanceof LivingEntity) + { + e.setYaw((float)NumericValue.asNumber(v).getDouble() % 360); + } + }); + put("pitch", (e, v) -> { updatePosition(e, e.getX(), e.getY(), e.getZ(), e.yaw, MathHelper.clamp((float)NumericValue.asNumber(v).getDouble(), -90, 90));