diff --git a/src/main/java/com/minelittlepony/Pony.java b/src/main/java/com/minelittlepony/Pony.java index 83b9120b..ace6d2c7 100644 --- a/src/main/java/com/minelittlepony/Pony.java +++ b/src/main/java/com/minelittlepony/Pony.java @@ -14,71 +14,71 @@ import net.minecraft.client.resources.IResource; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.ResourceLocation; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; import java.awt.image.BufferedImage; +import java.io.FileNotFoundException; import java.io.IOException; import java.util.concurrent.atomic.AtomicInteger; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.annotation.concurrent.Immutable; +@Immutable public class Pony { private static final AtomicInteger ponyCount = new AtomicInteger(); private final int ponyId = ponyCount.getAndIncrement(); - private ResourceLocation textureResourceLocation; - public PonyData metadata = new PonyData(); - - private int skinCheckCount; - private boolean skinChecked; + private final ResourceLocation texture; + private final PonyData metadata; public Pony(AbstractClientPlayer player) { - this.textureResourceLocation = player.getLocationSkin(); - MineLittlePony.logger.debug("+ Initialising new pony #{} for player {} ({}) with resource location {}.", this.ponyId, - player.getName(), player.getUniqueID(), this.textureResourceLocation); - this.checkSkin(this.textureResourceLocation); + this.texture = player.getLocationSkin(); + this.metadata = this.checkSkin(this.texture); + + MineLittlePony.logger.debug("+ Initialising new pony #{} for player {} ({}) with resource location {}.", + this.ponyId, player.getName(), player.getUniqueID(), this.texture); } - public Pony(ResourceLocation aTextureResourceLocation) { - this.textureResourceLocation = aTextureResourceLocation; - MineLittlePony.logger.debug("+ Initialising new pony #{} with resource location {}.", this.ponyId, this.textureResourceLocation); - this.checkSkin(this.textureResourceLocation); + public Pony(ResourceLocation resourceLocation) { + this(resourceLocation, null); } - public void invalidateSkinCheck() { - this.skinChecked = false; - metadata = new PonyData(); + public Pony(ResourceLocation aTextureResourceLocation, @Nullable PonyData meta) { + this.texture = aTextureResourceLocation; + this.metadata = meta != null ? meta : this.checkSkin(this.texture); + + MineLittlePony.logger.debug("+ Initialising new pony #{} with resource location {}.", this.ponyId, this.texture); } - public void checkSkin() { - if (!this.skinChecked) { - this.checkSkin(this.textureResourceLocation); - } - } - - private void checkSkin(ResourceLocation textureResourceLocation) { - if(!checkPonyMeta(textureResourceLocation)) { + private PonyData checkSkin(ResourceLocation textureResourceLocation) { + PonyData data = checkPonyMeta(textureResourceLocation); + if (data == null) { BufferedImage skinImage = this.getBufferedImage(textureResourceLocation); if (skinImage != null) { - this.checkSkin(skinImage); + data = this.checkSkin(skinImage); + } else { + data = new PonyData(); } } + return data; } - private boolean checkPonyMeta(ResourceLocation location) { + @Nullable + private PonyData checkPonyMeta(ResourceLocation location) { try { IResource res = Minecraft.getMinecraft().getResourceManager().getResource(location); if (res.hasMetadata()) { PonyData data = res.getMetadata(PonyDataSerialzier.NAME); if (data != null) { - metadata = data; - this.skinChecked = true; + return data; } - return true; } + } catch (FileNotFoundException e) { + // Ignore uploaded texture } catch (IOException e) { MineLittlePony.logger.warn("Unable to read {} metadata", location, e); } - return false; + return null; } @Nullable @@ -113,10 +113,9 @@ public class Pony { return skinImage; } - public void checkSkin(BufferedImage bufferedimage) { - MineLittlePony.logger.debug("\tStart skin check #{} for pony #{} with image {}.", ++this.skinCheckCount, this.ponyId, bufferedimage); - metadata = PonyData.parse(bufferedimage); - this.skinChecked = true; + private PonyData checkSkin(BufferedImage bufferedimage) { + MineLittlePony.logger.debug("\tStart skin check for pony #{} with image {}.", this.ponyId, bufferedimage); + return PonyData.parse(bufferedimage); } public boolean isPegasusFlying(EntityPlayer player) { @@ -149,8 +148,11 @@ public class Pony { return model; } - public ResourceLocation getTextureResourceLocation() { - return this.textureResourceLocation; + public ResourceLocation getTexture() { + return this.texture; } + public PonyData getMetadata() { + return metadata; + } } diff --git a/src/main/java/com/minelittlepony/PonyData.java b/src/main/java/com/minelittlepony/PonyData.java index ac566463..20b67047 100644 --- a/src/main/java/com/minelittlepony/PonyData.java +++ b/src/main/java/com/minelittlepony/PonyData.java @@ -4,7 +4,9 @@ import com.google.common.collect.ImmutableBiMap; import java.awt.image.BufferedImage; import java.util.Map; +import javax.annotation.concurrent.Immutable; +@Immutable public class PonyData implements IPonyData { private static final Map RACE_COLORS = ImmutableBiMap.builder() @@ -26,11 +28,11 @@ public class PonyData implements IPonyData { .put(0x534b76, PonySize.TALL) .build(); - private PonyRace race; - private TailLengths tailSize; - private PonyGender gender; - private PonySize size; - private int glowColor; + private final PonyRace race; + private final TailLengths tailSize; + private final PonyGender gender; + private final PonySize size; + private final int glowColor; public PonyData() { this(PonyRace.HUMAN, TailLengths.FULL, PonyGender.MARE, PonySize.NORMAL, 0x4444aa); @@ -48,34 +50,18 @@ public class PonyData implements IPonyData { return race; } - public void setRace(PonyRace race) { - this.race = race; - } - public TailLengths getTail() { return tailSize; } - public void setTail(TailLengths tailSize) { - this.tailSize = tailSize; - } - public PonyGender getGender() { return gender; } - public void setGender(PonyGender gender) { - this.gender = gender; - } - public PonySize getSize() { return MineLittlePony.getConfig().sizes ? size : PonySize.NORMAL; } - public void setSize(PonySize size) { - this.size = size; - } - public int getGlowColor() { return glowColor; } @@ -84,10 +70,6 @@ public class PonyData implements IPonyData { return this.race != null && this.race.hasHorn() && this.glowColor != 0; } - public void setGlowColor(int glowColor) { - this.glowColor = glowColor & 0xffffff; - } - static PonyData parse(BufferedImage image) { int racePx = TriggerPixels.RACE.readColor(image); PonyRace race = RACE_COLORS.getOrDefault(racePx, PonyRace.HUMAN); diff --git a/src/main/java/com/minelittlepony/PonyManager.java b/src/main/java/com/minelittlepony/PonyManager.java index ce1dff2e..ed340954 100644 --- a/src/main/java/com/minelittlepony/PonyManager.java +++ b/src/main/java/com/minelittlepony/PonyManager.java @@ -46,21 +46,25 @@ public class PonyManager implements IResourceManagerReloadListener { MineLittlePony.logger.info("Done initializing models."); } - public Pony getPonyFromResourceRegistry(ResourceLocation skinResourceLocation) { + public Pony getPony(ResourceLocation skinResourceLocation) { return this.poniesCache.computeIfAbsent(skinResourceLocation, Pony::new); } - public Pony getPonyFromResourceRegistry(AbstractClientPlayer player) { + public Pony getPony(AbstractClientPlayer player) { Pony myLittlePony = this.poniesCache.computeIfAbsent(player.getLocationSkin(), res -> new Pony(player)); - if (config.getPonyLevel() == PonyLevel.PONIES && myLittlePony.metadata.getRace() == PonyRace.HUMAN) { + if (config.getPonyLevel() == PonyLevel.PONIES && myLittlePony.getMetadata().getRace() == PonyRace.HUMAN) { myLittlePony = this.getPonyFromBackgroundResourceRegistry(player); } return myLittlePony; } + public Pony removePony(ResourceLocation location) { + return this.poniesCache.remove(location); + } + private ResourceLocation getBackgroundPonyResource(UUID id) { if (getNumberOfPonies() > 0) { int backgroundIndex = id.hashCode() % this.getNumberOfPonies(); diff --git a/src/main/java/com/minelittlepony/hdskins/gui/GuiSkinsMineLP.java b/src/main/java/com/minelittlepony/hdskins/gui/GuiSkinsMineLP.java index 468bb17f..c9b81071 100644 --- a/src/main/java/com/minelittlepony/hdskins/gui/GuiSkinsMineLP.java +++ b/src/main/java/com/minelittlepony/hdskins/gui/GuiSkinsMineLP.java @@ -25,20 +25,20 @@ public class GuiSkinsMineLP extends GuiSkins { @Override protected void onSetLocalSkin(BufferedImage skin, MinecraftProfileTexture.Type type) { MineLittlePony.logger.debug("Invalidating old local skin, checking updated local skin"); - ponyManager.getPonyFromResourceRegistry(this.localPlayer.getSkinTexture()).checkSkin(skin); + ponyManager.removePony(this.localPlayer.getSkinTexture()); } @Override protected void onSetRemoteSkin(MinecraftProfileTexture.Type type) { MineLittlePony.logger.debug("Invalidating old remote skin, checking updated remote skin"); - ponyManager.getPonyFromResourceRegistry(this.remotePlayer.getSkinTexture()).invalidateSkinCheck(); + ponyManager.removePony(this.remotePlayer.getSkinTexture()); } @Override public void onGuiClosed() { super.onGuiClosed(); - ponyManager.getPonyFromResourceRegistry(this.localPlayer.getSkinTexture()).invalidateSkinCheck(); - ponyManager.getPonyFromResourceRegistry(this.remotePlayer.getSkinTexture()).invalidateSkinCheck(); + ponyManager.removePony(this.localPlayer.getSkinTexture()); + ponyManager.removePony(this.remotePlayer.getSkinTexture()); } } diff --git a/src/main/java/com/minelittlepony/hdskins/gui/RenderPonyModel.java b/src/main/java/com/minelittlepony/hdskins/gui/RenderPonyModel.java index 224123cf..88b9ab81 100644 --- a/src/main/java/com/minelittlepony/hdskins/gui/RenderPonyModel.java +++ b/src/main/java/com/minelittlepony/hdskins/gui/RenderPonyModel.java @@ -16,6 +16,7 @@ import net.minecraft.entity.EntityLivingBase; import net.minecraft.init.Items; import net.minecraft.inventory.EntityEquipmentSlot; import net.minecraft.item.ItemStack; +import net.minecraft.util.ResourceLocation; public class RenderPonyModel extends RenderPlayerModel { @@ -25,13 +26,15 @@ public class RenderPonyModel extends RenderPlayerModel { @Override public ModelPlayer getEntityModel(EntityPonyModel playermodel) { - Pony thePony = MineLittlePony.getInstance().getManager().getPonyFromResourceRegistry(this.getEntityTexture(playermodel)); - thePony.invalidateSkinCheck(); - thePony.checkSkin(); + ResourceLocation loc = this.getEntityTexture(playermodel); + if (loc == null) { + return super.getEntityModel(playermodel); + } + Pony thePony = MineLittlePony.getInstance().getManager().getPony(loc); // TODO small arms PlayerModel pm = thePony.getModel(true, false); - pm.apply(thePony.metadata); + pm.apply(thePony.getMetadata()); return pm.getModel(); } diff --git a/src/main/java/com/minelittlepony/mixin/MixinRenderPlayer.java b/src/main/java/com/minelittlepony/mixin/MixinRenderPlayer.java index 9cd2f55c..75b813bb 100644 --- a/src/main/java/com/minelittlepony/mixin/MixinRenderPlayer.java +++ b/src/main/java/com/minelittlepony/mixin/MixinRenderPlayer.java @@ -79,7 +79,7 @@ public abstract class MixinRenderPlayer extends RenderLivingBase extends Rende public RenderPonySkeleton(RenderManager rm) { super(rm, PMAPI.skeleton); + } + + @Override + protected void addLayers() { + super.addLayers(); this.addLayer(new LayerBipedArmor(this) { @Override protected void initArmor() { - this.modelLeggings = PMAPI.skeleton.getArmor().modelArmor; - this.modelArmor = PMAPI.skeleton.getArmor().modelArmorChestplate; + this.modelLeggings = getPony().getArmor().modelArmor; + this.modelArmor = getPony().getArmor().modelArmorChestplate; } }); }