superb-warfare/src/main/java/com/atsuishio/superbwarfare/entity/projectile/Mk82Entity.java
2025-05-13 23:13:59 +08:00

180 lines
6 KiB
Java

package com.atsuishio.superbwarfare.entity.projectile;
import com.atsuishio.superbwarfare.config.server.ExplosionConfig;
import com.atsuishio.superbwarfare.entity.LoudlyEntity;
import com.atsuishio.superbwarfare.init.ModEntities;
import com.atsuishio.superbwarfare.init.ModItems;
import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.tools.ProjectileTool;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.damagesource.DamageTypes;
import net.minecraft.world.entity.AreaEffectCloud;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.projectile.ThrowableItemProjectile;
import net.minecraft.world.entity.projectile.ThrownPotion;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.BlockHitResult;
import org.jetbrains.annotations.NotNull;
import software.bernie.geckolib.animatable.GeoEntity;
import software.bernie.geckolib.animatable.instance.AnimatableInstanceCache;
import software.bernie.geckolib.animation.*;
import software.bernie.geckolib.util.GeckoLibUtil;
public class Mk82Entity extends FastThrowableProjectile implements GeoEntity, DestroyableProjectileEntity, LoudlyEntity, AerialBombEntity {
public static final EntityDataAccessor<Float> HEALTH = SynchedEntityData.defineId(Mk82Entity.class, EntityDataSerializers.FLOAT);
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
private float explosionDamage = ExplosionConfig.MK_82_EXPLOSION_DAMAGE.get();
private float explosionRadius = ExplosionConfig.MK_82_EXPLOSION_RADIUS.get().floatValue();
public Mk82Entity(EntityType<? extends Mk82Entity> type, Level world) {
super(type, world);
this.noCulling = true;
}
public Mk82Entity(LivingEntity entity, Level level) {
super(ModEntities.MK_82.get(), entity, level);
}
public Mk82Entity(EntityType<? extends ThrowableItemProjectile> pEntityType, double pX, double pY, double pZ, Level pLevel) {
super(pEntityType, pX, pY, pZ, pLevel);
this.noCulling = true;
}
@Override
protected @NotNull Item getDefaultItem() {
return ModItems.MEDIUM_AERIAL_BOMB.get();
}
@Override
public boolean hurt(DamageSource source, float amount) {
if (source.getDirectEntity() instanceof ThrownPotion || source.getDirectEntity() instanceof AreaEffectCloud)
return false;
if (source.is(DamageTypes.FALL))
return false;
if (source.is(DamageTypes.CACTUS))
return false;
if (source.is(DamageTypes.DROWN))
return false;
if (source.is(DamageTypes.DRAGON_BREATH))
return false;
if (source.is(DamageTypes.WITHER))
return false;
if (source.is(DamageTypes.WITHER_SKULL))
return false;
if (source.getDirectEntity() instanceof Mk82Entity)
return false;
this.entityData.set(HEALTH, this.entityData.get(HEALTH) - amount);
return super.hurt(source, amount);
}
@Override
protected void defineSynchedData(SynchedEntityData.@NotNull Builder builder) {
super.defineSynchedData(builder);
builder.define(HEALTH, 50f);
}
@Override
public boolean isPickable() {
return !this.isRemoved();
}
@Override
public void readAdditionalSaveData(@NotNull CompoundTag compound) {
super.readAdditionalSaveData(compound);
if (compound.contains("Health")) {
this.entityData.set(HEALTH, compound.getFloat("Health"));
}
}
@Override
public void addAdditionalSaveData(@NotNull CompoundTag compound) {
super.addAdditionalSaveData(compound);
compound.putFloat("Health", this.entityData.get(HEALTH));
}
@Override
public boolean shouldRenderAtSqrDistance(double pDistance) {
return true;
}
@Override
public void onHitBlock(@NotNull BlockHitResult blockHitResult) {
super.onHitBlock(blockHitResult);
ProjectileTool.causeCustomExplode(this, this.explosionDamage, this.explosionRadius, 1.2f);
this.discard();
}
@Override
public void tick() {
super.tick();
if (tickCount > 600 || this.entityData.get(HEALTH) <= 0) {
if (!this.level().isClientSide) {
ProjectileTool.causeCustomExplode(this, this.explosionDamage, this.explosionRadius, 1.2f);
}
this.discard();
}
}
private PlayState movementPredicate(AnimationState<Mk82Entity> event) {
return event.setAndContinue(RawAnimation.begin().thenPlayAndHold("animation.mk_82.start"));
}
@Override
protected double getDefaultGravity() {
return 0.06F;
}
@Override
public void registerControllers(AnimatableManager.ControllerRegistrar data) {
data.add(new AnimationController<>(this, "movement", 0, this::movementPredicate));
}
@Override
public AnimatableInstanceCache getAnimatableInstanceCache() {
return this.cache;
}
@Override
public @NotNull SoundEvent getCloseSound() {
return LoudlyEntity.super.getCloseSound();
}
@Override
public @NotNull SoundEvent getSound() {
return ModSounds.SHELL_FLY.get();
}
@Override
public float getVolume() {
return 0.7f;
}
@Override
public boolean shouldSyncMotion() {
return true;
}
@Override
public void setDamage(float damage) {
}
@Override
public void setExplosionDamage(float damage) {
this.explosionDamage = damage;
}
@Override
public void setExplosionRadius(float radius) {
this.explosionRadius = radius;
}
}