public static String fromComponent(IChatBaseComponent component, EnumChatFormat defaultColor) { if (component == null) return ""; StringBuilder out = new StringBuilder(); for (IChatBaseComponent c : (Iterable<IChatBaseComponent>) component) { ChatModifier modi = c.getChatModifier(); out.append(modi.getColor() == null ? defaultColor : modi.getColor()); if (modi.isBold()) { out.append(EnumChatFormat.BOLD); } if (modi.isItalic()) { out.append(EnumChatFormat.ITALIC); } if (modi.isUnderlined()) { out.append(EnumChatFormat.UNDERLINE); } if (modi.isStrikethrough()) { out.append(EnumChatFormat.STRIKETHROUGH); } if (modi.isRandom()) { out.append(EnumChatFormat.OBFUSCATED); } out.append(c.getText()); } return out.toString().replaceFirst("^(" + defaultColor + ")*", ""); }
private StringMessage(String message) { this.message = message; if (message == null) { output = new IChatBaseComponent[] { currentChatComponent }; return; } list.add(currentChatComponent); Matcher matcher = INCREMENTAL_PATTERN.matcher(message); String match = null; while (matcher.find()) { int groupId = 0; while ((match = matcher.group(++groupId)) == null) { // NOOP } appendNewComponent(matcher.start(groupId)); switch (groupId) { case 1: EnumChatFormat format = formatMap.get(match.toLowerCase().charAt(1)); if (format == EnumChatFormat.RESET) { modifier = new ChatModifier(); } else if (format.isFormat()) { switch (format) { case BOLD: modifier.setBold(Boolean.TRUE); break; case ITALIC: modifier.setItalic(Boolean.TRUE); break; case STRIKETHROUGH: modifier.setStrikethrough(Boolean.TRUE); break; case UNDERLINE: modifier.setUnderline(Boolean.TRUE); break; case RANDOM: modifier.setRandom(Boolean.TRUE); break; default: throw new AssertionError("Unexpected message format"); } } else { // Color resets formatting modifier = new ChatModifier().setColor(format); } break; case 2: currentChatComponent = null; break; case 3: modifier.setChatClickable(new ChatClickable(EnumClickAction.OPEN_URL, match)); appendNewComponent(matcher.end(groupId)); modifier.setChatClickable((ChatClickable) null); } currentIndex = matcher.end(groupId); } if (currentIndex < message.length()) { appendNewComponent(message.length()); } output = list.toArray(new IChatBaseComponent[0]); }
private StringMessage(String message, boolean keepNewlines) { this.message = message; if (message == null) { output = new IChatBaseComponent[] { currentChatComponent }; return; } list.add(currentChatComponent); Matcher matcher = INCREMENTAL_PATTERN.matcher(message); String match = null; while (matcher.find()) { int groupId = 0; while ((match = matcher.group(++groupId)) == null) { // NOOP } appendNewComponent(matcher.start(groupId)); switch (groupId) { case 1: EnumChatFormat format = formatMap.get(match.toLowerCase().charAt(1)); if (format == EnumChatFormat.RESET) { modifier = new ChatModifier(); } else if (format.isFormat()) { switch (format) { case BOLD: modifier.setBold(Boolean.TRUE); break; case ITALIC: modifier.setItalic(Boolean.TRUE); break; case STRIKETHROUGH: modifier.setStrikethrough(Boolean.TRUE); break; case UNDERLINE: modifier.setUnderline(Boolean.TRUE); break; case OBFUSCATED: modifier.setRandom(Boolean.TRUE); break; default: throw new AssertionError("Unexpected message format"); } } else { // Color resets formatting modifier = new ChatModifier().setColor(format); } break; case 2: if (keepNewlines) { currentChatComponent.addSibling(new ChatComponentText("\n")); } else { currentChatComponent = null; } break; case 3: if ( !( match.startsWith( "http://" ) || match.startsWith( "https://" ) ) ) { match = "http://" + match; } modifier.setChatClickable(new ChatClickable(EnumClickAction.OPEN_URL, match)); appendNewComponent(matcher.end(groupId)); modifier.setChatClickable((ChatClickable) null); } currentIndex = matcher.end(groupId); } if (currentIndex < message.length()) { appendNewComponent(message.length()); } output = list.toArray(new IChatBaseComponent[list.size()]); }
private StringMessage(String message) { this.message = message; if (message == null) { output = new IChatBaseComponent[] { currentChatComponent }; return; } list.add(currentChatComponent); Matcher matcher = INCREMENTAL_PATTERN.matcher(message); String match = null; while (matcher.find()) { int groupId = 0; while ((match = matcher.group(++groupId)) == null) { // NOOP } appendNewComponent(matcher.start(groupId)); switch (groupId) { case 1: EnumChatFormat format = formatMap.get(match.toLowerCase().charAt(1)); if (format == EnumChatFormat.RESET) { modifier = new ChatModifier(); } else if (format.isFormat()) { switch (format) { case BOLD: modifier.setBold(Boolean.TRUE); break; case ITALIC: modifier.setItalic(Boolean.TRUE); break; case STRIKETHROUGH: modifier.setStrikethrough(Boolean.TRUE); break; case UNDERLINE: modifier.setUnderline(Boolean.TRUE); break; case RANDOM: modifier.setRandom(Boolean.TRUE); break; default: throw new AssertionError("Unexpected message format"); } } else { // Color resets formatting modifier = new ChatModifier().setColor(format); } break; case 2: currentChatComponent = null; break; case 3: if ( !( match.startsWith( "http://" ) || match.startsWith( "https://" ) ) ) { match = "http://" + match; } modifier.setChatClickable(new ChatClickable(EnumClickAction.OPEN_URL, match)); appendNewComponent(matcher.end(groupId)); modifier.setChatClickable((ChatClickable) null); } currentIndex = matcher.end(groupId); } if (currentIndex < message.length()) { appendNewComponent(message.length()); } output = list.toArray(new IChatBaseComponent[list.size()]); }