From d73c6fb2e358c3d1e42f2b084fc0839125c1959d Mon Sep 17 00:00:00 2001 From: Sollace Date: Wed, 21 Dec 2022 23:17:24 +0100 Subject: [PATCH] Some minor optimisations when computing villager clothing --- .../entity/npc/AbstractNpcRenderer.java | 5 +-- .../render/entity/npc/NpcClothingFeature.java | 40 +++++++------------ 2 files changed, 16 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/minelittlepony/client/render/entity/npc/AbstractNpcRenderer.java b/src/main/java/com/minelittlepony/client/render/entity/npc/AbstractNpcRenderer.java index 657509e9..60cd8616 100644 --- a/src/main/java/com/minelittlepony/client/render/entity/npc/AbstractNpcRenderer.java +++ b/src/main/java/com/minelittlepony/client/render/entity/npc/AbstractNpcRenderer.java @@ -16,8 +16,7 @@ import com.minelittlepony.client.render.entity.PonyRenderer; import com.minelittlepony.client.render.entity.feature.*; import com.minelittlepony.client.render.entity.npc.textures.*; -import java.util.HashMap; -import java.util.Map; +import java.util.*; abstract class AbstractNpcRenderer extends PonyRenderer.Caster> { @@ -25,7 +24,7 @@ abstract class AbstractNpcRenderer private final String entityType; - private final Map>> models = new HashMap<>(); + private final Map>> models = new EnumMap<>(Race.class); private final NpcClothingFeature, AbstractNpcRenderer> clothing; diff --git a/src/main/java/com/minelittlepony/client/render/entity/npc/NpcClothingFeature.java b/src/main/java/com/minelittlepony/client/render/entity/npc/NpcClothingFeature.java index c4564c80..cda4cd46 100644 --- a/src/main/java/com/minelittlepony/client/render/entity/npc/NpcClothingFeature.java +++ b/src/main/java/com/minelittlepony/client/render/entity/npc/NpcClothingFeature.java @@ -37,6 +37,7 @@ class NpcClothingFeature< a.put(4, new Identifier("emerald")); a.put(5, new Identifier("diamond")); }); + private final Set loadedTextures = new HashSet<>(); private final String entityType; @@ -45,27 +46,6 @@ class NpcClothingFeature< entityType = type; } - @Deprecated(forRemoval = true) - public static Identifier getClothingTexture(VillagerDataContainer entity, String entityType) { - VillagerProfession profession = entity.getVillagerData().getProfession(); - - return createTexture("minelittlepony", entityType, "profession", Registries.VILLAGER_PROFESSION.getId(profession)); - } - - public static Identifier createTexture(String namespace, String entityType, String category, Identifier profession) { - return new Identifier(namespace, String.format("textures/entity/%s/%s/%s.png", entityType, category, profession.getPath())); - } - - public Identifier createTexture(VillagerDataContainer entity, String category) { - VillagerProfession profession = entity.getVillagerData().getProfession(); - - return createTexture(category, Registries.VILLAGER_PROFESSION.getId(profession)); - } - - public Identifier createTexture(String category, Identifier identifier) { - return createTexture("minelittlepony", entityType, category, identifier); - } - @Override public void render(MatrixStack matrixStack, VertexConsumerProvider provider, int i, T entity, float f, float g, float h, float j, float k, float l) { if (entity.isInvisible()) { @@ -94,22 +74,30 @@ class NpcClothingFeature< Identifier key = new Identifier("minelittlepony", (typeId + "/" + profId + "/" + level).replace(':', '_')); - if (MinecraftClient.getInstance().getTextureManager().getOrDefault(key, null) == null) { - TextureFlattener.flatten(computeTextures(type, profession, typeId, profId, level), key); + if (loadedTextures.add(key) && MinecraftClient.getInstance().getTextureManager().getOrDefault(key, null) == null) { + TextureFlattener.flatten(computeTextures(typeId, profId, profession == VillagerProfession.NITWIT ? -1 : level), key); } return key; } - private List computeTextures(VillagerType type, VillagerProfession profession, Identifier typeId, Identifier profId, int level) { + private List computeTextures(Identifier typeId, Identifier profId, int level) { List skins = new ArrayList<>(); skins.add(createTexture("type", typeId)); - skins.add(createTexture("profession", Registries.VILLAGER_PROFESSION.getId(profession))); - if (profession != VillagerProfession.NITWIT) { + skins.add(createTexture("profession", profId)); + if (level != -1) { skins.add(createTexture("profession_level", LEVEL_TO_ID.get(level))); } return skins; } + + public Identifier createTexture(VillagerDataContainer entity, String category) { + return createTexture(category, Registries.VILLAGER_PROFESSION.getId(entity.getVillagerData().getProfession())); + } + + private Identifier createTexture(String category, Identifier identifier) { + return new Identifier("minelittlepony", String.format("textures/entity/%s/%s/%s.png", entityType, category, identifier.getPath())); + } }