Skip to content

Instantly share code, notes, and snippets.

@bconlon1
Created May 25, 2024 01:03
Show Gist options
  • Save bconlon1/d68005bcfd5c7601a7f459cd7cf2557a to your computer and use it in GitHub Desktop.
Save bconlon1/d68005bcfd5c7601a7f459cd7cf2557a to your computer and use it in GitHub Desktop.
public void attack(Entity pTarget) {
if (CommonHooks.onPlayerAttackTarget(this, pTarget)) {
if (pTarget.isAttackable() && !pTarget.skipAttackInteraction(this)) {
float f = (float)this.getAttributeValue(Attributes.ATTACK_DAMAGE);
float f1;
if (pTarget instanceof LivingEntity) {
f1 = EnchantmentHelper.getDamageBonus(this.getMainHandItem(), ((LivingEntity)pTarget).getMobType());
} else {
f1 = EnchantmentHelper.getDamageBonus(this.getMainHandItem(), MobType.UNDEFINED);
}
float f2 = this.getAttackStrengthScale(0.5F);
f *= 0.2F + f2 * f2 * 0.8F;
f1 *= f2;
if (f > 0.0F || f1 > 0.0F) {
boolean flag = f2 > 0.9F;
boolean flag1 = false;
float i = (float)this.getAttributeValue(Attributes.ATTACK_KNOCKBACK);
i += (float)EnchantmentHelper.getKnockbackBonus(this);
if (this.isSprinting() && flag) {
this.level().playSound((Player)null, this.getX(), this.getY(), this.getZ(), SoundEvents.PLAYER_ATTACK_KNOCKBACK, this.getSoundSource(), 1.0F, 1.0F);
++i;
flag1 = true;
}
boolean flag2 = flag && this.fallDistance > 0.0F && !this.onGround() && !this.onClimbable() && !this.isInWater() && !this.hasEffect(MobEffects.BLINDNESS) && !this.isPassenger() && pTarget instanceof LivingEntity;
flag2 = flag2 && !this.isSprinting();
CriticalHitEvent hitResult = CommonHooks.getCriticalHit(this, pTarget, flag2, flag2 ? 1.5F : 1.0F);
flag2 = hitResult != null;
if (flag2) {
f *= hitResult.getDamageModifier();
}
f += f1;
boolean flag3 = false;
double d0 = (double)(this.walkDist - this.walkDistO);
if (flag && !flag2 && !flag1 && this.onGround() && d0 < (double)this.getSpeed()) {
ItemStack itemstack = this.getItemInHand(InteractionHand.MAIN_HAND);
flag3 = itemstack.canPerformAction(ToolActions.SWORD_SWEEP);
}
float f4 = 0.0F;
boolean flag4 = false;
int j = EnchantmentHelper.getFireAspect(this);
if (pTarget instanceof LivingEntity) {
f4 = ((LivingEntity)pTarget).getHealth();
if (j > 0 && !pTarget.isOnFire()) {
flag4 = true;
pTarget.setSecondsOnFire(1);
}
}
Vec3 vec3 = pTarget.getDeltaMovement();
boolean flag5 = pTarget.hurt(this.damageSources().playerAttack(this), f);
if (flag5) {
if (i > 0.0F) {
if (pTarget instanceof LivingEntity) {
((LivingEntity)pTarget).knockback((double)(i * 0.5F), (double)Mth.sin(this.getYRot() * 0.017453292F), (double)(-Mth.cos(this.getYRot() * 0.017453292F)));
} else {
pTarget.push((double)(-Mth.sin(this.getYRot() * 0.017453292F) * i * 0.5F), 0.1, (double)(Mth.cos(this.getYRot() * 0.017453292F) * i * 0.5F));
}
this.setDeltaMovement(this.getDeltaMovement().multiply(0.6, 1.0, 0.6));
this.setSprinting(false);
}
if (flag3) {
float f3 = 1.0F + EnchantmentHelper.getSweepingDamageRatio(this) * f;
Iterator var19 = this.level().getEntitiesOfClass(LivingEntity.class, pTarget.getBoundingBox().inflate(1.0, 0.25, 1.0)).iterator();
label173:
while(true) {
LivingEntity livingentity;
double entityReachSq;
do {
do {
do {
do {
if (!var19.hasNext()) {
this.level().playSound((Player)null, this.getX(), this.getY(), this.getZ(), SoundEvents.PLAYER_ATTACK_SWEEP, this.getSoundSource(), 1.0F, 1.0F);
this.sweepAttack();
break label173;
}
livingentity = (LivingEntity)var19.next();
entityReachSq = Mth.square(this.getEntityReach());
} while(livingentity == this);
} while(livingentity == pTarget);
} while(this.isAlliedTo(livingentity));
} while(livingentity instanceof ArmorStand && ((ArmorStand)livingentity).isMarker());
if (this.distanceToSqr(livingentity) < entityReachSq) {
livingentity.knockback(0.4000000059604645, (double)Mth.sin(this.getYRot() * 0.017453292F), (double)(-Mth.cos(this.getYRot() * 0.017453292F)));
livingentity.hurt(this.damageSources().playerAttack(this), f3);
}
}
}
if (pTarget instanceof ServerPlayer && pTarget.hurtMarked) {
((ServerPlayer)pTarget).connection.send(new ClientboundSetEntityMotionPacket(pTarget));
pTarget.hurtMarked = false;
pTarget.setDeltaMovement(vec3);
}
if (flag2) {
this.level().playSound((Player)null, this.getX(), this.getY(), this.getZ(), SoundEvents.PLAYER_ATTACK_CRIT, this.getSoundSource(), 1.0F, 1.0F);
this.crit(pTarget);
}
if (!flag2 && !flag3) {
if (flag) {
this.level().playSound((Player)null, this.getX(), this.getY(), this.getZ(), SoundEvents.PLAYER_ATTACK_STRONG, this.getSoundSource(), 1.0F, 1.0F);
} else {
this.level().playSound((Player)null, this.getX(), this.getY(), this.getZ(), SoundEvents.PLAYER_ATTACK_WEAK, this.getSoundSource(), 1.0F, 1.0F);
}
}
if (f1 > 0.0F) {
this.magicCrit(pTarget);
}
this.setLastHurtMob(pTarget);
if (pTarget instanceof LivingEntity) {
EnchantmentHelper.doPostHurtEffects((LivingEntity)pTarget, this);
}
EnchantmentHelper.doPostDamageEffects(this, pTarget);
ItemStack itemstack1 = this.getMainHandItem();
Entity entity = pTarget;
if (pTarget instanceof PartEntity) {
entity = ((PartEntity)pTarget).getParent();
}
if (!this.level().isClientSide && !itemstack1.isEmpty() && entity instanceof LivingEntity) {
ItemStack copy = itemstack1.copy();
itemstack1.hurtEnemy((LivingEntity)entity, this);
if (itemstack1.isEmpty()) {
EventHooks.onPlayerDestroyItem(this, copy, InteractionHand.MAIN_HAND);
this.setItemInHand(InteractionHand.MAIN_HAND, ItemStack.EMPTY);
}
}
if (pTarget instanceof LivingEntity) {
float f5 = f4 - ((LivingEntity)pTarget).getHealth();
this.awardStat(Stats.DAMAGE_DEALT, Math.round(f5 * 10.0F));
if (j > 0) {
pTarget.setSecondsOnFire(j * 4);
}
if (this.level() instanceof ServerLevel && f5 > 2.0F) {
int k = (int)((double)f5 * 0.5);
((ServerLevel)this.level()).sendParticles(ParticleTypes.DAMAGE_INDICATOR, pTarget.getX(), pTarget.getY(0.5), pTarget.getZ(), k, 0.1, 0.0, 0.1, 0.2);
}
}
this.causeFoodExhaustion(0.1F);
} else {
this.level().playSound((Player)null, this.getX(), this.getY(), this.getZ(), SoundEvents.PLAYER_ATTACK_NODAMAGE, this.getSoundSource(), 1.0F, 1.0F);
if (flag4) {
pTarget.clearFire();
}
}
}
this.resetAttackStrengthTicker();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment