BetterEnd/src/main/java/org/betterx/betterend/entity/DragonflyEntity.java

218 lines
7.5 KiB
Java

package org.betterx.betterend.entity;
import org.betterx.bclib.entity.DespawnableAnimal;
import org.betterx.bclib.util.BlocksHelper;
import org.betterx.bclib.util.MHelper;
import org.betterx.betterend.registry.EndEntities;
import org.betterx.betterend.registry.EndSounds;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvent;
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.control.FlyingMoveControl;
import net.minecraft.world.entity.ai.control.LookControl;
import net.minecraft.world.entity.ai.goal.BreedGoal;
import net.minecraft.world.entity.ai.goal.FloatGoal;
import net.minecraft.world.entity.ai.goal.FollowParentGoal;
import net.minecraft.world.entity.ai.goal.Goal;
import net.minecraft.world.entity.ai.navigation.FlyingPathNavigation;
import net.minecraft.world.entity.ai.navigation.PathNavigation;
import net.minecraft.world.entity.ai.util.AirAndWaterRandomPos;
import net.minecraft.world.entity.ai.util.HoverRandomPos;
import net.minecraft.world.entity.animal.FlyingAnimal;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.pathfinder.BlockPathTypes;
import net.minecraft.world.level.pathfinder.Path;
import net.minecraft.world.phys.Vec3;
import java.util.EnumSet;
public class DragonflyEntity extends DespawnableAnimal implements FlyingAnimal {
public DragonflyEntity(EntityType<DragonflyEntity> entityType, Level world) {
super(entityType, world);
this.moveControl = new FlyingMoveControl(this, 20, true);
this.lookControl = new DragonflyLookControl(this);
this.setPathfindingMalus(BlockPathTypes.WATER, -1.0F);
this.setPathfindingMalus(BlockPathTypes.DANGER_FIRE, -1.0F);
this.xpReward = 1;
}
public static AttributeSupplier.Builder createMobAttributes() {
return LivingEntity
.createLivingAttributes()
.add(Attributes.MAX_HEALTH, 8.0D)
.add(Attributes.FOLLOW_RANGE, 16.0D)
.add(Attributes.FLYING_SPEED, 1.0D)
.add(Attributes.MOVEMENT_SPEED, 0.1D);
}
@Override
public boolean canBeLeashed(Player player) {
return false;
}
@Override
protected PathNavigation createNavigation(Level world) {
FlyingPathNavigation birdNavigation = new FlyingPathNavigation(this, world) {
public boolean isStableDestination(BlockPos pos) {
BlockState state = this.level.getBlockState(pos);
return state.isAir() || !state.blocksMotion();
}
public void tick() {
super.tick();
}
};
birdNavigation.setCanOpenDoors(false);
birdNavigation.setCanFloat(false);
birdNavigation.setCanPassDoors(true);
return birdNavigation;
}
@Override
public float getWalkTargetValue(BlockPos pos, LevelReader world) {
return world.getBlockState(pos).isAir() ? 10.0F : 0.0F;
}
@Override
protected void registerGoals() {
this.goalSelector.addGoal(1, new FloatGoal(this));
this.goalSelector.addGoal(2, new BreedGoal(this, 1.0D));
this.goalSelector.addGoal(3, new FollowParentGoal(this, 1.0D));
this.goalSelector.addGoal(4, new WanderAroundGoal());
}
@Override
public boolean isPushable() {
return false;
}
@Override
public boolean causeFallDamage(float fallDistance, float damageMultiplier, DamageSource damageSource) {
return false;
}
@Override
protected Entity.MovementEmission getMovementEmission() {
return Entity.MovementEmission.EVENTS;
}
@Override
public boolean isFlying() {
return !this.onGround();
}
@Override
public boolean isNoGravity() {
return true;
}
@Override
public SoundEvent getAmbientSound() {
return EndSounds.ENTITY_DRAGONFLY.value();
}
@Override
protected float getSoundVolume() {
return MHelper.randRange(0.25F, 0.5F, random);
}
class DragonflyLookControl extends LookControl {
DragonflyLookControl(Mob entity) {
super(entity);
}
protected boolean resetXRotOnTick() {
return true;
}
}
class WanderAroundGoal extends Goal {
WanderAroundGoal() {
this.setFlags(EnumSet.of(Goal.Flag.MOVE));
}
public boolean canUse() {
return DragonflyEntity.this.navigation.isDone() && DragonflyEntity.this.random.nextInt(10) == 0;
}
public boolean canContinueToUse() {
return DragonflyEntity.this.navigation.isInProgress();
}
public void start() {
Vec3 vec3d = this.getRandomLocation();
if (vec3d != null) {
BlockPos pos = new BlockPos((int) vec3d.x, (int) vec3d.y, (int) vec3d.z);
try {
Path path = DragonflyEntity.this.navigation.createPath(pos, 1);
if (path != null) {
DragonflyEntity.this.navigation.moveTo(path, 1.0D);
}
} catch (Exception e) {
}
}
super.start();
}
private Vec3 getRandomLocation() {
int h = BlocksHelper.downRay(DragonflyEntity.this.level(), DragonflyEntity.this.blockPosition(), 16);
Vec3 rotation = DragonflyEntity.this.getViewVector(0.0F);
Vec3 airPos = HoverRandomPos.getPos(DragonflyEntity.this, 8, 7, rotation.x, rotation.z, 1.5707964F, 3, 1);
if (airPos != null) {
if (isInVoid(airPos)) {
for (int i = 0; i < 8; i++) {
airPos = HoverRandomPos.getPos(
DragonflyEntity.this,
16,
7,
rotation.x,
rotation.z,
MHelper.PI2,
3,
1
);
if (airPos != null && !isInVoid(airPos)) {
return airPos;
}
}
return null;
}
if (h > 5 && airPos.y() >= DragonflyEntity.this.blockPosition().getY()) {
airPos = new Vec3(airPos.x, airPos.y - h * 0.5, airPos.z);
}
return airPos;
}
return AirAndWaterRandomPos.getPos(
DragonflyEntity.this,
8,
4,
-2,
rotation.x,
rotation.z,
1.5707963705062866D
);
}
private boolean isInVoid(Vec3 pos) {
int h = BlocksHelper.downRay(
DragonflyEntity.this.level(),
new BlockPos((int) pos.x, (int) pos.y, (int) pos.z),
128
);
return h > 100;
}
}
@Override
public AgeableMob getBreedOffspring(ServerLevel world, AgeableMob entity) {
return EndEntities.DRAGONFLY.type().create(world);
}
}