From 504e8cafb467ac6d915df8df20bfd06fe074ddf6 Mon Sep 17 00:00:00 2001 From: Konstantin Shandurenko Date: Mon, 22 Feb 2021 15:33:03 +0300 Subject: [PATCH] Codestyle formatting and several comments --- .../entity/ai/goal/RangedAttackGoal.java | 20 +++++------ .../type/projectile/EntityAbstractArrow.java | 34 ++++++++++++++----- 2 files changed, 35 insertions(+), 19 deletions(-) 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 bd705205d..d3c10c96e 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 @@ -16,13 +16,13 @@ import java.util.function.BiFunction; public class RangedAttackGoal extends GoalSelector { - private long lastShot; - private final int delay; + private long lastShot; + private final int delay; private final TimeUnit timeUnit; - private final int attackRangeSquared; - private final int desirableRangeSquared; - private final boolean comeClose; - private final double spread; + private final int attackRangeSquared; + private final int desirableRangeSquared; + private final boolean comeClose; + private final double spread; private BiFunction projectileGenerator; @@ -71,8 +71,8 @@ public class RangedAttackGoal extends GoalSelector { this.stop = true; return; } - double distanceSquared = this.entityCreature.getDistanceSquared(target); - boolean comeClose = false; + double distanceSquared = this.entityCreature.getDistanceSquared(target); + boolean comeClose = false; if (distanceSquared <= this.attackRangeSquared) { if (!CooldownUtils.hasCooldown(time, this.lastShot, this.timeUnit, this.delay)) { if (this.entityCreature.hasLineOfSight(target)) { @@ -91,8 +91,8 @@ public class RangedAttackGoal extends GoalSelector { } } } - Navigator navigator = this.entityCreature.getNavigator(); - Position pathPosition = navigator.getPathPosition(); + Navigator navigator = this.entityCreature.getNavigator(); + Position pathPosition = navigator.getPathPosition(); if (!comeClose && distanceSquared <= this.desirableRangeSquared) { if (pathPosition != null) { navigator.setPathTo(null); diff --git a/src/main/java/net/minestom/server/entity/type/projectile/EntityAbstractArrow.java b/src/main/java/net/minestom/server/entity/type/projectile/EntityAbstractArrow.java index ad729aa08..7b4999de4 100644 --- a/src/main/java/net/minestom/server/entity/type/projectile/EntityAbstractArrow.java +++ b/src/main/java/net/minestom/server/entity/type/projectile/EntityAbstractArrow.java @@ -19,7 +19,7 @@ import java.util.stream.Collectors; public class EntityAbstractArrow extends ObjectEntity implements Projectile { private final static byte CRITICAL_BIT = 0x01; - private final static byte NO_CLIP_BIT = 0x02; + private final static byte NO_CLIP_BIT = 0x02; private final Entity shooter; @@ -53,21 +53,33 @@ public class EntityAbstractArrow extends ObjectEntity implements Projectile { } } + /** + * Checks whether an arrow is stuck in block / hit an entity. + * + * @param pos position right before current tick. + * @param posNow position after current tick. + * @return if an arrow is stuck in block / hit an entity. + */ @SuppressWarnings("ConstantConditions") private boolean isStuck(Position pos, Position posNow) { if (pos.isSimilar(posNow)) { return true; } - Instance instance = getInstance(); - Chunk chunk = null; + Instance instance = getInstance(); + Chunk chunk = null; Collection entities = null; - double part = .25D; // half of the bounding box - Vector dir = posNow.toVector().subtract(pos.toVector()); - int parts = (int) Math.ceil(dir.length() / part); + /* + What we're about to do is to discretely jump from the previous position to the new one. + For each point we will be checking blocks and entities we're in. + */ + double part = .25D; // half of the bounding box + Vector dir = posNow.toVector().subtract(pos.toVector()); + int parts = (int) Math.ceil(dir.length() / part); Position direction = dir.normalize().multiply(part).toPosition(); for (int i = 0; i < parts; ++i) { + // If we're at last part, we can't just add another direction-vector, because we can exceed end point. if (i == parts - 1) { pos.setX(posNow.getX()); pos.setY(posNow.getY()); @@ -75,8 +87,8 @@ public class EntityAbstractArrow extends ObjectEntity implements Projectile { } else { pos.add(direction); } - BlockPosition bpos = pos.toBlockPosition(); - Block block = getInstance().getBlock(bpos.getX(), bpos.getY() - 1, bpos.getZ()); + BlockPosition bpos = pos.toBlockPosition(); + Block block = instance.getBlock(bpos.getX(), bpos.getY() - 1, bpos.getZ()); if (!block.isAir() && !block.isLiquid()) { teleport(pos); return true; @@ -90,6 +102,10 @@ public class EntityAbstractArrow extends ObjectEntity implements Projectile { .filter(entity -> entity instanceof LivingEntity) .collect(Collectors.toSet()); } + /* + We won't check collisions with entities for first ticks of arrow's life, because it spawns in the + shooter and will immediately damage him. + */ if (getAliveTicks() < 3) { continue; } @@ -140,7 +156,7 @@ public class EntityAbstractArrow extends ObjectEntity implements Projectile { } private void modifyMask(byte bit, boolean value) { - byte mask = getMask(); + byte mask = getMask(); boolean isPresent = (mask & bit) == bit; if (isPresent == value) { return;