| 1 | package net.minecraft.world.entity.monster;
|
| 2 |
|
| 3 | import java.util.EnumSet;
|
| 4 | import net.minecraft.core.particles.ParticleTypes;
|
| 5 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 6 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 7 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.sounds.SoundEvent;
|
| 10 | import net.minecraft.sounds.SoundEvents;
|
| 11 | import net.minecraft.world.damagesource.DamageSource;
|
| 12 | import net.minecraft.world.entity.EntityType;
|
| 13 | import net.minecraft.world.entity.LivingEntity;
|
| 14 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 15 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 16 | import net.minecraft.world.entity.ai.goal.Goal;
|
| 17 | import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
|
| 18 | import net.minecraft.world.entity.ai.goal.MoveTowardsRestrictionGoal;
|
| 19 | import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;
|
| 20 | import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;
|
| 21 | import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal;
|
| 22 | import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
|
| 23 | import net.minecraft.world.entity.player.Player;
|
| 24 | import net.minecraft.world.entity.projectile.hurtingprojectile.SmallFireball;
|
| 25 | import net.minecraft.world.level.Level;
|
| 26 | import net.minecraft.world.level.pathfinder.PathType;
|
| 27 | import net.minecraft.world.phys.Vec3;
|
| 28 |
|
| 29 | public class Blaze extends Monster {
|
| 30 | private float allowedHeightOffset = 0.5F;
|
| 31 | private int nextHeightOffsetChangeTick;
|
| 32 | private static final EntityDataAccessor<Byte> DATA_FLAGS_ID = SynchedEntityData.defineId(Blaze.class, EntityDataSerializers.BYTE);
|
| 33 |
|
| 34 | public Blaze(final EntityType<? extends Blaze> blaze, final Level level) {
|
| 35 | super(blaze, level);
|
| 36 | this.setPathfindingMalus(PathType.WATER, -1.0F);
|
| 37 | this.setPathfindingMalus(PathType.LAVA, 8.0F);
|
| 38 | this.setPathfindingMalus(PathType.FIRE_IN_NEIGHBOR, 0.0F);
|
| 39 | this.setPathfindingMalus(PathType.FIRE, 0.0F);
|
| 40 | this.xpReward = 10;
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | protected void registerGoals() {
|
| 45 | this.goalSelector.addGoal(4, new Blaze.BlazeAttackGoal(this));
|
| 46 | this.goalSelector.addGoal(5, new MoveTowardsRestrictionGoal(this, 1.0));
|
| 47 | this.goalSelector.addGoal(7, new WaterAvoidingRandomStrollGoal(this, 1.0, 0.0F));
|
| 48 | this.goalSelector.addGoal(8, new LookAtPlayerGoal(this, Player.class, 8.0F));
|
| 49 | this.goalSelector.addGoal(8, new RandomLookAroundGoal(this));
|
| 50 | this.targetSelector.addGoal(1, new HurtByTargetGoal(this).setAlertOthers());
|
| 51 | this.targetSelector.addGoal(2, new NearestAttackableTargetGoal<>(this, Player.class, true));
|
| 52 | }
|
| 53 |
|
| 54 | public static AttributeSupplier.Builder createAttributes() {
|
| 55 | return Monster.createMonsterAttributes().add(Attributes.ATTACK_DAMAGE, 6.0).add(Attributes.MOVEMENT_SPEED, 0.23F).add(Attributes.FOLLOW_RANGE, 48.0);
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 60 | super.defineSynchedData(entityData);
|
| 61 | entityData.define(DATA_FLAGS_ID, (byte)0);
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | protected SoundEvent getAmbientSound() {
|
| 66 | return SoundEvents.BLAZE_AMBIENT;
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 71 | return SoundEvents.BLAZE_HURT;
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | protected SoundEvent getDeathSound() {
|
| 76 | return SoundEvents.BLAZE_DEATH;
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | public float getLightLevelDependentMagicValue() {
|
| 81 | return 1.0F;
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public void aiStep() {
|
| 86 | if (!this.onGround() && this.getDeltaMovement().y < 0.0) {
|
| 87 | this.setDeltaMovement(this.getDeltaMovement().multiply(1.0, 0.6, 1.0));
|
| 88 | }
|
| 89 |
|
| 90 | if (this.level().isClientSide()) {
|
| 91 | if (this.random.nextInt(24) == 0 && !this.isSilent()) {
|
| 92 | this.level()
|
| 93 | .playLocalSound(
|
| 94 | this.getX() + 0.5,
|
| 95 | this.getY() + 0.5,
|
| 96 | this.getZ() + 0.5,
|
| 97 | SoundEvents.BLAZE_BURN,
|
| 98 | this.getSoundSource(),
|
| 99 | 1.0F + this.random.nextFloat(),
|
| 100 | this.random.nextFloat() * 0.7F + 0.3F,
|
| 101 | false
|
| 102 | );
|
| 103 | }
|
| 104 |
|
| 105 | for (int i = 0; i < 2; i++) {
|
| 106 | this.level().addParticle(ParticleTypes.LARGE_SMOKE, this.getRandomX(0.5), this.getRandomY(), this.getRandomZ(0.5), 0.0, 0.0, 0.0);
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | super.aiStep();
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | public boolean isSensitiveToWater() {
|
| 115 | return true;
|
| 116 | }
|
| 117 |
|
| 118 | @Override
|
| 119 | protected void customServerAiStep(final ServerLevel level) {
|
| 120 | this.nextHeightOffsetChangeTick--;
|
| 121 | if (this.nextHeightOffsetChangeTick <= 0) {
|
| 122 | this.nextHeightOffsetChangeTick = 100;
|
| 123 | this.allowedHeightOffset = (float)this.random.triangle(0.5, 6.891);
|
| 124 | }
|
| 125 |
|
| 126 | LivingEntity target = this.getTarget();
|
| 127 | if (target != null && target.getEyeY() > this.getEyeY() + this.allowedHeightOffset && this.canAttack(target)) {
|
| 128 | Vec3 movement = this.getDeltaMovement();
|
| 129 | this.setDeltaMovement(this.getDeltaMovement().add(0.0, (0.3F - movement.y) * 0.3F, 0.0));
|
| 130 | this.needsSync = true;
|
| 131 | }
|
| 132 |
|
| 133 | super.customServerAiStep(level);
|
| 134 | }
|
| 135 |
|
| 136 | @Override
|
| 137 | public boolean isOnFire() {
|
| 138 | return this.isCharged();
|
| 139 | }
|
| 140 |
|
| 141 | private boolean isCharged() {
|
| 142 | return (this.entityData.get(DATA_FLAGS_ID) & 1) != 0;
|
| 143 | }
|
| 144 |
|
| 145 | private void setCharged(final boolean value) {
|
| 146 | byte flags = this.entityData.get(DATA_FLAGS_ID);
|
| 147 | if (value) {
|
| 148 | flags = (byte)(flags | 1);
|
| 149 | } else {
|
| 150 | flags = (byte)(flags & -2);
|
| 151 | }
|
| 152 |
|
| 153 | this.entityData.set(DATA_FLAGS_ID, flags);
|
| 154 | }
|
| 155 |
|
| 156 | private static class BlazeAttackGoal extends Goal {
|
| 157 | private final Blaze blaze;
|
| 158 | private int attackStep;
|
| 159 | private int attackTime;
|
| 160 | private int lastSeen;
|
| 161 |
|
| 162 | public BlazeAttackGoal(final Blaze blaze) {
|
| 163 | this.blaze = blaze;
|
| 164 | this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK));
|
| 165 | }
|
| 166 |
|
| 167 | @Override
|
| 168 | public boolean canUse() {
|
| 169 | LivingEntity target = this.blaze.getTarget();
|
| 170 | return target != null && target.isAlive() && this.blaze.canAttack(target);
|
| 171 | }
|
| 172 |
|
| 173 | @Override
|
| 174 | public void start() {
|
| 175 | this.attackStep = 0;
|
| 176 | }
|
| 177 |
|
| 178 | @Override
|
| 179 | public void stop() {
|
| 180 | this.blaze.setCharged(false);
|
| 181 | this.lastSeen = 0;
|
| 182 | }
|
| 183 |
|
| 184 | @Override
|
| 185 | public boolean requiresUpdateEveryTick() {
|
| 186 | return true;
|
| 187 | }
|
| 188 |
|
| 189 | @Override
|
| 190 | public void tick() {
|
| 191 | this.attackTime--;
|
| 192 | LivingEntity target = this.blaze.getTarget();
|
| 193 | if (target != null) {
|
| 194 | boolean hasLineOfSight = this.blaze.getSensing().hasLineOfSight(target);
|
| 195 | if (hasLineOfSight) {
|
| 196 | this.lastSeen = 0;
|
| 197 | } else {
|
| 198 | this.lastSeen++;
|
| 199 | }
|
| 200 |
|
| 201 | double distance = this.blaze.distanceToSqr(target);
|
| 202 | if (distance < 4.0) {
|
| 203 | if (!hasLineOfSight) {
|
| 204 | return;
|
| 205 | }
|
| 206 |
|
| 207 | if (this.attackTime <= 0) {
|
| 208 | this.attackTime = 20;
|
| 209 | this.blaze.doHurtTarget(getServerLevel(this.blaze), target);
|
| 210 | }
|
| 211 |
|
| 212 | this.blaze.getMoveControl().setWantedPosition(target.getX(), target.getY(), target.getZ(), 1.0);
|
| 213 | } else if (distance < this.getFollowDistance() * this.getFollowDistance() && hasLineOfSight) {
|
| 214 | double xd = target.getX() - this.blaze.getX();
|
| 215 | double yd = target.getY(0.5) - this.blaze.getY(0.5);
|
| 216 | double zd = target.getZ() - this.blaze.getZ();
|
| 217 | if (this.attackTime <= 0) {
|
| 218 | this.attackStep++;
|
| 219 | if (this.attackStep == 1) {
|
| 220 | this.attackTime = 60;
|
| 221 | this.blaze.setCharged(true);
|
| 222 | } else if (this.attackStep <= 4) {
|
| 223 | this.attackTime = 6;
|
| 224 | } else {
|
| 225 | this.attackTime = 100;
|
| 226 | this.attackStep = 0;
|
| 227 | this.blaze.setCharged(false);
|
| 228 | }
|
| 229 |
|
| 230 | if (this.attackStep > 1) {
|
| 231 | double sqd = Math.sqrt(Math.sqrt(distance)) * 0.5;
|
| 232 | if (!this.blaze.isSilent()) {
|
| 233 | this.blaze.level().levelEvent(null, 1018, this.blaze.blockPosition(), 0);
|
| 234 | }
|
| 235 |
|
| 236 | for (int i = 0; i < 1; i++) {
|
| 237 | Vec3 direction = new Vec3(this.blaze.getRandom().triangle(xd, 2.297 * sqd), yd, this.blaze.getRandom().triangle(zd, 2.297 * sqd));
|
| 238 | SmallFireball entity = new SmallFireball(this.blaze.level(), this.blaze, direction.normalize());
|
| 239 | entity.setPos(entity.getX(), this.blaze.getY(0.5) + 0.5, entity.getZ());
|
| 240 | this.blaze.level().addFreshEntity(entity);
|
| 241 | }
|
| 242 | }
|
| 243 | }
|
| 244 |
|
| 245 | this.blaze.getLookControl().setLookAt(target, 10.0F, 10.0F);
|
| 246 | } else if (this.lastSeen < 5) {
|
| 247 | this.blaze.getMoveControl().setWantedPosition(target.getX(), target.getY(), target.getZ(), 1.0);
|
| 248 | }
|
| 249 |
|
| 250 | super.tick();
|
| 251 | }
|
| 252 | }
|
| 253 |
|
| 254 | private double getFollowDistance() {
|
| 255 | return this.blaze.getAttributeValue(Attributes.FOLLOW_RANGE);
|
| 256 | }
|
| 257 | }
|
| 258 | }
|
| 259 |
|