MineLittlePony/src/main/java/com/minelittlepony/Pony.java

137 lines
5.1 KiB
Java
Raw Normal View History

2016-11-17 05:45:04 +01:00
package com.minelittlepony;
2015-08-02 00:36:33 +02:00
2016-11-17 05:45:04 +01:00
import com.minelittlepony.model.PMAPI;
import com.minelittlepony.model.PlayerModel;
import com.minelittlepony.util.PonyFields;
import com.voxelmodpack.hdskins.DynamicTextureImage;
import com.voxelmodpack.hdskins.ThreadDownloadImageETag;
2015-08-02 00:36:33 +02:00
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.AbstractClientPlayer;
import net.minecraft.client.renderer.ThreadDownloadImageData;
import net.minecraft.client.renderer.texture.ITextureObject;
import net.minecraft.client.renderer.texture.TextureUtil;
import net.minecraft.client.resources.IResource;
import net.minecraft.entity.player.EntityPlayer;
2015-08-02 00:36:33 +02:00
import net.minecraft.util.ResourceLocation;
2016-11-25 05:40:19 +01:00
import javax.annotation.Nonnull;
import java.awt.image.BufferedImage;
import java.io.IOException;
2015-08-02 00:36:33 +02:00
public class Pony {
private static PonyConfig config = MineLittlePony.getConfig();
2015-08-02 00:36:33 +02:00
private static int ponyCount = 0;
2015-08-15 05:25:40 +02:00
private final int ponyId = ponyCount++;
public ResourceLocation textureResourceLocation;
public PonyData metadata = new PonyData();
2015-08-02 00:36:33 +02:00
private int skinCheckCount;
private boolean skinChecked;
2016-11-17 05:45:04 +01:00
public Pony(@Nonnull AbstractClientPlayer player) {
2015-08-02 00:36:33 +02:00
this.textureResourceLocation = player.getLocationSkin();
2016-11-25 05:40:19 +01:00
MineLittlePony.logger.debug("+ Initialising new pony #%d for player %s (%s) with resource location %s.", this.ponyId,
player.getName(), player.getUniqueID(), this.textureResourceLocation);
2015-08-02 00:36:33 +02:00
this.checkSkin(this.textureResourceLocation);
}
2016-11-17 05:45:04 +01:00
public Pony(@Nonnull ResourceLocation aTextureResourceLocation) {
2015-08-02 00:36:33 +02:00
this.textureResourceLocation = aTextureResourceLocation;
2016-11-25 05:40:19 +01:00
MineLittlePony.logger.debug("+ Initialising new pony #%d with resource location %s.", this.ponyId, this.textureResourceLocation);
2015-08-02 00:36:33 +02:00
this.checkSkin(this.textureResourceLocation);
}
public void invalidateSkinCheck() {
this.skinChecked = false;
metadata = new PonyData();
2015-08-02 00:36:33 +02:00
}
public void checkSkin() {
if (!this.skinChecked) {
this.checkSkin(this.textureResourceLocation);
}
}
public void checkSkin(ResourceLocation textureResourceLocation) {
BufferedImage skinImage = this.getBufferedImage(textureResourceLocation);
if (skinImage != null) {
this.checkSkin(skinImage);
}
}
2016-11-17 05:45:04 +01:00
public BufferedImage getBufferedImage(@Nonnull ResourceLocation textureResourceLocation) {
2015-08-02 00:36:33 +02:00
BufferedImage skinImage = null;
try {
IResource skin = Minecraft.getMinecraft().getResourceManager().getResource(textureResourceLocation);
skinImage = TextureUtil.readBufferedImage(skin.getInputStream());
2016-11-25 05:40:19 +01:00
MineLittlePony.logger.debug("Obtained skin from resource location %s", textureResourceLocation);
// this.checkSkin(skinImage);
2016-11-25 05:40:19 +01:00
} catch (IOException e) {
2015-08-02 00:36:33 +02:00
try {
ITextureObject e2 = Minecraft.getMinecraft().getTextureManager().getTexture(textureResourceLocation);
if (e2 instanceof ThreadDownloadImageData) {
2016-03-01 05:41:02 +01:00
2016-01-26 09:16:11 +01:00
skinImage = PonyFields.downloadedImage.get((ThreadDownloadImageData) e2);
2015-08-02 00:36:33 +02:00
if (skinImage != null) {
2016-11-25 05:40:19 +01:00
MineLittlePony.logger.debug("Successfully reflected downloadedImage from texture object", e);
// this.checkSkin(skinImage);
2015-08-02 00:36:33 +02:00
}
} else if (e2 instanceof ThreadDownloadImageETag) {
skinImage = ((ThreadDownloadImageETag) e2).getBufferedImage();
} else if (e2 instanceof DynamicTextureImage) {
skinImage = ((DynamicTextureImage) e2).getImage();
2015-08-02 00:36:33 +02:00
}
2016-11-25 05:40:19 +01:00
} catch (Exception ignored) {
2015-09-02 04:18:45 +02:00
2015-08-02 00:36:33 +02:00
}
}
return skinImage;
}
public void checkSkin(BufferedImage bufferedimage) {
2016-11-25 05:40:19 +01:00
MineLittlePony.logger.debug("\tStart skin check #%d for pony #%d with image %s.", ++this.skinCheckCount, this.ponyId);
metadata = PonyData.parse(bufferedimage);
2015-08-02 00:36:33 +02:00
this.skinChecked = true;
}
public boolean isPegasusFlying(EntityPlayer player) {
2016-11-25 05:40:19 +01:00
//noinspection SimplifiableIfStatement
if (this.metadata.getRace() == null || !this.metadata.getRace().hasWings()) {
2015-08-11 20:31:03 +02:00
return false;
2015-08-02 00:36:33 +02:00
}
return player.capabilities.isFlying || !(player.onGround || player.isRiding() || player.isOnLadder() || player.isInWater() || player.isElytraFlying());
2015-12-12 05:17:04 +01:00
}
2015-08-02 00:36:33 +02:00
public PlayerModel getModel(boolean ignorePony, boolean smallArms) {
2015-08-02 00:36:33 +02:00
boolean is_a_pony = false;
2016-01-26 09:16:11 +01:00
switch (ignorePony ? PonyLevel.BOTH : config.getPonyLevel()) {
2016-11-25 05:40:19 +01:00
case HUMANS:
is_a_pony = false;
break;
case BOTH:
is_a_pony = metadata.getRace() != null;
break;
case PONIES:
is_a_pony = true;
2015-08-02 00:36:33 +02:00
}
PlayerModel model;
if (is_a_pony) {
model = smallArms ? PMAPI.ponySmall : PMAPI.pony;
2015-08-02 00:36:33 +02:00
} else {
model = smallArms ? PMAPI.humanSmall : PMAPI.human;
2015-08-02 00:36:33 +02:00
}
return model;
}
public ResourceLocation getTextureResourceLocation() {
return this.textureResourceLocation;
}
}