Advertisement
Creepinson

1

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