diff --git a/src/main/java/net/minestom/server/entity/ai/goal/CombinedAttackGoal.java b/src/main/java/net/minestom/server/entity/ai/goal/CombinedAttackGoal.java index cc0c53d1a..8b2b1bd9a 100644 --- a/src/main/java/net/minestom/server/entity/ai/goal/CombinedAttackGoal.java +++ b/src/main/java/net/minestom/server/entity/ai/goal/CombinedAttackGoal.java @@ -9,6 +9,7 @@ import net.minestom.server.entity.type.projectile.EntityProjectile; import net.minestom.server.utils.Position; import net.minestom.server.utils.time.Cooldown; import net.minestom.server.utils.time.TimeUnit; +import net.minestom.server.utils.time.UpdateOption; import net.minestom.server.utils.validate.Check; import org.jetbrains.annotations.NotNull; @@ -19,6 +20,9 @@ import java.util.function.Function; */ public class CombinedAttackGoal extends GoalSelector { + private final UpdateOption pathUpdateOptions = new UpdateOption(5, TimeUnit.TICK); + private long lastPathUpdate; + private final int meleeRangeSquared; private final int meleeDelay; private final TimeUnit meleeTimeUnit; @@ -90,6 +94,10 @@ public class CombinedAttackGoal extends GoalSelector { Check.argCondition(desirableRange > rangedRange, "Desirable range can not exceed ranged range!"); } + public UpdateOption getPathUpdateOptions() { + return this.pathUpdateOptions; + } + public void setProjectileGenerator(Function projectileGenerator) { this.projectileGenerator = projectileGenerator; } @@ -159,7 +167,10 @@ public class CombinedAttackGoal extends GoalSelector { // Otherwise going to the target. Position targetPosition = target.getPosition(); if (pathPosition == null || !pathPosition.isSimilar(targetPosition)) { - navigator.setPathTo(targetPosition); + if (!Cooldown.hasCooldown(time, this.lastPathUpdate, getPathUpdateOptions())) { + this.lastPathUpdate = time; + navigator.setPathTo(targetPosition); + } } } diff --git a/src/main/java/net/minestom/server/entity/ai/goal/MeleeAttackGoal.java b/src/main/java/net/minestom/server/entity/ai/goal/MeleeAttackGoal.java index 001d8044b..892c324be 100644 --- a/src/main/java/net/minestom/server/entity/ai/goal/MeleeAttackGoal.java +++ b/src/main/java/net/minestom/server/entity/ai/goal/MeleeAttackGoal.java @@ -8,6 +8,7 @@ import net.minestom.server.entity.pathfinding.Navigator; import net.minestom.server.utils.Position; import net.minestom.server.utils.time.Cooldown; import net.minestom.server.utils.time.TimeUnit; +import net.minestom.server.utils.time.UpdateOption; import org.jetbrains.annotations.NotNull; /** @@ -16,6 +17,9 @@ import org.jetbrains.annotations.NotNull; */ public class MeleeAttackGoal extends GoalSelector { + private final UpdateOption pathUpdateOptions = new UpdateOption(5, TimeUnit.TICK); + private long lastPathUpdate; + private long lastHit; private final int delay; private final TimeUnit timeUnit; @@ -37,6 +41,10 @@ public class MeleeAttackGoal extends GoalSelector { this.timeUnit = timeUnit; } + public UpdateOption getPathUpdateOptions() { + return this.pathUpdateOptions; + } + @Override public boolean shouldStart() { this.cachedTarget = findTarget(); @@ -77,7 +85,10 @@ public class MeleeAttackGoal extends GoalSelector { final Position pathPosition = navigator.getPathPosition(); final Position targetPosition = target.getPosition(); if (pathPosition == null || !pathPosition.isSimilar(targetPosition)) { - navigator.setPathTo(targetPosition); + if (!Cooldown.hasCooldown(time, this.lastPathUpdate, getPathUpdateOptions())) { + this.lastPathUpdate = time; + navigator.setPathTo(targetPosition); + } } } } diff --git a/src/main/java/net/minestom/server/entity/ai/goal/RangedAttackGoal.java b/src/main/java/net/minestom/server/entity/ai/goal/RangedAttackGoal.java index 14f7f8de8..66128e7b2 100644 --- a/src/main/java/net/minestom/server/entity/ai/goal/RangedAttackGoal.java +++ b/src/main/java/net/minestom/server/entity/ai/goal/RangedAttackGoal.java @@ -9,6 +9,7 @@ import net.minestom.server.entity.type.projectile.EntityProjectile; import net.minestom.server.utils.Position; import net.minestom.server.utils.time.Cooldown; import net.minestom.server.utils.time.TimeUnit; +import net.minestom.server.utils.time.UpdateOption; import net.minestom.server.utils.validate.Check; import org.jetbrains.annotations.NotNull; @@ -16,6 +17,9 @@ import java.util.function.Function; public class RangedAttackGoal extends GoalSelector { + private final UpdateOption pathUpdateOptions = new UpdateOption(5, TimeUnit.TICK); + private long lastPathUpdate; + private long lastShot; private final int delay; private final TimeUnit timeUnit; @@ -52,6 +56,10 @@ public class RangedAttackGoal extends GoalSelector { Check.argCondition(desirableRange > attackRange, "Desirable range can not exceed attack range!"); } + public UpdateOption getPathUpdateOptions() { + return this.pathUpdateOptions; + } + public void setProjectileGenerator(Function projectileGenerator) { this.projectileGenerator = projectileGenerator; } @@ -111,7 +119,10 @@ public class RangedAttackGoal extends GoalSelector { } Position targetPosition = target.getPosition(); if (pathPosition == null || !pathPosition.isSimilar(targetPosition)) { - navigator.setPathTo(targetPosition); + if (!Cooldown.hasCooldown(time, this.lastPathUpdate, getPathUpdateOptions())) { + this.lastPathUpdate = time; + navigator.setPathTo(targetPosition); + } } }