From c67c16f8adfffdc6f4815c7b31a3fe9857412cd2 Mon Sep 17 00:00:00 2001 From: LadyCailin Date: Thu, 25 Apr 2024 19:53:20 +0200 Subject: [PATCH] Add set_entity_rotation --- .../com/laytonsmith/abstraction/MCEntity.java | 2 + .../bukkit/entities/BukkitMCEntity.java | 4 + .../core/functions/EntityManagement.java | 73 +++++++++++++++++++ 3 files changed, 79 insertions(+) diff --git a/src/main/java/com/laytonsmith/abstraction/MCEntity.java b/src/main/java/com/laytonsmith/abstraction/MCEntity.java index b94d1e24c..45403cbae 100644 --- a/src/main/java/com/laytonsmith/abstraction/MCEntity.java +++ b/src/main/java/com/laytonsmith/abstraction/MCEntity.java @@ -112,4 +112,6 @@ public interface MCEntity extends MCMetadatable { int getEntityId(); boolean isInWater(); + + void setRotation(float yaw, float pitch); } diff --git a/src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCEntity.java b/src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCEntity.java index d0d0811df..c2ac8d17c 100644 --- a/src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCEntity.java +++ b/src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCEntity.java @@ -398,4 +398,8 @@ public boolean isInWater() { return e.isInWater(); } + @Override + public void setRotation(float yaw, float pitch) { + e.setRotation(yaw, pitch); + } } diff --git a/src/main/java/com/laytonsmith/core/functions/EntityManagement.java b/src/main/java/com/laytonsmith/core/functions/EntityManagement.java index fcf443a92..39ea6b486 100644 --- a/src/main/java/com/laytonsmith/core/functions/EntityManagement.java +++ b/src/main/java/com/laytonsmith/core/functions/EntityManagement.java @@ -151,6 +151,7 @@ import com.laytonsmith.core.exceptions.CRE.CREPlayerOfflineException; import com.laytonsmith.core.exceptions.CRE.CRERangeException; import com.laytonsmith.core.exceptions.CRE.CREThrowable; +import com.laytonsmith.core.exceptions.CRE.CREUnsupportedOperationException; import com.laytonsmith.core.exceptions.ConfigCompileException; import com.laytonsmith.core.exceptions.ConfigRuntimeException; import com.laytonsmith.core.natives.interfaces.Mixed; @@ -5491,6 +5492,78 @@ public Mixed exec(Target t, Environment env, Mixed... args) throws ConfigRuntime return CBoolean.get(entity.isInWater()); } + @Override + public Version since() { + return MSVersion.V3_3_5; + } + } + + @api + public static class set_entity_rotation extends AbstractFunction { + + @Override + public Class[] thrown() { + return new Class[]{CREUnsupportedOperationException.class, CRELengthException.class}; + } + + @Override + public boolean isRestricted() { + return true; + } + + @Override + public Boolean runAsync() { + return false; + } + + @Override + public Mixed exec(Target t, Environment env, Mixed... args) throws ConfigRuntimeException { + MCEntity entity = Static.getEntity(args[0], t); + + if(entity instanceof MCPlayer) { + throw new CREUnsupportedOperationException(getName() + " cannot be used on players.", t); + } + + float yaw = (float) ArgumentValidation.getDouble(args[1], t); + yaw %= 360.0F; + if(yaw >= 180.0) { + yaw -= 360.0F; + } else if(yaw < -180.0) { + yaw += 360.0F; + } + + float pitch; + if(args.length == 3) { + pitch = (float) ArgumentValidation.getDouble(args[2], t); + if(pitch > 90.0) { + pitch = 90.0F; + } else if(pitch < -90.0) { + pitch = -90.0F; + } + } else { + pitch = entity.getLocation().getPitch(); + } + + entity.setRotation(yaw, pitch); + return CVoid.VOID; + } + + @Override + public String getName() { + return "set_entity_rotation"; + } + + @Override + public Integer[] numArgs() { + return new Integer[]{2, 3}; + } + + @Override + public String docs() { + return "void {entityUUID, yaw, [pitch]} Sets an entity's yaw and pitch without teleporting or ejecting. If used" + + " on a player, an UnsupportedOperationException is thrown."; + } + @Override public Version since() { return MSVersion.V3_3_5;