ZombieVillager .java

net.minecraft.world.entity.monster.zombie.ZombieVillager (class, 391 lines): members: VILLAGER_CONVERSION_WAIT_MAX, VILLAGER_CONVERSION_WAIT_MIN, gossips.

net.minecraft.world.entity.monster.zombieclass391 linescommonextends Zombie14.1 KB

Implements

API Surface (26)

void defineSynchedData(final SynchedEntityData.Builder entityData)
void addAdditionalSaveData(final ValueOutput output)
void readAdditionalSaveData(final ValueInput input)
void tick()
InteractionResult mobInteract(final Player player, final InteractionHand hand)
boolean convertsInWater()
boolean removeWhenFarAway(final double distSqr)
boolean isConverting()
EntityDimensions getDefaultDimensions(final Pose pose)
void handleEntityEvent(final @EntityEvent.Value byte id)
void setVillagerConversionTime(final int conversionTime)
float getVoicePitch()
SoundEvent getAmbientSound()
SoundEvent getHurtSound(final DamageSource source)
SoundEvent getDeathSound()
SoundEvent getStepSound()
void setTradeOffers(final MerchantOffers tradeOffers)
void setGossips(final GossipContainer gossips)
void setVillagerData(final VillagerData villagerData)
boolean getVillagerDataFinalized()
void setVillagerDataFinalized(final boolean villagerDataFinalized)
VillagerData getVillagerData()
int getVillagerXp()
void setVillagerXp(final int villagerXp)
void applyImplicitComponents(final DataComponentGetter components)
<T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value)

Fields & Constants (16)

DATA_CONVERTING_ID
DATA_VILLAGER_DATA
DATA_VILLAGER_DATA_FINALIZED
VILLAGER_CONVERSION_WAIT_MIN
VILLAGER_CONVERSION_WAIT_MAX
MAX_SPECIAL_BLOCKS_COUNT
SPECIAL_BLOCK_RADIUS
NOT_CONVERTING
DEFAULT_XP
BABY_DIMENSIONS
villagerConversionTime
conversionStarter
gossips
tradeOffers
villagerXp
spawnReason

String literals (6)

"VillagerData"
"VillagerDataFinalized"
"Offers"
"Gossips"
"ConversionTime"
"ConversionPlayer"

Code Graph: Dependencies (55)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

