/* * Decompiled with CFR 0.152. * * Could not load the following classes: * com.mojang.datafixers.kinds.App * com.mojang.datafixers.kinds.Applicative * com.mojang.serialization.Codec * com.mojang.serialization.codecs.RecordCodecBuilder */ package net.minecraft.world.entity.npc; import com.mojang.datafixers.kinds.App; import com.mojang.datafixers.kinds.Applicative; import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; import net.minecraft.core.Holder; import net.minecraft.core.HolderGetter; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.core.registries.Registries; import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; import net.minecraft.resources.ResourceKey; import net.minecraft.world.entity.npc.VillagerProfession; import net.minecraft.world.entity.npc.VillagerType; public record VillagerData(Holder type, Holder profession, int level) { public static final int MIN_VILLAGER_LEVEL = 1; public static final int MAX_VILLAGER_LEVEL = 5; private static final int[] NEXT_LEVEL_XP_THRESHOLDS = new int[]{0, 10, 70, 150, 250}; public static final Codec CODEC = RecordCodecBuilder.create(i -> i.group((App)BuiltInRegistries.VILLAGER_TYPE.holderByNameCodec().fieldOf("type").orElseGet(() -> BuiltInRegistries.VILLAGER_TYPE.getOrThrow(VillagerType.PLAINS)).forGetter(d -> d.type), (App)BuiltInRegistries.VILLAGER_PROFESSION.holderByNameCodec().fieldOf("profession").orElseGet(() -> BuiltInRegistries.VILLAGER_PROFESSION.getOrThrow(VillagerProfession.NONE)).forGetter(d -> d.profession), (App)Codec.INT.fieldOf("level").orElse((Object)1).forGetter(d -> d.level)).apply((Applicative)i, VillagerData::new)); public static final StreamCodec STREAM_CODEC = StreamCodec.composite(ByteBufCodecs.holderRegistry(Registries.VILLAGER_TYPE), VillagerData::type, ByteBufCodecs.holderRegistry(Registries.VILLAGER_PROFESSION), VillagerData::profession, ByteBufCodecs.VAR_INT, VillagerData::level, VillagerData::new); public VillagerData { level = Math.max(1, level); } public VillagerData withType(Holder type) { return new VillagerData(type, this.profession, this.level); } public VillagerData withType(HolderGetter.Provider registries, ResourceKey type) { return this.withType(registries.getOrThrow(type)); } public VillagerData withProfession(Holder profession) { return new VillagerData(this.type, profession, this.level); } public VillagerData withProfession(HolderGetter.Provider registries, ResourceKey profession) { return this.withProfession(registries.getOrThrow(profession)); } public VillagerData withLevel(int level) { return new VillagerData(this.type, this.profession, level); } public static int getMinXpPerLevel(int level) { return VillagerData.canLevelUp(level) ? NEXT_LEVEL_XP_THRESHOLDS[level - 1] : 0; } public static int getMaxXpPerLevel(int level) { return VillagerData.canLevelUp(level) ? NEXT_LEVEL_XP_THRESHOLDS[level] : 0; } public static boolean canLevelUp(int currentLevel) { return currentLevel >= 1 && currentLevel < 5; } }