| 1 | package net.minecraft.world.entity.monster.spider;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Holder;
|
| 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.sounds.SoundEvent;
|
| 9 | import net.minecraft.sounds.SoundEvents;
|
| 10 | import net.minecraft.util.RandomSource;
|
| 11 | import net.minecraft.world.Difficulty;
|
| 12 | import net.minecraft.world.DifficultyInstance;
|
| 13 | import net.minecraft.world.damagesource.DamageSource;
|
| 14 | import net.minecraft.world.effect.MobEffect;
|
| 15 | import net.minecraft.world.effect.MobEffectInstance;
|
| 16 | import net.minecraft.world.effect.MobEffects;
|
| 17 | import net.minecraft.world.entity.Entity;
|
| 18 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 19 | import net.minecraft.world.entity.EntityType;
|
| 20 | import net.minecraft.world.entity.EntityTypes;
|
| 21 | import net.minecraft.world.entity.LivingEntity;
|
| 22 | import net.minecraft.world.entity.SpawnGroupData;
|
| 23 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 24 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 25 | import net.minecraft.world.entity.ai.goal.AvoidEntityGoal;
|
| 26 | import net.minecraft.world.entity.ai.goal.FloatGoal;
|
| 27 | import net.minecraft.world.entity.ai.goal.LeapAtTargetGoal;
|
| 28 | import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
|
| 29 | import net.minecraft.world.entity.ai.goal.MeleeAttackGoal;
|
| 30 | import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;
|
| 31 | import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;
|
| 32 | import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal;
|
| 33 | import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
|
| 34 | import net.minecraft.world.entity.ai.navigation.PathNavigation;
|
| 35 | import net.minecraft.world.entity.ai.navigation.WallClimberNavigation;
|
| 36 | import net.minecraft.world.entity.animal.armadillo.Armadillo;
|
| 37 | import net.minecraft.world.entity.animal.golem.IronGolem;
|
| 38 | import net.minecraft.world.entity.monster.Monster;
|
| 39 | import net.minecraft.world.entity.monster.skeleton.Skeleton;
|
| 40 | import net.minecraft.world.entity.player.Player;
|
| 41 | import net.minecraft.world.level.Level;
|
| 42 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 43 | import net.minecraft.world.level.block.Blocks;
|
| 44 | import net.minecraft.world.level.block.state.BlockState;
|
| 45 | import net.minecraft.world.phys.Vec3;
|
| 46 | import org.jspecify.annotations.Nullable;
|
| 47 |
|
| 48 | public class Spider extends Monster {
|
| 49 | private static final EntityDataAccessor<Byte> DATA_FLAGS_ID = SynchedEntityData.defineId(Spider.class, EntityDataSerializers.BYTE);
|
| 50 | private static final float SPIDER_SPECIAL_EFFECT_CHANCE = 0.1F;
|
| 51 |
|
| 52 | public Spider(final EntityType<? extends Spider> type, final Level level) {
|
| 53 | super(type, level);
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | protected void registerGoals() {
|
| 58 | this.goalSelector.addGoal(1, new FloatGoal(this));
|
| 59 | this.goalSelector.addGoal(2, new AvoidEntityGoal<>(this, Armadillo.class, 6.0F, 1.0, 1.2, entity -> !((Armadillo)entity).isScared()));
|
| 60 | this.goalSelector.addGoal(3, new LeapAtTargetGoal(this, 0.4F));
|
| 61 | this.goalSelector.addGoal(4, new Spider.SpiderAttackGoal(this));
|
| 62 | this.goalSelector.addGoal(5, new WaterAvoidingRandomStrollGoal(this, 0.8));
|
| 63 | this.goalSelector.addGoal(6, new LookAtPlayerGoal(this, Player.class, 8.0F));
|
| 64 | this.goalSelector.addGoal(6, new RandomLookAroundGoal(this));
|
| 65 | this.targetSelector.addGoal(1, new HurtByTargetGoal(this));
|
| 66 | this.targetSelector.addGoal(2, new Spider.SpiderTargetGoal<>(this, Player.class));
|
| 67 | this.targetSelector.addGoal(3, new Spider.SpiderTargetGoal<>(this, IronGolem.class));
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | protected PathNavigation createNavigation(final Level level) {
|
| 72 | return new WallClimberNavigation(this, level);
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 77 | super.defineSynchedData(entityData);
|
| 78 | entityData.define(DATA_FLAGS_ID, (byte)0);
|
| 79 | }
|
| 80 |
|
| 81 | @Override
|
| 82 | public void tick() {
|
| 83 | super.tick();
|
| 84 | if (!this.level().isClientSide()) {
|
| 85 | this.setClimbing(this.horizontalCollision);
|
| 86 | }
|
| 87 | }
|
| 88 |
|
| 89 | public static AttributeSupplier.Builder createAttributes() {
|
| 90 | return Monster.createMonsterAttributes().add(Attributes.MAX_HEALTH, 16.0).add(Attributes.MOVEMENT_SPEED, 0.3F);
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | protected SoundEvent getAmbientSound() {
|
| 95 | return SoundEvents.SPIDER_AMBIENT;
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 100 | return SoundEvents.SPIDER_HURT;
|
| 101 | }
|
| 102 |
|
| 103 | @Override
|
| 104 | protected SoundEvent getDeathSound() {
|
| 105 | return SoundEvents.SPIDER_DEATH;
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | protected void playStepSound(final BlockPos pos, final BlockState blockState) {
|
| 110 | this.playSound(SoundEvents.SPIDER_STEP, 0.15F, 1.0F);
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | public boolean onClimbable() {
|
| 115 | return this.isClimbing();
|
| 116 | }
|
| 117 |
|
| 118 | @Override
|
| 119 | public void makeStuckInBlock(final BlockState state, final Vec3 speedMultiplier) {
|
| 120 | if (!state.is(Blocks.COBWEB)) {
|
| 121 | super.makeStuckInBlock(state, speedMultiplier);
|
| 122 | }
|
| 123 | }
|
| 124 |
|
| 125 | @Override
|
| 126 | public boolean canBeAffected(final MobEffectInstance newEffect) {
|
| 127 | return newEffect.is(MobEffects.POISON) ? false : super.canBeAffected(newEffect);
|
| 128 | }
|
| 129 |
|
| 130 | public boolean isClimbing() {
|
| 131 | return (this.entityData.get(DATA_FLAGS_ID) & 1) != 0;
|
| 132 | }
|
| 133 |
|
| 134 | public void setClimbing(final boolean value) {
|
| 135 | byte flags = this.entityData.get(DATA_FLAGS_ID);
|
| 136 | if (value) {
|
| 137 | flags = (byte)(flags | 1);
|
| 138 | } else {
|
| 139 | flags = (byte)(flags & -2);
|
| 140 | }
|
| 141 |
|
| 142 | this.entityData.set(DATA_FLAGS_ID, flags);
|
| 143 | }
|
| 144 |
|
| 145 | @Override
|
| 146 | public @Nullable SpawnGroupData finalizeSpawn(
|
| 147 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, @Nullable SpawnGroupData groupData
|
| 148 | ) {
|
| 149 | groupData = super.finalizeSpawn(level, difficulty, spawnReason, groupData);
|
| 150 | RandomSource random = level.getRandom();
|
| 151 | if (random.nextInt(100) == 0) {
|
| 152 | Skeleton skeleton = EntityTypes.SKELETON.create(this.level(), EntitySpawnReason.JOCKEY);
|
| 153 | if (skeleton != null) {
|
| 154 | skeleton.snapTo(this.getX(), this.getY(), this.getZ(), this.getYRot(), 0.0F);
|
| 155 | skeleton.finalizeSpawn(level, difficulty, spawnReason, null);
|
| 156 | skeleton.startRiding(this, false, false);
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | if (groupData == null) {
|
| 161 | groupData = new Spider.SpiderEffectsGroupData();
|
| 162 | if (level.getDifficulty() == Difficulty.HARD && random.nextFloat() < 0.1F * difficulty.getSpecialMultiplier()) {
|
| 163 | ((Spider.SpiderEffectsGroupData)groupData).setRandomEffect(random);
|
| 164 | }
|
| 165 | }
|
| 166 |
|
| 167 | if (groupData instanceof Spider.SpiderEffectsGroupData spiderEffectsGroupData) {
|
| 168 | Holder<MobEffect> effect = spiderEffectsGroupData.effect;
|
| 169 | if (effect != null) {
|
| 170 | this.addEffect(new MobEffectInstance(effect, -1));
|
| 171 | }
|
| 172 | }
|
| 173 |
|
| 174 | return groupData;
|
| 175 | }
|
| 176 |
|
| 177 | @Override
|
| 178 | public Vec3 getVehicleAttachmentPoint(final Entity vehicle) {
|
| 179 | return vehicle.getBbWidth() <= this.getBbWidth() ? new Vec3(0.0, 0.3125 * this.getScale(), 0.0) : super.getVehicleAttachmentPoint(vehicle);
|
| 180 | }
|
| 181 |
|
| 182 | private static class SpiderAttackGoal extends MeleeAttackGoal {
|
| 183 | public SpiderAttackGoal(final Spider mob) {
|
| 184 | super(mob, 1.0, true);
|
| 185 | }
|
| 186 |
|
| 187 | @Override
|
| 188 | public boolean canUse() {
|
| 189 | return super.canUse() && !this.mob.isVehicle();
|
| 190 | }
|
| 191 |
|
| 192 | @Override
|
| 193 | public boolean canContinueToUse() {
|
| 194 | float br = this.mob.getLightLevelDependentMagicValue();
|
| 195 | if (br >= 0.5F && this.mob.getRandom().nextInt(100) == 0) {
|
| 196 | this.mob.setTarget(null);
|
| 197 | return false;
|
| 198 | } else {
|
| 199 | return super.canContinueToUse();
|
| 200 | }
|
| 201 | }
|
| 202 | }
|
| 203 |
|
| 204 | public static class SpiderEffectsGroupData implements SpawnGroupData {
|
| 205 | public @Nullable Holder<MobEffect> effect;
|
| 206 |
|
| 207 | public void setRandomEffect(final RandomSource random) {
|
| 208 | int selection = random.nextInt(5);
|
| 209 | if (selection <= 1) {
|
| 210 | this.effect = MobEffects.SPEED;
|
| 211 | } else if (selection <= 2) {
|
| 212 | this.effect = MobEffects.STRENGTH;
|
| 213 | } else if (selection <= 3) {
|
| 214 | this.effect = MobEffects.REGENERATION;
|
| 215 | } else if (selection <= 4) {
|
| 216 | this.effect = MobEffects.INVISIBILITY;
|
| 217 | }
|
| 218 | }
|
| 219 | }
|
| 220 |
|
| 221 | private static class SpiderTargetGoal<T extends LivingEntity> extends NearestAttackableTargetGoal<T> {
|
| 222 | public SpiderTargetGoal(final Spider mob, final Class<T> targetType) {
|
| 223 | super(mob, targetType, true);
|
| 224 | }
|
| 225 |
|
| 226 | @Override
|
| 227 | public boolean canUse() {
|
| 228 | float br = this.mob.getLightLevelDependentMagicValue();
|
| 229 | return br >= 0.5F ? false : super.canUse();
|
| 230 | }
|
| 231 | }
|
| 232 | }
|
| 233 |
|