| 1 | package net.minecraft.world.entity.animal.equine;
|
| 2 |
|
| 3 | import net.minecraft.core.component.DataComponentGetter;
|
| 4 | import net.minecraft.core.component.DataComponentType;
|
| 5 | import net.minecraft.core.component.DataComponents;
|
| 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.server.level.ServerLevel;
|
| 10 | import net.minecraft.sounds.SoundEvent;
|
| 11 | import net.minecraft.sounds.SoundEvents;
|
| 12 | import net.minecraft.util.RandomSource;
|
| 13 | import net.minecraft.util.Util;
|
| 14 | import net.minecraft.world.DifficultyInstance;
|
| 15 | import net.minecraft.world.InteractionHand;
|
| 16 | import net.minecraft.world.InteractionResult;
|
| 17 | import net.minecraft.world.damagesource.DamageSource;
|
| 18 | import net.minecraft.world.entity.AgeableMob;
|
| 19 | import net.minecraft.world.entity.EntityAttachment;
|
| 20 | import net.minecraft.world.entity.EntityAttachments;
|
| 21 | import net.minecraft.world.entity.EntityDimensions;
|
| 22 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 23 | import net.minecraft.world.entity.EntityType;
|
| 24 | import net.minecraft.world.entity.EntityTypes;
|
| 25 | import net.minecraft.world.entity.EquipmentSlot;
|
| 26 | import net.minecraft.world.entity.Pose;
|
| 27 | import net.minecraft.world.entity.SpawnGroupData;
|
| 28 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 29 | import net.minecraft.world.entity.animal.Animal;
|
| 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.level.block.SoundType;
|
| 36 | import net.minecraft.world.level.storage.ValueInput;
|
| 37 | import net.minecraft.world.level.storage.ValueOutput;
|
| 38 | import org.jspecify.annotations.Nullable;
|
| 39 |
|
| 40 | public class Horse extends AbstractHorse {
|
| 41 | private static final EntityDataAccessor<Integer> DATA_ID_TYPE_VARIANT = SynchedEntityData.defineId(Horse.class, EntityDataSerializers.INT);
|
| 42 | private static final EntityDimensions BABY_DIMENSIONS = EntityTypes.HORSE
|
| 43 | .getDimensions()
|
| 44 | .withAttachments(EntityAttachments.builder().attach(EntityAttachment.PASSENGER, 0.0F, EntityTypes.HORSE.getHeight() - 0.125F, 0.0F))
|
| 45 | .scale(0.7F);
|
| 46 | private static final int DEFAULT_VARIANT = 0;
|
| 47 |
|
| 48 | public Horse(final EntityType<? extends Horse> type, final Level level) {
|
| 49 | super(type, level);
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | protected void randomizeAttributes(final RandomSource random) {
|
| 54 | this.getAttribute(Attributes.MAX_HEALTH).setBaseValue(generateMaxHealth(random::nextInt));
|
| 55 | this.getAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(generateSpeed(random::nextDouble));
|
| 56 | this.getAttribute(Attributes.JUMP_STRENGTH).setBaseValue(generateJumpStrength(random::nextDouble));
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 61 | super.defineSynchedData(entityData);
|
| 62 | entityData.define(DATA_ID_TYPE_VARIANT, 0);
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 67 | super.addAdditionalSaveData(output);
|
| 68 | output.putInt("Variant", this.getTypeVariant());
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 73 | super.readAdditionalSaveData(input);
|
| 74 | this.setTypeVariant(input.getIntOr("Variant", 0));
|
| 75 | }
|
| 76 |
|
| 77 | private void setTypeVariant(final int i) {
|
| 78 | this.entityData.set(DATA_ID_TYPE_VARIANT, i);
|
| 79 | }
|
| 80 |
|
| 81 | private int getTypeVariant() {
|
| 82 | return this.entityData.get(DATA_ID_TYPE_VARIANT);
|
| 83 | }
|
| 84 |
|
| 85 | private void setVariantAndMarkings(final Variant variant, final Markings markings) {
|
| 86 | this.setTypeVariant(variant.getId() & 0xFF | markings.getId() << 8 & 0xFF00);
|
| 87 | }
|
| 88 |
|
| 89 | public Variant getVariant() {
|
| 90 | return Variant.byId(this.getTypeVariant() & 0xFF);
|
| 91 | }
|
| 92 |
|
| 93 | private void setVariant(final Variant variant) {
|
| 94 | this.setTypeVariant(variant.getId() & 0xFF | this.getTypeVariant() & -256);
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | public <T> @Nullable T get(final DataComponentType<? extends T> type) {
|
| 99 | return type == DataComponents.HORSE_VARIANT ? castComponentValue((DataComponentType<T>)type, this.getVariant()) : super.get(type);
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | protected void applyImplicitComponents(final DataComponentGetter components) {
|
| 104 | this.applyImplicitComponentIfPresent(components, DataComponents.HORSE_VARIANT);
|
| 105 | super.applyImplicitComponents(components);
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | protected <T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value) {
|
| 110 | if (type == DataComponents.HORSE_VARIANT) {
|
| 111 | this.setVariant(castComponentValue(DataComponents.HORSE_VARIANT, value));
|
| 112 | return true;
|
| 113 | } else {
|
| 114 | return super.applyImplicitComponent(type, value);
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | public Markings getMarkings() {
|
| 119 | return Markings.byId((this.getTypeVariant() & 0xFF00) >> 8);
|
| 120 | }
|
| 121 |
|
| 122 | @Override
|
| 123 | protected void playGallopSound(final SoundType soundType) {
|
| 124 | super.playGallopSound(soundType);
|
| 125 | if (this.random.nextInt(10) == 0) {
|
| 126 | this.playSound(this.isBaby() ? SoundEvents.HORSE_BREATHE_BABY : SoundEvents.HORSE_BREATHE, soundType.getVolume() * 0.6F, soundType.getPitch());
|
| 127 | }
|
| 128 | }
|
| 129 |
|
| 130 | @Override
|
| 131 | protected SoundEvent getAmbientSound() {
|
| 132 | return this.isBaby() ? SoundEvents.HORSE_AMBIENT_BABY : SoundEvents.HORSE_AMBIENT;
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | protected SoundEvent getDeathSound() {
|
| 137 | return this.isBaby() ? SoundEvents.HORSE_DEATH_BABY : SoundEvents.HORSE_DEATH;
|
| 138 | }
|
| 139 |
|
| 140 | @Override
|
| 141 | protected SoundEvent getEatingSound() {
|
| 142 | return this.isBaby() ? SoundEvents.HORSE_EAT_BABY : SoundEvents.HORSE_EAT;
|
| 143 | }
|
| 144 |
|
| 145 | @Override
|
| 146 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 147 | return this.isBaby() ? SoundEvents.HORSE_HURT_BABY : SoundEvents.HORSE_HURT;
|
| 148 | }
|
| 149 |
|
| 150 | @Override
|
| 151 | protected SoundEvent getAngrySound() {
|
| 152 | return this.isBaby() ? SoundEvents.HORSE_ANGRY_BABY : SoundEvents.HORSE_ANGRY;
|
| 153 | }
|
| 154 |
|
| 155 | @Override
|
| 156 | public InteractionResult mobInteract(final Player player, final InteractionHand hand) {
|
| 157 | boolean shouldOpenInventory = !this.isBaby() && this.isTamed() && player.isSecondaryUseActive();
|
| 158 | if (!this.isVehicle() && !shouldOpenInventory && (!this.isBaby() || !player.isHolding(Items.GOLDEN_DANDELION))) {
|
| 159 | ItemStack itemStack = player.getItemInHand(hand);
|
| 160 | if (!itemStack.isEmpty()) {
|
| 161 | if (this.isFood(itemStack)) {
|
| 162 | return this.fedFood(player, itemStack);
|
| 163 | }
|
| 164 |
|
| 165 | if (!this.isTamed()) {
|
| 166 | this.makeMad();
|
| 167 | return InteractionResult.SUCCESS;
|
| 168 | }
|
| 169 | }
|
| 170 |
|
| 171 | return super.mobInteract(player, hand);
|
| 172 | } else {
|
| 173 | return super.mobInteract(player, hand);
|
| 174 | }
|
| 175 | }
|
| 176 |
|
| 177 | @Override
|
| 178 | public boolean canMate(final Animal partner) {
|
| 179 | if (partner == this) {
|
| 180 | return false;
|
| 181 | } else {
|
| 182 | return !(partner instanceof Donkey) && !(partner instanceof Horse) ? false : this.canParent() && ((AbstractHorse)partner).canParent();
|
| 183 | }
|
| 184 | }
|
| 185 |
|
| 186 | @Override
|
| 187 | public @Nullable AgeableMob getBreedOffspring(final ServerLevel level, final AgeableMob partner) {
|
| 188 | if (partner instanceof Donkey) {
|
| 189 | Mule baby = EntityTypes.MULE.create(level, EntitySpawnReason.BREEDING);
|
| 190 | if (baby != null) {
|
| 191 | this.setOffspringAttributes(partner, baby);
|
| 192 | }
|
| 193 |
|
| 194 | return baby;
|
| 195 | } else {
|
| 196 | Horse horsePartner = (Horse)partner;
|
| 197 | Horse baby = EntityTypes.HORSE.create(level, EntitySpawnReason.BREEDING);
|
| 198 | if (baby != null) {
|
| 199 | int selectSkin = this.random.nextInt(9);
|
| 200 | Variant variant;
|
| 201 | if (selectSkin < 4) {
|
| 202 | variant = this.getVariant();
|
| 203 | } else if (selectSkin < 8) {
|
| 204 | variant = horsePartner.getVariant();
|
| 205 | } else {
|
| 206 | variant = Util.getRandom(Variant.values(), this.random);
|
| 207 | }
|
| 208 |
|
| 209 | int selectMarking = this.random.nextInt(5);
|
| 210 | Markings markings;
|
| 211 | if (selectMarking < 2) {
|
| 212 | markings = this.getMarkings();
|
| 213 | } else if (selectMarking < 4) {
|
| 214 | markings = horsePartner.getMarkings();
|
| 215 | } else {
|
| 216 | markings = Util.getRandom(Markings.values(), this.random);
|
| 217 | }
|
| 218 |
|
| 219 | baby.setVariantAndMarkings(variant, markings);
|
| 220 | this.setOffspringAttributes(partner, baby);
|
| 221 | }
|
| 222 |
|
| 223 | return baby;
|
| 224 | }
|
| 225 | }
|
| 226 |
|
| 227 | @Override
|
| 228 | public boolean canUseSlot(final EquipmentSlot slot) {
|
| 229 | return true;
|
| 230 | }
|
| 231 |
|
| 232 | @Override
|
| 233 | protected void hurtArmor(final DamageSource damageSource, final float damage) {
|
| 234 | this.doHurtEquipment(damageSource, damage, EquipmentSlot.BODY);
|
| 235 | }
|
| 236 |
|
| 237 | @Override
|
| 238 | public @Nullable SpawnGroupData finalizeSpawn(
|
| 239 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, @Nullable SpawnGroupData groupData
|
| 240 | ) {
|
| 241 | RandomSource random = level.getRandom();
|
| 242 | Variant variant;
|
| 243 | if (groupData instanceof Horse.HorseGroupData) {
|
| 244 | variant = ((Horse.HorseGroupData)groupData).variant;
|
| 245 | } else {
|
| 246 | variant = Util.getRandom(Variant.values(), random);
|
| 247 | groupData = new Horse.HorseGroupData(variant);
|
| 248 | }
|
| 249 |
|
| 250 | this.setVariantAndMarkings(variant, Util.getRandom(Markings.values(), random));
|
| 251 | return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
|
| 252 | }
|
| 253 |
|
| 254 | @Override
|
| 255 | public EntityDimensions getDefaultDimensions(final Pose pose) {
|
| 256 | return this.isBaby() ? BABY_DIMENSIONS : super.getDefaultDimensions(pose);
|
| 257 | }
|
| 258 |
|
| 259 | public static class HorseGroupData extends AgeableMob.AgeableMobGroupData {
|
| 260 | public final Variant variant;
|
| 261 |
|
| 262 | public HorseGroupData(final Variant variant) {
|
| 263 | super(true);
|
| 264 | this.variant = variant;
|
| 265 | }
|
| 266 | }
|
| 267 | }
|
| 268 |
|