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;
|
2016-11-17 05:45:04 +01:00
|
|
|
import com.minelittlepony.model.PMAPI;
|
|
|
|
import com.minelittlepony.model.PlayerModel;
|
|
|
|
import com.minelittlepony.util.PonyFields;
|
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.entity.AbstractClientPlayer;
|
|
|
|
import net.minecraft.client.renderer.ThreadDownloadImageData;
|
|
|
|
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();
|
|
|
|
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;
|
|
|
|
private final PonyData metadata;
|
2015-08-02 00:36:33 +02:00
|
|
|
|
2017-06-13 05:55:50 +02:00
|
|
|
public Pony(AbstractClientPlayer player) {
|
2017-06-16 07:41:36 +02:00
|
|
|
this.texture = player.getLocationSkin();
|
|
|
|
this.metadata = this.checkSkin(this.texture);
|
2015-08-02 00:36:33 +02:00
|
|
|
|
2017-06-16 07:41:36 +02:00
|
|
|
MineLittlePony.logger.debug("+ Initialising new pony #{} for player {} ({}) with resource location {}.",
|
|
|
|
this.ponyId, player.getName(), player.getUniqueID(), this.texture);
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 07:41:36 +02:00
|
|
|
public Pony(ResourceLocation resourceLocation) {
|
|
|
|
this(resourceLocation, null);
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 07:41:36 +02:00
|
|
|
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);
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 07:41:36 +02:00
|
|
|
private PonyData checkSkin(ResourceLocation textureResourceLocation) {
|
|
|
|
PonyData data = checkPonyMeta(textureResourceLocation);
|
|
|
|
if (data == null) {
|
2017-06-13 05:55:50 +02:00
|
|
|
BufferedImage skinImage = this.getBufferedImage(textureResourceLocation);
|
|
|
|
if (skinImage != null) {
|
2017-06-16 07:41:36 +02:00
|
|
|
data = this.checkSkin(skinImage);
|
|
|
|
} else {
|
|
|
|
data = new PonyData();
|
2017-06-13 05:55:50 +02:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
@Nullable
|
|
|
|
private PonyData 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
|
|
|
|
private BufferedImage getBufferedImage(@Nonnull ResourceLocation textureResourceLocation) {
|
2015-08-02 00:36:33 +02:00
|
|
|
BufferedImage skinImage = null;
|
|
|
|
try {
|
2016-08-25 07:36:00 +02:00
|
|
|
IResource skin = Minecraft.getMinecraft().getResourceManager().getResource(textureResourceLocation);
|
|
|
|
skinImage = TextureUtil.readBufferedImage(skin.getInputStream());
|
2017-06-13 05:55:50 +02:00
|
|
|
MineLittlePony.logger.debug("Obtained skin from resource location {}", textureResourceLocation);
|
2015-12-09 07:01:34 +01:00
|
|
|
// 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);
|
2015-12-09 07:01:34 +01:00
|
|
|
// this.checkSkin(skinImage);
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
2016-12-28 00:54:23 +01:00
|
|
|
} else if (e2 instanceof ThreadDownloadImageETag) {
|
|
|
|
skinImage = ((ThreadDownloadImageETag) e2).getBufferedImage();
|
2016-08-25 07:36:00 +02:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
2017-06-16 07:41:36 +02:00
|
|
|
private PonyData checkSkin(BufferedImage bufferedimage) {
|
|
|
|
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
|
2017-06-13 05:55:50 +02:00
|
|
|
if (!this.metadata.getRace().hasWings()) {
|
2015-08-11 20:31:03 +02:00
|
|
|
return false;
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
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
|
|
|
|
2016-01-19 06:34:07 +01:00
|
|
|
public PlayerModel getModel(boolean ignorePony, boolean smallArms) {
|
2015-08-02 00:36:33 +02:00
|
|
|
boolean is_a_pony = false;
|
2017-06-13 05:55:50 +02:00
|
|
|
switch (ignorePony ? PonyLevel.BOTH : MineLittlePony.getConfig().getPonyLevel()) {
|
2016-11-25 05:40:19 +01:00
|
|
|
case HUMANS:
|
|
|
|
is_a_pony = false;
|
|
|
|
break;
|
|
|
|
case BOTH:
|
2017-06-13 05:55:50 +02:00
|
|
|
is_a_pony = metadata.getRace() != PonyRace.HUMAN;
|
2016-11-25 05:40:19 +01:00
|
|
|
break;
|
|
|
|
case PONIES:
|
|
|
|
is_a_pony = true;
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PlayerModel model;
|
|
|
|
if (is_a_pony) {
|
2016-01-19 06:34:07 +01:00
|
|
|
model = smallArms ? PMAPI.ponySmall : PMAPI.pony;
|
2015-08-02 00:36:33 +02:00
|
|
|
} else {
|
2016-01-19 06:34:07 +01:00
|
|
|
model = smallArms ? PMAPI.humanSmall : PMAPI.human;
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
|
2017-06-16 07:41:36 +02:00
|
|
|
public ResourceLocation getTexture() {
|
|
|
|
return this.texture;
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 07:41:36 +02:00
|
|
|
public PonyData getMetadata() {
|
|
|
|
return metadata;
|
|
|
|
}
|
2017-06-16 07:41:36 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return MoreObjects.toStringHelper(this)
|
|
|
|
.add("texture", texture)
|
|
|
|
.add("metadata", metadata)
|
|
|
|
.toString();
|
|
|
|
}
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|