重构击退判定

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 int fireLevel = 0;
private boolean dragonBreath = false;
private double knockback = 0.05;
private final ArrayList<MobEffectInstance> mobEffects = new ArrayList<>();
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) {
if (entity.equals(this.shooter)) continue;
if (entity.equals(this.shooter.getVehicle())) continue;
if (entity instanceof TargetEntity && entity.getEntityData().get(TargetEntity.DOWN_TIME) > 0) continue;
EntityResult result = this.getHitResult(entity, startVec, endVec);
if (result == null) continue;
Vec3 hitPos = result.getHitPos();
if (hitPos == null) continue;
double distanceToHit = startVec.distanceTo(hitPos);
@ -221,12 +219,12 @@ public class ProjectileEntity extends Entity implements IEntityAdditionalSpawnDa
boolean headshot = false;
boolean legShot = false;
float eyeHeight = entity.getEyeHeight();
float BodyHeight = entity.getBbHeight();
float bodyHeight = entity.getBbHeight();
if ((eyeHeight - 0.35) < hitBoxPos.y && hitBoxPos.y < (eyeHeight + 0.4) &&
!(entity instanceof ClaymoreEntity || entity instanceof MortarEntity || entity instanceof IVehicleEntity || entity instanceof DroneEntity)) {
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)) {
legShot = true;
}
@ -310,12 +308,10 @@ public class ProjectileEntity extends Entity implements IEntityAdditionalSpawnDa
@Override
protected void readAdditionalSaveData(CompoundTag compoundTag) {
}
@Override
protected void addAdditionalSaveData(CompoundTag compoundTag) {
}
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));
}
performDamage(entity, this.damage, true);
performOnHit(entity, this.damage, true, this.knockback);
} else {
if (!this.shooter.level().isClientSide() && this.shooter instanceof ServerPlayer player) {
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()));
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;
}
performDamage(entity, this.damage, false);
performOnHit(entity, this.damage, false, this.knockback);
}
if (!this.mobEffects.isEmpty() && entity instanceof LivingEntity living) {
@ -552,6 +546,17 @@ public class ProjectileEntity extends Entity implements IEntityAdditionalSpawnDa
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) {
CustomExplosion explosion = new CustomExplosion(projectile.level(), projectile,
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_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.config.common.GameplayConfig;
import com.atsuishio.superbwarfare.entity.ICannonEntity;
import com.atsuishio.superbwarfare.entity.ICustomKnockback;
import com.atsuishio.superbwarfare.entity.IVehicleEntity;
import com.atsuishio.superbwarfare.entity.TargetEntity;
import com.atsuishio.superbwarfare.entity.projectile.ProjectileEntity;
@ -785,4 +786,12 @@ public class LivingEventHandler {
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;
import com.atsuishio.superbwarfare.init.ModDamageTypes;
import net.minecraft.world.damagesource.DamageSource;
import com.atsuishio.superbwarfare.entity.ICustomKnockback;
import net.minecraft.world.entity.LivingEntity;
import org.spongepowered.asm.mixin.Mixin;
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)
public class LivingEntityMixin {
public class LivingEntityMixin implements ICustomKnockback {
@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"))
private void capture(DamageSource source, float amount, CallbackInfoReturnable<Boolean> cir) {
this.target$source = source;
@Override
public void superbWarfare$setKnockbackStrength(double strength) {
this.superbwarfare$knockbackStrength = strength;
}
@ModifyArg(method = "hurt", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/LivingEntity;knockback(DDD)V"), index = 0)
private double modifyApplyKnockbackArgs(double original) {
if (this.target$source.is(ModDamageTypes.GUN_FIRE) || this.target$source.is(ModDamageTypes.GUN_FIRE_HEADSHOT)
|| 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;
@Override
public void superbWarfare$resetKnockbackStrength() {
this.superbwarfare$knockbackStrength = -1;
}
if (this.target$source.is(ModDamageTypes.LASER) || this.target$source.is(ModDamageTypes.LASER_HEADSHOT)) {
return -original;
}
return original;
@Override
public double superbWarfare$getKnockbackStrength() {
return this.superbwarfare$knockbackStrength;
}
}