diff --git a/src/main/java/net/mcreator/target/entity/ProjectileEntity.java b/src/main/java/net/mcreator/target/entity/ProjectileEntity.java index 7a15182ac..dce9c1262 100644 --- a/src/main/java/net/mcreator/target/entity/ProjectileEntity.java +++ b/src/main/java/net/mcreator/target/entity/ProjectileEntity.java @@ -39,6 +39,7 @@ import net.mcreator.target.util.math.ExtendedEntityRayTraceResult; import javax.annotation.Nullable; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Optional; import java.util.function.BiFunction; @@ -177,15 +178,74 @@ public class ProjectileEntity extends ThrowableItemProjectile { return new EntityResult(entity, hitPos, headshot); } - + @Override - public void tick() { + public void tick() + { super.tick(); - if(this.tickCount >= 20){ + this.updateHeading(); + this.onProjectileTick(); + + if(!this.level().isClientSide()) + { + Vec3 startVec = this.position(); + Vec3 endVec = startVec.add(this.getDeltaMovement()); + HitResult result = rayTraceBlocks(this.level(), new ClipContext(startVec, endVec, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, this), IGNORE_LEAVES); + if(result.getType() != HitResult.Type.MISS) + { + endVec = result.getLocation(); + } + + List hitEntities = null; + int level = 0; + if(level == 0) + { + EntityResult entityResult = this.findEntityOnPath(startVec, endVec); + if(entityResult != null) + { + hitEntities = Collections.singletonList(entityResult); + } + } + else + { + hitEntities = this.findEntitiesOnPath(startVec, endVec); + } + + if(hitEntities != null && hitEntities.size() > 0) + { + for(EntityResult entityResult : hitEntities) + { + result = new ExtendedEntityRayTraceResult(entityResult); + if(((EntityHitResult) result).getEntity() instanceof Player) + { + Player player = (Player) ((EntityHitResult) result).getEntity(); + + if(this.shooter instanceof Player && !((Player) this.shooter).canHarmPlayer(player)) + { + result = null; + } + } + if(result != null) + { + this.onHit(result, startVec, endVec); + } + } + } + else + { + this.onHit(result, startVec, endVec); + } + } + + if(this.tickCount>200){ this.discard(); } } + protected void onProjectileTick() + { + } + @Override protected void onHitBlock(BlockHitResult hitResult) { super.onHitBlock(hitResult); @@ -234,10 +294,10 @@ public class ProjectileEntity extends ThrowableItemProjectile { if(headshot){ - ProjectileHeadshotEntity.execute(this.level(), entity, this, this.getOwner()); + ProjectileHeadshotEntity.execute(this.level(), entity, this, this.shooter); } - ProjectileHitEntity.execute(this.level(), entity, this, this.getOwner()); + ProjectileHitEntity.execute(this.level(), entity, this, this.shooter); } @Override @@ -350,6 +410,24 @@ public class ProjectileEntity extends ThrowableItemProjectile { } } + public LivingEntity getShooter() + { + return this.shooter; + } + public int getShooterId() + { + return this.shooterId; + } + + public void updateHeading() + { + double horizontalDistance = this.getDeltaMovement().horizontalDistance(); + this.setYRot((float) (Mth.atan2(this.getDeltaMovement().x(), this.getDeltaMovement().z()) * (180D / Math.PI))); + this.setXRot((float) (Mth.atan2(this.getDeltaMovement().y(), horizontalDistance) * (180D / Math.PI))); + this.yRotO = this.getYRot(); + this.xRotO = this.getXRot(); + } + public static class EntityResult { private Entity entity;