| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import com.google.common.collect.Iterators;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import java.util.Arrays;
|
| 6 | import java.util.IllegalFormatException;
|
| 7 | import java.util.Iterator;
|
| 8 | import java.util.List;
|
| 9 | import java.util.regex.Matcher;
|
| 10 | import java.util.regex.Pattern;
|
| 11 |
|
| 12 | public class ChatComponentTranslation extends ChatComponentStyle
|
| 13 | {
|
| 14 | private final String key;
|
| 15 | private final Object[] formatArgs;
|
| 16 | private final Object syncLock = new Object();
|
| 17 | private long lastTranslationUpdateTimeInMilliseconds = -1L;
|
| 18 | List<IChatComponent> children = Lists.<IChatComponent>newArrayList();
|
| 19 | public static final Pattern stringVariablePattern = Pattern.compile("%(?:(\\d+)\\$)?([A-Za-z%]|$)");
|
| 20 |
|
| 21 | public ChatComponentTranslation(String translationKey, Object... args)
|
| 22 | {
|
| 23 | this.key = translationKey;
|
| 24 | this.formatArgs = args;
|
| 25 |
|
| 26 | for (Object object : args)
|
| 27 | {
|
| 28 | if (object instanceof IChatComponent)
|
| 29 | {
|
| 30 | ((IChatComponent)object).getChatStyle().setParentStyle(this.getChatStyle());
|
| 31 | }
|
| 32 | }
|
| 33 | }
|
| 34 |
|
| 35 | /**
|
| 36 | * ensures that our children are initialized from the most recent string translation mapping.
|
| 37 | */
|
| 38 | synchronized void ensureInitialized()
|
| 39 | {
|
| 40 | synchronized (this.syncLock)
|
| 41 | {
|
| 42 | long i = StatCollector.getLastTranslationUpdateTimeInMilliseconds();
|
| 43 |
|
| 44 | if (i == this.lastTranslationUpdateTimeInMilliseconds)
|
| 45 | {
|
| 46 | return;
|
| 47 | }
|
| 48 |
|
| 49 | this.lastTranslationUpdateTimeInMilliseconds = i;
|
| 50 | this.children.clear();
|
| 51 | }
|
| 52 |
|
| 53 | try
|
| 54 | {
|
| 55 | this.initializeFromFormat(StatCollector.translateToLocal(this.key));
|
| 56 | }
|
| 57 | catch (ChatComponentTranslationFormatException chatcomponenttranslationformatexception)
|
| 58 | {
|
| 59 | this.children.clear();
|
| 60 |
|
| 61 | try
|
| 62 | {
|
| 63 | this.initializeFromFormat(StatCollector.translateToFallback(this.key));
|
| 64 | }
|
| 65 | catch (ChatComponentTranslationFormatException var5)
|
| 66 | {
|
| 67 | throw chatcomponenttranslationformatexception;
|
| 68 | }
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | /**
|
| 73 | * initializes our children from a format string, using the format args to fill in the placeholder variables.
|
| 74 | */
|
| 75 | protected void initializeFromFormat(String format)
|
| 76 | {
|
| 77 | boolean flag = false;
|
| 78 | Matcher matcher = stringVariablePattern.matcher(format);
|
| 79 | int i = 0;
|
| 80 | int j = 0;
|
| 81 |
|
| 82 | try
|
| 83 | {
|
| 84 | int l;
|
| 85 |
|
| 86 | for (; matcher.find(j); j = l)
|
| 87 | {
|
| 88 | int k = matcher.start();
|
| 89 | l = matcher.end();
|
| 90 |
|
| 91 | if (k > j)
|
| 92 | {
|
| 93 | ChatComponentText chatcomponenttext = new ChatComponentText(String.format(format.substring(j, k), new Object[0]));
|
| 94 | chatcomponenttext.getChatStyle().setParentStyle(this.getChatStyle());
|
| 95 | this.children.add(chatcomponenttext);
|
| 96 | }
|
| 97 |
|
| 98 | String s2 = matcher.group(2);
|
| 99 | String s = format.substring(k, l);
|
| 100 |
|
| 101 | if ("%".equals(s2) && "%%".equals(s))
|
| 102 | {
|
| 103 | ChatComponentText chatcomponenttext2 = new ChatComponentText("%");
|
| 104 | chatcomponenttext2.getChatStyle().setParentStyle(this.getChatStyle());
|
| 105 | this.children.add(chatcomponenttext2);
|
| 106 | }
|
| 107 | else
|
| 108 | {
|
| 109 | if (!"s".equals(s2))
|
| 110 | {
|
| 111 | throw new ChatComponentTranslationFormatException(this, "Unsupported format: \'" + s + "\'");
|
| 112 | }
|
| 113 |
|
| 114 | String s1 = matcher.group(1);
|
| 115 | int i1 = s1 != null ? Integer.parseInt(s1) - 1 : i++;
|
| 116 |
|
| 117 | if (i1 < this.formatArgs.length)
|
| 118 | {
|
| 119 | this.children.add(this.getFormatArgumentAsComponent(i1));
|
| 120 | }
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | if (j < format.length())
|
| 125 | {
|
| 126 | ChatComponentText chatcomponenttext1 = new ChatComponentText(String.format(format.substring(j), new Object[0]));
|
| 127 | chatcomponenttext1.getChatStyle().setParentStyle(this.getChatStyle());
|
| 128 | this.children.add(chatcomponenttext1);
|
| 129 | }
|
| 130 | }
|
| 131 | catch (IllegalFormatException illegalformatexception)
|
| 132 | {
|
| 133 | throw new ChatComponentTranslationFormatException(this, illegalformatexception);
|
| 134 | }
|
| 135 | }
|
| 136 |
|
| 137 | private IChatComponent getFormatArgumentAsComponent(int index)
|
| 138 | {
|
| 139 | if (index >= this.formatArgs.length)
|
| 140 | {
|
| 141 | throw new ChatComponentTranslationFormatException(this, index);
|
| 142 | }
|
| 143 | else
|
| 144 | {
|
| 145 | Object object = this.formatArgs[index];
|
| 146 | IChatComponent ichatcomponent;
|
| 147 |
|
| 148 | if (object instanceof IChatComponent)
|
| 149 | {
|
| 150 | ichatcomponent = (IChatComponent)object;
|
| 151 | }
|
| 152 | else
|
| 153 | {
|
| 154 | ichatcomponent = new ChatComponentText(object == null ? "null" : object.toString());
|
| 155 | ichatcomponent.getChatStyle().setParentStyle(this.getChatStyle());
|
| 156 | }
|
| 157 |
|
| 158 | return ichatcomponent;
|
| 159 | }
|
| 160 | }
|
| 161 |
|
| 162 | public IChatComponent setChatStyle(ChatStyle style)
|
| 163 | {
|
| 164 | super.setChatStyle(style);
|
| 165 |
|
| 166 | for (Object object : this.formatArgs)
|
| 167 | {
|
| 168 | if (object instanceof IChatComponent)
|
| 169 | {
|
| 170 | ((IChatComponent)object).getChatStyle().setParentStyle(this.getChatStyle());
|
| 171 | }
|
| 172 | }
|
| 173 |
|
| 174 | if (this.lastTranslationUpdateTimeInMilliseconds > -1L)
|
| 175 | {
|
| 176 | for (IChatComponent ichatcomponent : this.children)
|
| 177 | {
|
| 178 | ichatcomponent.getChatStyle().setParentStyle(style);
|
| 179 | }
|
| 180 | }
|
| 181 |
|
| 182 | return this;
|
| 183 | }
|
| 184 |
|
| 185 | public Iterator<IChatComponent> iterator()
|
| 186 | {
|
| 187 | this.ensureInitialized();
|
| 188 | return Iterators.<IChatComponent>concat(createDeepCopyIterator(this.children), createDeepCopyIterator(this.siblings));
|
| 189 | }
|
| 190 |
|
| 191 | /**
|
| 192 | * Gets the text of this component, without any special formatting codes added, for chat. TODO: why is this two
|
| 193 | * different methods?
|
| 194 | */
|
| 195 | public String getUnformattedTextForChat()
|
| 196 | {
|
| 197 | this.ensureInitialized();
|
| 198 | StringBuilder stringbuilder = new StringBuilder();
|
| 199 |
|
| 200 | for (IChatComponent ichatcomponent : this.children)
|
| 201 | {
|
| 202 | stringbuilder.append(ichatcomponent.getUnformattedTextForChat());
|
| 203 | }
|
| 204 |
|
| 205 | return stringbuilder.toString();
|
| 206 | }
|
| 207 |
|
| 208 | /**
|
| 209 | * Creates a copy of this component. Almost a deep copy, except the style is shallow-copied.
|
| 210 | */
|
| 211 | public ChatComponentTranslation createCopy()
|
| 212 | {
|
| 213 | Object[] aobject = new Object[this.formatArgs.length];
|
| 214 |
|
| 215 | for (int i = 0; i < this.formatArgs.length; ++i)
|
| 216 | {
|
| 217 | if (this.formatArgs[i] instanceof IChatComponent)
|
| 218 | {
|
| 219 | aobject[i] = ((IChatComponent)this.formatArgs[i]).createCopy();
|
| 220 | }
|
| 221 | else
|
| 222 | {
|
| 223 | aobject[i] = this.formatArgs[i];
|
| 224 | }
|
| 225 | }
|
| 226 |
|
| 227 | ChatComponentTranslation chatcomponenttranslation = new ChatComponentTranslation(this.key, aobject);
|
| 228 | chatcomponenttranslation.setChatStyle(this.getChatStyle().createShallowCopy());
|
| 229 |
|
| 230 | for (IChatComponent ichatcomponent : this.getSiblings())
|
| 231 | {
|
| 232 | chatcomponenttranslation.appendSibling(ichatcomponent.createCopy());
|
| 233 | }
|
| 234 |
|
| 235 | return chatcomponenttranslation;
|
| 236 | }
|
| 237 |
|
| 238 | public boolean equals(Object p_equals_1_)
|
| 239 | {
|
| 240 | if (this == p_equals_1_)
|
| 241 | {
|
| 242 | return true;
|
| 243 | }
|
| 244 | else if (!(p_equals_1_ instanceof ChatComponentTranslation))
|
| 245 | {
|
| 246 | return false;
|
| 247 | }
|
| 248 | else
|
| 249 | {
|
| 250 | ChatComponentTranslation chatcomponenttranslation = (ChatComponentTranslation)p_equals_1_;
|
| 251 | return Arrays.equals(this.formatArgs, chatcomponenttranslation.formatArgs) && this.key.equals(chatcomponenttranslation.key) && super.equals(p_equals_1_);
|
| 252 | }
|
| 253 | }
|
| 254 |
|
| 255 | public int hashCode()
|
| 256 | {
|
| 257 | int i = super.hashCode();
|
| 258 | i = 31 * i + this.key.hashCode();
|
| 259 | i = 31 * i + Arrays.hashCode(this.formatArgs);
|
| 260 | return i;
|
| 261 | }
|
| 262 |
|
| 263 | public String toString()
|
| 264 | {
|
| 265 | return "TranslatableComponent{key=\'" + this.key + '\'' + ", args=" + Arrays.toString(this.formatArgs) + ", siblings=" + this.siblings + ", style=" + this.getChatStyle() + '}';
|
| 266 | }
|
| 267 |
|
| 268 | public String getKey()
|
| 269 | {
|
| 270 | return this.key;
|
| 271 | }
|
| 272 |
|
| 273 | public Object[] getFormatArgs()
|
| 274 | {
|
| 275 | return this.formatArgs;
|
| 276 | }
|
| 277 | }
|
| 278 |
|