2016-11-17 05:45:04 +01:00
|
|
|
package com.minelittlepony;
|
2015-08-02 00:36:33 +02:00
|
|
|
|
2017-06-16 07:41:36 +02:00
|
|
|
import com.google.common.base.MoreObjects;
|
2018-04-24 14:55:32 +02:00
|
|
|
import com.minelittlepony.ducks.IDownloadImageData;
|
2016-11-17 05:45:04 +01:00
|
|
|
import com.minelittlepony.model.PlayerModel;
|
2016-08-25 07:36:00 +02:00
|
|
|
import com.voxelmodpack.hdskins.DynamicTextureImage;
|
2016-12-28 00:54:23 +01:00
|
|
|
import com.voxelmodpack.hdskins.ThreadDownloadImageETag;
|
2015-08-02 00:36:33 +02:00
|
|
|
import net.minecraft.client.Minecraft;
|
|
|
|
import net.minecraft.client.renderer.texture.ITextureObject;
|
2016-08-25 07:36:00 +02:00
|
|
|
import net.minecraft.client.renderer.texture.TextureUtil;
|
|
|
|
import net.minecraft.client.resources.IResource;
|
2015-12-09 04:14:42 +01:00
|
|
|
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 java.awt.image.BufferedImage;
|
2017-06-16 07:41:36 +02:00
|
|
|
import java.io.FileNotFoundException;
|
2016-11-25 05:40:19 +01:00
|
|
|
import java.io.IOException;
|
2017-06-13 05:55:50 +02:00
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
2017-06-16 07:41:36 +02:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
import javax.annotation.concurrent.Immutable;
|
2016-11-25 05:40:19 +01:00
|
|
|
|
2017-06-16 07:41:36 +02:00
|
|
|
@Immutable
|
2015-08-02 00:36:33 +02:00
|
|
|
public class Pony {
|
2015-11-17 06:17:35 +01:00
|
|
|
|
2017-06-13 05:55:50 +02:00
|
|
|
private static final AtomicInteger ponyCount = new AtomicInteger();
|
2018-04-24 14:55:32 +02:00
|
|
|
|
2017-06-13 05:55:50 +02:00
|
|
|
private final int ponyId = ponyCount.getAndIncrement();
|
2015-12-09 04:14:42 +01:00
|
|
|
|
2017-06-16 07:41:36 +02:00
|
|
|
private final ResourceLocation texture;
|
2018-04-24 14:55:32 +02:00
|
|
|
private final IPonyData metadata;
|
2015-08-02 00:36:33 +02:00
|
|
|
|
2018-04-24 14:55:32 +02:00
|
|
|
private final boolean smallArms;
|
2015-08-02 00:36:33 +02:00
|
|
|
|
2018-04-24 14:55:32 +02:00
|
|
|
public Pony(ResourceLocation resource, boolean slim) {
|
|
|
|
this.texture = resource;
|
|
|
|
this.metadata = this.checkSkin(this.texture);
|
|
|
|
this.smallArms = slim;
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
|
|
|
|
2018-04-24 14:55:32 +02:00
|
|
|
private IPonyData checkSkin(ResourceLocation textureResourceLocation) {
|
|
|
|
IPonyData data = checkPonyMeta(textureResourceLocation);
|
|
|
|
if (data != null) return data;
|
2017-06-16 07:41:36 +02:00
|
|
|
|
2018-04-24 14:55:32 +02:00
|
|
|
BufferedImage skinImage = this.getBufferedImage(textureResourceLocation);
|
|
|
|
return this.checkSkin(skinImage);
|
2017-06-13 05:55:50 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 07:41:36 +02:00
|
|
|
@Nullable
|
2018-04-24 14:55:32 +02:00
|
|
|
private IPonyData checkPonyMeta(ResourceLocation location) {
|
2017-06-13 05:55:50 +02:00
|
|
|
try {
|
|
|
|
IResource res = Minecraft.getMinecraft().getResourceManager().getResource(location);
|
|
|
|
if (res.hasMetadata()) {
|
|
|
|
PonyData data = res.getMetadata(PonyDataSerialzier.NAME);
|
|
|
|
if (data != null) {
|
2017-06-16 07:41:36 +02:00
|
|
|
return data;
|
2017-06-13 05:55:50 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-16 07:41:36 +02:00
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
// Ignore uploaded texture
|
2017-06-13 05:55:50 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
MineLittlePony.logger.warn("Unable to read {} metadata", location, e);
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
2017-06-16 07:41:36 +02:00
|
|
|
return null;
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
|
|
|
|
2017-06-13 05:55:50 +02:00
|
|
|
@Nullable
|
2018-04-24 14:55:32 +02:00
|
|
|
private BufferedImage getBufferedImage(@Nonnull ResourceLocation resource) {
|
2015-08-02 00:36:33 +02:00
|
|
|
try {
|
2018-04-24 14:55:32 +02:00
|
|
|
IResource skin = Minecraft.getMinecraft().getResourceManager().getResource(resource);
|
|
|
|
BufferedImage skinImage = TextureUtil.readBufferedImage(skin.getInputStream());
|
|
|
|
MineLittlePony.logger.debug("Obtained skin from resource location {}", resource);
|
2015-08-02 00:36:33 +02:00
|
|
|
|
2018-04-24 14:55:32 +02:00
|
|
|
return skinImage;
|
|
|
|
} catch (IOException ignored) { }
|
2015-09-02 04:18:45 +02:00
|
|
|
|
2018-04-24 14:55:32 +02:00
|
|
|
try {
|
|
|
|
ITextureObject e2 = Minecraft.getMinecraft().getTextureManager().getTexture(resource);
|
|
|
|
|
|
|
|
if (e2 instanceof IDownloadImageData) {
|
|
|
|
return ((IDownloadImageData) e2).getBufferedImage();
|
|
|
|
} else if (e2 instanceof ThreadDownloadImageETag) {
|
|
|
|
return ((ThreadDownloadImageETag) e2).getBufferedImage();
|
|
|
|
} else if (e2 instanceof DynamicTextureImage) {
|
|
|
|
return ((DynamicTextureImage) e2).getImage();
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
2018-04-24 14:55:32 +02:00
|
|
|
} catch (Exception ignored) { }
|
2015-08-02 00:36:33 +02:00
|
|
|
|
2018-04-24 14:55:32 +02:00
|
|
|
return null;
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
|
|
|
|
2018-04-24 14:55:32 +02:00
|
|
|
private IPonyData checkSkin(BufferedImage bufferedimage) {
|
|
|
|
if (bufferedimage == null) return new PonyData();
|
2017-06-16 07:41:36 +02:00
|
|
|
MineLittlePony.logger.debug("\tStart skin check for pony #{} with image {}.", this.ponyId, bufferedimage);
|
|
|
|
return PonyData.parse(bufferedimage);
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
|
|
|
|
2015-12-09 04:14:42 +01:00
|
|
|
public boolean isPegasusFlying(EntityPlayer player) {
|
2016-11-25 05:40:19 +01:00
|
|
|
//noinspection SimplifiableIfStatement
|
2018-04-24 14:55:32 +02:00
|
|
|
if (!getRace(false).hasWings()) return false;
|
|
|
|
|
2016-05-10 21:10:29 +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
|
|
|
|
2018-04-24 14:55:32 +02:00
|
|
|
public PlayerModel getModel(boolean ignorePony) {
|
|
|
|
return getRace(ignorePony).getModel().getModel(smallArms);
|
|
|
|
}
|
2015-08-02 00:36:33 +02:00
|
|
|
|
2018-04-24 14:55:32 +02:00
|
|
|
public PonyRace getRace(boolean ignorePony) {
|
|
|
|
return metadata.getRace().getEffectiveRace(MineLittlePony.getConfig().getPonyLevel(ignorePony));
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 07:41:36 +02:00
|
|
|
public ResourceLocation getTexture() {
|
2018-04-24 14:55:32 +02:00
|
|
|
return texture;
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
|
|
|
|
2018-04-24 14:55:32 +02:00
|
|
|
public IPonyData getMetadata() {
|
2017-06-16 07:41:36 +02:00
|
|
|
return metadata;
|
|
|
|
}
|
2017-06-16 07:41:36 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2018-04-24 14:55:32 +02:00
|
|
|
return MoreObjects.toStringHelper(this).add("texture", texture).add("metadata", metadata).toString();
|
2017-06-16 07:41:36 +02:00
|
|
|
}
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|