superb-warfare/src/main/java/net/mcreator/target/entity/TargetEntity.java
2024-05-09 20:02:05 +08:00

260 lines
8.9 KiB
Java

package net.mcreator.target.entity;
import net.mcreator.target.init.TargetModEntities;
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.damagesource.DamageSource;
import net.minecraft.world.damagesource.DamageTypes;
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.control.FlyingMoveControl;
import net.minecraft.world.entity.ai.navigation.FlyingPathNavigation;
import net.minecraft.world.entity.ai.navigation.PathNavigation;
import net.minecraft.world.entity.projectile.ThrownPotion;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
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 TargetEntity extends PathfinderMob implements GeoEntity, AnimatedEntity {
public static final EntityDataAccessor<Boolean> SHOOT = SynchedEntityData.defineId(TargetEntity.class, EntityDataSerializers.BOOLEAN);
public static final EntityDataAccessor<String> ANIMATION = SynchedEntityData.defineId(TargetEntity.class, EntityDataSerializers.STRING);
public static final EntityDataAccessor<String> TEXTURE = SynchedEntityData.defineId(TargetEntity.class, EntityDataSerializers.STRING);
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
private boolean swinging;
private boolean lastloop;
private long lastSwing;
public String animationProcedure = "empty";
public TargetEntity(PlayMessages.SpawnEntity packet, Level world) {
this(TargetModEntities.TARGET.get(), world);
}
public TargetEntity(EntityType<TargetEntity> type, Level world) {
super(type, world);
xpReward = 0;
setNoAi(true);
setPersistenceRequired();
this.moveControl = new FlyingMoveControl(this, 10, true);
}
@Override
protected void defineSynchedData() {
super.defineSynchedData();
this.entityData.define(SHOOT, false);
this.entityData.define(ANIMATION, "undefined");
this.entityData.define(TEXTURE, "target");
}
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 PathNavigation createNavigation(Level world) {
return new FlyingPathNavigation(this, world);
}
@Override
public MobType getMobType() {
return MobType.UNDEFINED;
}
@Override
public boolean removeWhenFarAway(double distanceToClosestPlayer) {
return false;
}
@Override
public SoundEvent getHurtSound(DamageSource ds) {
return ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("target:hit"));
}
@Override
public SoundEvent getDeathSound() {
return ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("target:hit"));
}
@Override
public boolean causeFallDamage(float l, float d, DamageSource source) {
return false;
}
@Override
public boolean hurt(DamageSource source, float amount) {
if (source.is(DamageTypes.IN_FIRE))
return false;
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.LIGHTNING_BOLT))
return false;
if (source.is(DamageTypes.EXPLOSION))
return false;
if (source.is(DamageTypes.FALLING_ANVIL))
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;
return super.hurt(source, amount);
}
@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();
this.refreshDimensions();
}
@Override
public EntityDimensions getDimensions(Pose p_33597_) {
return super.getDimensions(p_33597_).scale((float) 1);
}
@Override
public boolean isPushable() {
return false;
}
@Override
protected void doPush(Entity entityIn) {
}
@Override
protected void pushEntities() {
}
@Override
protected void checkFallDamage(double y, boolean onGroundIn, BlockState state, BlockPos pos) {
}
@Override
public void setNoGravity(boolean ignored) {
super.setNoGravity(true);
}
@Override
public void aiStep() {
super.aiStep();
this.updateSwingTime();
this.setNoGravity(true);
}
public static void init() {
}
public static AttributeSupplier.Builder createAttributes() {
return Mob.createMobAttributes()
.add(Attributes.MOVEMENT_SPEED, 0)
.add(Attributes.MAX_HEALTH, 5)
.add(Attributes.ARMOR, 0)
.add(Attributes.ATTACK_DAMAGE, 0)
.add(Attributes.FOLLOW_RANGE, 16)
.add(Attributes.KNOCKBACK_RESISTANCE, 10)
.add(Attributes.FLYING_SPEED, 0);
}
private PlayState movementPredicate(AnimationState event) {
if (this.animationProcedure.equals("empty")) {
if (this.isDeadOrDying()) {
return event.setAndContinue(RawAnimation.begin().thenPlay("animation.target2.down"));
}
return event.setAndContinue(RawAnimation.begin().thenLoop("animation.target2.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 == 20) {
this.remove(TargetEntity.RemovalReason.KILLED);
this.dropExperience();
}
}
@Override
public String getSyncedAnimation() {
return this.entityData.get(ANIMATION);
}
@Override
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", 0, this::movementPredicate));
data.add(new AnimationController<>(this, "procedure", 0, this::procedurePredicate));
}
@Override
public AnimatableInstanceCache getAnimatableInstanceCache() {
return this.cache;
}
}