current.content(input.substring(from, pos)); } else if(current == null) { current = TextComponent.builder("");
current.content(input.substring(from, pos)); } else if(current == null) { current = TextComponent.builder("");
private Component render(final TranslatableComponent component, final C context) { final /* @Nullable */ MessageFormat format = this.translation(context.locale(), component.key()); if(format == null) { return component; } final List<Component> args = component.args(); final TextComponent.Builder builder = TextComponent.builder(); this.mergeStyle(component, builder, context); // no arguments makes this render very simple if(args.isEmpty()) { return builder.content(format.format(null, new StringBuffer(), null).toString()).build(); } final Object[] nulls = new Object[args.size()]; final StringBuffer sb = format.format(nulls, new StringBuffer(), null); final AttributedCharacterIterator it = format.formatToCharacterIterator(nulls); while(it.getIndex() < it.getEndIndex()) { final int end = it.getRunLimit(); final Integer index = (Integer) it.getAttribute(MessageFormat.Field.ARGUMENT); if(index != null) { builder.append(this.render(args.get(index), context)); } else { builder.append(TextComponent.of(sb.substring(it.getIndex(), end))); } it.setIndex(end); } return builder.content("").build(); }
.content("Visit the ") .append(TextComponent.builder("Velocity website") .color(TextColor.GREEN)
private Component render(final TranslatableComponent component, final C context) { final /* @Nullable */ MessageFormat format = this.translation(context.locale(), component.key()); if(format == null) { return component; } final List<Component> args = component.args(); final TextComponent.Builder builder = TextComponent.builder(); this.mergeStyle(component, builder, context); // no arguments makes this render very simple if(args.isEmpty()) { return builder.content(format.format(null, new StringBuffer(), null).toString()).build(); } final Object[] nulls = new Object[args.size()]; final StringBuffer sb = format.format(nulls, new StringBuffer(), null); final AttributedCharacterIterator it = format.formatToCharacterIterator(nulls); while(it.getIndex() < it.getEndIndex()) { final int end = it.getRunLimit(); final Integer index = (Integer) it.getAttribute(MessageFormat.Field.ARGUMENT); if(index != null) { builder.append(this.render(args.get(index), context)); } else { builder.append(TextComponent.of(sb.substring(it.getIndex(), end))); } it.setIndex(end); } return builder.content("").build(); }
private Component render(final TranslatableComponent component, final C context) { final /* @Nullable */ MessageFormat format = this.translation(context.locale(), component.key()); if(format == null) { return component; } final List<Component> args = component.args(); final TextComponent.Builder builder = TextComponent.builder(); this.mergeStyle(component, builder, context); // no arguments makes this render very simple if(args.isEmpty()) { return builder.content(format.format(null, new StringBuffer(), null).toString()).build(); } final Object[] nulls = new Object[args.size()]; final StringBuffer sb = format.format(nulls, new StringBuffer(), null); final AttributedCharacterIterator it = format.formatToCharacterIterator(nulls); while(it.getIndex() < it.getEndIndex()) { final int end = it.getRunLimit(); final Integer index = (Integer) it.getAttribute(MessageFormat.Field.ARGUMENT); if(index != null) { builder.append(this.render(args.get(index), context)); } else { builder.append(TextComponent.of(sb.substring(it.getIndex(), end))); } it.setIndex(end); } return builder.content("").build(); }
/** * Handles unexpected disconnects. * @param server the server we disconnected from * @param disconnect the disconnect packet */ public void handleConnectionException(RegisteredServer server, Disconnect disconnect) { Component disconnectReason = ComponentSerializers.JSON.deserialize(disconnect.getReason()); String plainTextReason = PASS_THRU_TRANSLATE.serialize(disconnectReason); if (connectedServer != null && connectedServer.getServerInfo().equals(server.getServerInfo())) { logger.error("{}: kicked from server {}: {}", this, server.getServerInfo().getName(), plainTextReason); handleConnectionException(server, disconnectReason, TextComponent.builder() .content("Kicked from " + server.getServerInfo().getName() + ": ") .color(TextColor.RED) .append(disconnectReason) .build()); } else { logger.error("{}: disconnected while connecting to {}: {}", this, server.getServerInfo().getName(), plainTextReason); handleConnectionException(server, disconnectReason, TextComponent.builder() .content("Can't connect to server " + server.getServerInfo().getName() + ": ") .color(TextColor.RED) .append(disconnectReason) .build()); } }
@Test void testDecorations() { TextComponent component = TextComponent.builder().content("Kittens!").build(); // The bold decoration should not be set at this point. assertFalse(component.hasDecoration(TextDecoration.BOLD)); assertEquals(TextDecoration.State.NOT_SET, component.decoration(TextDecoration.BOLD)); component = component.decoration(TextDecoration.BOLD, TextDecoration.State.TRUE); final Set<TextDecoration> decorations = component.decorations(); // The bold decoration should be set and true at this point. assertTrue(component.hasDecoration(TextDecoration.BOLD)); assertEquals(TextDecoration.State.TRUE, component.decoration(TextDecoration.BOLD)); assertEquals(component.decoration(TextDecoration.BOLD) == TextDecoration.State.TRUE, decorations.contains(TextDecoration.BOLD)); assertTrue(decorations.contains(TextDecoration.BOLD)); assertFalse(decorations.contains(TextDecoration.OBFUSCATED)); }
@Test void testCycleHoverChild() { assertThrows(IllegalStateException.class, () -> { final Component hoverComponent = TextComponent.builder().content("hover child").build(); final Component component = TextComponent.builder().content("cat") .hoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.builder().content("hover").build().append(hoverComponent))) .build(); // component's hover event value contains hoverComponent, we should not be able to add it hoverComponent.append(component); fail("A component was added to itself"); }); } }
@Test void testCompound() { final TextComponent component = TextComponent.builder() .content("hi there ") .append(TextComponent.builder("this bit is green ") .color(TextColor.GREEN) .build()) .append(TextComponent.of("this isn't ").resetStyle()) .append(TextComponent.builder("and woa, this is again") .color(TextColor.GREEN) .build()) .build(); assertEquals("hi there &athis bit is green &rthis isn't &aand woa, this is again", LegacyComponentSerializer.INSTANCE.serialize(component, '&')); }
@Test void testCycleHoverRoot() { assertThrows(IllegalStateException.class, () -> { final Component hoverComponent = TextComponent.builder().content("hover").build(); final Component component = TextComponent.builder() .content("cat") .hoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, hoverComponent)) .build(); // component's hover event value is hoverComponent, we should not be able to add it hoverComponent.append(component); fail("A component was added to itself"); }); }
/** * Creates a text component builder with content. * * @param content the plain text content * @return a builder */ public static Builder builder(final @NonNull String content) { return new Builder().content(content); }
@Test void testMake() { final TextComponent component = TextComponent.make(builder -> { builder.content("foo"); builder.color(TextColor.DARK_PURPLE); }); assertEquals("foo", component.content()); assertEquals(TextColor.DARK_PURPLE, component.color()); }
@Test void testCopy() { final TextComponent.Builder component = TextComponent.builder().content("").color(TextColor.GRAY); component.append(TextComponent.builder().content("This is a test").color(TextColor.DARK_PURPLE).build()); component.append(TextComponent.builder().content(" ").build()); component.append(TextComponent.builder().content("A what?").color(TextColor.DARK_AQUA).clickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/what")).build()); assertEquals(component.build(), component.build().copy()); }
/** * Creates a text component builder with content. * * @param content the plain text content * @return a builder */ public static Builder builder(final @NonNull String content) { return new Builder().content(content); }
/** * Creates a text component builder with content. * * @param content the plain text content * @return a builder */ public static Builder builder(final @NonNull String content) { return new Builder().content(content); }
@Test void testCycleSelf() { assertThrows(IllegalStateException.class, () -> { final Component component = TextComponent.builder().content("cat").build(); component.append(component); fail("A component was added to itself"); }); }
@Test void testStyleReset() { Component component = TextComponent.builder() .content("kittens") .build(); assertFalse(component.hasStyling()); component = component.decoration(TextDecoration.BOLD, TextDecoration.State.TRUE); assertTrue(component.hasStyling()); component = component.resetStyle(); assertFalse(component.hasStyling()); }
@Test void testContains() { final Component child = TextComponent.of("kittens"); final Component component = TextComponent.builder() .content("cat") .append(child) .build(); assertTrue(component.contains(child)); }
@Test void testSerializeDeserialize() { final TextComponent expected = TextComponent.builder() .content("Hello!") .color(TextColor.DARK_PURPLE) .decoration(TextDecoration.BOLD, TextDecoration.State.TRUE) .clickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://google.com/")) .hoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.builder().content(":o").color(TextColor.DARK_AQUA).build())) .build(); final String json = GsonComponentSerializer.INSTANCE.serialize(expected); assertEquals(expected, GsonComponentSerializer.INSTANCE.deserialize(json)); }