diff --git a/main/src/main/java/net/citizensnpcs/npc/skin/SkinUpdateTracker.java b/main/src/main/java/net/citizensnpcs/npc/skin/SkinUpdateTracker.java index ad835a912..42fa59b76 100644 --- a/main/src/main/java/net/citizensnpcs/npc/skin/SkinUpdateTracker.java +++ b/main/src/main/java/net/citizensnpcs/npc/skin/SkinUpdateTracker.java @@ -84,10 +84,10 @@ private boolean canSee(Player player, SkinnableEntity skinnable, boolean checkFo double deltaX = skinLoc.getX() - playerLoc.getX(); double deltaZ = skinLoc.getZ() - playerLoc.getZ(); double angle = Math.atan2(deltaX, deltaZ); - float skinYaw = Util.clampYaw(-(float) Math.toDegrees(angle)); - float playerYaw = Util.clampYaw(playerLoc.getYaw()); - float upperBound = Util.clampYaw(playerYaw + FIELD_OF_VIEW); - float lowerBound = Util.clampYaw(playerYaw - FIELD_OF_VIEW); + float skinYaw = Util.clamp(-(float) Math.toDegrees(angle)); + float playerYaw = Util.clamp(playerLoc.getYaw()); + float upperBound = Util.clamp(playerYaw + FIELD_OF_VIEW); + float lowerBound = Util.clamp(playerYaw - FIELD_OF_VIEW); if (upperBound == -180.0 && playerYaw > 0) { upperBound = 0; } @@ -404,10 +404,10 @@ void reset(Player player) { player.getLocation(this.location); if (rotationCount < 3) { float rotationDegrees = Setting.NPC_SKIN_ROTATION_UPDATE_DEGREES.asFloat(); - float yaw = Util.clampYaw(this.location.getYaw()); + float yaw = Util.clamp(this.location.getYaw()); this.startYaw = yaw; - this.upperBound = Util.clampYaw(yaw + rotationDegrees); - this.lowerBound = Util.clampYaw(yaw - rotationDegrees); + this.upperBound = Util.clamp(yaw + rotationDegrees); + this.lowerBound = Util.clamp(yaw - rotationDegrees); if (upperBound == -180.0 && startYaw > 0) { upperBound = 0; } @@ -423,7 +423,7 @@ boolean shouldUpdate(Player player) { } if (rotationCount < 3) { - float yaw = Util.clampYaw(currentLoc.getYaw()); + float yaw = Util.clamp(currentLoc.getYaw()); boolean hasRotated; if (startYaw - 90 < -180 || startYaw + 90 > 180) { hasRotated = yaw > lowerBound && yaw < upperBound; diff --git a/main/src/main/java/net/citizensnpcs/trait/Poses.java b/main/src/main/java/net/citizensnpcs/trait/Poses.java index 1b4252892..4e584354b 100644 --- a/main/src/main/java/net/citizensnpcs/trait/Poses.java +++ b/main/src/main/java/net/citizensnpcs/trait/Poses.java @@ -64,7 +64,7 @@ private void assumePose(float yaw, float pitch) { if (!npc.isSpawned()) { npc.spawn(npc.getOrAddTrait(CurrentLocation.class).getLocation(), SpawnReason.COMMAND); } - Util.assumePose(npc.getEntity(), yaw, pitch); + Util.setRotation(npc.getEntity(), yaw, pitch); } /** diff --git a/main/src/main/java/net/citizensnpcs/util/Util.java b/main/src/main/java/net/citizensnpcs/util/Util.java index 3c9a43f6e..b8879dd2c 100644 --- a/main/src/main/java/net/citizensnpcs/util/Util.java +++ b/main/src/main/java/net/citizensnpcs/util/Util.java @@ -4,10 +4,8 @@ import java.util.Random; import java.util.Set; import java.util.UUID; -import java.util.regex.Pattern; import org.bukkit.Bukkit; -import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; @@ -33,14 +31,9 @@ import net.citizensnpcs.npc.ai.NPCHolder; public class Util { - // Static class for small (emphasis small) utility methods private Util() { } - public static void assumePose(Entity entity, float yaw, float pitch) { - NMS.look(entity, yaw, pitch); - } - public static void callCollisionEvent(NPC npc, Entity entity) { if (NPCCollisionEvent.getHandlerList().getRegisteredListeners().length > 0) { Bukkit.getPluginManager().callEvent(new NPCCollisionEvent(npc, entity)); @@ -65,15 +58,18 @@ public static Vector callPushEvent(NPC npc, double x, double y, double z) { return !event.isCancelled() ? event.getCollisionVector() : null; } - public static float clampYaw(float yaw) { - while (yaw < -180.0F) { - yaw += 360.0F; + /** + * Clamps the rotation angle to [-180, 180] + */ + public static float clamp(float angle) { + while (angle < -180.0F) { + angle += 360.0F; } - while (yaw >= 180.0F) { - yaw -= 360.0F; + while (angle >= 180.0F) { + angle -= 360.0F; } - return yaw; + return angle; } public static void face(Entity entity, float yaw, float pitch) { @@ -82,13 +78,13 @@ public static void face(Entity entity, float yaw, float pitch) { faceLocation(entity, entity.getLocation(AT_LOCATION).clone().add(vector)); } - public static void faceEntity(Entity entity, Entity at) { - if (at == null || entity == null || entity.getWorld() != at.getWorld()) + public static void faceEntity(Entity entity, Entity to) { + if (to == null || entity == null || entity.getWorld() != to.getWorld()) return; - if (at instanceof LivingEntity) { - NMS.look(entity, at); + if (to instanceof LivingEntity) { + NMS.look(entity, to); } else { - faceLocation(entity, at.getLocation(AT_LOCATION)); + faceLocation(entity, to.getLocation(AT_LOCATION)); } } @@ -124,7 +120,7 @@ public static void generateTeamFor(NPC npc, String name, String teamName) { } public static Location getCenterLocation(Block block) { - Location bloc = block.getLocation(); + Location bloc = block.getLocation(AT_LOCATION); Location center = new Location(bloc.getWorld(), bloc.getBlockX() + 0.5, bloc.getBlockY(), bloc.getBlockZ() + 0.5); BoundingBox bb = NMS.getCollisionBox(block); @@ -134,6 +130,9 @@ public static Location getCenterLocation(Block block) { return center; } + /** + * Returns the yaw to face along the given velocity (corrected for dragon yaw i.e. facing backwards) + */ public static float getDragonYaw(Entity entity, double motX, double motZ) { Location location = entity.getLocation(AT_LOCATION); double x = location.getX(); @@ -250,7 +249,7 @@ public static boolean locationWithinRange(Location current, Location target, dou return false; if (current.getWorld() != target.getWorld()) return false; - return current.distanceSquared(target) <= Math.pow(range, 2); + return current.distance(target) <= range; } public static EntityType matchEntityType(String toMatch) { @@ -343,52 +342,11 @@ public static void sendTeamPacketToOnlinePlayers(Team team, int mode) { } } - public static String[] splitPlayerName(String coloredName) { - String name = coloredName, prefix = null, suffix = null; - if (coloredName.length() > 16) { - if (coloredName.length() >= 30) { - prefix = coloredName.substring(0, 16); - int len = 30; - name = coloredName.substring(16, 30); - String prefixColors = ChatColor.getLastColors(prefix); - if (prefixColors.isEmpty()) { - if (NON_ALPHABET_MATCHER.matcher(name).matches()) { - if (coloredName.length() >= 32) { - len = 32; - name = coloredName.substring(16, 32); - } else if (coloredName.length() == 31) { - len = 31; - name = coloredName.substring(16, 31); - } - } else { - prefixColors = ChatColor.RESET.toString(); - } - } else if (prefixColors.length() > 2) { - prefixColors = prefixColors.substring(prefixColors.length() - 2); - } - name = prefixColors + name; - suffix = coloredName.substring(len); - } else { - prefix = coloredName.substring(0, coloredName.length() - 16); - name = coloredName.substring(prefix.length()); - if (prefix.endsWith(String.valueOf(ChatColor.COLOR_CHAR))) { - prefix = prefix.substring(0, prefix.length() - 1); - name = ChatColor.COLOR_CHAR + name; - } - String prefixColors = ChatColor.getLastColors(prefix); - if (prefixColors.isEmpty() && !NON_ALPHABET_MATCHER.matcher(name).matches()) { - prefixColors = ChatColor.RESET.toString(); - } else if (prefixColors.length() > 2) { - prefixColors = prefixColors.substring(prefixColors.length() - 2); - } - name = prefixColors + name; - if (name.length() > 16) { - suffix = name.substring(16); - name = name.substring(0, 16); - } - } - } - return new String[] { name, prefix, suffix }; + /** + * Sets the entity's yaw and pitch directly including head yaw. + */ + public static void setRotation(Entity entity, float yaw, float pitch) { + NMS.look(entity, yaw, pitch); } public static void updateNPCTeams(Player toUpdate, int mode) { @@ -407,5 +365,4 @@ public static void updateNPCTeams(Player toUpdate, int mode) { private static final Location AT_LOCATION = new Location(null, 0, 0, 0); private static final Scoreboard DUMMY_SCOREBOARD = Bukkit.getScoreboardManager().getNewScoreboard(); private static String MINECRAFT_REVISION; - private static final Pattern NON_ALPHABET_MATCHER = Pattern.compile(".*[^A-Za-z0-9_].*"); }