superb-warfare/src/main/java/net/mcreator/target/entity/SenpaiEntity.java
2024-05-09 19:53:45 +08:00

244 lines
9.8 KiB
Java

package net.mcreator.target.entity;
import net.mcreator.target.init.TargetModEntities;
import net.mcreator.target.procedures.SenpaiDangShiTiGengXinKeShiProcedure;
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.resources.ResourceLocation;
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.monster.Spider;
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 net.minecraftforge.registries.ForgeRegistries;
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 Spider implements GeoEntity, AnimatedEntity {
public static final EntityDataAccessor<Boolean> SHOOT = SynchedEntityData.defineId(SenpaiEntity.class, EntityDataSerializers.BOOLEAN);
public static final EntityDataAccessor<String> ANIMATION = SynchedEntityData.defineId(SenpaiEntity.class, EntityDataSerializers.STRING);
public static final EntityDataAccessor<String> TEXTURE = SynchedEntityData.defineId(SenpaiEntity.class, EntityDataSerializers.STRING);
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
private boolean swinging;
private boolean lastloop;
private long lastSwing;
public String animationProcedure = "empty";
public SenpaiEntity(PlayMessages.SpawnEntity packet, Level world) {
this(TargetModEntities.SENPAI.get(), world);
}
public SenpaiEntity(EntityType<SenpaiEntity> type, Level world) {
super(type, world);
xpReward = 20;
setNoAi(false);
}
@Override
protected void defineSynchedData() {
super.defineSynchedData();
this.entityData.define(SHOOT, false);
this.entityData.define(ANIMATION, "undefined");
this.entityData.define(TEXTURE, "senpai");
}
public void setTexture(String texture) {
this.entityData.set(TEXTURE, texture);
}
public String getTexture() {
return this.entityData.get(TEXTURE);
}
@Override
public Packet<ClientGamePacketListener> getAddEntityPacket() {
return NetworkHooks.getEntitySpawningPacket(this);
}
@Override
protected void registerGoals() {
super.registerGoals();
this.goalSelector.addGoal(1, new MeleeAttackGoal(this, 1.6, 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);
this.spawnAtLocation(new ItemStack(Items.GOLDEN_APPLE));
}
@Override
public SoundEvent getAmbientSound() {
return ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("target:idle"));
}
@Override
public void playStepSound(BlockPos pos, BlockState blockIn) {
this.playSound(ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("target:step")), 0.15f, 1);
}
@Override
public SoundEvent getHurtSound(DamageSource ds) {
return ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("target:ouch"));
}
@Override
public SoundEvent getDeathSound() {
return ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("target:growl"));
}
@Override
public void addAdditionalSaveData(CompoundTag compound) {
super.addAdditionalSaveData(compound);
compound.putString("Texture", this.getTexture());
}
@Override
public void readAdditionalSaveData(CompoundTag compound) {
super.readAdditionalSaveData(compound);
if (compound.contains("Texture"))
this.setTexture(compound.getString("Texture"));
}
@Override
public void baseTick() {
super.baseTick();
SenpaiDangShiTiGengXinKeShiProcedure.execute(this.level(), this.getX(), this.getY(), this.getZ(), this);
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(TargetModEntities.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(TargetModEntities.SENPAI.get(), 180);
}
public static AttributeSupplier.Builder createAttributes() {
AttributeSupplier.Builder builder = Mob.createMobAttributes();
builder = builder.add(Attributes.MOVEMENT_SPEED, 0.25);
builder = builder.add(Attributes.MAX_HEALTH, 51);
builder = builder.add(Attributes.ARMOR, 0);
builder = builder.add(Attributes.ATTACK_DAMAGE, 5);
builder = builder.add(Attributes.FOLLOW_RANGE, 1024);
builder = builder.add(Attributes.KNOCKBACK_RESISTANCE, 0.5);
return builder;
}
private PlayState movementPredicate(AnimationState 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 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
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;
}
}