From ada764f94674dd53dab57679b7479a586de2324f Mon Sep 17 00:00:00 2001 From: Sollace Date: Fri, 17 Aug 2018 15:08:37 +0200 Subject: [PATCH] Move the armour texture resolver to a class and interface --- .../DefaultPonyArmorTextureResolver.java | 94 +++++++++++++++++++ .../model/armour/IArmorTextureResolver.java | 15 +++ .../render/layer/LayerPonyArmor.java | 90 +++--------------- 3 files changed, 120 insertions(+), 79 deletions(-) create mode 100644 src/main/java/com/minelittlepony/model/armour/DefaultPonyArmorTextureResolver.java create mode 100644 src/main/java/com/minelittlepony/model/armour/IArmorTextureResolver.java diff --git a/src/main/java/com/minelittlepony/model/armour/DefaultPonyArmorTextureResolver.java b/src/main/java/com/minelittlepony/model/armour/DefaultPonyArmorTextureResolver.java new file mode 100644 index 00000000..d109d805 --- /dev/null +++ b/src/main/java/com/minelittlepony/model/armour/DefaultPonyArmorTextureResolver.java @@ -0,0 +1,94 @@ +package com.minelittlepony.model.armour; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.resources.ResourcePackRepository; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.inventory.EntityEquipmentSlot; +import net.minecraft.item.ItemArmor; +import net.minecraft.item.ItemStack; +import net.minecraft.util.ResourceLocation; + +import com.google.common.collect.Maps; +import com.minelittlepony.ForgeProxy; +import com.minelittlepony.model.armour.IEquestrianArmor.ArmorLayer; + +import javax.annotation.Nullable; + +import java.io.IOException; +import java.util.Map; + +public class DefaultPonyArmorTextureResolver implements IArmorTextureResolver { + + private final Map HUMAN_ARMOUR = Maps.newHashMap(); + private final Map PONY_ARMOUR = Maps.newHashMap(); + + public ResourceLocation getArmorTexture(T entity, ItemStack itemstack, EntityEquipmentSlot slot, ArmorLayer layer, @Nullable String type) { + ItemArmor item = (ItemArmor) itemstack.getItem(); + String texture = item.getArmorMaterial().getName(); + + String domain = "minecraft"; + + int idx = texture.indexOf(':'); + if (idx > -1) { + domain = texture.substring(0, idx); + texture = texture.substring(idx + 1); + } + + type = type == null ? "" : String.format("_%s", type); + + String ponyRes = String.format("%s:textures/models/armor/%s_layer_%s%s.png", domain, texture, layer.name().toLowerCase(), type); + String oldPonyRes = String.format("%s:textures/models/armor/%s_layer_%d%s.png", domain, texture, layer == ArmorLayer.INNER ? 2 : 1, type); + + ResourceLocation human = getArmorTexture(entity, itemstack, ponyRes, slot, type); + ResourceLocation pony = ponifyResource(human); + + ResourceLocation oldPony = ponifyResource(getArmorTexture(entity, itemstack, oldPonyRes, slot, type)); + + return resolve(pony, oldPony, human); + } + + private ResourceLocation resolve(ResourceLocation... resources) { + // check resource packs for either texture. + for (ResourcePackRepository.Entry entry : Minecraft.getMinecraft().getResourcePackRepository().getRepositoryEntries()) { + for (ResourceLocation candidate : resources) { + if (entry.getResourcePack().resourceExists(candidate)) { + // ponies are more important + return candidate; + } + } + } + + // the default pack + for (ResourceLocation candidate : resources) { + try { + Minecraft.getMinecraft().getResourceManager().getResource(candidate); + return candidate; + } catch (IOException e) { } + } + + return resources[resources.length - 1]; + } + + private ResourceLocation ponifyResource(ResourceLocation human) { + return PONY_ARMOUR.computeIfAbsent(human, key -> { + String domain = human.getNamespace(); + if ("minecraft".equals(domain)) { + domain = "minelittlepony"; // it's a vanilla armor. I provide these. + } + + return new ResourceLocation(domain, human.getPath().replace(".png", "_pony.png")); + }); + } + + private ResourceLocation getArmorTexture(T entity, ItemStack item, String def, EntityEquipmentSlot slot, @Nullable String type) { + return HUMAN_ARMOUR.computeIfAbsent(ForgeProxy.getArmorTexture(entity, item, def, slot, type), ResourceLocation::new); + } +} + + + + + + + + diff --git a/src/main/java/com/minelittlepony/model/armour/IArmorTextureResolver.java b/src/main/java/com/minelittlepony/model/armour/IArmorTextureResolver.java new file mode 100644 index 00000000..ca41cfb1 --- /dev/null +++ b/src/main/java/com/minelittlepony/model/armour/IArmorTextureResolver.java @@ -0,0 +1,15 @@ +package com.minelittlepony.model.armour; + +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.inventory.EntityEquipmentSlot; +import net.minecraft.item.ItemStack; +import net.minecraft.util.ResourceLocation; + +import com.minelittlepony.model.armour.IEquestrianArmor.ArmorLayer; + +import javax.annotation.Nullable; + +public interface IArmorTextureResolver { + + ResourceLocation getArmorTexture(T entity, ItemStack itemstack, EntityEquipmentSlot slot, ArmorLayer layer, @Nullable String type); +} diff --git a/src/main/java/com/minelittlepony/render/layer/LayerPonyArmor.java b/src/main/java/com/minelittlepony/render/layer/LayerPonyArmor.java index 70c87764..cfc9b5e8 100644 --- a/src/main/java/com/minelittlepony/render/layer/LayerPonyArmor.java +++ b/src/main/java/com/minelittlepony/render/layer/LayerPonyArmor.java @@ -1,20 +1,20 @@ package com.minelittlepony.render.layer; -import com.google.common.collect.Maps; import com.minelittlepony.ForgeProxy; import com.minelittlepony.model.ModelWrapper; import com.minelittlepony.model.armour.IEquestrianArmor; import com.minelittlepony.model.armour.IEquestrianArmor.ArmorLayer; +import com.minelittlepony.model.armour.IArmorTextureResolver; import com.minelittlepony.model.armour.ModelPonyArmor; +import com.minelittlepony.model.armour.DefaultPonyArmorTextureResolver; import com.minelittlepony.model.capabilities.IModelArmor; import com.minelittlepony.util.coordinates.Color; -import net.minecraft.client.Minecraft; + import net.minecraft.client.model.ModelBase; import net.minecraft.client.model.ModelBiped; import net.minecraft.client.renderer.GlStateManager; import net.minecraft.client.renderer.entity.RenderLivingBase; import net.minecraft.client.renderer.entity.layers.LayerArmorBase; -import net.minecraft.client.resources.ResourcePackRepository; import net.minecraft.entity.EntityLivingBase; import net.minecraft.inventory.EntityEquipmentSlot; import net.minecraft.inventory.EntityEquipmentSlot.Type; @@ -22,17 +22,10 @@ import net.minecraft.item.ItemArmor; import net.minecraft.item.ItemArmor.ArmorMaterial; import net.minecraft.item.ItemStack; import net.minecraft.util.ResourceLocation; -import net.minecraft.util.Tuple; - -import java.io.IOException; -import java.util.Map; - -import javax.annotation.Nullable; public class LayerPonyArmor extends AbstractPonyLayer { - private static final Map HUMAN_ARMOUR = Maps.newHashMap(); - private static final Map PONY_ARMOUR = Maps.newHashMap(); + private static final IArmorTextureResolver textures = new DefaultPonyArmorTextureResolver<>(); private ModelWrapper pony; @@ -65,17 +58,20 @@ public class LayerPonyArmor extends AbstractPonyLaye armour.setRotationAngles(move, swing, ticks, headYaw, headPitch, scale, entity); armour.synchroniseLegs(pony.getBody()); - Tuple armourTexture = getArmorTexture(entity, itemstack, armorSlot, layer, null); + @SuppressWarnings("unchecked") + IArmorTextureResolver resolver = armour instanceof IArmorTextureResolver ? (IArmorTextureResolver)armour : (IArmorTextureResolver)textures; - getRenderer().bindTexture(armourTexture.getFirst()); + ResourceLocation armourTexture = resolver.getArmorTexture(entity, itemstack, armorSlot, layer, null); + + getRenderer().bindTexture(armourTexture); ItemArmor itemarmor = (ItemArmor) itemstack.getItem(); if (itemarmor.getArmorMaterial() == ArmorMaterial.LEATHER) { Color.glColor(itemarmor.getColor(itemstack), 1); armour.render(entity, move, swing, ticks, headYaw, headPitch, scale); - armourTexture = getArmorTexture(entity, itemstack, armorSlot, layer, "overlay"); - getRenderer().bindTexture(armourTexture.getFirst()); + armourTexture = resolver.getArmorTexture(entity, itemstack, armorSlot, layer, "overlay"); + getRenderer().bindTexture(armourTexture); } GlStateManager.color(1, 1, 1, 1); @@ -88,69 +84,6 @@ public class LayerPonyArmor extends AbstractPonyLaye } } - private Tuple getArmorTexture(T entity, ItemStack itemstack, EntityEquipmentSlot slot, ArmorLayer layer, @Nullable String type) { - ItemArmor item = (ItemArmor) itemstack.getItem(); - String texture = item.getArmorMaterial().getName(); - - String domain = "minecraft"; - - int idx = texture.indexOf(':'); - if (idx > -1) { - domain = texture.substring(0, idx); - texture = texture.substring(idx + 1); - } - - type = type == null ? "" : String.format("_%s", type); - - String ponyRes = String.format("%s:textures/models/armor/%s_layer_%s%s.png", domain, texture, layer.name().toLowerCase(), type); - String oldPonyRes = String.format("%s:textures/models/armor/%s_layer_%d%s.png", domain, texture, layer == ArmorLayer.INNER ? 2 : 1, type); - - ResourceLocation human = getArmorTexture(entity, itemstack, ponyRes, slot, type); - ResourceLocation oldPony = ponifyResource(getArmorTexture(entity, itemstack, oldPonyRes, slot, type)); - ResourceLocation pony = ponifyResource(human); - - // check resource packs for either texture. - for (ResourcePackRepository.Entry entry : Minecraft.getMinecraft().getResourcePackRepository().getRepositoryEntries()) { - if (entry.getResourcePack().resourceExists(pony)) { - // ponies are more important - return new Tuple<>(pony, true); - } else if (entry.getResourcePack().resourceExists(oldPony)) { - return new Tuple<>(oldPony, true); - } else if (entry.getResourcePack().resourceExists(human)) { - // but I guess I'll take a human - return new Tuple<>(human, false); - } - } - - // the default pack - try { - Minecraft.getMinecraft().getResourceManager().getResource(pony); - return new Tuple<>(pony, true); - } catch (IOException e) { - try { - Minecraft.getMinecraft().getResourceManager().getResource(oldPony); - return new Tuple<>(oldPony, true); - } catch (IOException r) { - return new Tuple<>(human, false); - } - } - } - - private static ResourceLocation ponifyResource(ResourceLocation human) { - return PONY_ARMOUR.computeIfAbsent(human, key -> { - String domain = human.getNamespace(); - if ("minecraft".equals(domain)) { - domain = "minelittlepony"; // it's a vanilla armor. I provide these. - } - String path = human.getPath().replace(".png", "_pony.png"); - return new ResourceLocation(domain, path); - }); - } - - private static ResourceLocation getArmorTexture(EntityLivingBase entity, ItemStack item, String def, EntityEquipmentSlot slot, @Nullable String type) { - return HUMAN_ARMOUR.computeIfAbsent(ForgeProxy.getArmorTexture(entity, item, def, slot, type), ResourceLocation::new); - } - private static ModelPonyArmor getArmorModel(EntityLivingBase entity, ItemStack itemstack, EntityEquipmentSlot slot, ArmorLayer layer, ModelPonyArmor def) { ModelBase model = ForgeProxy.getArmorModel(entity, itemstack, slot, def); @@ -164,5 +97,4 @@ public class LayerPonyArmor extends AbstractPonyLaye return def; } - }