ZombieVillager.java391 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.monster.zombie;
2
3
import com.google.common.annotations.VisibleForTesting;
4
import java.util.Optional;
5
import java.util.UUID;
6
import net.minecraft.advancements.triggers.CriteriaTriggers;
7
import net.minecraft.core.BlockPos;
8
import net.minecraft.core.Holder;
9
import net.minecraft.core.UUIDUtil;
10
import net.minecraft.core.component.DataComponentGetter;
11
import net.minecraft.core.component.DataComponentType;
12
import net.minecraft.core.component.DataComponents;
13
import net.minecraft.core.registries.BuiltInRegistries;
14
import net.minecraft.network.syncher.EntityDataAccessor;
15
import net.minecraft.network.syncher.EntityDataSerializers;
16
import net.minecraft.network.syncher.SynchedEntityData;
17
import net.minecraft.server.level.ServerLevel;
18
import net.minecraft.server.level.ServerPlayer;
19
import net.minecraft.sounds.SoundEvent;
20
import net.minecraft.sounds.SoundEvents;
21
import net.minecraft.tags.BlockTags;
22
import net.minecraft.util.RandomSource;
23
import net.minecraft.world.DifficultyInstance;
24
import net.minecraft.world.InteractionHand;
25
import net.minecraft.world.InteractionResult;
26
import net.minecraft.world.damagesource.DamageSource;
27
import net.minecraft.world.effect.MobEffectInstance;
28
import net.minecraft.world.effect.MobEffects;
29
import net.minecraft.world.entity.ConversionParams;
30
import net.minecraft.world.entity.EntityAttachment;
31
import net.minecraft.world.entity.EntityAttachments;
32
import net.minecraft.world.entity.EntityDimensions;
33
import net.minecraft.world.entity.EntityEvent;
34
import net.minecraft.world.entity.EntitySpawnReason;
35
import net.minecraft.world.entity.EntityType;
36
import net.minecraft.world.entity.EntityTypes;
37
import net.minecraft.world.entity.EquipmentSlot;
38
import net.minecraft.world.entity.Pose;
39
import net.minecraft.world.entity.SlotAccess;
40
import net.minecraft.world.entity.SpawnGroupData;
41
import net.minecraft.world.entity.ai.gossip.GossipContainer;
42
import net.minecraft.world.entity.ai.village.ReputationEventType;
43
import net.minecraft.world.entity.npc.villager.Villager;
44
import net.minecraft.world.entity.npc.villager.VillagerData;
45
import net.minecraft.world.entity.npc.villager.VillagerDataHolder;
46
import net.minecraft.world.entity.npc.villager.VillagerProfession;
47
import net.minecraft.world.entity.npc.villager.VillagerType;
48
import net.minecraft.world.entity.player.Player;
49
import net.minecraft.world.item.ItemStack;
50
import net.minecraft.world.item.Items;
51
import net.minecraft.world.item.enchantment.EnchantmentEffectComponents;
52
import net.minecraft.world.item.enchantment.EnchantmentHelper;
53
import net.minecraft.world.item.trading.MerchantOffers;
54
import net.minecraft.world.level.Level;
55
import net.minecraft.world.level.ServerLevelAccessor;
56
import net.minecraft.world.level.block.state.BlockState;
57
import net.minecraft.world.level.storage.ValueInput;
58
import net.minecraft.world.level.storage.ValueOutput;
59
import org.jspecify.annotations.Nullable;
60
61
public class ZombieVillager extends Zombie implements VillagerDataHolder {
62
	private static final EntityDataAccessor<Boolean> DATA_CONVERTING_ID = SynchedEntityData.defineId(ZombieVillager.class, EntityDataSerializers.BOOLEAN);
63
	private static final EntityDataAccessor<VillagerData> DATA_VILLAGER_DATA = SynchedEntityData.defineId(
64
		ZombieVillager.class, EntityDataSerializers.VILLAGER_DATA
65
	);
66
	private static final EntityDataAccessor<Boolean> DATA_VILLAGER_DATA_FINALIZED = SynchedEntityData.defineId(ZombieVillager.class, EntityDataSerializers.BOOLEAN);
67
	private static final int VILLAGER_CONVERSION_WAIT_MIN = 3600;
68
	private static final int VILLAGER_CONVERSION_WAIT_MAX = 6000;
69
	private static final int MAX_SPECIAL_BLOCKS_COUNT = 14;
70
	private static final int SPECIAL_BLOCK_RADIUS = 4;
71
	private static final int NOT_CONVERTING = -1;
72
	private static final int DEFAULT_XP = 0;
73
	private int villagerConversionTime;
74
	private @Nullable UUID conversionStarter;
75
	private @Nullable GossipContainer gossips;
76
	private @Nullable MerchantOffers tradeOffers;
77
	private int villagerXp = 0;
78
	private static final EntityDimensions BABY_DIMENSIONS = EntityDimensions.scalable(0.49F, 0.98F)
79
		.withEyeHeight(0.67F)
80
		.withAttachments(EntityAttachments.builder().attach(EntityAttachment.VEHICLE, 0.0F, 0.125F, 0.0F));
81
82
	public ZombieVillager(final EntityType<? extends ZombieVillager> type, final Level level) {
83
		super(type, level);
84
	}
85
86
	@Override
87
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
88
		super.defineSynchedData(entityData);
89
		entityData.define(DATA_CONVERTING_ID, false);
90
		entityData.define(DATA_VILLAGER_DATA, initializeZombieVillagerData(this.random));
91
		entityData.define(DATA_VILLAGER_DATA_FINALIZED, false);
92
	}
93
94
	@Override
