aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/mondecitronne/homunculus/EntityHomunculus.java
blob: d070f08564e0bbd52a70b9644c4b60ced1eead9a (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.mondecitronne.homunculus;

import com.mojang.authlib.GameProfile;
import com.mondecitronne.homunculus.skin.DefaultSkin;
import com.mondecitronne.homunculus.skin.FallbackSkin;
import com.mondecitronne.homunculus.skin.HTTPSkin;
import com.mondecitronne.homunculus.skin.PlayerSkin;
import com.mondecitronne.homunculus.skin.Skin;
import io.netty.util.internal.StringUtil;
import net.minecraft.entity.EntityLiving;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTUtil;
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.network.datasync.DataSerializers;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.world.World;

public class EntityHomunculus extends EntityLiving {
	private static final DataParameter<NBTTagCompound> SKIN_SOURCE = EntityDataManager.createKey(EntityHomunculus.class, DataSerializers.COMPOUND_TAG);
	
	private final Skin defaultSkin;
	private static final Skin FALLBACK_SKIN = new FallbackSkin();
	private Skin skin;
	
	public EntityHomunculus(World world) {
		super(world);
		getDataManager().register(SKIN_SOURCE, new NBTTagCompound());
		defaultSkin = new DefaultSkin(getUniqueID());
	}
	
	@Override
	protected void entityInit() {
		super.entityInit();
	}
	
	private boolean isPlayerProfileUpdated(GameProfile a, GameProfile b) {
		if (b == null) {
			return a != null;
		} else {
			return !a.getName().toLowerCase().equals(b.getName().toLowerCase()) || (a.getId() != null && !b.getId().equals(a.getId()));
		}
	}
	
	private void dispatchFetchSkin() {
		if (this.getEntityWorld().isRemote && skin != null) {
			skin.dispatchFetch();
		}
	}
	
	private void updateSkin() {
		NBTTagCompound sourceNBT = getDataManager().get(SKIN_SOURCE);
		if (sourceNBT.hasKey("Type")) {
			switch (sourceNBT.getString("Type")) {
			case "player":
				GameProfile sourceProfile = NBTUtil.readGameProfileFromNBT(sourceNBT);
				if (sourceProfile == null || StringUtil.isNullOrEmpty(sourceProfile.getName())) {
					skin = null;
				}
				if (!(skin instanceof PlayerSkin) || isPlayerProfileUpdated(((PlayerSkin) skin).getPlayerProfile(), sourceProfile)) {
					skin = new PlayerSkin(sourceProfile, getEntityWorld().isRemote);
					dispatchFetchSkin();
				}
				break;
			case "http":
				String url = sourceNBT.getString("URL");
				String modelType = sourceNBT.getString("Model");
				if (!StringUtil.isNullOrEmpty(url)) {
					if (modelType == null || !modelType.equals("default") && !modelType.equals("slim")) {
						modelType = "default";
					}
					skin = new HTTPSkin(url, modelType);
					dispatchFetchSkin();
				} else {
					skin = null;
				}
			default:
				skin = null;
				break;
			}
		} else {
			skin = null;
		}
	}
	
	public Skin getSkin() {
		updateSkin();
		if (skin != null) {
			if (skin.isLoaded()) {
				return skin;
			} else {
				return FALLBACK_SKIN;
			}
		} else {
			return defaultSkin;
		}
	}
	
	@Override
	public void readFromNBT(NBTTagCompound compound) {
		super.readFromNBT(compound);
		if (compound.hasKey("SkinSource")) {
			NBTTagCompound sourceCompound = compound.getCompoundTag("SkinSource");
			this.getDataManager().set(SKIN_SOURCE, sourceCompound);
		} else if (compound.hasKey("SkinOwner")) {
			// backwards compatibility with old NBT
			NBTTagCompound ownerCompound = compound.getCompoundTag("SkinOwner").copy();
			if (ownerCompound.hasKey("Name")) {
				ownerCompound.setString("Type", "player");
				this.getDataManager().set(SKIN_SOURCE, ownerCompound);
			}
		}
	}
	
	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) {
		super.writeToNBT(compound);
		updateSkin();
		if (skin instanceof PlayerSkin) {
			NBTTagCompound profileCompound = new NBTTagCompound();
			NBTUtil.writeGameProfile(profileCompound, ((PlayerSkin) skin).getPlayerProfile());
			NBTTagCompound sourceCompound = new NBTTagCompound();
            sourceCompound.setTag("Name", profileCompound.getTag("Name"));
            if (profileCompound.hasKey("Id")) {
            	sourceCompound.setTag("Id", profileCompound.getTag("Id"));
            }
            sourceCompound.setString("Type", "player");
            compound.setTag("SkinSource", sourceCompound);
		} else if (skin instanceof HTTPSkin) {
			NBTTagCompound sourceCompound = new NBTTagCompound();
			sourceCompound.setString("URL", ((HTTPSkin) skin).getUrl());
			sourceCompound.setString("Model", skin.getModelType());
			sourceCompound.setString("Type", "http");
			compound.setTag("SkinSource", sourceCompound);
		} else if (compound.hasKey("SkinSource")) {
			compound.removeTag("SkinSource");
		}
		return compound;
	}
}