| 1 | package net.minecraft.tileentity;
|
| 2 |
|
| 3 | import com.google.common.collect.Iterables;
|
| 4 | import com.mojang.authlib.GameProfile;
|
| 5 | import com.mojang.authlib.properties.Property;
|
| 6 | import java.util.UUID;
|
| 7 | import net.minecraft.nbt.NBTTagCompound;
|
| 8 | import net.minecraft.nbt.NBTUtil;
|
| 9 | import net.minecraft.network.Packet;
|
| 10 | import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
|
| 11 | import net.minecraft.server.MinecraftServer;
|
| 12 | import net.minecraft.util.StringUtils;
|
| 13 |
|
| 14 | public class TileEntitySkull extends TileEntity
|
| 15 | {
|
| 16 | private int skullType;
|
| 17 | private int skullRotation;
|
| 18 | private GameProfile playerProfile = null;
|
| 19 |
|
| 20 | public void writeToNBT(NBTTagCompound compound)
|
| 21 | {
|
| 22 | super.writeToNBT(compound);
|
| 23 | compound.setByte("SkullType", (byte)(this.skullType & 255));
|
| 24 | compound.setByte("Rot", (byte)(this.skullRotation & 255));
|
| 25 |
|
| 26 | if (this.playerProfile != null)
|
| 27 | {
|
| 28 | NBTTagCompound nbttagcompound = new NBTTagCompound();
|
| 29 | NBTUtil.writeGameProfile(nbttagcompound, this.playerProfile);
|
| 30 | compound.setTag("Owner", nbttagcompound);
|
| 31 | }
|
| 32 | }
|
| 33 |
|
| 34 | public void readFromNBT(NBTTagCompound compound)
|
| 35 | {
|
| 36 | super.readFromNBT(compound);
|
| 37 | this.skullType = compound.getByte("SkullType");
|
| 38 | this.skullRotation = compound.getByte("Rot");
|
| 39 |
|
| 40 | if (this.skullType == 3)
|
| 41 | {
|
| 42 | if (compound.hasKey("Owner", 10))
|
| 43 | {
|
| 44 | this.playerProfile = NBTUtil.readGameProfileFromNBT(compound.getCompoundTag("Owner"));
|
| 45 | }
|
| 46 | else if (compound.hasKey("ExtraType", 8))
|
| 47 | {
|
| 48 | String s = compound.getString("ExtraType");
|
| 49 |
|
| 50 | if (!StringUtils.isNullOrEmpty(s))
|
| 51 | {
|
| 52 | this.playerProfile = new GameProfile((UUID)null, s);
|
| 53 | this.updatePlayerProfile();
|
| 54 | }
|
| 55 | }
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | public GameProfile getPlayerProfile()
|
| 60 | {
|
| 61 | return this.playerProfile;
|
| 62 | }
|
| 63 |
|
| 64 | /**
|
| 65 | * Allows for a specialized description packet to be created. This is often used to sync tile entity data from the
|
| 66 | * server to the client easily. For example this is used by signs to synchronise the text to be displayed.
|
| 67 | */
|
| 68 | public Packet getDescriptionPacket()
|
| 69 | {
|
| 70 | NBTTagCompound nbttagcompound = new NBTTagCompound();
|
| 71 | this.writeToNBT(nbttagcompound);
|
| 72 | return new S35PacketUpdateTileEntity(this.pos, 4, nbttagcompound);
|
| 73 | }
|
| 74 |
|
| 75 | public void setType(int type)
|
| 76 | {
|
| 77 | this.skullType = type;
|
| 78 | this.playerProfile = null;
|
| 79 | }
|
| 80 |
|
| 81 | public void setPlayerProfile(GameProfile playerProfile)
|
| 82 | {
|
| 83 | this.skullType = 3;
|
| 84 | this.playerProfile = playerProfile;
|
| 85 | this.updatePlayerProfile();
|
| 86 | }
|
| 87 |
|
| 88 | private void updatePlayerProfile()
|
| 89 | {
|
| 90 | this.playerProfile = updateGameprofile(this.playerProfile);
|
| 91 | this.markDirty();
|
| 92 | }
|
| 93 |
|
| 94 | public static GameProfile updateGameprofile(GameProfile input)
|
| 95 | {
|
| 96 | if (input != null && !StringUtils.isNullOrEmpty(input.getName()))
|
| 97 | {
|
| 98 | if (input.isComplete() && input.getProperties().containsKey("textures"))
|
| 99 | {
|
| 100 | return input;
|
| 101 | }
|
| 102 | else if (MinecraftServer.getServer() == null)
|
| 103 | {
|
| 104 | return input;
|
| 105 | }
|
| 106 | else
|
| 107 | {
|
| 108 | GameProfile gameprofile = MinecraftServer.getServer().getPlayerProfileCache().getGameProfileForUsername(input.getName());
|
| 109 |
|
| 110 | if (gameprofile == null)
|
| 111 | {
|
| 112 | return input;
|
| 113 | }
|
| 114 | else
|
| 115 | {
|
| 116 | Property property = (Property)Iterables.getFirst(gameprofile.getProperties().get("textures"), null);
|
| 117 |
|
| 118 | if (property == null)
|
| 119 | {
|
| 120 | gameprofile = MinecraftServer.getServer().getMinecraftSessionService().fillProfileProperties(gameprofile, true);
|
| 121 | }
|
| 122 |
|
| 123 | return gameprofile;
|
| 124 | }
|
| 125 | }
|
| 126 | }
|
| 127 | else
|
| 128 | {
|
| 129 | return input;
|
| 130 | }
|
| 131 | }
|
| 132 |
|
| 133 | public int getSkullType()
|
| 134 | {
|
| 135 | return this.skullType;
|
| 136 | }
|
| 137 |
|
| 138 | public int getSkullRotation()
|
| 139 | {
|
| 140 | return this.skullRotation;
|
| 141 | }
|
| 142 |
|
| 143 | public void setSkullRotation(int rotation)
|
| 144 | {
|
| 145 | this.skullRotation = rotation;
|
| 146 | }
|
| 147 | }
|
| 148 |
|