Codestyle formatting and several comments

This commit is contained in:
Konstantin Shandurenko 2021-02-22 15:33:03 +03:00
parent 6e23552d9a
commit 504e8cafb4
2 changed files with 35 additions and 19 deletions

View File

@ -53,6 +53,13 @@ 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)) {
@ -63,11 +70,16 @@ public class EntityAbstractArrow extends ObjectEntity implements Projectile {
Chunk chunk = null;
Collection<Entity> entities = null;
/*
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());
@ -76,7 +88,7 @@ public class EntityAbstractArrow extends ObjectEntity implements Projectile {
pos.add(direction);
}
BlockPosition bpos = pos.toBlockPosition();
Block block = getInstance().getBlock(bpos.getX(), bpos.getY() - 1, bpos.getZ());
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;
}