重构击退判定

This commit is contained in:
17146 2024-12-24 20:42:12 +08:00
parent 50e3127558
commit 3d1cf74f01
4 changed files with 63 additions and 33 deletions

View file

@ -0,0 +1,19 @@
package com.atsuishio.superbwarfare.entity;
import net.minecraft.world.entity.LivingEntity;
/**
* Codes Based On @TACZ
*/
public interface ICustomKnockback {
static ICustomKnockback getInstance(LivingEntity entity) {
return (ICustomKnockback) entity;
}
void superbWarfare$setKnockbackStrength(double strength);
void superbWarfare$resetKnockbackStrength();
double superbWarfare$getKnockbackStrength();
}

View file

@ -99,6 +99,7 @@ public class ProjectileEntity extends Entity implements IEntityAdditionalSpawnDa
private boolean fireBullet = false; private boolean fireBullet = false;
private int fireLevel = 0; private int fireLevel = 0;
private boolean dragonBreath = false; private boolean dragonBreath = false;
private double knockback = 0.05;
private final ArrayList<MobEffectInstance> mobEffects = new ArrayList<>(); private final ArrayList<MobEffectInstance> mobEffects = new ArrayList<>();
public ProjectileEntity(EntityType<? extends ProjectileEntity> p_i50159_1_, Level p_i50159_2_) { public ProjectileEntity(EntityType<? extends ProjectileEntity> p_i50159_1_, Level p_i50159_2_) {
@ -133,16 +134,13 @@ public class ProjectileEntity extends Entity implements IEntityAdditionalSpawnDa
for (Entity entity : entities) { for (Entity entity : entities) {
if (entity.equals(this.shooter)) continue; if (entity.equals(this.shooter)) continue;
if (entity.equals(this.shooter.getVehicle())) continue; if (entity.equals(this.shooter.getVehicle())) continue;
if (entity instanceof TargetEntity && entity.getEntityData().get(TargetEntity.DOWN_TIME) > 0) continue; if (entity instanceof TargetEntity && entity.getEntityData().get(TargetEntity.DOWN_TIME) > 0) continue;
EntityResult result = this.getHitResult(entity, startVec, endVec); EntityResult result = this.getHitResult(entity, startVec, endVec);
if (result == null) continue; if (result == null) continue;
Vec3 hitPos = result.getHitPos(); Vec3 hitPos = result.getHitPos();
if (hitPos == null) continue; if (hitPos == null) continue;
double distanceToHit = startVec.distanceTo(hitPos); double distanceToHit = startVec.distanceTo(hitPos);
@ -221,12 +219,12 @@ public class ProjectileEntity extends Entity implements IEntityAdditionalSpawnDa
boolean headshot = false; boolean headshot = false;
boolean legShot = false; boolean legShot = false;
float eyeHeight = entity.getEyeHeight(); float eyeHeight = entity.getEyeHeight();
float BodyHeight = entity.getBbHeight(); float bodyHeight = entity.getBbHeight();
if ((eyeHeight - 0.35) < hitBoxPos.y && hitBoxPos.y < (eyeHeight + 0.4) && if ((eyeHeight - 0.35) < hitBoxPos.y && hitBoxPos.y < (eyeHeight + 0.4) &&
!(entity instanceof ClaymoreEntity || entity instanceof MortarEntity || entity instanceof IVehicleEntity || entity instanceof DroneEntity)) { !(entity instanceof ClaymoreEntity || entity instanceof MortarEntity || entity instanceof IVehicleEntity || entity instanceof DroneEntity)) {
headshot = true; headshot = true;
} }
if (hitBoxPos.y < (0.33 * BodyHeight) && !(entity instanceof ClaymoreEntity || entity instanceof MortarEntity || if (hitBoxPos.y < (0.33 * bodyHeight) && !(entity instanceof ClaymoreEntity || entity instanceof MortarEntity ||
entity instanceof IVehicleEntity || entity instanceof DroneEntity)) { entity instanceof IVehicleEntity || entity instanceof DroneEntity)) {
legShot = true; legShot = true;
} }
@ -310,12 +308,10 @@ public class ProjectileEntity extends Entity implements IEntityAdditionalSpawnDa
@Override @Override
protected void readAdditionalSaveData(CompoundTag compoundTag) { protected void readAdditionalSaveData(CompoundTag compoundTag) {
} }
@Override @Override
protected void addAdditionalSaveData(CompoundTag compoundTag) { protected void addAdditionalSaveData(CompoundTag compoundTag) {
} }
protected void onProjectileTick() { protected void onProjectileTick() {
@ -518,13 +514,11 @@ public class ProjectileEntity extends Entity implements IEntityAdditionalSpawnDa
ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new ClientIndicatorMessage(1, 5)); ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new ClientIndicatorMessage(1, 5));
} }
performOnHit(entity, this.damage, true, this.knockback);
performDamage(entity, this.damage, true);
} else { } else {
if (!this.shooter.level().isClientSide() && this.shooter instanceof ServerPlayer player) { if (!this.shooter.level().isClientSide() && this.shooter instanceof ServerPlayer player) {
var holder = Holder.direct(ModSounds.INDICATION.get()); var holder = Holder.direct(ModSounds.INDICATION.get());
player.connection.send(new ClientboundSoundPacket(holder, SoundSource.PLAYERS, player.getX(), player.getY(), player.getZ(), 1f, 1f, player.level().random.nextLong())); player.connection.send(new ClientboundSoundPacket(holder, SoundSource.PLAYERS, player.getX(), player.getY(), player.getZ(), 1f, 1f, player.level().random.nextLong()));
ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new ClientIndicatorMessage(0, 5)); ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new ClientIndicatorMessage(0, 5));
} }
@ -540,7 +534,7 @@ public class ProjectileEntity extends Entity implements IEntityAdditionalSpawnDa
this.damage *= this.legShot; this.damage *= this.legShot;
} }
performDamage(entity, this.damage, false); performOnHit(entity, this.damage, false, this.knockback);
} }
if (!this.mobEffects.isEmpty() && entity instanceof LivingEntity living) { if (!this.mobEffects.isEmpty() && entity instanceof LivingEntity living) {
@ -552,6 +546,17 @@ public class ProjectileEntity extends Entity implements IEntityAdditionalSpawnDa
this.discard(); this.discard();
} }
public void performOnHit(Entity entity, float damage, boolean headshot, double knockback) {
if (entity instanceof LivingEntity living) {
ICustomKnockback iCustomKnockback = ICustomKnockback.getInstance(living);
iCustomKnockback.superbWarfare$setKnockbackStrength(knockback);
performDamage(entity, damage, headshot);
iCustomKnockback.superbWarfare$resetKnockbackStrength();
} else {
performDamage(entity, damage, headshot);
}
}
protected void explosionBulletBlock(Entity projectile, float damage, int heLevel, float monsterMultiple, Vec3 hitVec) { protected void explosionBulletBlock(Entity projectile, float damage, int heLevel, float monsterMultiple, Vec3 hitVec) {
CustomExplosion explosion = new CustomExplosion(projectile.level(), projectile, CustomExplosion explosion = new CustomExplosion(projectile.level(), projectile,
ModDamageTypes.causeProjectileBoomDamage(projectile.level().registryAccess(), projectile, this.getShooter()), (float) ((0.9 * damage) * (1 + 0.1 * heLevel)), ModDamageTypes.causeProjectileBoomDamage(projectile.level().registryAccess(), projectile, this.getShooter()), (float) ((0.9 * damage) * (1 + 0.1 * heLevel)),
@ -857,4 +862,9 @@ public class ProjectileEntity extends Entity implements IEntityAdditionalSpawnDa
this.entityData.set(COLOR_G, rgb[1]); this.entityData.set(COLOR_G, rgb[1]);
this.entityData.set(COLOR_B, rgb[2]); this.entityData.set(COLOR_B, rgb[2]);
} }
public ProjectileEntity knockback(double knockback) {
this.knockback = knockback;
return this;
}
} }

