| 1 | package net.minecraft.world.entity.animal.equine;
|
| 2 |
|
| 3 | import java.util.function.DoubleSupplier;
|
| 4 | import net.minecraft.server.level.ServerLevel;
|
| 5 | import net.minecraft.sounds.SoundEvent;
|
| 6 | import net.minecraft.sounds.SoundEvents;
|
| 7 | import net.minecraft.tags.ItemTags;
|
| 8 | import net.minecraft.util.RandomSource;
|
| 9 | import net.minecraft.world.DifficultyInstance;
|
| 10 | import net.minecraft.world.InteractionHand;
|
| 11 | import net.minecraft.world.InteractionResult;
|
| 12 | import net.minecraft.world.damagesource.DamageSource;
|
| 13 | import net.minecraft.world.entity.AgeableMob;
|
| 14 | import net.minecraft.world.entity.EntityAttachment;
|
| 15 | import net.minecraft.world.entity.EntityAttachments;
|
| 16 | import net.minecraft.world.entity.EntityDimensions;
|
| 17 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 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.Leashable;
|
| 22 | import net.minecraft.world.entity.Mob;
|
| 23 | import net.minecraft.world.entity.Pose;
|
| 24 | import net.minecraft.world.entity.SpawnGroupData;
|
| 25 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 26 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 27 | import net.minecraft.world.entity.ai.goal.FloatGoal;
|
| 28 | import net.minecraft.world.entity.ai.goal.TemptGoal;
|
| 29 | import net.minecraft.world.entity.monster.zombie.Zombie;
|
| 30 | import net.minecraft.world.entity.player.Player;
|
| 31 | import net.minecraft.world.item.ItemStack;
|
| 32 | import net.minecraft.world.item.Items;
|
| 33 | import net.minecraft.world.level.Level;
|
| 34 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 35 | import net.minecraft.world.phys.Vec3;
|
| 36 | import org.jspecify.annotations.Nullable;
|
| 37 |
|
| 38 | public class ZombieHorse extends AbstractHorse {
|
| 39 | private static final float SPEED_FACTOR = 42.16F;
|
| 40 | private static final double BASE_JUMP_STRENGTH = 0.5;
|
| 41 | private static final double PER_RANDOM_JUMP_STRENGTH = 0.06666666666666667;
|
| 42 | private static final double BASE_SPEED = 9.0;
|
| 43 | private static final double PER_RANDOM_SPEED = 1.0;
|
| 44 | private static final EntityDimensions BABY_DIMENSIONS = EntityTypes.ZOMBIE_HORSE
|
| 45 | .getDimensions()
|
| 46 | .withAttachments(EntityAttachments.builder().attach(EntityAttachment.PASSENGER, 0.0F, EntityTypes.ZOMBIE_HORSE.getHeight() - 0.25F, 0.0F))
|
| 47 | .scale(0.7F);
|
| 48 |
|
| 49 | public ZombieHorse(final EntityType<? extends ZombieHorse> type, final Level level) {
|
| 50 | super(type, level);
|
| 51 | }
|
| 52 |
|
| 53 | public static AttributeSupplier.Builder createAttributes() {
|
| 54 | return createBaseHorseAttributes().add(Attributes.MAX_HEALTH, 25.0);
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public InteractionResult interact(final Player player, final InteractionHand hand, final Vec3 location) {
|
| 59 | this.setPersistenceRequired();
|
| 60 | return super.interact(player, hand, location);
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | public boolean removeWhenFarAway(final double distSqr) {
|
| 65 | return true;
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public boolean isMobControlled() {
|
| 70 | return this.getFirstPassenger() instanceof Mob;
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | protected void randomizeAttributes(final RandomSource random) {
|
| 75 | this.getAttribute(Attributes.JUMP_STRENGTH).setBaseValue(generateZombieHorseJumpStrength(random::nextDouble));
|
| 76 | this.getAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(generateZombieHorseSpeed(random::nextDouble));
|
| 77 | }
|
| 78 |
|
| 79 | private static double generateZombieHorseJumpStrength(final DoubleSupplier probabilityProvider) {
|
| 80 | return 0.5
|
| 81 | + probabilityProvider.getAsDouble() * 0.06666666666666667
|
| 82 | + probabilityProvider.getAsDouble() * 0.06666666666666667
|
| 83 | + probabilityProvider.getAsDouble() * 0.06666666666666667;
|
| 84 | }
|
| 85 |
|
| 86 | private static double generateZombieHorseSpeed(final DoubleSupplier probabilityProvider) {
|
| 87 | return (9.0 + probabilityProvider.getAsDouble() * 1.0 + probabilityProvider.getAsDouble() * 1.0 + probabilityProvider.getAsDouble() * 1.0) / 42.16F;
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | protected SoundEvent getAmbientSound() {
|
| 92 | return SoundEvents.ZOMBIE_HORSE_AMBIENT;
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | protected SoundEvent getDeathSound() {
|
| 97 | return SoundEvents.ZOMBIE_HORSE_DEATH;
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 102 | return SoundEvents.ZOMBIE_HORSE_HURT;
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | protected SoundEvent getAngrySound() {
|
| 107 | return SoundEvents.ZOMBIE_HORSE_ANGRY;
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | protected SoundEvent getEatingSound() {
|
| 112 | return SoundEvents.ZOMBIE_HORSE_EAT;
|
| 113 | }
|
| 114 |
|
| 115 | @Override
|
| 116 | public @Nullable AgeableMob getBreedOffspring(final ServerLevel level, final AgeableMob partner) {
|
| 117 | return EntityTypes.ZOMBIE_HORSE.create(level, EntitySpawnReason.BREEDING);
|
| 118 | }
|
| 119 |
|
| 120 | @Override
|
| 121 | public boolean canFallInLove() {
|
| 122 | return false;
|
| 123 | }
|
| 124 |
|
| 125 | @Override
|
| 126 | protected void addBehaviourGoals() {
|
| 127 | this.goalSelector.addGoal(0, new FloatGoal(this));
|
| 128 | this.goalSelector.addGoal(3, new TemptGoal(this, 1.25, i -> i.is(ItemTags.ZOMBIE_HORSE_FOOD), false));
|
| 129 | }
|
| 130 |
|
| 131 | @Override
|
| 132 | public @Nullable SpawnGroupData finalizeSpawn(
|
| 133 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, final @Nullable SpawnGroupData groupData
|
| 134 | ) {
|
| 135 | if (spawnReason == EntitySpawnReason.NATURAL) {
|
| 136 | Zombie zombie = EntityTypes.ZOMBIE.create(this.level(), EntitySpawnReason.JOCKEY);
|
| 137 | if (zombie != null) {
|
| 138 | zombie.snapTo(this.getX(), this.getY(), this.getZ(), this.getYRot(), 0.0F);
|
| 139 | zombie.finalizeSpawn(level, difficulty, spawnReason, null);
|
| 140 | zombie.setItemSlot(EquipmentSlot.MAINHAND, new ItemStack(Items.IRON_SPEAR));
|
| 141 | zombie.startRiding(this, false, false);
|
| 142 | }
|
| 143 | }
|
| 144 |
|
| 145 | return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
|
| 146 | }
|
| 147 |
|
| 148 | @Override
|
| 149 | public InteractionResult mobInteract(final Player player, final InteractionHand hand) {
|
| 150 | boolean shouldOpenInventory = !this.isBaby() && this.isTamed() && player.isSecondaryUseActive();
|
| 151 | if (!this.isVehicle() && !shouldOpenInventory) {
|
| 152 | ItemStack itemStack = player.getItemInHand(hand);
|
| 153 | if (!itemStack.isEmpty()) {
|
| 154 | if (this.isFood(itemStack)) {
|
| 155 | return this.fedFood(player, itemStack);
|
| 156 | }
|
| 157 |
|
| 158 | if (!this.isTamed()) {
|
| 159 | this.makeMad();
|
| 160 | return InteractionResult.SUCCESS;
|
| 161 | }
|
| 162 | }
|
| 163 |
|
| 164 | return super.mobInteract(player, hand);
|
| 165 | } else {
|
| 166 | return super.mobInteract(player, hand);
|
| 167 | }
|
| 168 | }
|
| 169 |
|
| 170 | @Override
|
| 171 | public boolean canUseSlot(final EquipmentSlot slot) {
|
| 172 | return true;
|
| 173 | }
|
| 174 |
|
| 175 | @Override
|
| 176 | public boolean canBeLeashed() {
|
| 177 | return this.isTamed() || !this.isMobControlled();
|
| 178 | }
|
| 179 |
|
| 180 | @Override
|
| 181 | public boolean isFood(final ItemStack itemStack) {
|
| 182 | return itemStack.is(ItemTags.ZOMBIE_HORSE_FOOD);
|
| 183 | }
|
| 184 |
|
| 185 | @Override
|
| 186 | protected EquipmentSlot sunProtectionSlot() {
|
| 187 | return EquipmentSlot.BODY;
|
| 188 | }
|
| 189 |
|
| 190 | @Override
|
| 191 | public Vec3[] getQuadLeashOffsets() {
|
| 192 | return Leashable.createQuadLeashOffsets(this, 0.04, 0.41, 0.18, 0.73);
|
| 193 | }
|
| 194 |
|
| 195 | @Override
|
| 196 | public EntityDimensions getDefaultDimensions(final Pose pose) {
|
| 197 | return this.isBaby() ? BABY_DIMENSIONS : super.getDefaultDimensions(pose);
|
| 198 | }
|
| 199 |
|
| 200 | @Override
|
| 201 | public float chargeSpeedModifier() {
|
| 202 | return 1.4F;
|
| 203 | }
|
| 204 |
|
| 205 | @Override
|
| 206 | public boolean canAgeUp() {
|
| 207 | return false;
|
| 208 | }
|
| 209 | }
|
| 210 |
|