Cow .java

net.minecraft.world.entity.animal.cow.Cow (class, 145 lines): members: getSoundSet, setVariant, DATA_SOUND_VARIANT_ID, DATA_VARIANT_ID, getVariant, get.

net.minecraft.world.entity.animal.cowclass145 linescommonextends AbstractCow6.1 KB

API Surface (10)

void defineSynchedData(final SynchedEntityData.Builder entityData)
void addAdditionalSaveData(final ValueOutput output)
void readAdditionalSaveData(final ValueInput input)
SpawnGroupData finalizeSpawn(final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, final @Nullable SpawnGroupData groupData)
void setVariant(final Holder<CowVariant> variant)
Holder<CowVariant> getVariant()
CowSoundVariant getSoundSet()
EntityDimensions getDefaultDimensions(final Pose pose)
void applyImplicitComponents(final DataComponentGetter components)
<T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value)

Fields & Constants (4)

DATA_VARIANT_ID
DATA_SOUND_VARIANT_ID
BABY_DIMENSIONS
spawnReason

String literals (1)

"sound_variant"

Full Source

Cow.java145 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.animal.cow;
2
3
import net.minecraft.core.Holder;
4
import net.minecraft.core.Registry;
5
import net.minecraft.core.component.DataComponentGetter;
6
import net.minecraft.core.component.DataComponentType;
7
import net.minecraft.core.component.DataComponents;
8
import net.minecraft.core.registries.Registries;
9
import net.minecraft.network.syncher.EntityDataAccessor;
10
import net.minecraft.network.syncher.EntityDataSerializers;
11
import net.minecraft.network.syncher.SynchedEntityData;
12
import net.minecraft.resources.ResourceKey;
13
import net.minecraft.server.level.ServerLevel;
14
import net.minecraft.world.DifficultyInstance;
15
import net.minecraft.world.entity.AgeableMob;
16
import net.minecraft.world.entity.EntityAttachment;
17
import net.minecraft.world.entity.EntityAttachments;
18
import net.minecraft.world.entity.EntityDimensions;
19
import net.minecraft.world.entity.EntitySpawnReason;
20
import net.minecraft.world.entity.EntityType;
21
import net.minecraft.world.entity.EntityTypes;
22
import net.minecraft.world.entity.Pose;
23
import net.minecraft.world.entity.SpawnGroupData;
24
import net.minecraft.world.entity.variant.SpawnContext;
25
import net.minecraft.world.entity.variant.VariantUtils;
26
import net.minecraft.world.level.Level;
27
import net.minecraft.world.level.ServerLevelAccessor;
28
import net.minecraft.world.level.storage.ValueInput;
29
import net.minecraft.world.level.storage.ValueOutput;
30
import org.jspecify.annotations.Nullable;
31
32
public class Cow extends AbstractCow {
33
	private static final EntityDataAccessor<Holder<CowVariant>> DATA_VARIANT_ID = SynchedEntityData.defineId(Cow.class, EntityDataSerializers.COW_VARIANT);
34
	private static final EntityDataAccessor<Holder<CowSoundVariant>> DATA_SOUND_VARIANT_ID = SynchedEntityData.defineId(
35
		Cow.class, EntityDataSerializers.COW_SOUND_VARIANT
36
	);
37
	private static final EntityDimensions BABY_DIMENSIONS = EntityDimensions.scalable(0.45F, 0.7F)
38
		.withEyeHeight(0.69F)
39
		.withAttachments(EntityAttachments.builder().attach(EntityAttachment.PASSENGER, 0.0F, 0.75F, 0.0F));
40
41
	public Cow(final EntityType<? extends Cow> type, final Level level) {
42
		super(type, level);
43
	}
44
45
	@Override
46
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
47
		super.defineSynchedData(entityData);
48
		Registry<CowSoundVariant> cowSoundVariants = this.registryAccess().lookupOrThrow(Registries.COW_SOUND_VARIANT);
49
		entityData.define(DATA_VARIANT_ID, VariantUtils.getDefaultOrAny(this.registryAccess(), CowVariants.TEMPERATE));
50
		entityData.define(DATA_SOUND_VARIANT_ID, cowSoundVariants.get(CowSoundVariants.CLASSIC).or(cowSoundVariants::getAny).orElseThrow());
51
	}
52
53
	@Override
