Commit 7fce24deec88037ecf4a976583ced326dfcdb297

Authored by Mumfrey
1 parent c6092c8f

Fix compilation under JDK7 and later

java/common/com/mumfrey/liteloader/util/ChatUtilities.java
1 -package com.mumfrey.liteloader.util;  
2 -  
3 -import java.util.List;  
4 -  
5 -import net.minecraft.util.ChatComponentText;  
6 -import net.minecraft.util.ChatStyle;  
7 -import net.minecraft.util.EnumChatFormatting;  
8 -import net.minecraft.util.IChatComponent;  
9 -  
10 -/**  
11 - * Utility functions for chat  
12 - *  
13 - * @author Adam Mummery-Smith  
14 - */  
15 -public abstract class ChatUtilities  
16 -{  
17 - private static String formattingCodeLookup;  
18 -  
19 - static  
20 - {  
21 - StringBuilder formattingCodes = new StringBuilder();  
22 -  
23 - for (EnumChatFormatting chatFormat : EnumChatFormatting.values())  
24 - {  
25 - formattingCodes.append(chatFormat.toString().charAt(1));  
26 - }  
27 -  
28 - ChatUtilities.formattingCodeLookup = formattingCodes.toString();  
29 - }  
30 -  
31 - private ChatUtilities() {}  
32 -  
33 - /**  
34 - * Get a chat style from a legacy formatting code  
35 - *  
36 - * @param code Code  
37 - * @return  
38 - */  
39 - public static ChatStyle getChatStyleFromCode(char code)  
40 - {  
41 - int pos = ChatUtilities.formattingCodeLookup.indexOf(code);  
42 - if (pos < 0) return null;  
43 - EnumChatFormatting format = EnumChatFormatting.values()[pos];  
44 -  
45 - ChatStyle style = new ChatStyle();  
46 - if (format.isColor())  
47 - {  
48 - style.setColor(format);  
49 - }  
50 - else if (format.isFancyStyling())  
51 - {  
52 - switch (format)  
53 - {  
54 - case BOLD: style.setBold(true); break;  
55 - case ITALIC: style.setItalic(true); break;  
56 - case STRIKETHROUGH: style.setStrikethrough(true); break;  
57 - case UNDERLINE: style.setUnderlined(true); break;  
58 - case OBFUSCATED: style.setObfuscated(true); break;  
59 - }  
60 - }  
61 -  
62 - return style;  
63 - }  
64 -  
65 - /**  
66 - * Convert a component containing text formatted with legacy codes to a native ChatComponent structure  
67 - */  
68 - public static IChatComponent convertLegacyCodes(IChatComponent chat)  
69 - {  
70 - return ChatUtilities.covertCodesInPlace(chat);  
71 - }  
72 -  
73 - private static List<IChatComponent> covertCodesInPlace(List<IChatComponent> siblings)  
74 - {  
75 - for (int index = 0; index < siblings.size(); index++)  
76 - {  
77 - siblings.set(index, ChatUtilities.covertCodesInPlace(siblings.get(index)));  
78 - }  
79 -  
80 - return siblings;  
81 - }  
82 -  
83 - @SuppressWarnings("unchecked")  
84 - private static IChatComponent covertCodesInPlace(IChatComponent component)  
85 - {  
86 - IChatComponent newComponent = null;  
87 - if (component instanceof ChatComponentText)  
88 - {  
89 - ChatComponentText textComponent = (ChatComponentText)component;  
90 - ChatStyle style = textComponent.getChatStyle();  
91 - String text = textComponent.getChatComponentText_TextValue();  
92 -  
93 - int pos = text.indexOf('\247');  
94 - while (pos > -1 && text != null)  
95 - {  
96 - if (pos < text.length() - 1)  
97 - {  
98 - IChatComponent head = new ChatComponentText(pos > 0 ? text.substring(0, pos) : "").setChatStyle(style);  
99 - style = ChatUtilities.getChatStyleFromCode(text.charAt(pos + 1));  
100 - text = text.substring(pos + 2);  
101 - newComponent = (newComponent == null) ? head : newComponent.appendSibling(head);  
102 - pos = text.indexOf('\247');  
103 - }  
104 - else  
105 - {  
106 - text = null;  
107 - }  
108 - }  
109 -  
110 - if (text != null)  
111 - {  
112 - IChatComponent tail = new ChatComponentText(text).setChatStyle(style);  
113 - newComponent = (newComponent == null) ? tail : newComponent.appendSibling(tail);  
114 - }  
115 - }  
116 -  
117 - if (newComponent == null)  
118 - {  
119 - ChatUtilities.covertCodesInPlace(component.getSiblings());  
120 - return component;  
121 - }  
122 -  
123 - for (IChatComponent oldSibling : ChatUtilities.covertCodesInPlace(component.getSiblings()))  
124 - {  
125 - newComponent.appendSibling(oldSibling);  
126 - }  
127 -  
128 - return newComponent;  
129 - }  
130 -} 1 +package com.mumfrey.liteloader.util;
  2 +
  3 +import java.util.List;
  4 +
  5 +import net.minecraft.util.ChatComponentText;
  6 +import net.minecraft.util.ChatStyle;
  7 +import net.minecraft.util.EnumChatFormatting;
  8 +import net.minecraft.util.IChatComponent;
  9 +
  10 +/**
  11 + * Utility functions for chat
  12 + *
  13 + * @author Adam Mummery-Smith
  14 + */
  15 +public abstract class ChatUtilities
  16 +{
  17 + private static String formattingCodeLookup;
  18 +
  19 + static
  20 + {
  21 + StringBuilder formattingCodes = new StringBuilder();
  22 +
  23 + for (EnumChatFormatting chatFormat : EnumChatFormatting.values())
  24 + {
  25 + formattingCodes.append(chatFormat.toString().charAt(1));
  26 + }
  27 +
  28 + ChatUtilities.formattingCodeLookup = formattingCodes.toString();
  29 + }
  30 +
  31 + private ChatUtilities() {}
  32 +
  33 + /**
  34 + * Get a chat style from a legacy formatting code
  35 + *
  36 + * @param code Code
  37 + * @return
  38 + */
  39 + public static ChatStyle getChatStyleFromCode(char code)
  40 + {
  41 + int pos = ChatUtilities.formattingCodeLookup.indexOf(code);
  42 + if (pos < 0) return null;
  43 + EnumChatFormatting format = EnumChatFormatting.values()[pos];
  44 +
  45 + ChatStyle style = new ChatStyle();
  46 + if (format.isColor())
  47 + {
  48 + style.setColor(format);
  49 + }
  50 + else if (format.isFancyStyling())
  51 + {
  52 + switch (format)
  53 + {
  54 + case BOLD: style.setBold(true); break;
  55 + case ITALIC: style.setItalic(true); break;
  56 + case STRIKETHROUGH: style.setStrikethrough(true); break;
  57 + case UNDERLINE: style.setUnderlined(true); break;
  58 + case OBFUSCATED: style.setObfuscated(true); break;
  59 + }
  60 + }
  61 +
  62 + return style;
  63 + }
  64 +
  65 + /**
  66 + * Convert a component containing text formatted with legacy codes to a native ChatComponent structure
  67 + */
  68 + public static IChatComponent convertLegacyCodes(IChatComponent chat)
  69 + {
  70 + return ChatUtilities.covertCodesInPlace(chat);
  71 + }
  72 +
  73 + private static List<IChatComponent> covertCodesInPlace(List<IChatComponent> siblings)
  74 + {
  75 + for (int index = 0; index < siblings.size(); index++)
  76 + {
  77 + siblings.set(index, ChatUtilities.covertCodesInPlace(siblings.get(index)));
  78 + }
  79 +
  80 + return siblings;
  81 + }
  82 +
  83 + @SuppressWarnings("unchecked")
  84 + private static IChatComponent covertCodesInPlace(IChatComponent component)
  85 + {
  86 + IChatComponent newComponent = null;
  87 + if (component instanceof ChatComponentText)
  88 + {
  89 + ChatComponentText textComponent = (ChatComponentText)component;
  90 + ChatStyle style = textComponent.getChatStyle();
  91 + String text = textComponent.getChatComponentText_TextValue();
  92 +
  93 + int pos = text.indexOf('\247');
  94 + while (pos > -1 && text != null)
  95 + {
  96 + if (pos < text.length() - 1)
  97 + {
  98 + IChatComponent head = new ChatComponentText(pos > 0 ? text.substring(0, pos) : "").setChatStyle(style);
  99 + style = ChatUtilities.getChatStyleFromCode(text.charAt(pos + 1));
  100 + text = text.substring(pos + 2);
  101 + newComponent = (newComponent == null) ? head : newComponent.appendSibling(head);
  102 + pos = text.indexOf('\247');
  103 + }
  104 + else
  105 + {
  106 + text = null;
  107 + }
  108 + }
  109 +
  110 + if (text != null)
  111 + {
  112 + IChatComponent tail = new ChatComponentText(text).setChatStyle(style);
  113 + newComponent = (newComponent == null) ? tail : newComponent.appendSibling(tail);
  114 + }
  115 + }
  116 +
  117 + if (newComponent == null)
  118 + {
  119 + ChatUtilities.covertCodesInPlace(component.getSiblings());
  120 + return component;
  121 + }
  122 +
  123 + for (IChatComponent oldSibling : ChatUtilities.covertCodesInPlace((List<IChatComponent>)component.getSiblings()))
  124 + {
  125 + newComponent.appendSibling(oldSibling);
  126 + }
  127 +
  128 + return newComponent;
  129 + }
  130 +}