2015-11-17 06:09:04 +01:00
|
|
|
package com.brohoof.minelittlepony;
|
2015-08-02 00:36:33 +02:00
|
|
|
|
|
|
|
import java.awt.image.BufferedImage;
|
2016-01-26 09:16:11 +01:00
|
|
|
import java.io.IOException;
|
2016-05-22 05:56:50 +02:00
|
|
|
import java.util.List;
|
2015-08-02 00:36:33 +02:00
|
|
|
|
2016-11-17 05:45:04 +01:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
2015-11-17 06:09:04 +01:00
|
|
|
import com.brohoof.minelittlepony.model.PMAPI;
|
|
|
|
import com.brohoof.minelittlepony.model.PlayerModel;
|
|
|
|
import com.brohoof.minelittlepony.util.MineLPLogger;
|
2016-01-26 09:16:11 +01:00
|
|
|
import com.brohoof.minelittlepony.util.PonyFields;
|
2016-05-22 05:56:50 +02:00
|
|
|
import com.google.common.base.Function;
|
|
|
|
import com.google.common.base.Predicate;
|
|
|
|
import com.google.common.base.Splitter;
|
|
|
|
import com.google.common.collect.Lists;
|
2016-05-20 22:22:07 +02:00
|
|
|
import com.mojang.authlib.GameProfile;
|
|
|
|
import com.mumfrey.webprefs.WebPreferencesManager;
|
|
|
|
import com.mumfrey.webprefs.interfaces.IWebPreferences;
|
2016-08-25 07:36:00 +02:00
|
|
|
import com.voxelmodpack.hdskins.DynamicTextureImage;
|
2016-05-20 22:22:07 +02:00
|
|
|
import com.voxelmodpack.hdskins.HDSkinManager;
|
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;
|
|
|
|
|
|
|
|
public class Pony {
|
2015-11-17 06:17:35 +01:00
|
|
|
|
|
|
|
private static PonyConfig config = MineLittlePony.getConfig();
|
2015-12-09 04:14:42 +01:00
|
|
|
|
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++;
|
2015-12-09 04:14:42 +01:00
|
|
|
|
2016-05-20 22:22:07 +02:00
|
|
|
public GameProfile profile;
|
2015-12-09 04:14:42 +01:00
|
|
|
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) {
|
2016-05-20 22:22:07 +02:00
|
|
|
this.profile = player.getGameProfile();
|
2015-08-02 00:36:33 +02:00
|
|
|
this.textureResourceLocation = player.getLocationSkin();
|
2015-11-17 06:17:35 +01:00
|
|
|
MineLPLogger.debug("+ Initialising new pony #%d for player %s (%s) with resource location %s.", this.ponyId,
|
2016-03-01 06:33:09 +01:00
|
|
|
player.getName(), player.getUniqueID(), this.textureResourceLocation);
|
2015-08-02 00:36:33 +02:00
|
|
|
this.checkSkin(this.textureResourceLocation);
|
2016-05-20 22:22:07 +02:00
|
|
|
this.checkMeta(profile);
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
|
|
|
|
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-05-20 22:22:07 +02:00
|
|
|
MineLPLogger.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;
|
2016-08-25 07:36:00 +02:00
|
|
|
metadata = new PonyData();
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void checkSkin() {
|
|
|
|
if (!this.skinChecked) {
|
|
|
|
this.checkSkin(this.textureResourceLocation);
|
2016-05-20 22:22:07 +02:00
|
|
|
this.checkMeta(this.profile);
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void checkSkin(ResourceLocation textureResourceLocation) {
|
|
|
|
BufferedImage skinImage = this.getBufferedImage(textureResourceLocation);
|
|
|
|
if (skinImage != null) {
|
|
|
|
this.checkSkin(skinImage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-20 22:22:07 +02:00
|
|
|
private void checkMeta(GameProfile profile) {
|
2016-05-22 05:56:50 +02:00
|
|
|
final IWebPreferences prefs = WebPreferencesManager.getDefault().getPreferences(profile);
|
2016-05-20 06:03:40 +02:00
|
|
|
|
|
|
|
if (prefs == null)
|
|
|
|
return;
|
|
|
|
|
2016-05-22 05:56:50 +02:00
|
|
|
final List<String> keys;
|
|
|
|
if (prefs.has(HDSkinManager.METADATA_KEY)) {
|
|
|
|
String list = prefs.get(HDSkinManager.METADATA_KEY);
|
|
|
|
keys = Splitter.on(',').splitToList(list);
|
|
|
|
} else {
|
|
|
|
keys = Lists.newArrayList();
|
|
|
|
}
|
2016-05-20 22:22:07 +02:00
|
|
|
|
2016-05-22 05:56:50 +02:00
|
|
|
// >inb4 java 8
|
|
|
|
// checkMeta(Predicates.and(keys::contains, prefs::has), prefs::get);
|
|
|
|
checkMeta(new Predicate<String>() {
|
|
|
|
@Override
|
|
|
|
public boolean apply(String key) {
|
|
|
|
return keys.contains(key) && prefs.has(key);
|
|
|
|
}
|
|
|
|
}, new Function<String, String>() {
|
|
|
|
@Override
|
|
|
|
public String apply(String key) {
|
|
|
|
return prefs.get(key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-05-20 06:03:40 +02:00
|
|
|
|
2016-05-22 05:56:50 +02:00
|
|
|
private void checkMeta(Predicate<String> has, Function<String, String> prefs) {
|
|
|
|
|
|
|
|
if (has.apply(MineLittlePony.MLP_RACE))
|
|
|
|
metadata.setRace(PonyRace.valueOf(prefs.apply(MineLittlePony.MLP_RACE)));
|
|
|
|
if (has.apply(MineLittlePony.MLP_TAIL))
|
|
|
|
metadata.setTail(TailLengths.valueOf(prefs.apply(MineLittlePony.MLP_TAIL)));
|
|
|
|
if (has.apply(MineLittlePony.MLP_GENDER))
|
|
|
|
metadata.setGender(PonyGender.valueOf(prefs.apply(MineLittlePony.MLP_GENDER)));
|
|
|
|
if (has.apply(MineLittlePony.MLP_SIZE))
|
|
|
|
metadata.setSize(PonySize.valueOf(prefs.apply(MineLittlePony.MLP_SIZE)));
|
|
|
|
if (has.apply(MineLittlePony.MLP_MAGIC))
|
|
|
|
metadata.setGlowColor(Integer.parseInt(prefs.apply(MineLittlePony.MLP_MAGIC)));
|
2016-05-20 22:22:07 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
2016-08-25 07:36:00 +02:00
|
|
|
IResource skin = Minecraft.getMinecraft().getResourceManager().getResource(textureResourceLocation);
|
|
|
|
skinImage = TextureUtil.readBufferedImage(skin.getInputStream());
|
2015-08-02 00:36:33 +02:00
|
|
|
MineLPLogger.debug("Obtained skin from resource location %s", textureResourceLocation);
|
2015-12-09 07:01:34 +01:00
|
|
|
// this.checkSkin(skinImage);
|
2016-01-26 09:16:11 +01:00
|
|
|
} catch (IOException var6) {
|
2015-08-02 00:36:33 +02:00
|
|
|
Exception e = var6;
|
|
|
|
|
|
|
|
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) {
|
|
|
|
MineLPLogger.debug(e, "Successfully reflected downloadedImage from texture object");
|
2015-12-09 07:01:34 +01:00
|
|
|
// this.checkSkin(skinImage);
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
} catch (Exception var5) {
|
2015-09-02 04:18:45 +02:00
|
|
|
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return skinImage;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void checkSkin(BufferedImage bufferedimage) {
|
|
|
|
MineLPLogger.debug("\tStart skin check #%d for pony #%d with image %s.", ++this.skinCheckCount, this.ponyId);
|
2015-12-09 04:14:42 +01:00
|
|
|
metadata = PonyData.parse(bufferedimage);
|
2015-08-02 00:36:33 +02:00
|
|
|
this.skinChecked = true;
|
|
|
|
}
|
|
|
|
|
2015-12-09 04:14:42 +01:00
|
|
|
public boolean isPegasusFlying(EntityPlayer player) {
|
|
|
|
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
|
|
|
}
|
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;
|
2016-01-26 09:16:11 +01:00
|
|
|
switch (ignorePony ? PonyLevel.BOTH : config.getPonyLevel()) {
|
2015-08-02 00:36:33 +02:00
|
|
|
case HUMANS:
|
|
|
|
is_a_pony = false;
|
|
|
|
break;
|
2015-11-17 06:17:35 +01:00
|
|
|
case BOTH:
|
2015-12-09 04:14:42 +01:00
|
|
|
is_a_pony = metadata.getRace() != null;
|
2015-08-02 00:36:33 +02:00
|
|
|
break;
|
|
|
|
case PONIES:
|
|
|
|
is_a_pony = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ResourceLocation getTextureResourceLocation() {
|
|
|
|
return this.textureResourceLocation;
|
|
|
|
}
|
|
|
|
|
2016-05-20 22:22:07 +02:00
|
|
|
public GameProfile getProfile() {
|
|
|
|
return profile;
|
|
|
|
}
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|