95
	protected void addAdditionalSaveData(final ValueOutput output) {
96
		super.addAdditionalSaveData(output);
97
		output.store("VillagerData", VillagerData.CODEC, this.getVillagerData());
98
		output.putBoolean("VillagerDataFinalized", this.getVillagerDataFinalized());
99
		output.storeNullable("Offers", MerchantOffers.CODEC, this.tradeOffers);
100
		output.storeNullable("Gossips", GossipContainer.CODEC, this.gossips);
101
		output.putInt("ConversionTime", this.isConverting() ? this.villagerConversionTime : -1);
102
		output.storeNullable("ConversionPlayer", UUIDUtil.CODEC, this.conversionStarter);
103
		output.putInt("Xp", this.villagerXp);
104
	}
105
106
	@Override
107
	protected void readAdditionalSaveData(final ValueInput input) {
108
		super.readAdditionalSaveData(input);
109
		Optional<VillagerData> villagerDataOptional = input.read("VillagerData", VillagerData.CODEC);
110
		if (input.getBooleanOr("VillagerDataFinalized", false) || villagerDataOptional.isPresent()) {
111
			this.setVillagerDataFinalized(true);
112
			VillagerData villagerData = villagerDataOptional.orElseGet(() -> initializeZombieVillagerData(this.random));
113
			this.entityData.set(DATA_VILLAGER_DATA, villagerData);
114
		}
115
116
		this.tradeOffers = input.read("Offers", MerchantOffers.CODEC).orElse(null);
117
		this.gossips = input.read("Gossips", GossipContainer.CODEC).orElse(null);
118
		int conversionTime = input.getIntOr("ConversionTime", -1);
119
		if (conversionTime != -1) {
120
			UUID conversionStarter = input.read("ConversionPlayer", UUIDUtil.CODEC).orElse(null);
121
			this.startConverting(conversionStarter, conversionTime);
122
		} else {
123
			this.getEntityData().set(DATA_CONVERTING_ID, false);
124
			this.villagerConversionTime = -1;
125
		}
126
127
		this.villagerXp = input.getIntOr("Xp", 0);
128
	}
129
130
	@Override
131
	public @Nullable SpawnGroupData finalizeSpawn(
132
		final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, final @Nullable SpawnGroupData groupData
133
	) {
134
		this.finalizeVillagerType(level, this.blockPosition());
135
		return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
136
	}
137
138
	private static VillagerData initializeZombieVillagerData(final RandomSource random) {
139
		VillagerData villagerData = Villager.createDefaultVillagerData();
140
		Optional<Holder.Reference<VillagerProfession>> profession = BuiltInRegistries.VILLAGER_PROFESSION.getRandom(random);
141
		if (profession.isPresent()) {
142
			villagerData = villagerData.withProfession(profession.get());
143
		}
144
145
		return villagerData;
146
	}
147
148
	@Override
149
	public void tick() {
150
		if (!this.level().isClientSide() && this.isAlive() && this.isConverting()) {
151
			int amount = this.getConversionProgress();
152
			this.villagerConversionTime -= amount;
153
			if (this.villagerConversionTime <= 0) {
154
				this.finishConversion((ServerLevel)this.level());
155
			}
156
		}
157
158
		super.tick();
159
	}
160
161
	@Override
162
	public InteractionResult mobInteract(final Player player, final InteractionHand hand) {
163
		ItemStack itemStack = player.getItemInHand(hand);
164
		if (itemStack.is(Items.GOLDEN_APPLE)) {
165
			if (this.hasEffect(MobEffects.WEAKNESS)) {
166
				itemStack.consume(1, player);
167
				if (!this.level().isClientSide()) {
168
					this.startConverting(player.getUUID(), this.random.nextInt(2401) + 3600);
169
				}
170
171
				return InteractionResult.SUCCESS_SERVER;
172
			} else {
173
				return InteractionResult.CONSUME;
174
			}
175
		} else {
176
			return super.mobInteract(player, hand);
177
		}
178
	}
179
180
	@Override
181
	protected boolean convertsInWater() {
182
		return false;
183
	}
184
185
	@Override
186
	public boolean removeWhenFarAway(final double distSqr) {
187
		return !this.isConverting() && this.villagerXp == 0;
188
	}
189
190
	public boolean isConverting() {
191
		return this.getEntityData().get(DATA_CONVERTING_ID);
192
	}
