@Override Builder<String, Object> serialize(Builder<String, Object> builder) { super.serialize(builder); if (hasTitle()) { builder.put(BOOK_TITLE.BUKKIT, title); } if (hasAuthor()) { builder.put(BOOK_AUTHOR.BUKKIT, author); } if (hasPages()) { List<String> pagesString = new ArrayList<String>(); for (IChatBaseComponent comp : pages) { pagesString.add(CraftChatMessage.fromComponent(comp)); } builder.put(BOOK_PAGES.BUKKIT, pagesString); } if (generation != null) { builder.put(GENERATION.BUKKIT, generation); } return builder; }
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 + ")*", ""); }
@Override public PacketTracer a(IChatBaseComponent chat) { value("Chat", chat); try { mute = true; super.a(chat); } finally { mute = false; } return this; }
@Override public boolean update(boolean force, boolean applyPhysics) { boolean result = super.update(force, applyPhysics); if (result) { IChatBaseComponent[] newLines = sanitizeLines(lines); System.arraycopy(newLines, 0, sign.lines, 0, 4); sign.update(); } return result; }
public static IChatBaseComponent[] sanitizeLines(String[] lines) { IChatBaseComponent[] components = new IChatBaseComponent[4]; for (int i = 0; i < 4; i++) { if (i < lines.length && lines[i] != null) { components[i] = CraftChatMessage.fromString(lines[i])[0]; } else { components[i] = new ChatComponentText(""); } } return components; }
public static String[] revertComponents(IChatBaseComponent[] components) { String[] lines = new String[components.length]; for (int i = 0; i < lines.length; i++) { lines[i] = revertComponent(components[i]); } return lines; }
@Override void applyToItem(NBTTagCompound itemData) { super.applyToItem(itemData, false); if (hasTitle()) { itemData.setString(BOOK_TITLE.NBT, this.title); } else { itemData.setString(BOOK_TITLE.NBT, " "); } if (hasAuthor()) { itemData.setString(BOOK_AUTHOR.NBT, this.author); } else { itemData.setString(BOOK_AUTHOR.NBT, " "); } if (hasPages()) { NBTTagList list = new NBTTagList(); for (IChatBaseComponent page : pages) { list.add(new NBTTagString( ChatSerializer.a(page) )); } itemData.set(BOOK_PAGES.NBT, list); } itemData.setBoolean(RESOLVED.NBT, true); if (generation != null) { itemData.setInt(GENERATION.NBT, generation); } else { itemData.setInt(GENERATION.NBT, 0); } }
void applyToItem(NBTTagCompound itemData, boolean handlePages) { super.applyToItem(itemData); if (hasTitle()) { itemData.setString(BOOK_TITLE.NBT, this.title); } if (hasAuthor()) { itemData.setString(BOOK_AUTHOR.NBT, this.author); } if (handlePages) { if (hasPages()) { NBTTagList list = new NBTTagList(); for (IChatBaseComponent page : pages) { list.add(new NBTTagString(CraftChatMessage.fromComponent(page))); } itemData.set(BOOK_PAGES.NBT, list); } itemData.remove(RESOLVED.NBT); } if (generation != null) { itemData.setInt(GENERATION.NBT, generation); } }
public List<String> getPages() { final List<IChatBaseComponent> copy = ImmutableList.copyOf(pages); return new AbstractList<String>() { @Override public String get(int index) { return CraftChatMessage.fromComponent(copy.get(index)); } @Override public int size() { return copy.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: 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 IChatBaseComponent[] getOutput() { return output; }
public static IChatBaseComponent[] fromString(String message) { return new StringMessage(message).getOutput(); }
private static String revertComponent(IChatBaseComponent component) { return CraftChatMessage.fromComponent(component); }
@Override public IChatBaseComponent getScoreboardDisplayName() { return new ChatComponentText(title); }
@Override public CraftMetaBook clone() { CraftMetaBook meta = (CraftMetaBook) super.clone(); meta.pages = new ArrayList<IChatBaseComponent>(pages); return meta; }
@Override public IChatBaseComponent getScoreboardDisplayName() { return CraftChatMessage.fromString(getName())[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()]); }
public static IChatBaseComponent[] fromString(String message) { return fromString(message, false); }
public static IChatBaseComponent[] fromString(String message, boolean keepNewlines) { return new StringMessage(message, keepNewlines).getOutput(); }
public static String fromComponent(IChatBaseComponent component) { return fromComponent(component, EnumChatFormat.BLACK); }
public static IChatBaseComponent fixComponent(IChatBaseComponent component) { Matcher matcher = LINK_PATTERN.matcher(""); return fixComponent(component, matcher); }
public void sendMessage(String message) { for (IChatBaseComponent component : CraftChatMessage.fromString(message)) { commandBlock.sendMessage(component); } }
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()]); }