mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-08 09:27:58 +01:00
Entity collisions for arrows
This commit is contained in:
parent
dbfebc50ec
commit
b475a4cd78
@ -1,18 +1,21 @@
|
|||||||
package net.minestom.server.entity.type.projectile;
|
package net.minestom.server.entity.type.projectile;
|
||||||
|
|
||||||
import net.minestom.server.entity.Entity;
|
import net.minestom.server.entity.*;
|
||||||
import net.minestom.server.entity.EntityType;
|
import net.minestom.server.entity.damage.DamageType;
|
||||||
import net.minestom.server.entity.Metadata;
|
|
||||||
import net.minestom.server.entity.ObjectEntity;
|
|
||||||
import net.minestom.server.entity.type.Projectile;
|
import net.minestom.server.entity.type.Projectile;
|
||||||
|
import net.minestom.server.instance.Chunk;
|
||||||
|
import net.minestom.server.instance.Instance;
|
||||||
import net.minestom.server.instance.block.Block;
|
import net.minestom.server.instance.block.Block;
|
||||||
import net.minestom.server.network.packet.server.play.EntityTeleportPacket;
|
|
||||||
import net.minestom.server.utils.BlockPosition;
|
import net.minestom.server.utils.BlockPosition;
|
||||||
import net.minestom.server.utils.Position;
|
import net.minestom.server.utils.Position;
|
||||||
import net.minestom.server.utils.Vector;
|
import net.minestom.server.utils.Vector;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by k.shandurenko on 22.02.2021
|
* Created by k.shandurenko on 22.02.2021
|
||||||
*/
|
*/
|
||||||
@ -21,11 +24,11 @@ public class EntityAbstractArrow extends ObjectEntity implements Projectile {
|
|||||||
private final static byte CRITICAL_BIT = 0x01;
|
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 int shooterID;
|
private final Entity shooter;
|
||||||
|
|
||||||
EntityAbstractArrow(@Nullable Entity shooter, @NotNull EntityType entityType, @NotNull Position spawnPosition) {
|
EntityAbstractArrow(@Nullable Entity shooter, @NotNull EntityType entityType, @NotNull Position spawnPosition) {
|
||||||
super(entityType, spawnPosition);
|
super(entityType, spawnPosition);
|
||||||
this.shooterID = shooter == null ? 0 : shooter.getEntityId();
|
this.shooter = shooter;
|
||||||
super.hasPhysics = false;
|
super.hasPhysics = false;
|
||||||
|
|
||||||
setBoundingBox(.5F, .5F, .5F);
|
setBoundingBox(.5F, .5F, .5F);
|
||||||
@ -53,17 +56,25 @@ public class EntityAbstractArrow extends ObjectEntity implements Projectile {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("ConstantConditions")
|
||||||
private boolean isStuck(Position pos, Position posNow) {
|
private boolean isStuck(Position pos, Position posNow) {
|
||||||
if (pos.isSimilar(posNow)) {
|
if (pos.isSimilar(posNow)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Instance instance = getInstance();
|
||||||
|
Chunk chunk = null;
|
||||||
|
Collection<Entity> entities = null;
|
||||||
|
|
||||||
double part = .25D; // half of the bounding box
|
double part = .25D; // half of the bounding box
|
||||||
Vector dir = posNow.toVector().subtract(pos.toVector());
|
Vector dir = posNow.toVector().subtract(pos.toVector());
|
||||||
int parts = (int) Math.ceil(dir.length() / part);
|
int parts = (int) Math.ceil(dir.length() / part);
|
||||||
Position direction = dir.normalize().multiply(part).toPosition();
|
Position direction = dir.normalize().multiply(part).toPosition();
|
||||||
for (int i = 0; i < parts; ++i) {
|
for (int i = 0; i < parts; ++i) {
|
||||||
if (i == parts - 1) {
|
if (i == parts - 1) {
|
||||||
pos = posNow;
|
pos.setX(posNow.getX());
|
||||||
|
pos.setY(posNow.getY());
|
||||||
|
pos.setZ(posNow.getZ());
|
||||||
} else {
|
} else {
|
||||||
pos.add(direction);
|
pos.add(direction);
|
||||||
}
|
}
|
||||||
@ -73,6 +84,25 @@ public class EntityAbstractArrow extends ObjectEntity implements Projectile {
|
|||||||
teleport(pos);
|
teleport(pos);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Chunk currentChunk = instance.getChunkAt(pos);
|
||||||
|
if (currentChunk != chunk) {
|
||||||
|
chunk = currentChunk;
|
||||||
|
entities = instance.getChunkEntities(chunk)
|
||||||
|
.stream()
|
||||||
|
.filter(entity -> entity instanceof LivingEntity)
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
Optional<Entity> victimOptional = entities.stream()
|
||||||
|
.filter(entity -> entity.getBoundingBox().intersect(pos.getX(), pos.getY(), pos.getZ()))
|
||||||
|
.findAny();
|
||||||
|
if (victimOptional.isPresent()) {
|
||||||
|
LivingEntity victim = (LivingEntity) victimOptional.get();
|
||||||
|
victim.setArrowCount(victim.getArrowCount() + 1);
|
||||||
|
victim.damage(DamageType.fromProjectile(this.shooter, this), 2F);
|
||||||
|
remove();
|
||||||
|
return super.onGround;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -125,7 +155,7 @@ public class EntityAbstractArrow extends ObjectEntity implements Projectile {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getObjectData() {
|
public int getObjectData() {
|
||||||
return this.shooterID + 1;
|
return this.shooter == null ? 0 : this.shooter.getEntityId() + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user