mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-26 02:57:37 +01:00
Merge pull request #102 from LeoDog896/master
Add EatBlockGoal instance check & MeleeAttackGoal range.
This commit is contained in:
commit
e6706ec327
@ -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<Short, Short> eatBelowMap;
|
||||
private final Map<Short, Short> 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<Short, Short> eatInMap,
|
||||
@NotNull Map<Short, Short> eatBelowMap,
|
||||
@NotNull Short2ShortArrayMap eatInMap,
|
||||
@NotNull Short2ShortArrayMap eatBelowMap,
|
||||
int chancePerTick) {
|
||||
super(entityCreature);
|
||||
this.eatInMap = eatInMap;
|
||||
@ -39,7 +40,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));
|
||||
|
@ -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;
|
||||
|
@ -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)) {
|
||||
|
Loading…
Reference in New Issue
Block a user