| 1 | package net.minecraft.nbt;
|
| 2 |
|
| 3 | import com.mojang.authlib.GameProfile;
|
| 4 | import com.mojang.authlib.properties.Property;
|
| 5 | import java.util.UUID;
|
| 6 | import net.minecraft.util.StringUtils;
|
| 7 |
|
| 8 | public final class NBTUtil
|
| 9 | {
|
| 10 | /**
|
| 11 | * Reads and returns a GameProfile that has been saved to the passed in NBTTagCompound
|
| 12 | */
|
| 13 | public static GameProfile readGameProfileFromNBT(NBTTagCompound compound)
|
| 14 | {
|
| 15 | String s = null;
|
| 16 | String s1 = null;
|
| 17 |
|
| 18 | if (compound.hasKey("Name", 8))
|
| 19 | {
|
| 20 | s = compound.getString("Name");
|
| 21 | }
|
| 22 |
|
| 23 | if (compound.hasKey("Id", 8))
|
| 24 | {
|
| 25 | s1 = compound.getString("Id");
|
| 26 | }
|
| 27 |
|
| 28 | if (StringUtils.isNullOrEmpty(s) && StringUtils.isNullOrEmpty(s1))
|
| 29 | {
|
| 30 | return null;
|
| 31 | }
|
| 32 | else
|
| 33 | {
|
| 34 | UUID uuid;
|
| 35 |
|
| 36 | try
|
| 37 | {
|
| 38 | uuid = UUID.fromString(s1);
|
| 39 | }
|
| 40 | catch (Throwable var12)
|
| 41 | {
|
| 42 | uuid = null;
|
| 43 | }
|
| 44 |
|
| 45 | GameProfile gameprofile = new GameProfile(uuid, s);
|
| 46 |
|
| 47 | if (compound.hasKey("Properties", 10))
|
| 48 | {
|
| 49 | NBTTagCompound nbttagcompound = compound.getCompoundTag("Properties");
|
| 50 |
|
| 51 | for (String s2 : nbttagcompound.getKeySet())
|
| 52 | {
|
| 53 | NBTTagList nbttaglist = nbttagcompound.getTagList(s2, 10);
|
| 54 |
|
| 55 | for (int i = 0; i < nbttaglist.tagCount(); ++i)
|
| 56 | {
|
| 57 | NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
|
| 58 | String s3 = nbttagcompound1.getString("Value");
|
| 59 |
|
| 60 | if (nbttagcompound1.hasKey("Signature", 8))
|
| 61 | {
|
| 62 | gameprofile.getProperties().put(s2, new Property(s2, s3, nbttagcompound1.getString("Signature")));
|
| 63 | }
|
| 64 | else
|
| 65 | {
|
| 66 | gameprofile.getProperties().put(s2, new Property(s2, s3));
|
| 67 | }
|
| 68 | }
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | return gameprofile;
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | /**
|
| 77 | * Writes a GameProfile to an NBTTagCompound.
|
| 78 | */
|
| 79 | public static NBTTagCompound writeGameProfile(NBTTagCompound tagCompound, GameProfile profile)
|
| 80 | {
|
| 81 | if (!StringUtils.isNullOrEmpty(profile.getName()))
|
| 82 | {
|
| 83 | tagCompound.setString("Name", profile.getName());
|
| 84 | }
|
| 85 |
|
| 86 | if (profile.getId() != null)
|
| 87 | {
|
| 88 | tagCompound.setString("Id", profile.getId().toString());
|
| 89 | }
|
| 90 |
|
| 91 | if (!profile.getProperties().isEmpty())
|
| 92 | {
|
| 93 | NBTTagCompound nbttagcompound = new NBTTagCompound();
|
| 94 |
|
| 95 | for (String s : profile.getProperties().keySet())
|
| 96 | {
|
| 97 | NBTTagList nbttaglist = new NBTTagList();
|
| 98 |
|
| 99 | for (Property property : profile.getProperties().get(s))
|
| 100 | {
|
| 101 | NBTTagCompound nbttagcompound1 = new NBTTagCompound();
|
| 102 | nbttagcompound1.setString("Value", property.getValue());
|
| 103 |
|
| 104 | if (property.hasSignature())
|
| 105 | {
|
| 106 | nbttagcompound1.setString("Signature", property.getSignature());
|
| 107 | }
|
| 108 |
|
| 109 | nbttaglist.appendTag(nbttagcompound1);
|
| 110 | }
|
| 111 |
|
| 112 | nbttagcompound.setTag(s, nbttaglist);
|
| 113 | }
|
| 114 |
|
| 115 | tagCompound.setTag("Properties", nbttagcompound);
|
| 116 | }
|
| 117 |
|
| 118 | return tagCompound;
|
| 119 | }
|
| 120 |
|
| 121 | public static boolean func_181123_a(NBTBase p_181123_0_, NBTBase p_181123_1_, boolean p_181123_2_)
|
| 122 | {
|
| 123 | if (p_181123_0_ == p_181123_1_)
|
| 124 | {
|
| 125 | return true;
|
| 126 | }
|
| 127 | else if (p_181123_0_ == null)
|
| 128 | {
|
| 129 | return true;
|
| 130 | }
|
| 131 | else if (p_181123_1_ == null)
|
| 132 | {
|
| 133 | return false;
|
| 134 | }
|
| 135 | else if (!p_181123_0_.getClass().equals(p_181123_1_.getClass()))
|
| 136 | {
|
| 137 | return false;
|
| 138 | }
|
| 139 | else if (p_181123_0_ instanceof NBTTagCompound)
|
| 140 | {
|
| 141 | NBTTagCompound nbttagcompound = (NBTTagCompound)p_181123_0_;
|
| 142 | NBTTagCompound nbttagcompound1 = (NBTTagCompound)p_181123_1_;
|
| 143 |
|
| 144 | for (String s : nbttagcompound.getKeySet())
|
| 145 | {
|
| 146 | NBTBase nbtbase1 = nbttagcompound.getTag(s);
|
| 147 |
|
| 148 | if (!func_181123_a(nbtbase1, nbttagcompound1.getTag(s), p_181123_2_))
|
| 149 | {
|
| 150 | return false;
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | return true;
|
| 155 | }
|
| 156 | else if (p_181123_0_ instanceof NBTTagList && p_181123_2_)
|
| 157 | {
|
| 158 | NBTTagList nbttaglist = (NBTTagList)p_181123_0_;
|
| 159 | NBTTagList nbttaglist1 = (NBTTagList)p_181123_1_;
|
| 160 |
|
| 161 | if (nbttaglist.tagCount() == 0)
|
| 162 | {
|
| 163 | return nbttaglist1.tagCount() == 0;
|
| 164 | }
|
| 165 | else
|
| 166 | {
|
| 167 | for (int i = 0; i < nbttaglist.tagCount(); ++i)
|
| 168 | {
|
| 169 | NBTBase nbtbase = nbttaglist.get(i);
|
| 170 | boolean flag = false;
|
| 171 |
|
| 172 | for (int j = 0; j < nbttaglist1.tagCount(); ++j)
|
| 173 | {
|
| 174 | if (func_181123_a(nbtbase, nbttaglist1.get(j), p_181123_2_))
|
| 175 | {
|
| 176 | flag = true;
|
| 177 | break;
|
| 178 | }
|
| 179 | }
|
| 180 |
|
| 181 | if (!flag)
|
| 182 | {
|
| 183 | return false;
|
| 184 | }
|
| 185 | }
|
| 186 |
|
| 187 | return true;
|
| 188 | }
|
| 189 | }
|
| 190 | else
|
| 191 | {
|
| 192 | return p_181123_0_.equals(p_181123_1_);
|
| 193 | }
|
| 194 | }
|
| 195 | }
|
| 196 |
|