aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/mondecitronne/homunculus/skin/PlayerSkin.java
blob: c6cbfdd0dce7f35f8f7e266c6cdd0883c2adb2ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.mondecitronne.homunculus.skin;

import java.util.Map;
import javax.annotation.Nullable;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.minecraft.MinecraftProfileTexture;
import com.mojang.authlib.minecraft.MinecraftProfileTexture.Type;
import com.mondecitronne.homunculus.GameProfileFetcher;
import net.minecraft.client.Minecraft;
import net.minecraft.util.ResourceLocation;

public class PlayerSkin extends Skin {
	private final GameProfile profile;
	private final GameProfileFetcher profileFetcher;
	private final boolean isRemote;
	boolean profileTexturesLoading;

	public PlayerSkin(GameProfile profile, boolean isRemote) {
		this.profile = profile;
		profileFetcher = GameProfileFetcher.fetchProfile(profile);
		this.isRemote = isRemote;
		profileTexturesLoading = false;
	}

	@Override
	public boolean isLoaded() {
		if (this.isRemote) {
			return getTexture() != null && getModelType() != null;
		} else {
			return profileFetcher.getGameProfile() != null;
		}
	}

	public GameProfile getPlayerProfile() {
		GameProfile fetchProfile = profileFetcher.getGameProfile();
		if (fetchProfile != null) {
			return fetchProfile;
		} else {
			return profile;
		}
	}

	protected MinecraftProfileTexture getProfileTexture() {
		assert (this.isRemote);
		GameProfile playerProfile = profileFetcher.getGameProfile();
		if (playerProfile != null) {
			Minecraft minecraft = Minecraft.getMinecraft();
			if (!profileTexturesLoading) {
				minecraft.getSkinManager().loadProfileTextures(playerProfile, null, false);
				profileTexturesLoading = true;
			}
			Map<Type, MinecraftProfileTexture> map = minecraft.getSkinManager().loadSkinFromCache(playerProfile);
			if (map != null) {
				return map.get(Type.SKIN);
			} else {
				return null;
			}
		} else {
			return null;
		}
	}

	@Override
	public String getModelType() {
		assert (this.isRemote);
		if (getTexture() != null) {
			MinecraftProfileTexture tex = getProfileTexture();
			if (tex != null) {
				return tex.getMetadata("model") != null ? tex.getMetadata("model") : "default";
			}
		}
		return null;
	}

	@Override
	@Nullable
	public ResourceLocation getTexture() {
		assert (this.isRemote);
		MinecraftProfileTexture tex = getProfileTexture();
		if (tex != null) {
			ResourceLocation location = Minecraft.getMinecraft().getSkinManager().loadSkin(tex, Type.SKIN);
			if (location != null) {
				return location;
			}
		}
		return null;
	}
}