VillagerData .java

net.minecraft.world.entity.npc.villager.VillagerData (record): members: profession, NEXT_LEVEL_XP_THRESHOLDS, MAX_VILLAGER_LEVEL, MIN_VILLAGER_LEVEL, type.

net.minecraft.world.entity.npc.villagerrecord82 linescommon3.1 KB

API Surface (9)

record VillagerData(Holder<VillagerType> type, Holder<VillagerProfession> profession, int level)
VillagerData withType(final Holder<VillagerType> type)
VillagerData withType(final HolderGetter.Provider registries, final ResourceKey<VillagerType> type)
VillagerData withProfession(final Holder<VillagerProfession> profession)
VillagerData withProfession(final HolderGetter.Provider registries, final ResourceKey<VillagerProfession> profession)
VillagerData withLevel(final int level)
int getMinXpPerLevel(final int level)
int getMaxXpPerLevel(final int level)
boolean canLevelUp(final int currentLevel)

Fields & Constants (6)

DEFAULT_TYPE
MIN_VILLAGER_LEVEL
MAX_VILLAGER_LEVEL
NEXT_LEVEL_XP_THRESHOLDS
CODEC
STREAM_CODEC

String literals (2)

"profession"
"level"

Full Source

VillagerData.java82 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.npc.villager;
2
3
import com.mojang.serialization.Codec;
4
import com.mojang.serialization.codecs.RecordCodecBuilder;
5
import net.minecraft.core.Holder;
6
import net.minecraft.core.HolderGetter;
7
import net.minecraft.core.registries.BuiltInRegistries;
8
import net.minecraft.core.registries.Registries;
9
import net.minecraft.network.RegistryFriendlyByteBuf;
10
import net.minecraft.network.codec.ByteBufCodecs;
11
import net.minecraft.network.codec.StreamCodec;
12
import net.minecraft.resources.ResourceKey;
13
import net.minecraft.util.ExtraCodecs;
14
15
public record VillagerData(Holder<VillagerType> type, Holder<VillagerProfession> profession, int level) {
16
	public static final ResourceKey<VillagerType> DEFAULT_TYPE = VillagerType.PLAINS;
17
	public static final int MIN_VILLAGER_LEVEL = 1;
18
	public static final int MAX_VILLAGER_LEVEL = 5;
19
	private static final int[] NEXT_LEVEL_XP_THRESHOLDS = new int[]{0, 10, 70, 150, 250};
20
	public static final Codec<VillagerData> CODEC = RecordCodecBuilder.create(
21
		i -> i.group(
22
				BuiltInRegistries.VILLAGER_TYPE
23
					.holderByNameCodec()
24
					.fieldOf("type")
25
					.orElseGet(() -> BuiltInRegistries.VILLAGER_TYPE.getOrThrow(DEFAULT_TYPE))
26
					.forGetter(d -> d.type),
27
				BuiltInRegistries.VILLAGER_PROFESSION
28
					.holderByNameCodec()
29
					.fieldOf("profession")
30
					.orElseGet(() -> BuiltInRegistries.VILLAGER_PROFESSION.getOrThrow(VillagerProfession.NONE))
31
					.forGetter(d -> d.profession),
32
				ExtraCodecs.optionalAlwaysPresentFieldOf(Codec.INT, "level", 1).forGetter(d -> d.level)
33
			)
34
			.apply(i, VillagerData::new)
35
	);
36
	public static final StreamCodec<RegistryFriendlyByteBuf, VillagerData> STREAM_CODEC = StreamCodec.composite(
37
		ByteBufCodecs.holderRegistry(Registries.VILLAGER_TYPE),
38
		VillagerData::type,
39
		ByteBufCodecs.holderRegistry(Registries.VILLAGER_PROFESSION),
40
		VillagerData::profession,
41
		ByteBufCodecs.VAR_INT,
42
		VillagerData::level,
43
		VillagerData::new
44
	);
45
46
	public VillagerData {
47
		level = Math.max(1, level);
48
	}
49
50
	public VillagerData withType(final Holder<VillagerType> type) {
51
		return new VillagerData(type, this.profession, this.level);
52
	}
53
54
	public VillagerData withType(final HolderGetter.Provider registries, final ResourceKey<VillagerType> type) {
55
		return this.withType(registries.getOrThrow(type));
56
	}
57
58
	public VillagerData withProfession(final Holder<VillagerProfession> profession) {
59
		return new VillagerData(this.type, profession, this.level);
60
	}
61
62
	public VillagerData withProfession(final HolderGetter.Provider registries, final ResourceKey<VillagerProfession> profession) {
63
		return this.withProfession(registries.getOrThrow(profession));
64
	}
65
66
	public VillagerData withLevel(final int level) {
67
		return new VillagerData(this.type, this.profession, level);
68
	}
69
70
	public static int getMinXpPerLevel(final int level) {
71
		return canLevelUp(level) ? NEXT_LEVEL_XP_THRESHOLDS[level - 1] : 0;
72
	}
73
74
	public static int getMaxXpPerLevel(final int level) {
75
		return canLevelUp(level) ? NEXT_LEVEL_XP_THRESHOLDS[level] : 0;
76
	}
77
78
	public static boolean canLevelUp(final int currentLevel) {
79
		return currentLevel >= 1 && currentLevel < 5;
80
	}
81
}
82

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