superb-warfare/src/main/java/com/atsuishio/superbwarfare/entity/projectile/GunGrenadeEntity.java
2025-06-16 15:41:18 +08:00

200 lines
7.4 KiB
Java

package com.atsuishio.superbwarfare.entity.projectile;
import com.atsuishio.superbwarfare.init.ModDamageTypes;
import com.atsuishio.superbwarfare.init.ModEntities;
import com.atsuishio.superbwarfare.init.ModItems;
import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.network.message.receive.ClientIndicatorMessage;
import com.atsuishio.superbwarfare.tools.ParticleTool;
import com.atsuishio.superbwarfare.tools.ProjectileTool;
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.monster.Monster;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BellBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.EntityHitResult;
import net.neoforged.neoforge.network.PacketDistributor;
import org.jetbrains.annotations.NotNull;
import software.bernie.geckolib.animatable.GeoEntity;
import software.bernie.geckolib.animatable.instance.AnimatableInstanceCache;
import software.bernie.geckolib.animation.AnimatableManager;
import software.bernie.geckolib.util.GeckoLibUtil;
public class GunGrenadeEntity extends FastThrowableProjectile implements GeoEntity, ExplosiveProjectile {
private float monsterMultiplier = 0.0f;
private float damage = 40.0f;
private float explosionDamage = 80f;
private float explosionRadius = 5f;
private boolean charged = false;
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
public GunGrenadeEntity(EntityType<? extends GunGrenadeEntity> type, Level world) {
super(type, world);
this.noCulling = true;
}
public GunGrenadeEntity(LivingEntity entity, Level level, float damage, float explosionDamage, float explosionRadius) {
super(ModEntities.GUN_GRENADE.get(), entity, level);
this.damage = damage;
this.explosionDamage = explosionDamage;
this.explosionRadius = explosionRadius;
}
@Override
public void addAdditionalSaveData(@NotNull CompoundTag pCompound) {
super.addAdditionalSaveData(pCompound);
pCompound.putFloat("Damage", this.damage);
pCompound.putFloat("ExplosionDamage", this.explosionDamage);
pCompound.putFloat("Radius", this.explosionRadius);
}
@Override
public void readAdditionalSaveData(@NotNull CompoundTag pCompound) {
super.readAdditionalSaveData(pCompound);
if (pCompound.contains("Damage")) {
this.damage = pCompound.getFloat("Damage");
}
if (pCompound.contains("ExplosionDamage")) {
this.explosionDamage = pCompound.getFloat("ExplosionDamage");
}
if (pCompound.contains("Radius")) {
this.explosionRadius = pCompound.getFloat("Radius");
}
}
@Override
public void setDamage(float damage) {
this.damage = damage;
}
@Override
public void setExplosionDamage(float explosionDamage) {
this.explosionDamage = explosionDamage;
}
@Override
public void setExplosionRadius(float explosionRadius) {
this.explosionRadius = explosionRadius;
}
public void setCharged(boolean charged) {
this.charged = charged;
}
public void setMonsterMultiplier(float monsterMultiplier) {
this.monsterMultiplier = monsterMultiplier;
}
public void charged(boolean charged) {
this.charged = charged;
}
@Override
protected @NotNull Item getDefaultItem() {
return ModItems.GRENADE_40MM.get();
}
@Override
public boolean shouldRenderAtSqrDistance(double pDistance) {
return true;
}
@Override
protected void onHitEntity(EntityHitResult result) {
float damageMultiplier = 1 + this.monsterMultiplier;
Entity entity = result.getEntity();
if (this.getOwner() != null && this.getOwner().getVehicle() != null && entity == this.getOwner().getVehicle())
return;
if (this.getOwner() instanceof LivingEntity living) {
if (!living.level().isClientSide() && living instanceof ServerPlayer player) {
living.level().playSound(null, living.blockPosition(), ModSounds.INDICATION.get(), SoundSource.VOICE, 1, 1);
PacketDistributor.sendToPlayer(player, new ClientIndicatorMessage(0, 5));
}
}
if (charged) {
damage *= 1.25f;
}
if (entity instanceof Monster monster) {
monster.hurt(ModDamageTypes.causeCannonFireDamage(this.level().registryAccess(), this, this.getOwner()), 1.2f * this.damage * damageMultiplier);
} else {
entity.hurt(ModDamageTypes.causeCannonFireDamage(this.level().registryAccess(), this, this.getOwner()), damage);
}
if (entity instanceof LivingEntity) {
entity.invulnerableTime = 0;
}
if (this.tickCount > 0) {
if (this.level() instanceof ServerLevel) {
ProjectileTool.causeCustomExplode(this,
ModDamageTypes.causeProjectileBoomDamage(this.level().registryAccess(), this, this.getOwner()),
entity, this.explosionDamage, this.explosionRadius, this.monsterMultiplier);
}
}
this.discard();
}
@Override
public void onHitBlock(@NotNull BlockHitResult blockHitResult) {
super.onHitBlock(blockHitResult);
BlockPos resultPos = blockHitResult.getBlockPos();
BlockState state = this.level().getBlockState(resultPos);
if (state.getBlock() instanceof BellBlock bell) {
bell.attemptToRing(this.level(), resultPos, blockHitResult.getDirection());
}
if (this.tickCount > 0) {
if (this.level() instanceof ServerLevel) {
ProjectileTool.causeCustomExplode(this,
ModDamageTypes.causeProjectileBoomDamage(this.level().registryAccess(), this, this.getOwner()),
this, this.explosionDamage, this.explosionRadius, this.monsterMultiplier);
}
}
this.discard();
}
@Override
public void tick() {
super.tick();
if (!this.level().isClientSide() && this.level() instanceof ServerLevel serverLevel) {
ParticleTool.sendParticle(serverLevel, ParticleTypes.SMOKE, this.xo, this.yo, this.zo,
1, 0, 0, 0, 0.02, true);
}
if (this.tickCount > 200 || this.isInWater()) {
if (this.level() instanceof ServerLevel) {
ProjectileTool.causeCustomExplode(this,
ModDamageTypes.causeProjectileBoomDamage(this.level().registryAccess(), this, this.getOwner()),
this, this.explosionDamage, this.explosionRadius, this.monsterMultiplier);
}
this.discard();
}
}
@Override
public void registerControllers(AnimatableManager.ControllerRegistrar data) {
}
@Override
public AnimatableInstanceCache getAnimatableInstanceCache() {
return this.cache;
}
}