| 1 | package net.minecraft.world.entity.monster;
|
| 2 |
|
| 3 | import net.minecraft.core.Holder;
|
| 4 | import net.minecraft.core.component.DataComponents;
|
| 5 | import net.minecraft.core.particles.ParticleTypes;
|
| 6 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 7 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 8 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 9 | import net.minecraft.resources.Identifier;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.sounds.SoundEvent;
|
| 12 | import net.minecraft.sounds.SoundEvents;
|
| 13 | import net.minecraft.tags.DamageTypeTags;
|
| 14 | import net.minecraft.tags.FluidTags;
|
| 15 | import net.minecraft.world.damagesource.DamageSource;
|
| 16 | import net.minecraft.world.effect.MobEffects;
|
| 17 | import net.minecraft.world.entity.EntityEvent;
|
| 18 | import net.minecraft.world.entity.EntityType;
|
| 19 | import net.minecraft.world.entity.EntityTypes;
|
| 20 | import net.minecraft.world.entity.EquipmentSlot;
|
| 21 | import net.minecraft.world.entity.LivingEntity;
|
| 22 | import net.minecraft.world.entity.ai.attributes.AttributeInstance;
|
| 23 | import net.minecraft.world.entity.ai.attributes.AttributeModifier;
|
| 24 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 25 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 26 | import net.minecraft.world.entity.ai.goal.FloatGoal;
|
| 27 | import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
|
| 28 | import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;
|
| 29 | import net.minecraft.world.entity.ai.goal.RangedAttackGoal;
|
| 30 | import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;
|
| 31 | import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal;
|
| 32 | import net.minecraft.world.entity.ai.goal.target.NearestAttackableWitchTargetGoal;
|
| 33 | import net.minecraft.world.entity.ai.goal.target.NearestHealableRaiderTargetGoal;
|
| 34 | import net.minecraft.world.entity.player.Player;
|
| 35 | import net.minecraft.world.entity.projectile.Projectile;
|
| 36 | import net.minecraft.world.entity.projectile.throwableitemprojectile.ThrownSplashPotion;
|
| 37 | import net.minecraft.world.entity.raid.Raider;
|
| 38 | import net.minecraft.world.item.ItemStack;
|
| 39 | import net.minecraft.world.item.Items;
|
| 40 | import net.minecraft.world.item.alchemy.Potion;
|
| 41 | import net.minecraft.world.item.alchemy.PotionContents;
|
| 42 | import net.minecraft.world.item.alchemy.Potions;
|
| 43 | import net.minecraft.world.level.Level;
|
| 44 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 45 | import net.minecraft.world.phys.Vec3;
|
| 46 |
|
| 47 | public class Witch extends Raider implements RangedAttackMob {
|
| 48 | private static final Identifier SPEED_MODIFIER_DRINKING_ID = Identifier.withDefaultNamespace("drinking");
|
| 49 | private static final AttributeModifier SPEED_MODIFIER_DRINKING = new AttributeModifier(
|
| 50 | SPEED_MODIFIER_DRINKING_ID, -0.25, AttributeModifier.Operation.ADD_VALUE
|
| 51 | );
|
| 52 | private static final EntityDataAccessor<Boolean> DATA_USING_ITEM = SynchedEntityData.defineId(Witch.class, EntityDataSerializers.BOOLEAN);
|
| 53 | private int usingTime;
|
| 54 | private NearestHealableRaiderTargetGoal<Raider> healRaidersGoal;
|
| 55 | private NearestAttackableWitchTargetGoal<Player> attackPlayersGoal;
|
| 56 |
|
| 57 | public Witch(final EntityType<? extends Witch> type, final Level level) {
|
| 58 | super(type, level);
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | protected void registerGoals() {
|
| 63 | super.registerGoals();
|
| 64 | this.healRaidersGoal = new NearestHealableRaiderTargetGoal<>(
|
| 65 | this, Raider.class, true, (target, level) -> this.hasActiveRaid() && !target.is(EntityTypes.WITCH)
|
| 66 | );
|
| 67 | this.attackPlayersGoal = new NearestAttackableWitchTargetGoal<>(this, Player.class, 10, true, false, null);
|
| 68 | this.goalSelector.addGoal(1, new FloatGoal(this));
|
| 69 | this.goalSelector.addGoal(2, new RangedAttackGoal(this, 1.0, 60, 10.0F));
|
| 70 | this.goalSelector.addGoal(2, new WaterAvoidingRandomStrollGoal(this, 1.0));
|
| 71 | this.goalSelector.addGoal(3, new LookAtPlayerGoal(this, Player.class, 8.0F));
|
| 72 | this.goalSelector.addGoal(3, new RandomLookAroundGoal(this));
|
| 73 | this.targetSelector.addGoal(1, new HurtByTargetGoal(this, Raider.class));
|
| 74 | this.targetSelector.addGoal(2, this.healRaidersGoal);
|
| 75 | this.targetSelector.addGoal(3, this.attackPlayersGoal);
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 80 | super.defineSynchedData(entityData);
|
| 81 | entityData.define(DATA_USING_ITEM, false);
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | protected SoundEvent getAmbientSound() {
|
| 86 | return SoundEvents.WITCH_AMBIENT;
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 91 | return SoundEvents.WITCH_HURT;
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | protected SoundEvent getDeathSound() {
|
| 96 | return SoundEvents.WITCH_DEATH;
|
| 97 | }
|
| 98 |
|
| 99 | public void setUsingItem(final boolean using) {
|
| 100 | this.getEntityData().set(DATA_USING_ITEM, using);
|
| 101 | }
|
| 102 |
|
| 103 | public boolean isDrinkingPotion() {
|
| 104 | return this.getEntityData().get(DATA_USING_ITEM);
|
| 105 | }
|
| 106 |
|
| 107 | public static AttributeSupplier.Builder createAttributes() {
|
| 108 | return Monster.createMonsterAttributes().add(Attributes.MAX_HEALTH, 26.0).add(Attributes.MOVEMENT_SPEED, 0.25);
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public void aiStep() {
|
| 113 | if (!this.level().isClientSide() && this.isAlive()) {
|
| 114 | this.healRaidersGoal.decrementCooldown();
|
| 115 | if (this.healRaidersGoal.getCooldown() <= 0) {
|
| 116 | this.attackPlayersGoal.setCanAttack(true);
|
| 117 | } else {
|
| 118 | this.attackPlayersGoal.setCanAttack(false);
|
| 119 | }
|
| 120 |
|
| 121 | if (this.isDrinkingPotion()) {
|
| 122 | if (this.usingTime-- <= 0) {
|
| 123 | this.setUsingItem(false);
|
| 124 | ItemStack itemStack = this.getMainHandItem();
|
| 125 | this.setItemSlot(EquipmentSlot.MAINHAND, ItemStack.EMPTY);
|
| 126 | PotionContents potion = itemStack.get(DataComponents.POTION_CONTENTS);
|
| 127 | if (itemStack.is(Items.POTION) && potion != null) {
|
| 128 | potion.forEachEffect(this::addEffect, itemStack.getOrDefault(DataComponents.POTION_DURATION_SCALE, 1.0F));
|
| 129 | }
|
| 130 |
|
| 131 | this.gameEvent(GameEvent.DRINK);
|
| 132 | this.getAttribute(Attributes.MOVEMENT_SPEED).removeModifier(SPEED_MODIFIER_DRINKING.id());
|
| 133 | }
|
| 134 | } else {
|
| 135 | Holder<Potion> potion = null;
|
| 136 | if (this.random.nextFloat() < 0.15F && this.isEyeInFluid(FluidTags.WATER) && !this.hasEffect(MobEffects.WATER_BREATHING)) {
|
| 137 | potion = Potions.WATER_BREATHING;
|
| 138 | } else if (this.random.nextFloat() < 0.15F
|
| 139 | && (this.isOnFire() || this.getLastDamageSource() != null && this.getLastDamageSource().is(DamageTypeTags.IS_FIRE))
|
| 140 | && !this.hasEffect(MobEffects.FIRE_RESISTANCE)) {
|
| 141 | potion = Potions.FIRE_RESISTANCE;
|
| 142 | } else if (this.random.nextFloat() < 0.05F && this.getHealth() < this.getMaxHealth()) {
|
| 143 | potion = Potions.HEALING;
|
| 144 | } else if (this.random.nextFloat() < 0.5F && this.getTarget() != null && !this.hasEffect(MobEffects.SPEED) && this.getTarget().distanceToSqr(this) > 121.0) {
|
| 145 | potion = Potions.SWIFTNESS;
|
| 146 | }
|
| 147 |
|
| 148 | if (potion != null) {
|
| 149 | this.setItemSlot(EquipmentSlot.MAINHAND, PotionContents.createItemStack(Items.POTION, potion));
|
| 150 | this.usingTime = this.getMainHandItem().getUseDuration(this);
|
| 151 | this.setUsingItem(true);
|
| 152 | if (!this.isSilent()) {
|
| 153 | this.level()
|
| 154 | .playSound(null, this.getX(), this.getY(), this.getZ(), SoundEvents.WITCH_DRINK, this.getSoundSource(), 1.0F, 0.8F + this.random.nextFloat() * 0.4F);
|
| 155 | }
|
| 156 |
|
| 157 | AttributeInstance speed = this.getAttribute(Attributes.MOVEMENT_SPEED);
|
| 158 | speed.removeModifier(SPEED_MODIFIER_DRINKING_ID);
|
| 159 | speed.addTransientModifier(SPEED_MODIFIER_DRINKING);
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | if (this.random.nextFloat() < 7.5E-4F) {
|
| 164 | this.level().broadcastEntityEvent(this, (byte)15);
|
| 165 | }
|
| 166 | }
|
| 167 |
|
| 168 | super.aiStep();
|
| 169 | }
|
| 170 |
|
| 171 | @Override
|
| 172 | public SoundEvent getCelebrateSound() {
|
| 173 | return SoundEvents.WITCH_CELEBRATE;
|
| 174 | }
|
| 175 |
|
| 176 | @Override
|
| 177 | public void handleEntityEvent(final @EntityEvent.Value byte id) {
|
| 178 | if (id == 15) {
|
| 179 | for (int i = 0; i < this.random.nextInt(35) + 10; i++) {
|
| 180 | this.level()
|
| 181 | .addParticle(
|
| 182 | ParticleTypes.WITCH,
|
| 183 | this.getX() + this.random.nextGaussian() * 0.13F,
|
| 184 | this.getBoundingBox().maxY + 0.5 + this.random.nextGaussian() * 0.13F,
|
| 185 | this.getZ() + this.random.nextGaussian() * 0.13F,
|
| 186 | 0.0,
|
| 187 | 0.0,
|
| 188 | 0.0
|
| 189 | );
|
| 190 | }
|
| 191 | } else {
|
| 192 | super.handleEntityEvent(id);
|
| 193 | }
|
| 194 | }
|
| 195 |
|
| 196 | @Override
|
| 197 | protected float getDamageAfterMagicAbsorb(final DamageSource damageSource, float damage) {
|
| 198 | damage = super.getDamageAfterMagicAbsorb(damageSource, damage);
|
| 199 | if (damageSource.getEntity() == this) {
|
| 200 | damage = 0.0F;
|
| 201 | }
|
| 202 |
|
| 203 | if (damageSource.is(DamageTypeTags.WITCH_RESISTANT_TO)) {
|
| 204 | damage *= 0.15F;
|
| 205 | }
|
| 206 |
|
| 207 | return damage;
|
| 208 | }
|
| 209 |
|
| 210 | @Override
|
| 211 | public void performRangedAttack(final LivingEntity target, final float power) {
|
| 212 | if (!this.isDrinkingPotion()) {
|
| 213 | Vec3 targetMovement = target.getDeltaMovement();
|
| 214 | double xd = target.getX() + targetMovement.x - this.getX();
|
| 215 | double yd = target.getEyeY() - 1.1F - this.getY();
|
| 216 | double zd = target.getZ() + targetMovement.z - this.getZ();
|
| 217 | double dist = Math.sqrt(xd * xd + zd * zd);
|
| 218 | Holder<Potion> potion = Potions.HARMING;
|
| 219 | if (target instanceof Raider) {
|
| 220 | if (target.getHealth() <= 4.0F) {
|
| 221 | potion = Potions.HEALING;
|
| 222 | } else {
|
| 223 | potion = Potions.REGENERATION;
|
| 224 | }
|
| 225 |
|
| 226 | this.setTarget(null);
|
| 227 | } else if (dist >= 8.0 && !target.hasEffect(MobEffects.SLOWNESS)) {
|
| 228 | potion = Potions.SLOWNESS;
|
| 229 | } else if (target.getHealth() >= 8.0F && !target.hasEffect(MobEffects.POISON)) {
|
| 230 | potion = Potions.POISON;
|
| 231 | } else if (dist <= 3.0 && !target.hasEffect(MobEffects.WEAKNESS) && this.random.nextFloat() < 0.25F) {
|
| 232 | potion = Potions.WEAKNESS;
|
| 233 | }
|
| 234 |
|
| 235 | if (this.level() instanceof ServerLevel serverLevel) {
|
| 236 | ItemStack itemStack = PotionContents.createItemStack(Items.SPLASH_POTION, potion);
|
| 237 | Projectile.spawnProjectileUsingShoot(
|
| 238 | ThrownSplashPotion::new, serverLevel, itemStack, this, xd, yd + dist * 0.2, zd, dist <= 2.0 ? 0.45F : 0.75F, this.rangedAttackUncertainty(serverLevel)
|
| 239 | );
|
| 240 | }
|
| 241 |
|
| 242 | if (!this.isSilent()) {
|
| 243 | this.level()
|
| 244 | .playSound(null, this.getX(), this.getY(), this.getZ(), SoundEvents.WITCH_THROW, this.getSoundSource(), 1.0F, 0.8F + this.random.nextFloat() * 0.4F);
|
| 245 | }
|
| 246 | }
|
| 247 | }
|
| 248 |
|
| 249 | @Override
|
| 250 | public float rangedAttackUncertainty(final Level level) {
|
| 251 | return 8.0F;
|
| 252 | }
|
| 253 |
|
| 254 | @Override
|
| 255 | public void applyRaidBuffs(final ServerLevel level, final int wave, final boolean isCaptain) {
|
| 256 | }
|
| 257 |
|
| 258 | @Override
|
| 259 | public boolean canBeLeader() {
|
| 260 | return false;
|
| 261 | }
|
| 262 | }
|
| 263 |
|