diff --git a/src/main/java/com/minelittlepony/render/ponies/RenderPonyVillager.java b/src/main/java/com/minelittlepony/render/ponies/RenderPonyVillager.java index 737b28df..2cec367c 100644 --- a/src/main/java/com/minelittlepony/render/ponies/RenderPonyVillager.java +++ b/src/main/java/com/minelittlepony/render/ponies/RenderPonyVillager.java @@ -2,12 +2,8 @@ package com.minelittlepony.render.ponies; import com.minelittlepony.model.PMAPI; import com.minelittlepony.render.RenderPonyMob; +import com.minelittlepony.util.render.ITextureSupplier; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.GlStateManager; import net.minecraft.client.renderer.entity.RenderManager; import net.minecraft.entity.passive.EntityVillager; @@ -15,20 +11,18 @@ import net.minecraft.util.ResourceLocation; public class RenderPonyVillager extends RenderPonyMob { - private static final ResourceLocation[] PROFESSIONS = { + private static final ITextureSupplier PROFESSIONS = new VillagerProfessionTextureCache( + "textures/entity/villager/%d_pony.png", new ResourceLocation("minelittlepony", "textures/entity/villager/farmer_pony.png"), new ResourceLocation("minelittlepony", "textures/entity/villager/librarian_pony.png"), new ResourceLocation("minelittlepony", "textures/entity/villager/priest_pony.png"), new ResourceLocation("minelittlepony", "textures/entity/villager/smith_pony.png"), new ResourceLocation("minelittlepony", "textures/entity/villager/butcher_pony.png"), new ResourceLocation("minelittlepony", "textures/entity/villager/villager_pony.png") - }; + ); private static final ResourceLocation EGG = new ResourceLocation("minelittlepony", "textures/entity/villager/silly_pony.png"); private static final ResourceLocation EGG_2 = new ResourceLocation("minelittlepony", "textures/entity/villager/tiny_silly_pony.png"); - - private static final Map MOD_PROFESSIONS = new HashMap<>(); - public RenderPonyVillager(RenderManager manager) { super(manager, PMAPI.villager); } @@ -41,18 +35,6 @@ public class RenderPonyVillager extends RenderPonyMob { @Override protected ResourceLocation getTexture(EntityVillager entity) { - ResourceLocation texture = getVillagerTexture(entity); - - try { - Minecraft.getMinecraft().getResourceManager().getResource(texture); - } catch (IOException e) { - return PROFESSIONS[5]; - } - - return texture; - } - - private ResourceLocation getVillagerTexture(EntityVillager entity) { if ("Derpy".equals(entity.getCustomNameTag())) { if (entity.isChild()) { return EGG_2; @@ -60,16 +42,6 @@ public class RenderPonyVillager extends RenderPonyMob { return EGG; } - int profession = entity.getProfession(); - - if (profession >= PROFESSIONS.length) { - return MOD_PROFESSIONS.computeIfAbsent(profession, this::getModProfessionResource); - } - - return PROFESSIONS[profession]; - } - - protected ResourceLocation getModProfessionResource(int professionId) { - return new ResourceLocation("minelittlepony", String.format("textures/entity/villager/%d_pony.png", professionId)); + return PROFESSIONS.supplyTexture(entity.getProfession()); } } diff --git a/src/main/java/com/minelittlepony/render/ponies/RenderPonyZombieVillager.java b/src/main/java/com/minelittlepony/render/ponies/RenderPonyZombieVillager.java index bfa4273c..ba17028e 100644 --- a/src/main/java/com/minelittlepony/render/ponies/RenderPonyZombieVillager.java +++ b/src/main/java/com/minelittlepony/render/ponies/RenderPonyZombieVillager.java @@ -2,6 +2,7 @@ package com.minelittlepony.render.ponies; import com.minelittlepony.model.PMAPI; import com.minelittlepony.render.RenderPonyMob; +import com.minelittlepony.util.render.ITextureSupplier; import net.minecraft.client.renderer.entity.RenderManager; import net.minecraft.entity.monster.EntityZombieVillager; @@ -9,14 +10,15 @@ import net.minecraft.util.ResourceLocation; public class RenderPonyZombieVillager extends RenderPonyMob { - private static final ResourceLocation[] PROFESSIONS = { + private static final ITextureSupplier PROFESSIONS = new VillagerProfessionTextureCache( + "textures/entity/zombie_villager/zombie_%d_pony.png", new ResourceLocation("minelittlepony", "textures/entity/zombie_villager/zombie_farmer_pony.png"), new ResourceLocation("minelittlepony", "textures/entity/zombie_villager/zombie_librarian_pony.png"), new ResourceLocation("minelittlepony", "textures/entity/zombie_villager/zombie_priest_pony.png"), new ResourceLocation("minelittlepony", "textures/entity/zombie_villager/zombie_smith_pony.png"), new ResourceLocation("minelittlepony", "textures/entity/zombie_villager/zombie_butcher_pony.png"), new ResourceLocation("minelittlepony", "textures/entity/zombie_villager/zombie_villager_pony.png") - }; + ); public RenderPonyZombieVillager(RenderManager manager) { super(manager, PMAPI.villager); @@ -24,7 +26,7 @@ public class RenderPonyZombieVillager extends RenderPonyMob { + + private final ResourceLocation[] pool; + private final String path; + + private final Map cache = new HashMap<>(); + + public VillagerProfessionTextureCache(String path, ResourceLocation... pool) { + this.path = path; + this.pool = pool; + } + + public ResourceLocation supplyTexture(Integer profession) { + ResourceLocation texture = getVillagerTexture(profession); + + try { + Minecraft.getMinecraft().getResourceManager().getResource(texture); + } catch (IOException e) { + return pool[5]; + } + + return texture; + } + + private ResourceLocation getVillagerTexture(int profession) { + if (profession >= pool.length) { + return cache.computeIfAbsent(profession, this::getModProfessionResource); + } + + return pool[profession]; + } + + private ResourceLocation getModProfessionResource(int professionId) { + return new ResourceLocation("minelittlepony", String.format(path, professionId)); + } +} diff --git a/src/main/java/com/minelittlepony/util/render/ITextureSupplier.java b/src/main/java/com/minelittlepony/util/render/ITextureSupplier.java new file mode 100644 index 00000000..ce4dc3f2 --- /dev/null +++ b/src/main/java/com/minelittlepony/util/render/ITextureSupplier.java @@ -0,0 +1,11 @@ +package com.minelittlepony.util.render; + +import net.minecraft.util.ResourceLocation; + +/** + * A texture pool for generating multiple associated textures. + */ +@FunctionalInterface +public interface ITextureSupplier { + ResourceLocation supplyTexture(T key); +}