54
	protected void addAdditionalSaveData(final ValueOutput output) {
55
		super.addAdditionalSaveData(output);
56
		super.addAdditionalSaveData(output);
57
		VariantUtils.writeVariant(output, this.getVariant());
58
		this.getSoundVariant()
59
			.unwrapKey()
60
			.ifPresent(soundVariant -> output.store("sound_variant", ResourceKey.codec(Registries.COW_SOUND_VARIANT), (ResourceKey<CowSoundVariant>)soundVariant));
61
	}
62
63
	@Override
64
	protected void readAdditionalSaveData(final ValueInput input) {
65
		super.readAdditionalSaveData(input);
66
		VariantUtils.readVariant(input, Registries.COW_VARIANT).ifPresent(this::setVariant);
67
		input.read("sound_variant", ResourceKey.codec(Registries.COW_SOUND_VARIANT))
68
			.flatMap(soundVariant -> this.registryAccess().lookupOrThrow(Registries.COW_SOUND_VARIANT).get((ResourceKey<CowSoundVariant>)soundVariant))
69
			.ifPresent(this::setSoundVariant);
70
	}
71
72
	public @Nullable Cow getBreedOffspring(final ServerLevel level, final AgeableMob partner) {
73
		Cow baby = EntityTypes.COW.create(level, EntitySpawnReason.BREEDING);
74
		if (baby != null && partner instanceof Cow partnerCow) {
75
			baby.setVariant(this.random.nextBoolean() ? this.getVariant() : partnerCow.getVariant());
76
		}
77
78
		return baby;
79
	}
80
81
	@Override
82
	public SpawnGroupData finalizeSpawn(
83
		final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, final @Nullable SpawnGroupData groupData
84
	) {
85
		VariantUtils.selectVariantToSpawn(SpawnContext.create(level, this.blockPosition()), Registries.COW_VARIANT).ifPresent(this::setVariant);
86
		this.setSoundVariant(CowSoundVariants.pickRandomSoundVariant(this.registryAccess(), level.getRandom()));
87
		return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
88
	}
89
90
	public void setVariant(final Holder<CowVariant> variant) {
91
		this.entityData.set(DATA_VARIANT_ID, variant);
92
	}
93
94
	public Holder<CowVariant> getVariant() {
95
		return this.entityData.get(DATA_VARIANT_ID);
96
	}
97
98
	private Holder<CowSoundVariant> getSoundVariant() {
99
		return this.entityData.get(DATA_SOUND_VARIANT_ID);
100
	}
101
102
	private void setSoundVariant(final Holder<CowSoundVariant> soundVariant) {
103
		this.entityData.set(DATA_SOUND_VARIANT_ID, soundVariant);
104
	}
105
106
	@Override
107
	protected CowSoundVariant getSoundSet() {
108
		return this.getSoundVariant().value();
109
	}
110
111
	@Override
112
	public EntityDimensions getDefaultDimensions(final Pose pose) {
113
		return this.isBaby() ? BABY_DIMENSIONS : super.getDefaultDimensions(pose);
114
	}
115
116
	@Override
117
	public <T> @Nullable T get(final DataComponentType<? extends T> type) {
118
		if (type == DataComponents.COW_VARIANT) {
119
			return castComponentValue((DataComponentType<T>)type, this.getVariant());
120
		} else {
121
			return type == DataComponents.COW_SOUND_VARIANT ? castComponentValue((DataComponentType<T>)type, this.getSoundVariant()) : super.get(type);
122
		}
123
	}
124
125
	@Override
126
	protected void applyImplicitComponents(final DataComponentGetter components) {
127
		this.applyImplicitComponentIfPresent(components, DataComponents.COW_VARIANT);
128
		this.applyImplicitComponentIfPresent(components, DataComponents.COW_SOUND_VARIANT);
129
		super.applyImplicitComponents(components);
130
	}
131
132
	@Override
133
	protected <T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value) {
134
		if (type == DataComponents.COW_VARIANT) {
135
			this.setVariant(castComponentValue(DataComponents.COW_VARIANT, value));
136
			return true;
137
		} else if (type == DataComponents.COW_SOUND_VARIANT) {
138
			this.setSoundVariant(castComponentValue(DataComponents.COW_SOUND_VARIANT, value));
139
			return true;
140
		} else {
141
			return super.applyImplicitComponent(type, value);
142
		}
143
	}
144
}
145

Decompiled Minecraft 26.3 source of Cow (net/minecraft/world/entity/animal/cow/Cow.java) with Mojang official mappings, stored verbatim on the site.