cleanup entitybullet

This commit is contained in:
Sen 2025-06-16 11:04:56 +02:00
parent d0984b143b
commit 431d54f779
Signed by: sen
GPG key ID: 3AC50A6F47D1B722

View file

@ -21,64 +21,60 @@ import common.world.World;
public class EntityBullet extends Entity implements IProjectile, IObjectData public class EntityBullet extends Entity implements IProjectile, IObjectData
{ {
public Entity shootingEntity; private Entity shooter;
private int ticksInAir; private int ticksMoved;
private int age; private int age;
private int damage = 5; private int damage = 5;
public EntityBullet(World worldIn) public EntityBullet(World world)
{ {
super(worldIn); super(world);
this.renderDistWeight = 10.0D; this.renderDistWeight = 10.0D;
this.setSize(0.5F, 0.5F); this.setSize(0.5F, 0.5F);
} }
public EntityBullet(World worldIn, double x, double y, double z) public EntityBullet(World world, double x, double y, double z)
{ {
super(worldIn); super(world);
this.renderDistWeight = 10.0D; this.renderDistWeight = 10.0D;
this.setSize(0.5F, 0.5F); this.setSize(0.5F, 0.5F);
this.setPosition(x, y, z); this.setPosition(x, y, z);
} }
public EntityBullet(World worldIn, double x, double y, double z, int data) public EntityBullet(World world, double x, double y, double z, int data)
{ {
this(worldIn, x, y, z); this(world, x, y, z);
Entity entity2 = worldIn.getEntityByID(data); Entity entity = world.getEntityByID(data);
if(entity2 instanceof EntityLiving) { if(entity instanceof EntityLiving)
this.shootingEntity = entity2; this.shooter = entity;
}
} }
public EntityBullet(World worldIn, EntityLiving shooter, EntityLiving target, float velocity, float innacuracy) public EntityBullet(World world, EntityLiving shooter, EntityLiving target, float velocity, float innacuracy)
{ {
super(worldIn); super(world);
this.renderDistWeight = 10.0D; this.renderDistWeight = 10.0D;
this.shootingEntity = shooter; this.shooter = shooter;
this.posY = shooter.posY + (double)shooter.getEyeHeight() - 0.10000000149011612D; this.posY = shooter.posY + (double)shooter.getEyeHeight() - 0.10000000149011612D;
double d0 = target.posX - shooter.posX; double xd = target.posX - shooter.posX;
double d1 = target.getEntityBoundingBox().minY + (double)(target.height * 0.7f) - this.posY; double yd = target.getEntityBoundingBox().minY + (double)(target.height * 0.7f) - this.posY;
double d2 = target.posZ - shooter.posZ; double zd = target.posZ - shooter.posZ;
double d3 = (double)ExtMath.sqrtd(d0 * d0 + d2 * d2); double xzd = (double)ExtMath.sqrtd(xd * xd + zd * zd);
if (xzd >= 1.0E-7D)
if (d3 >= 1.0E-7D)
{ {
float f = (float)(ExtMath.atan2(d2, d0) * 180.0D / Math.PI) - 90.0F; float yaw = (float)(ExtMath.atan2(zd, xd) * 180.0D / Math.PI) - 90.0F;
float f1 = (float)(-(ExtMath.atan2(d1, d3) * 180.0D / Math.PI)); float pitch = (float)(-(ExtMath.atan2(yd, xzd) * 180.0D / Math.PI));
double d4 = d0 / d3; double x = xd / xzd;
double d5 = d2 / d3; double z = zd / xzd;
this.setLocationAndAngles(shooter.posX + d4, this.posY, shooter.posZ + d5, f, f1); this.setLocationAndAngles(shooter.posX + x, this.posY, shooter.posZ + z, yaw, pitch);
this.setThrowableHeading(d0, d1, d2, velocity, innacuracy); this.setThrowableHeading(xd, yd, zd, velocity, innacuracy);
} }
} }
public EntityBullet(World worldIn, EntityLiving shooter, float velocity) public EntityBullet(World world, EntityLiving shooter, float velocity)
{ {
super(worldIn); super(world);
this.renderDistWeight = 10.0D; this.renderDistWeight = 10.0D;
this.shootingEntity = shooter; this.shooter = shooter;
this.setSize(0.5F, 0.5F); this.setSize(0.5F, 0.5F);
this.setLocationAndAngles(shooter.posX, shooter.posY + (double)shooter.getEyeHeight(), shooter.posZ, shooter.rotYaw, shooter.rotPitch); this.setLocationAndAngles(shooter.posX, shooter.posY + (double)shooter.getEyeHeight(), shooter.posZ, shooter.rotYaw, shooter.rotPitch);
this.posX -= (double)(ExtMath.cos(this.rotYaw / 180.0F * (float)Math.PI) * 0.16F); this.posX -= (double)(ExtMath.cos(this.rotYaw / 180.0F * (float)Math.PI) * 0.16F);
@ -97,10 +93,10 @@ public class EntityBullet extends Entity implements IProjectile, IObjectData
public void setThrowableHeading(double x, double y, double z, float velocity, float inaccuracy) public void setThrowableHeading(double x, double y, double z, float velocity, float inaccuracy)
{ {
float f = ExtMath.sqrtd(x * x + y * y + z * z); float sq = ExtMath.sqrtd(x * x + y * y + z * z);
x = x / (double)f; x = x / (double)sq;
y = y / (double)f; y = y / (double)sq;
z = z / (double)f; z = z / (double)sq;
x = x + this.rand.gaussian() * (double)(this.rand.chance() ? -1 : 1) * 0.007499999832361937D * (double)inaccuracy; x = x + this.rand.gaussian() * (double)(this.rand.chance() ? -1 : 1) * 0.007499999832361937D * (double)inaccuracy;
y = y + this.rand.gaussian() * (double)(this.rand.chance() ? -1 : 1) * 0.007499999832361937D * (double)inaccuracy; y = y + this.rand.gaussian() * (double)(this.rand.chance() ? -1 : 1) * 0.007499999832361937D * (double)inaccuracy;
z = z + this.rand.gaussian() * (double)(this.rand.chance() ? -1 : 1) * 0.007499999832361937D * (double)inaccuracy; z = z + this.rand.gaussian() * (double)(this.rand.chance() ? -1 : 1) * 0.007499999832361937D * (double)inaccuracy;
@ -126,12 +122,11 @@ public class EntityBullet extends Entity implements IProjectile, IObjectData
this.motionX = x; this.motionX = x;
this.motionY = y; this.motionY = y;
this.motionZ = z; this.motionZ = z;
if (this.prevPitch == 0.0F && this.prevYaw == 0.0F) if (this.prevPitch == 0.0F && this.prevYaw == 0.0F)
{ {
float f = ExtMath.sqrtd(x * x + z * z); float xz = ExtMath.sqrtd(x * x + z * z);
this.prevYaw = this.rotYaw = (float)(ExtMath.atan2(x, z) * 180.0D / Math.PI); this.prevYaw = this.rotYaw = (float)(ExtMath.atan2(x, z) * 180.0D / Math.PI);
this.prevPitch = this.rotPitch = (float)(ExtMath.atan2(y, (double)f) * 180.0D / Math.PI); this.prevPitch = this.rotPitch = (float)(ExtMath.atan2(y, (double)xz) * 180.0D / Math.PI);
this.prevPitch = this.rotPitch; this.prevPitch = this.rotPitch;
this.prevYaw = this.rotYaw; this.prevYaw = this.rotYaw;
this.setLocationAndAngles(this.posX, this.posY, this.posZ, this.rotYaw, this.rotPitch); this.setLocationAndAngles(this.posX, this.posY, this.posZ, this.rotYaw, this.rotPitch);
@ -144,49 +139,49 @@ public class EntityBullet extends Entity implements IProjectile, IObjectData
if (this.prevPitch == 0.0F && this.prevYaw == 0.0F) if (this.prevPitch == 0.0F && this.prevYaw == 0.0F)
{ {
float f = ExtMath.sqrtd(this.motionX * this.motionX + this.motionZ * this.motionZ); float xz = ExtMath.sqrtd(this.motionX * this.motionX + this.motionZ * this.motionZ);
this.prevYaw = this.rotYaw = (float)(ExtMath.atan2(this.motionX, this.motionZ) * 180.0D / Math.PI); this.prevYaw = this.rotYaw = (float)(ExtMath.atan2(this.motionX, this.motionZ) * 180.0D / Math.PI);
this.prevPitch = this.rotPitch = (float)(ExtMath.atan2(this.motionY, (double)f) * 180.0D / Math.PI); this.prevPitch = this.rotPitch = (float)(ExtMath.atan2(this.motionY, (double)xz) * 180.0D / Math.PI);
} }
if(!this.worldObj.client && ++this.age >= 200) { if(!this.worldObj.client && ++this.age >= 200) {
this.setDead(); this.setDead();
return; return;
} }
++this.ticksInAir; ++this.ticksMoved;
Vec3 vec31 = new Vec3(this.posX, this.posY, this.posZ); Vec3 current = new Vec3(this.posX, this.posY, this.posZ);
Vec3 vec3 = new Vec3(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ); Vec3 next = new Vec3(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
HitPosition movingobjectposition = this.worldObj.rayTraceBlocks(vec31, vec3, false, true, false); HitPosition hit = this.worldObj.rayTraceBlocks(current, next, false, true, false);
vec31 = new Vec3(this.posX, this.posY, this.posZ); current = new Vec3(this.posX, this.posY, this.posZ);
vec3 = new Vec3(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ); next = new Vec3(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
if (movingobjectposition != null) if (hit != null)
{ {
vec3 = new Vec3(movingobjectposition.vec.xCoord, movingobjectposition.vec.yCoord, movingobjectposition.vec.zCoord); next = new Vec3(hit.vec.xCoord, hit.vec.yCoord, hit.vec.zCoord);
} }
Entity entity = null; Entity entity = null;
List<Entity> list = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, this.getEntityBoundingBox().addCoord(this.motionX, this.motionY, this.motionZ).expand(1.0D, 1.0D, 1.0D)); List<Entity> list = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, this.getEntityBoundingBox().addCoord(this.motionX, this.motionY, this.motionZ).expand(1.0D, 1.0D, 1.0D));
double d0 = 0.0D; double min = 0.0D;
for (int i = 0; i < list.size(); ++i) for (int i = 0; i < list.size(); ++i)
{ {
Entity entity1 = (Entity)list.get(i); Entity ent = list.get(i);
if (entity1.canBeCollidedWith() && (entity1 != this.shootingEntity || this.ticksInAir >= 5)) if (ent.canBeCollidedWith() && (ent != this.shooter || this.ticksMoved >= 5))
{ {
float f1 = 0.3F; float expand = 0.3F;
BoundingBox axisalignedbb1 = entity1.getEntityBoundingBox().expand((double)f1, (double)f1, (double)f1); BoundingBox bb = ent.getEntityBoundingBox().expand((double)expand, (double)expand, (double)expand);
HitPosition movingobjectposition1 = axisalignedbb1.calculateIntercept(vec31, vec3); HitPosition entityHit = bb.calculateIntercept(current, next);
if (movingobjectposition1 != null) if (entityHit != null)
{ {
double d1 = vec31.squareDistanceTo(movingobjectposition1.vec); double dist = current.squareDistanceTo(entityHit.vec);
if (d1 < d0 || d0 == 0.0D) if (dist < min || min == 0.0D)
{ {
entity = entity1; entity = ent;
d0 = d1; min = dist;
} }
} }
} }
@ -194,48 +189,42 @@ public class EntityBullet extends Entity implements IProjectile, IObjectData
if (entity != null) if (entity != null)
{ {
movingobjectposition = new HitPosition(entity); hit = new HitPosition(entity);
} }
if (movingobjectposition != null) if (hit != null)
{ {
if (movingobjectposition.entity != null) if (hit.entity != null)
{ {
float f2 = ExtMath.sqrtd(this.motionX * this.motionX + this.motionY * this.motionY + this.motionZ * this.motionZ); float velo = ExtMath.sqrtd(this.motionX * this.motionX + this.motionY * this.motionY + this.motionZ * this.motionZ);
int l = ExtMath.ceild((double)f2 * (double)this.damage); int damage = ExtMath.ceild((double)velo * (double)this.damage);
DamageSource damagesource; DamageSource source;
if (this.shootingEntity == null) if (this.shooter == null)
{ {
damagesource = DamageSource.causeShotDamage(this, this); source = DamageSource.causeShotDamage(this, this);
} }
else else
{ {
damagesource = DamageSource.causeShotDamage(this, this.shootingEntity); source = DamageSource.causeShotDamage(this, this.shooter);
} }
if ((this.worldObj.client || Vars.damageBullet) && movingobjectposition.entity.attackEntityFrom(damagesource, l)) if ((this.worldObj.client || Vars.damageBullet) && hit.entity.attackEntityFrom(source, damage))
{ {
if (movingobjectposition.entity instanceof EntityLiving) if (hit.entity instanceof EntityLiving living && this.shooter instanceof EntityLiving shooter)
{ {
EntityLiving entitylivingbase = (EntityLiving)movingobjectposition.entity; EnchantmentHelper.applyThornEnchantments(living, shooter);
EnchantmentHelper.applyArthropodEnchantments(shooter, living);
if (this.shootingEntity instanceof EntityLiving)
{
EnchantmentHelper.applyThornEnchantments(entitylivingbase, this.shootingEntity);
EnchantmentHelper.applyArthropodEnchantments((EntityLiving)this.shootingEntity, entitylivingbase);
}
} }
this.playSound(SoundEvent.METALHIT, 0.2F); this.playSound(SoundEvent.METALHIT, 0.2F);
} }
this.setDead(); this.setDead();
} }
else if(!this.worldObj.client) else if(!this.worldObj.client)
{ {
State state = movingobjectposition.block != null ? this.worldObj.getState(movingobjectposition.block) : null; State state = hit.block != null ? this.worldObj.getState(hit.block) : null;
if(state == null || state.getBlock().onShot(this.worldObj, movingobjectposition.block, state, this)) { if(state == null || state.getBlock().onShot(this.worldObj, hit.block, state, this)) {
this.playSound(SoundEvent.METALHIT, 0.5F); this.playSound(SoundEvent.METALHIT, 0.5F);
this.setDead(); this.setDead();
} }
@ -245,10 +234,10 @@ public class EntityBullet extends Entity implements IProjectile, IObjectData
this.posX += this.motionX; this.posX += this.motionX;
this.posY += this.motionY; this.posY += this.motionY;
this.posZ += this.motionZ; this.posZ += this.motionZ;
float f3 = ExtMath.sqrtd(this.motionX * this.motionX + this.motionZ * this.motionZ); float xzMotion = ExtMath.sqrtd(this.motionX * this.motionX + this.motionZ * this.motionZ);
this.rotYaw = (float)(ExtMath.atan2(this.motionX, this.motionZ) * 180.0D / Math.PI); this.rotYaw = (float)(ExtMath.atan2(this.motionX, this.motionZ) * 180.0D / Math.PI);
for (this.rotPitch = (float)(ExtMath.atan2(this.motionY, (double)f3) * 180.0D / Math.PI); this.rotPitch - this.prevPitch < -180.0F; this.prevPitch -= 360.0F) for (this.rotPitch = (float)(ExtMath.atan2(this.motionY, (double)xzMotion) * 180.0D / Math.PI); this.rotPitch - this.prevPitch < -180.0F; this.prevPitch -= 360.0F)
{ {
; ;
} }
@ -273,16 +262,16 @@ public class EntityBullet extends Entity implements IProjectile, IObjectData
this.setPosition(this.posX, this.posY, this.posZ); this.setPosition(this.posX, this.posY, this.posZ);
} }
public void writeEntity(TagObject tagCompound) public void writeEntity(TagObject tag)
{ {
tagCompound.setInt("damage", this.damage); tag.setInt("damage", this.damage);
tagCompound.setInt("age", this.age); tag.setInt("age", this.age);
} }
public void readEntity(TagObject tagCompund) public void readEntity(TagObject tag)
{ {
this.damage = tagCompund.getInt("damage"); this.damage = tag.getInt("damage");
this.age = tagCompund.getInt("age"); this.age = tag.getInt("age");
} }
protected boolean canTriggerWalking() protected boolean canTriggerWalking()
@ -290,9 +279,9 @@ public class EntityBullet extends Entity implements IProjectile, IObjectData
return false; return false;
} }
public void setDamage(int damageIn) public void setDamage(int damage)
{ {
this.damage = damageIn; this.damage = damage;
} }
public int getDamage() public int getDamage()
@ -323,7 +312,7 @@ public class EntityBullet extends Entity implements IProjectile, IObjectData
} }
public int getPacketData() { public int getPacketData() {
return this.shootingEntity != null ? this.shootingEntity.getId() : this.getId(); return this.shooter != null ? this.shooter.getId() : this.getId();
} }
public boolean hasSpawnVelocity() { public boolean hasSpawnVelocity() {