View file

@ -5,6 +5,7 @@ import com.atsuishio.superbwarfare.capability.LaserCapability;
import com.atsuishio.superbwarfare.capability.ModCapabilities; import com.atsuishio.superbwarfare.capability.ModCapabilities;
import com.atsuishio.superbwarfare.config.common.GameplayConfig; import com.atsuishio.superbwarfare.config.common.GameplayConfig;
import com.atsuishio.superbwarfare.entity.ICannonEntity; import com.atsuishio.superbwarfare.entity.ICannonEntity;
import com.atsuishio.superbwarfare.entity.ICustomKnockback;
import com.atsuishio.superbwarfare.entity.IVehicleEntity; import com.atsuishio.superbwarfare.entity.IVehicleEntity;
import com.atsuishio.superbwarfare.entity.TargetEntity; import com.atsuishio.superbwarfare.entity.TargetEntity;
import com.atsuishio.superbwarfare.entity.projectile.ProjectileEntity; import com.atsuishio.superbwarfare.entity.projectile.ProjectileEntity;
@ -785,4 +786,12 @@ public class LivingEventHandler {
event.setAmount(event.getAmount() * (1.15f + 0.05f * level)); event.setAmount(event.getAmount() * (1.15f + 0.05f * level));
} }
@SubscribeEvent
public static void onKnockback(LivingKnockBackEvent event) {
ICustomKnockback knockback = ICustomKnockback.getInstance(event.getEntity());
if (knockback.superbWarfare$getKnockbackStrength() >= 0) {
event.setStrength((float) knockback.superbWarfare$getKnockbackStrength());
}
}
} }