193
194
	private void startConverting(final @Nullable UUID player, final int time) {
195
		this.conversionStarter = player;
196
		this.villagerConversionTime = time;
197
		this.getEntityData().set(DATA_CONVERTING_ID, true);
198
		this.removeEffect(MobEffects.WEAKNESS);
199
		this.addEffect(new MobEffectInstance(MobEffects.STRENGTH, time, Math.min(this.level().getDifficulty().getId() - 1, 0)));
200
		this.level().broadcastEntityEvent(this, (byte)16);
201
	}
202
203
	@Override
204
	public EntityDimensions getDefaultDimensions(final Pose pose) {
205
		return this.isBaby() ? BABY_DIMENSIONS : super.getDefaultDimensions(pose);
206
	}
207
208
	@Override
209
	public void handleEntityEvent(final @EntityEvent.Value byte id) {
210
		if (id == 16) {
211
			if (!this.isSilent()) {
212
				this.level()
213
					.playLocalSound(
214
						this.getX(),
215
						this.getEyeY(),
216
						this.getZ(),
217
						SoundEvents.ZOMBIE_VILLAGER_CURE,
218
						this.getSoundSource(),
219
						1.0F + this.random.nextFloat(),
220
						this.random.nextFloat() * 0.7F + 0.3F,
221
						false
222
					);
223
			}
224
		} else {
225
			super.handleEntityEvent(id);
226
		}
227
	}
228
229
	private void finishConversion(final ServerLevel level) {
230
		this.convertTo(
231
			EntityTypes.VILLAGER,
232
			ConversionParams.single(this, false, false),
233
			villager -> {
234
				for (EquipmentSlot undroppedSlot : this.dropPreservedEquipment(
235
					level, stack -> !EnchantmentHelper.has(stack, EnchantmentEffectComponents.PREVENT_ARMOR_CHANGE)
236
				)) {
237
					SlotAccess offsetSlot = villager.getSlot(undroppedSlot.getIndex() + 300);
238
					if (offsetSlot != null) {
239
						offsetSlot.set(this.getItemBySlot(undroppedSlot));
240
					}
241
				}
242
243
				villager.setVillagerDataFinalized(this.getVillagerDataFinalized());
244
				villager.setVillagerData(this.getVillagerData());
245
				if (this.gossips != null) {
246
					villager.setGossips(this.gossips);
247
				}
248
249
				if (this.tradeOffers != null) {
250
					villager.setOffers(this.tradeOffers.copy());
251
				}
252
253
				villager.setVillagerXp(this.villagerXp);
254
				villager.finalizeSpawn(level, level.getCurrentDifficultyAt(villager.blockPosition()), EntitySpawnReason.CONVERSION, null);
255
				villager.refreshBrain(level);
256
				if (this.conversionStarter != null) {
257
					Player player = level.getPlayerByUUID(this.conversionStarter);
258
					if (player instanceof ServerPlayer serverPlayer) {
259
						CriteriaTriggers.CURED_ZOMBIE_VILLAGER.trigger(serverPlayer, this, villager);
260
						level.onReputationEvent(ReputationEventType.ZOMBIE_VILLAGER_CURED, player, villager);
261
					}
262
				}
263
264
				villager.addEffect(new MobEffectInstance(MobEffects.NAUSEA, 200, 0));
265
				if (!this.isSilent()) {
266
					level.levelEvent(null, 1027, this.blockPosition(), 0);
267
				}
268
			}
269
		);
270
	}
271
272
	@VisibleForTesting
273
	public void setVillagerConversionTime(final int conversionTime) {
274
		this.villagerConversionTime = conversionTime;
275
	}
