From 85a84dbea6a591d78de40b09d4221bf6c4260b9a Mon Sep 17 00:00:00 2001 From: LeoDog896 Date: Wed, 13 Jan 2021 07:33:51 -0500 Subject: [PATCH 1/4] Add range to MeleeAttackGoal --- .../minestom/server/entity/ai/goal/MeleeAttackGoal.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) 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 30fe5f750..ee866b06a 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 @@ -20,17 +20,20 @@ public class MeleeAttackGoal extends GoalSelector { private long lastHit; private final int delay; private final TimeUnit timeUnit; + private final int range; private boolean stop; /** * @param entityCreature the entity to add the goal to * @param delay the delay between each attacks + * @param range the allowed range the entity can attack others. * @param timeUnit the unit of the delay */ - public MeleeAttackGoal(@NotNull EntityCreature entityCreature, int delay, @NotNull TimeUnit timeUnit) { + public MeleeAttackGoal(@NotNull EntityCreature entityCreature, int delay, int range, @NotNull TimeUnit timeUnit) { super(entityCreature); this.delay = delay; + this.range = range; this.timeUnit = timeUnit; } @@ -56,7 +59,7 @@ public class MeleeAttackGoal extends GoalSelector { if (!stop) { // Attack the target entity - if (entityCreature.getBoundingBox().intersect(target)) { + if (entityCreature.getDistance(target) <= range) { if (!CooldownUtils.hasCooldown(time, lastHit, timeUnit, delay)) { entityCreature.attack(target, true); this.lastHit = time; From 2cd5a362a4ed23ee08a8c698c98fd84a85057e7c Mon Sep 17 00:00:00 2001 From: LeoDog896 Date: Wed, 13 Jan 2021 07:37:20 -0500 Subject: [PATCH 2/4] Add eating check for instances. --- .../net/minestom/server/entity/ai/goal/EatBlockGoal.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/net/minestom/server/entity/ai/goal/EatBlockGoal.java b/src/main/java/net/minestom/server/entity/ai/goal/EatBlockGoal.java index 9fd6229a7..f5c11e391 100644 --- a/src/main/java/net/minestom/server/entity/ai/goal/EatBlockGoal.java +++ b/src/main/java/net/minestom/server/entity/ai/goal/EatBlockGoal.java @@ -39,7 +39,14 @@ public class EatBlockGoal extends GoalSelector { if (RANDOM.nextInt(chancePerTick) != 0) { return false; } + final Instance instance = entityCreature.getInstance(); + + // An entity shouldn't be eating blocks on null instances. + if (instance == null) { + return false; + } + final BlockPosition blockPosition = entityCreature.getPosition().toBlockPosition(); final short blockStateIdIn = instance.getBlockStateId(blockPosition.clone().subtract(0, 1, 0)); final short blockStateIdBelow = instance.getBlockStateId(blockPosition.clone().subtract(0, 2, 0)); From 96d581a49f829546fc7a99db02ae7ebd15382fe3 Mon Sep 17 00:00:00 2001 From: LeoDog896 Date: Wed, 13 Jan 2021 07:50:41 -0500 Subject: [PATCH 3/4] Optimize EatBlockGoal with fastutil --- .../net/minestom/server/entity/ai/goal/EatBlockGoal.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/ai/goal/EatBlockGoal.java b/src/main/java/net/minestom/server/entity/ai/goal/EatBlockGoal.java index f5c11e391..9104590dc 100644 --- a/src/main/java/net/minestom/server/entity/ai/goal/EatBlockGoal.java +++ b/src/main/java/net/minestom/server/entity/ai/goal/EatBlockGoal.java @@ -1,5 +1,6 @@ package net.minestom.server.entity.ai.goal; +import it.unimi.dsi.fastutil.shorts.Short2ShortArrayMap; import net.minestom.server.entity.EntityCreature; import net.minestom.server.entity.ai.GoalSelector; import net.minestom.server.instance.Instance; @@ -11,8 +12,8 @@ import java.util.Random; public class EatBlockGoal extends GoalSelector { private static final Random RANDOM = new Random(); - private final Map eatBelowMap; - private final Map eatInMap; + private final Short2ShortArrayMap eatBelowMap; + private final Short2ShortArrayMap eatInMap; private final int chancePerTick; private int eatAnimationTick; @@ -24,8 +25,8 @@ public class EatBlockGoal extends GoalSelector { */ public EatBlockGoal( @NotNull EntityCreature entityCreature, - @NotNull Map eatInMap, - @NotNull Map eatBelowMap, + @NotNull Short2ShortArrayMap eatInMap, + @NotNull Short2ShortArrayMap eatBelowMap, int chancePerTick) { super(entityCreature); this.eatInMap = eatInMap; From 97b7e122e971b4faa4851bdf92f51538d6ffdb0f Mon Sep 17 00:00:00 2001 From: LeoDog896 Date: Wed, 13 Jan 2021 07:54:06 -0500 Subject: [PATCH 4/4] Add instance check to NavigableEntity --- .../minestom/server/entity/pathfinding/NavigableEntity.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/net/minestom/server/entity/pathfinding/NavigableEntity.java b/src/main/java/net/minestom/server/entity/pathfinding/NavigableEntity.java index a34aeaad9..5e0b21145 100644 --- a/src/main/java/net/minestom/server/entity/pathfinding/NavigableEntity.java +++ b/src/main/java/net/minestom/server/entity/pathfinding/NavigableEntity.java @@ -110,6 +110,11 @@ public interface NavigableEntity { return false; } + // Can't path with a null instance. + if (instance == null) { + return false; + } + // Can't path outside of the world border final WorldBorder worldBorder = instance.getWorldBorder(); if (!worldBorder.isInside(position)) {