View file

@ -1,36 +1,28 @@
package com.atsuishio.superbwarfare.mixins; package com.atsuishio.superbwarfare.mixins;
import com.atsuishio.superbwarfare.init.ModDamageTypes; import com.atsuishio.superbwarfare.entity.ICustomKnockback;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.LivingEntity;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(LivingEntity.class) @Mixin(LivingEntity.class)
public class LivingEntityMixin { public class LivingEntityMixin implements ICustomKnockback {
@Unique @Unique
private DamageSource target$source; private double superbwarfare$knockbackStrength = 0;
@Inject(method = "hurt", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/LivingEntity;knockback(DDD)V")) @Override
private void capture(DamageSource source, float amount, CallbackInfoReturnable<Boolean> cir) { public void superbWarfare$setKnockbackStrength(double strength) {
this.target$source = source; this.superbwarfare$knockbackStrength = strength;
} }
@ModifyArg(method = "hurt", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/LivingEntity;knockback(DDD)V"), index = 0) @Override
private double modifyApplyKnockbackArgs(double original) { public void superbWarfare$resetKnockbackStrength() {
if (this.target$source.is(ModDamageTypes.GUN_FIRE) || this.target$source.is(ModDamageTypes.GUN_FIRE_HEADSHOT) this.superbwarfare$knockbackStrength = -1;
|| this.target$source.is(ModDamageTypes.SHOCK) || this.target$source.is(ModDamageTypes.GUN_FIRE_ABSOLUTE)
|| this.target$source.is(ModDamageTypes.GUN_FIRE_HEADSHOT_ABSOLUTE) || this.target$source.is(ModDamageTypes.BURN)) {
return 0.05 * original;
} }
if (this.target$source.is(ModDamageTypes.LASER) || this.target$source.is(ModDamageTypes.LASER_HEADSHOT)) {
return -original; @Override
} public double superbWarfare$getKnockbackStrength() {
return original; return this.superbwarfare$knockbackStrength;
} }
} }