superb-warfare/src/main/java/net/mcreator/superbwarfare/entity/SenpaiEntity.java
2024-08-15 14:59:37 +08:00

240 lines
8.9 KiB
Java

package net.mcreator.superbwarfare.entity;
import net.mcreator.superbwarfare.init.ModEntities;
import net.mcreator.superbwarfare.init.ModSounds;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
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.Difficulty;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.*;
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.ai.goal.FloatGoal;
import net.minecraft.world.entity.ai.goal.MeleeAttackGoal;
import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;
import net.minecraft.world.entity.ai.goal.RandomStrollGoal;
import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal;
import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
import net.minecraft.world.entity.monster.Monster;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.levelgen.Heightmap;
import net.minecraftforge.common.DungeonHooks;
import net.minecraftforge.network.NetworkHooks;
import net.minecraftforge.network.PlayMessages;
import software.bernie.geckolib.animatable.GeoEntity;
import software.bernie.geckolib.core.animatable.instance.AnimatableInstanceCache;
import software.bernie.geckolib.core.animation.AnimatableManager;
import software.bernie.geckolib.core.animation.AnimationController;
import software.bernie.geckolib.core.animation.AnimationState;
import software.bernie.geckolib.core.animation.RawAnimation;
import software.bernie.geckolib.core.object.PlayState;
import software.bernie.geckolib.util.GeckoLibUtil;
public class SenpaiEntity extends Monster implements GeoEntity, AnimatedEntity {
public static final EntityDataAccessor<String> ANIMATION = SynchedEntityData.defineId(SenpaiEntity.class, EntityDataSerializers.STRING);
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
public String animationProcedure = "empty";
public SenpaiEntity(PlayMessages.SpawnEntity packet, Level world) {
this(ModEntities.SENPAI.get(), world);
}
public SenpaiEntity(EntityType<SenpaiEntity> type, Level world) {
super(type, world);
xpReward = 40;
setNoAi(false);
}
@Override
protected void defineSynchedData() {
super.defineSynchedData();
this.entityData.define(ANIMATION, "undefined");
}
@Override
protected float getStandingEyeHeight(Pose poseIn, EntityDimensions sizeIn) {
return 1.75F;
}
@Override
public Packet<ClientGamePacketListener> getAddEntityPacket() {
return NetworkHooks.getEntitySpawningPacket(this);
}
@Override
protected void registerGoals() {
super.registerGoals();
this.goalSelector.addGoal(1, new MeleeAttackGoal(this, 1.4, false) {
@Override
protected double getAttackReachSqr(LivingEntity entity) {
return this.mob.getBbWidth() * this.mob.getBbWidth() + entity.getBbWidth();
}
});
this.targetSelector.addGoal(2, new HurtByTargetGoal(this).setAlertOthers());
this.goalSelector.addGoal(3, new RandomLookAroundGoal(this));
this.goalSelector.addGoal(4, new FloatGoal(this));
this.goalSelector.addGoal(5, new RandomStrollGoal(this, 0.8));
this.targetSelector.addGoal(6, new NearestAttackableTargetGoal<>(this, Player.class, false, false));
}
@Override
public MobType getMobType() {
return MobType.ILLAGER;
}
protected void dropCustomDeathLoot(DamageSource source, int looting, boolean recentlyHitIn) {
super.dropCustomDeathLoot(source, looting, recentlyHitIn);
double random = Math.random();
if (random < 0.01) {
this.spawnAtLocation(new ItemStack(Items.ENCHANTED_GOLDEN_APPLE));
} else if (random < 0.2) {
this.spawnAtLocation(new ItemStack(Items.GOLDEN_APPLE));
} else {
this.spawnAtLocation(new ItemStack(Items.APPLE));
}
}
@Override
public SoundEvent getAmbientSound() {
return ModSounds.IDLE.get();
}
@Override
public void playStepSound(BlockPos pos, BlockState blockIn) {
this.playSound(ModSounds.STEP.get(), 0.25f, 1);
}
@Override
public SoundEvent getHurtSound(DamageSource ds) {
return ModSounds.OUCH.get();
}
@Override
public SoundEvent getDeathSound() {
return ModSounds.GROWL.get();
}
@Override
public void addAdditionalSaveData(CompoundTag compound) {
super.addAdditionalSaveData(compound);
}
@Override
public void readAdditionalSaveData(CompoundTag compound) {
super.readAdditionalSaveData(compound);
}
@Override
public void baseTick() {
super.baseTick();
this.refreshDimensions();
}
@Override
public EntityDimensions getDimensions(Pose p_33597_) {
return super.getDimensions(p_33597_).scale((float) 1);
}
@Override
public void aiStep() {
super.aiStep();
this.updateSwingTime();
}
public static void init() {
SpawnPlacements.register(ModEntities.SENPAI.get(), SpawnPlacements.Type.ON_GROUND, Heightmap.Types.MOTION_BLOCKING_NO_LEAVES,
(entityType, world, reason, pos, random) -> (world.getDifficulty() != Difficulty.PEACEFUL && Monster.isDarkEnoughToSpawn(world, pos, random) && Mob.checkMobSpawnRules(entityType, world, reason, pos, random)));
DungeonHooks.addDungeonMob(ModEntities.SENPAI.get(), 180);
}
public static AttributeSupplier.Builder createAttributes() {
return Mob.createMobAttributes()
.add(Attributes.MOVEMENT_SPEED, 0.23)
.add(Attributes.MAX_HEALTH, 24)
.add(Attributes.ARMOR, 0)
.add(Attributes.ATTACK_DAMAGE, 5)
.add(Attributes.FOLLOW_RANGE, 64)
.add(Attributes.KNOCKBACK_RESISTANCE, 0.5);
}
private PlayState movementPredicate(AnimationState<SenpaiEntity> event) {
if (this.animationProcedure.equals("empty")) {
if ((event.isMoving() || !(event.getLimbSwingAmount() > -0.15F && event.getLimbSwingAmount() < 0.15F))
&& !this.isAggressive()) {
return event.setAndContinue(RawAnimation.begin().thenLoop("animation.senpai.walk"));
}
if (this.isDeadOrDying()) {
return event.setAndContinue(RawAnimation.begin().thenPlay("animation.senpai.die"));
}
if (this.isAggressive() && event.isMoving()) {
return event.setAndContinue(RawAnimation.begin().thenLoop("animation.senpai.run"));
}
return event.setAndContinue(RawAnimation.begin().thenLoop("animation.senpai.idle"));
}
return PlayState.STOP;
}
private PlayState procedurePredicate(AnimationState<SenpaiEntity> event) {
if (!animationProcedure.equals("empty") && event.getController().getAnimationState() == AnimationController.State.STOPPED) {
event.getController().setAnimation(RawAnimation.begin().thenPlay(this.animationProcedure));
if (event.getController().getAnimationState() == AnimationController.State.STOPPED) {
this.animationProcedure = "empty";
event.getController().forceAnimationReset();
}
} else if (animationProcedure.equals("empty")) {
return PlayState.STOP;
}
return PlayState.CONTINUE;
}
@Override
public void die(DamageSource source) {
super.die(source);
}
@Override
protected void tickDeath() {
++this.deathTime;
if (this.deathTime == 540) {
this.remove(SenpaiEntity.RemovalReason.KILLED);
this.dropExperience();
}
}
public String getSyncedAnimation() {
return this.entityData.get(ANIMATION);
}
public void setAnimation(String animation) {
this.entityData.set(ANIMATION, animation);
}
@Override
public void setAnimationProcedure(String procedure) {
this.animationProcedure = procedure;
}
@Override
public void registerControllers(AnimatableManager.ControllerRegistrar data) {
data.add(new AnimationController<>(this, "movement", 4, this::movementPredicate));
data.add(new AnimationController<>(this, "procedure", 4, this::procedurePredicate));
}
@Override
public AnimatableInstanceCache getAnimatableInstanceCache() {
return this.cache;
}
}