| 1 | package net.minecraft.client.renderer.entity.layers;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 4 | import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
| 5 | import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
| 6 | import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
|
| 7 | import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
|
| 8 | import java.io.IOException;
|
| 9 | import java.util.Optional;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import net.minecraft.client.model.EntityModel;
|
| 13 | import net.minecraft.client.model.VillagerLikeModel;
|
| 14 | import net.minecraft.client.renderer.SubmitNodeCollector;
|
| 15 | import net.minecraft.client.renderer.entity.RenderLayerParent;
|
| 16 | import net.minecraft.client.renderer.entity.state.LivingEntityRenderState;
|
| 17 | import net.minecraft.client.renderer.entity.state.VillagerDataHolderRenderState;
|
| 18 | import net.minecraft.client.renderer.texture.MissingTextureAtlasSprite;
|
| 19 | import net.minecraft.client.resources.metadata.animation.VillagerMetadataSection;
|
| 20 | import net.minecraft.core.Holder;
|
| 21 | import net.minecraft.resources.Identifier;
|
| 22 | import net.minecraft.resources.ResourceKey;
|
| 23 | import net.minecraft.server.packs.resources.ResourceManager;
|
| 24 | import net.minecraft.util.Mth;
|
| 25 | import net.minecraft.util.Util;
|
| 26 | import net.minecraft.world.entity.npc.villager.VillagerData;
|
| 27 | import net.minecraft.world.entity.npc.villager.VillagerProfession;
|
| 28 | import net.minecraft.world.entity.npc.villager.VillagerType;
|
| 29 |
|
| 30 | @Environment(EnvType.CLIENT)
|
| 31 | public class VillagerProfessionLayer<S extends LivingEntityRenderState & VillagerDataHolderRenderState, M extends EntityModel<S> & VillagerLikeModel<S>>
|
| 32 | extends RenderLayer<S, M> {
|
| 33 | private static final Int2ObjectMap<Identifier> LEVEL_LOCATIONS = Util.make(new Int2ObjectOpenHashMap<>(), map -> {
|
| 34 | map.put(1, Identifier.withDefaultNamespace("stone"));
|
| 35 | map.put(2, Identifier.withDefaultNamespace("iron"));
|
| 36 | map.put(3, Identifier.withDefaultNamespace("gold"));
|
| 37 | map.put(4, Identifier.withDefaultNamespace("emerald"));
|
| 38 | map.put(5, Identifier.withDefaultNamespace("diamond"));
|
| 39 | });
|
| 40 | private final Object2ObjectMap<ResourceKey<VillagerType>, VillagerMetadataSection.Hat> typeHatCache = new Object2ObjectOpenHashMap<>();
|
| 41 | private final Object2ObjectMap<ResourceKey<VillagerProfession>, VillagerMetadataSection.Hat> professionHatCache = new Object2ObjectOpenHashMap<>();
|
| 42 | private final ResourceManager resourceManager;
|
| 43 | private final String path;
|
| 44 | private final M noHatModel;
|
| 45 | private final M noHatBabyModel;
|
| 46 |
|
| 47 | public VillagerProfessionLayer(
|
| 48 | final RenderLayerParent<S, M> renderer, final ResourceManager resourceManager, final String path, final M noHatModel, final M noHatBabyModel
|
| 49 | ) {
|
| 50 | super(renderer);
|
| 51 | this.resourceManager = resourceManager;
|
| 52 | this.path = path;
|
| 53 | this.noHatModel = noHatModel;
|
| 54 | this.noHatBabyModel = noHatBabyModel;
|
| 55 | }
|
| 56 |
|
| 57 | public void submit(
|
| 58 | final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final int lightCoords, final S state, final float yRot, final float xRot
|
| 59 | ) {
|
| 60 | if (!state.isInvisible) {
|
| 61 | VillagerData villagerData = state.getVillagerData();
|
| 62 | if (villagerData != null) {
|
| 63 | Holder<VillagerType> type = villagerData.type();
|
| 64 | Holder<VillagerProfession> profession = villagerData.profession();
|
| 65 | VillagerMetadataSection.Hat typeHat = this.getHatData(this.typeHatCache, "type", type);
|
| 66 | VillagerMetadataSection.Hat professionHat = this.getHatData(this.professionHatCache, "profession", profession);
|
| 67 | M model = this.getParentModel();
|
| 68 | Identifier typeTexture = this.getIdentifier(state.isBaby ? "baby" : "type", type);
|
| 69 | boolean typeHatVisible = professionHat == VillagerMetadataSection.Hat.NONE
|
| 70 | || professionHat == VillagerMetadataSection.Hat.PARTIAL && typeHat != VillagerMetadataSection.Hat.FULL;
|
| 71 | M noHatModel = state.isBaby ? this.noHatBabyModel : this.noHatModel;
|
| 72 | renderColoredCutoutModel(typeHatVisible ? model : noHatModel, typeTexture, poseStack, submitNodeCollector, lightCoords, state, -1, 1);
|
| 73 | if (!profession.is(VillagerProfession.NONE) && !state.isBaby) {
|
| 74 | Identifier professionTexture = this.getIdentifier("profession", profession);
|
| 75 | renderColoredCutoutModel(model, professionTexture, poseStack, submitNodeCollector, lightCoords, state, -1, 2);
|
| 76 | if (!profession.is(VillagerProfession.NITWIT)) {
|
| 77 | Identifier professionLevelTexture = this.getIdentifier(
|
| 78 | "profession_level", LEVEL_LOCATIONS.get(Mth.clamp(villagerData.level(), 1, LEVEL_LOCATIONS.size()))
|
| 79 | );
|
| 80 | renderColoredCutoutModel(model, professionLevelTexture, poseStack, submitNodeCollector, lightCoords, state, -1, 3);
|
| 81 | }
|
| 82 | }
|
| 83 | }
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | private Identifier getIdentifier(final String type, final Identifier key) {
|
| 88 | return key.withPath(keyPath -> "textures/entity/" + this.path + "/" + type + "/" + keyPath + ".png");
|
| 89 | }
|
| 90 |
|
| 91 | private Identifier getIdentifier(final String type, final Holder<?> holder) {
|
| 92 | return holder.unwrapKey().map(k -> this.getIdentifier(type, k.identifier())).orElse(MissingTextureAtlasSprite.getLocation());
|
| 93 | }
|
| 94 |
|
| 95 | public <K> VillagerMetadataSection.Hat getHatData(
|
| 96 | final Object2ObjectMap<ResourceKey<K>, VillagerMetadataSection.Hat> cache, final String name, final Holder<K> holder
|
| 97 | ) {
|
| 98 | ResourceKey<K> key = holder.unwrapKey().orElse(null);
|
| 99 | return key == null
|
| 100 | ? VillagerMetadataSection.Hat.NONE
|
| 101 | : cache.computeIfAbsent(key, k -> this.resourceManager.getResource(this.getIdentifier(name, key.identifier())).flatMap(resource -> {
|
| 102 | try {
|
| 103 | return resource.metadata().getSection(VillagerMetadataSection.TYPE).map(VillagerMetadataSection::hat);
|
| 104 | } catch (IOException ignored) {
|
| 105 | return Optional.empty();
|
| 106 | }
|
| 107 | }).orElse(VillagerMetadataSection.Hat.NONE));
|
| 108 | }
|
| 109 | }
|
| 110 |
|