From bd8d5a7f743b229037f9f7e14587316e6a9a1518 Mon Sep 17 00:00:00 2001 From: fullwall Date: Mon, 13 Mar 2023 02:26:13 +0800 Subject: [PATCH] Fix NumberFormatException in latest builds --- .../main/java/net/citizensnpcs/Settings.java | 2 +- .../npc/ai/CitizensNavigator.java | 6 +- .../net/citizensnpcs/trait/SkinTrait.java | 4 +- .../net/citizensnpcs/util/Quaternion.java | 72 +++++++++++++++++++ 4 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 main/src/main/java/net/citizensnpcs/util/Quaternion.java diff --git a/main/src/main/java/net/citizensnpcs/Settings.java b/main/src/main/java/net/citizensnpcs/Settings.java index 293c16b2f..c325d94f4 100644 --- a/main/src/main/java/net/citizensnpcs/Settings.java +++ b/main/src/main/java/net/citizensnpcs/Settings.java @@ -100,7 +100,7 @@ public class Settings { DEFAULT_REALISTIC_LOOKING("npc.default.realistic-looking", "npc.default.look-close.realistic-looking", false), DEFAULT_SPAWN_NODAMAGE_DURATION("npc.default.spawn-nodamage-ticks", "npc.default.spawn-nodamage-duration", "1s"), - DEFAULT_STATIONARY_TICKS("npc.default.stationary-ticks", -1), + DEFAULT_STATIONARY_DURATION("npc.default.stationary-ticks", "npc.default.stationary-duration", -1), DEFAULT_STRAIGHT_LINE_TARGETING_DISTANCE("npc.pathfinding.straight-line-targeting-distance", 5), DEFAULT_TALK_CLOSE("npc.default.talk-close.enabled", false), DEFAULT_TALK_CLOSE_RANGE("npc.default.talk-close.range", 5), diff --git a/main/src/main/java/net/citizensnpcs/npc/ai/CitizensNavigator.java b/main/src/main/java/net/citizensnpcs/npc/ai/CitizensNavigator.java index 9dad16936..d85a29150 100644 --- a/main/src/main/java/net/citizensnpcs/npc/ai/CitizensNavigator.java +++ b/main/src/main/java/net/citizensnpcs/npc/ai/CitizensNavigator.java @@ -50,10 +50,10 @@ public class CitizensNavigator implements Navigator, Runnable { .range(Setting.DEFAULT_PATHFINDING_RANGE.asFloat()).debug(Setting.DEBUG_PATHFINDING.asBoolean()) .defaultAttackStrategy(MCTargetStrategy.DEFAULT_ATTACK_STRATEGY) .attackRange(Setting.NPC_ATTACK_DISTANCE.asDouble()) - .updatePathRate(Setting.DEFAULT_PATHFINDER_UPDATE_PATH_RATE.asInt()) + .updatePathRate(Setting.DEFAULT_PATHFINDER_UPDATE_PATH_RATE.asTicks()) .distanceMargin(Setting.DEFAULT_DISTANCE_MARGIN.asDouble()) .pathDistanceMargin(Setting.DEFAULT_PATH_DISTANCE_MARGIN.asDouble()) - .stationaryTicks(Setting.DEFAULT_STATIONARY_TICKS.asInt()).stuckAction(TeleportStuckAction.INSTANCE) + .stationaryTicks(Setting.DEFAULT_STATIONARY_DURATION.asTicks()).stuckAction(TeleportStuckAction.INSTANCE) .examiner(new MinecraftBlockExaminer()).useNewPathfinder(Setting.USE_NEW_PATHFINDER.asBoolean()) .straightLineTargetingDistance(Setting.DEFAULT_STRAIGHT_LINE_TARGETING_DISTANCE.asFloat()) .destinationTeleportMargin(Setting.DEFAULT_DESTINATION_TELEPORT_MARGIN.asDouble()); @@ -236,7 +236,7 @@ public class CitizensNavigator implements Navigator, Runnable { } else { root.removeKey("pathfindingrange"); } - if (defaultParams.stationaryTicks() != Setting.DEFAULT_STATIONARY_TICKS.asTicks()) { + if (defaultParams.stationaryTicks() != Setting.DEFAULT_STATIONARY_DURATION.asTicks()) { root.setInt("stationaryticks", defaultParams.stationaryTicks()); } else { root.removeKey("stationaryticks"); diff --git a/main/src/main/java/net/citizensnpcs/trait/SkinTrait.java b/main/src/main/java/net/citizensnpcs/trait/SkinTrait.java index c927c5264..03e21fe20 100644 --- a/main/src/main/java/net/citizensnpcs/trait/SkinTrait.java +++ b/main/src/main/java/net/citizensnpcs/trait/SkinTrait.java @@ -9,6 +9,7 @@ import net.citizensnpcs.api.persistence.Persist; import net.citizensnpcs.api.trait.Trait; import net.citizensnpcs.api.trait.TraitName; import net.citizensnpcs.api.util.DataKey; +import net.citizensnpcs.api.util.Messaging; import net.citizensnpcs.api.util.Placeholders; import net.citizensnpcs.npc.skin.SkinnableEntity; import net.md_5.bungee.api.ChatColor; @@ -35,7 +36,8 @@ public class SkinTrait extends Trait { private void checkPlaceholder(boolean update) { if (skinName == null) return; - String filled = ChatColor.stripColor(Placeholders.replace(skinName, null, npc).toLowerCase()); + String filled = ChatColor.stripColor(Placeholders.replace(skinName, null, npc).toLowerCase()); + Messaging.idebug(() -> skinName + " " + filled + " " + filledPlaceholder); if (!filled.equalsIgnoreCase(skinName) && !filled.equalsIgnoreCase(filledPlaceholder)) { filledPlaceholder = filled; if (update) { diff --git a/main/src/main/java/net/citizensnpcs/util/Quaternion.java b/main/src/main/java/net/citizensnpcs/util/Quaternion.java new file mode 100644 index 000000000..ef933a8a0 --- /dev/null +++ b/main/src/main/java/net/citizensnpcs/util/Quaternion.java @@ -0,0 +1,72 @@ +package net.citizensnpcs.util; + +import org.bukkit.util.EulerAngle; + +public class Quaternion { + public final double w; + public final double x; + public final double y; + public final double z; + + public Quaternion(double x, double y, double z, double w) { + this.w = w; + this.x = x; + this.y = y; + this.z = z; + } + + public double dot(Quaternion b) { + return x * b.x + y * b.y + z * b.z + w * b.w; + } + + public double length() { + return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w; + } + + public Quaternion mul(double m) { + return new Quaternion(x * m, y * m, z * m, w * m); + } + + public Quaternion norm() { + double length = length(); + if (length > 0.0001) { + double i = fastisqrt(length); + return new Quaternion(i * x, i * y, i * z, i * w); + } + return ZERO; + } + + private static double fastisqrt(double x) { + double xhalf = 0.5d * x; + long i = Double.doubleToLongBits(x); + i = 0x5fe6ec85e7de30daL - (i >> 1); + x = Double.longBitsToDouble(i); + x *= (1.5d - xhalf * x * x); + return x; + } + + public static Quaternion from(EulerAngle from) { + return fromEuler(from.getX(), from.getY(), from.getZ()); + } + + public static Quaternion fromEuler(double x, double y, double z) { + double c1 = Math.cos(x * 0.5); + double c2 = Math.cos(y * 0.5); + double c3 = Math.cos(z * 0.5); + double s1 = Math.sin(x * 0.5); + double s2 = Math.sin(y * 0.5); + double s3 = Math.sin(z * 0.5); + return new Quaternion(s1 * s2 * c3 + c1 * c2 * s3, s1 * c2 * c3 + c1 * s2 * s3, c1 * s2 * c3 - s1 * c2 * s3, + c1 * c2 * c3 - s1 * s2 * s3); + } + + public static Quaternion lerp(Quaternion a, Quaternion b, double t) { + if (a.dot(b) < 0) { + b = b.mul(-1); + } + return new Quaternion(a.x - t * (a.x - b.x), a.y - t * (a.y - b.y), a.z - t * (a.z - b.z), + a.w - t * (a.w - b.w)); + } + + private static final Quaternion ZERO = new Quaternion(0, 0, 0, 0); +}