package net.mcreator.conceptsmp.entity; import software.bernie.geckolib.util.GeckoLibUtil; import software.bernie.geckolib.core.object.PlayState; import software.bernie.geckolib.core.animation.RawAnimation; import software.bernie.geckolib.core.animation.AnimationState; import software.bernie.geckolib.core.animation.AnimationController; import software.bernie.geckolib.core.animation.AnimatableManager; import software.bernie.geckolib.core.animatable.instance.AnimatableInstanceCache; import software.bernie.geckolib.animatable.GeoEntity; import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.network.PlayMessages; import net.minecraftforge.network.NetworkHooks; import net.minecraft.world.phys.Vec3; import net.minecraft.world.level.Level; import net.minecraft.world.item.ItemStack; import net.minecraft.world.entity.projectile.ThrownPotion; import net.minecraft.world.entity.projectile.AbstractArrow; import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.entity.ai.attributes.AttributeSupplier; import net.minecraft.world.entity.Pose; import net.minecraft.world.entity.PathfinderMob; import net.minecraft.world.entity.MobType; import net.minecraft.world.entity.Mob; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.EntityDimensions; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.AreaEffectCloud; import net.minecraft.world.damagesource.DamageTypes; import net.minecraft.world.damagesource.DamageSource; import net.minecraft.world.InteractionResult; import net.minecraft.world.InteractionHand; import net.minecraft.sounds.SoundEvent; import net.minecraft.resources.ResourceLocation; import net.minecraft.network.syncher.SynchedEntityData; import net.minecraft.network.syncher.EntityDataSerializers; import net.minecraft.network.syncher.EntityDataAccessor; import net.minecraft.network.protocol.game.ClientGamePacketListener; import net.minecraft.network.protocol.Packet; import net.mcreator.conceptsmp.init.ConceptsmpModEntities; public class CarEntity extends PathfinderMob implements GeoEntity { public static final EntityDataAccessor SHOOT = SynchedEntityData.defineId(CarEntity.class, EntityDataSerializers.BOOLEAN); public static final EntityDataAccessor ANIMATION = SynchedEntityData.defineId(CarEntity.class, EntityDataSerializers.STRING); public static final EntityDataAccessor TEXTURE = SynchedEntityData.defineId(CarEntity.class, EntityDataSerializers.STRING); private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); private boolean swinging; private boolean lastloop; private long lastSwing; public String animationprocedure = "empty"; public CarEntity(PlayMessages.SpawnEntity packet, Level world) { this(ConceptsmpModEntities.CAR.get(), world); } public CarEntity(EntityType type, Level world) { super(type, world); xpReward = 0; setNoAi(false); setPersistenceRequired(); } @Override protected void defineSynchedData() { super.defineSynchedData(); this.entityData.define(SHOOT, false); this.entityData.define(ANIMATION, "undefined"); this.entityData.define(TEXTURE, "cartexture"); } public void setTexture(String texture) { this.entityData.set(TEXTURE, texture); } public String getTexture() { return this.entityData.get(TEXTURE); } @Override public Packet getAddEntityPacket() { return NetworkHooks.getEntitySpawningPacket(this); } @Override protected void registerGoals() { super.registerGoals(); } @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("block.copper.hit")); } @Override public SoundEvent getDeathSound() { return ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("block.copper.break")); } @Override public boolean hurt(DamageSource source, float amount) { if (source.is(DamageTypes.IN_FIRE)) return false; if (source.getDirectEntity() instanceof AbstractArrow) return false; if (source.getDirectEntity() instanceof ThrownPotion || source.getDirectEntity() instanceof AreaEffectCloud) return false; if (source.is(DamageTypes.CACTUS)) return false; if (source.is(DamageTypes.DROWN)) return false; return super.hurt(source, amount); } @Override public InteractionResult mobInteract(Player sourceentity, InteractionHand hand) { ItemStack itemstack = sourceentity.getItemInHand(hand); InteractionResult retval = InteractionResult.sidedSuccess(this.level().isClientSide()); super.mobInteract(sourceentity, hand); sourceentity.startRiding(this); return retval; } @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 travel(Vec3 dir) { Entity entity = this.getPassengers().isEmpty() ? null : (Entity) this.getPassengers().get(0); if (this.isVehicle()) { this.setYRot(entity.getYRot()); this.yRotO = this.getYRot(); this.setXRot(entity.getXRot() * 0.5F); this.setRot(this.getYRot(), this.getXRot()); this.yBodyRot = entity.getYRot(); this.yHeadRot = entity.getYRot(); this.maxUpStep = 1.0F; if (entity instanceof LivingEntity passenger) { this.setSpeed((float) this.getAttributeValue(Attributes.MOVEMENT_SPEED)); float forward = passenger.zza; float strafe = 0; super.travel(new Vec3(strafe, 0, forward)); } double d1 = this.getX() - this.xo; double d0 = this.getZ() - this.zo; float f1 = (float) Math.sqrt(d1 * d1 + d0 * d0) * 4; if (f1 > 1.0F) f1 = 1.0F; this.walkAnimation.setSpeed(this.walkAnimation.speed() + (f1 - this.walkAnimation.speed()) * 0.4F); this.walkAnimation.position(this.walkAnimation.position() + this.walkAnimation.speed()); this.calculateEntityAnimation(true); return; } this.maxUpStep = 0.5F; super.travel(dir); } @Override public void aiStep() { super.aiStep(); this.updateSwingTime(); } public static void init() { } public static AttributeSupplier.Builder createAttributes() { AttributeSupplier.Builder builder = Mob.createMobAttributes(); builder = builder.add(Attributes.MOVEMENT_SPEED, 0.3); builder = builder.add(Attributes.MAX_HEALTH, 50); builder = builder.add(Attributes.ARMOR, 0); builder = builder.add(Attributes.ATTACK_DAMAGE, 0); builder = builder.add(Attributes.FOLLOW_RANGE, 16); return builder; } private PlayState movementPredicate(AnimationState event) { if (this.animationprocedure.equals("empty")) { if ((event.isMoving() || !(event.getLimbSwingAmount() > -0.15F && event.getLimbSwingAmount() < 0.15F)) && !this.isVehicle()) { return event.setAndContinue(RawAnimation.begin().thenLoop("move.car")); } if (this.isVehicle() && event.isMoving()) { return event.setAndContinue(RawAnimation.begin().thenLoop("move.car")); } return event.setAndContinue(RawAnimation.begin().thenLoop("idle.car")); } return PlayState.STOP; } private PlayState procedurePredicate(AnimationState event) { Entity entity = this; Level world = entity.level(); boolean loop = false; double x = entity.getX(); double y = entity.getY(); double z = entity.getZ(); if (!loop && this.lastloop) { this.lastloop = false; event.getController().setAnimation(RawAnimation.begin().thenPlay(this.animationprocedure)); event.getController().forceAnimationReset(); return PlayState.STOP; } if (!this.animationprocedure.equals("empty") && event.getController().getAnimationState() == AnimationController.State.STOPPED) { if (!loop) { event.getController().setAnimation(RawAnimation.begin().thenPlay(this.animationprocedure)); if (event.getController().getAnimationState() == AnimationController.State.STOPPED) { this.animationprocedure = "empty"; event.getController().forceAnimationReset(); } } else { event.getController().setAnimation(RawAnimation.begin().thenLoop(this.animationprocedure)); this.lastloop = true; } } return PlayState.CONTINUE; } @Override protected void tickDeath() { ++this.deathTime; if (this.deathTime == 20) { this.remove(CarEntity.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 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; } }