94 lines
3.2 KiB
Java
94 lines
3.2 KiB
Java
package com.jenny.advancedarrows.entities;
|
|
|
|
import com.jenny.advancedarrows.config.ConfigClient;
|
|
import com.jenny.advancedarrows.config.ConfigCommon;
|
|
import com.jenny.advancedarrows.items.items;
|
|
import net.minecraft.core.particles.ParticleTypes;
|
|
import net.minecraft.world.entity.EntityType;
|
|
import net.minecraft.world.entity.LivingEntity;
|
|
import net.minecraft.world.item.Item;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.world.phys.AABB;
|
|
import net.minecraft.world.phys.Vec3;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class homingArrow extends baseArrow {
|
|
LivingEntity target;
|
|
static final Item item = items.ARROW_HOMING.get();
|
|
|
|
public homingArrow(EntityType<homingArrow> pEntityType, Level pLevel) {
|
|
super(pEntityType, pLevel, item);
|
|
}
|
|
|
|
public homingArrow(Level pLevel, LivingEntity pShooter) {
|
|
super(pLevel, pShooter, entities.ARROW_HOMING.get(), item);
|
|
setBaseDamage(1);
|
|
}
|
|
|
|
@Override
|
|
public void tick() {
|
|
if (!level().isClientSide) {
|
|
if (target == null || getTargetDist() > 64) {
|
|
findTarget();
|
|
} else {
|
|
if (!inGround) {
|
|
Vec3 targetvec = targetVector().normalize().scale(getDeltaMovement().length());
|
|
setDeltaMovement(getDeltaMovement().multiply(0.7, 0.7, 0.7).add(targetvec.multiply(0.3, 0.3, 0.3)));
|
|
}
|
|
}
|
|
}
|
|
super.tick();
|
|
}
|
|
|
|
public double getTargetDist() {
|
|
return targetVector().length();
|
|
}
|
|
|
|
private Vec3 targetVector() {
|
|
double y = target.getBoundingBox().getYsize() / 2;
|
|
return position().vectorTo(target.position().add(0, y, 0));
|
|
}
|
|
|
|
public void findTarget() {
|
|
double minAngle = Double.MAX_VALUE;
|
|
for (LivingEntity entity : getEntities()) {
|
|
Vec3 vecTarget = position().vectorTo(entity.position());
|
|
double newAngle = Math.acos(vecTarget.dot(getDeltaMovement()) / (getDeltaMovement().length() * vecTarget.length()));
|
|
if (newAngle < minAngle) {
|
|
target = entity;
|
|
minAngle = newAngle;
|
|
}
|
|
}
|
|
}
|
|
|
|
protected List<LivingEntity> getEntities() {
|
|
if (getOwner() == null) {
|
|
setOwner(this);
|
|
}
|
|
|
|
List<LivingEntity> ret_list = new ArrayList<>();
|
|
Vec3 corner1 = this.position().subtract(32, 32, 32);
|
|
Vec3 corner2 = this.position().add(32, 32, 32);
|
|
AABB boundingBox = new AABB(corner1, corner2);
|
|
|
|
for (LivingEntity entity : level().getEntitiesOfClass(LivingEntity.class, boundingBox)) {
|
|
if (entity.getUUID() != getOwner().getUUID()) { // prevents targeting itself
|
|
if (level().getPlayerByUUID(entity.getUUID()) == null || ConfigCommon.C_AIMBOT_PLAYER.get()) { // prevent aiming at player if config says so
|
|
ret_list.add(entity);
|
|
}
|
|
}
|
|
}
|
|
return ret_list;
|
|
}
|
|
|
|
@Override
|
|
public void spawnParticles() {
|
|
for (int i = 1; i <= ConfigClient.calcPCount(5); i++) {
|
|
Vec3 pos = createParticlePos(0.5f);
|
|
level().addParticle(ParticleTypes.GLOW_SQUID_INK, pos.x, pos.y, pos.z, 0, 0, 0);
|
|
}
|
|
}
|
|
}
|