276
277
	private int getConversionProgress() {
278
		int amount = 1;
279
		if (this.random.nextFloat() < 0.01F) {
280
			int specialBlocksCount = 0;
281
			BlockPos.MutableBlockPos blockPos = new BlockPos.MutableBlockPos();
282
283
			for (int xx = (int)this.getX() - 4; xx < (int)this.getX() + 4 && specialBlocksCount < 14; xx++) {
284
				for (int yy = (int)this.getY() - 4; yy < (int)this.getY() + 4 && specialBlocksCount < 14; yy++) {
285
					for (int zz = (int)this.getZ() - 4; zz < (int)this.getZ() + 4 && specialBlocksCount < 14; zz++) {
286
						BlockState state = this.level().getBlockState(blockPos.set(xx, yy, zz));
287
						if (state.is(BlockTags.SPEEDS_UP_ZOMBIE_VILLAGER_CURING)) {
288
							if (this.random.nextFloat() < 0.3F) {
289
								amount++;
290
							}
291
292
							specialBlocksCount++;
293
						}
294
					}
295
				}
296
			}
297
		}
298
299
		return amount;
300
	}
301
302
	@Override
303
	public float getVoicePitch() {
304
		return this.isBaby() ? (this.random.nextFloat() - this.random.nextFloat()) * 0.2F + 2.0F : (this.random.nextFloat() - this.random.nextFloat()) * 0.2F + 1.0F;
305
	}
306
307
	@Override
308
	public SoundEvent getAmbientSound() {
309
		return SoundEvents.ZOMBIE_VILLAGER_AMBIENT;
310
	}
311
312
	@Override
313
	public SoundEvent getHurtSound(final DamageSource source) {
314
		return SoundEvents.ZOMBIE_VILLAGER_HURT;
315
	}
316
317
	@Override
318
	public SoundEvent getDeathSound() {
319
		return SoundEvents.ZOMBIE_VILLAGER_DEATH;
320
	}
321
322
	@Override
323
	public SoundEvent getStepSound() {
324
		return SoundEvents.ZOMBIE_VILLAGER_STEP;
325
	}
326
327
	public void setTradeOffers(final MerchantOffers tradeOffers) {
328
		this.tradeOffers = tradeOffers;
329
	}
330
331
	public void setGossips(final GossipContainer gossips) {
332
		this.gossips = gossips;
333
	}
334
335
	@Override
336
	public void setVillagerData(final VillagerData villagerData) {
337
		VillagerData currentData = this.getVillagerData();
338
		if (!currentData.profession().equals(villagerData.profession())) {
339
			this.tradeOffers = null;
340
		}
341
342
		this.entityData.set(DATA_VILLAGER_DATA, villagerData);
343
	}
344
345
	@Override
346
	public boolean getVillagerDataFinalized() {
347
		return this.entityData.get(DATA_VILLAGER_DATA_FINALIZED);
348
	}
349
350
	@Override
351
	public void setVillagerDataFinalized(final boolean villagerDataFinalized) {
352
		this.entityData.set(DATA_VILLAGER_DATA_FINALIZED, villagerDataFinalized);
353
	}
354
355
	@Override
356
	public VillagerData getVillagerData() {
357
		return this.entityData.get(DATA_VILLAGER_DATA);
358
	}
359
360
	public int getVillagerXp() {
361
		return this.villagerXp;
362
	}
363
364
	public void setVillagerXp(final int villagerXp) {
365
		this.villagerXp = villagerXp;
366
	}
367
368
	@Override
369
	public <T> @Nullable T get(final DataComponentType<? extends T> type) {
370
		return type == DataComponents.VILLAGER_VARIANT ? castComponentValue((DataComponentType<T>)type, this.getVillagerData().type()) : super.get(type);
371
	}
372
373
	@Override
374
	protected void applyImplicitComponents(final DataComponentGetter components) {
375
		this.applyImplicitComponentIfPresent(components, DataComponents.VILLAGER_VARIANT);
376
		super.applyImplicitComponents(components);
377
	}
378
379
	@Override
380
	protected <T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value) {
381
		if (type == DataComponents.VILLAGER_VARIANT) {
382
			Holder<VillagerType> variant = castComponentValue(DataComponents.VILLAGER_VARIANT, value);
383
			this.setVillagerData(this.getVillagerData().withType(variant));
384
			this.setVillagerDataFinalized(true);
385
			return true;
386
		} else {
387
			return super.applyImplicitComponent(type, value);
388
		}
389
	}
390
}
391

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