protected AbstractBuilder(final @NonNull C component) { this.children = new ArrayList<>(component.children()); this.color = component.color(); this.obfuscated = component.decoration(TextDecoration.OBFUSCATED); this.bold = component.decoration(TextDecoration.BOLD); this.strikethrough = component.decoration(TextDecoration.STRIKETHROUGH); this.underlined = component.decoration(TextDecoration.UNDERLINED); this.italic = component.decoration(TextDecoration.ITALIC); this.clickEvent = Optional.ofNullable(component.clickEvent()).map(ClickEvent::copy).orElse(null); this.hoverEvent = Optional.ofNullable(component.hoverEvent()).map(HoverEvent::copy).orElse(null); this.insertion = component.insertion(); }
@Override public @NonNull B mapChildrenDeep(final @NonNull Function<BuildableComponent<? ,?>, BuildableComponent<? ,?>> function) { if(this.children == EMPTY_COMPONENT_LIST) { return (B) this; } final ListIterator<Component> it = this.children.listIterator(); while(it.hasNext()) { final Component child = it.next(); if(!(child instanceof BuildableComponent)) { continue; } final BuildableComponent mappedChild = function.apply((BuildableComponent) child); if(mappedChild.children().isEmpty()) { if(child == mappedChild) { continue; } it.set(mappedChild); } else { final Builder<?, ?> builder = mappedChild.toBuilder(); builder.mapChildrenDeep(function); it.set(builder.build()); } } return (B) this; }
@Test void testMergeDecorations() { final C c0 = this.builder().build(); assertNull(c0.color()); assertDecorations(c0, ImmutableMap.of()); assertNull(c0.clickEvent()); final C c1 = (C) c0.mergeDecorations(TextComponent.of("xyz", TextColor.RED, ImmutableSet.of(TextDecoration.BOLD)).clickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/foo"))); assertNull(c1.color()); assertDecorations(c1, ImmutableMap.of(TextDecoration.BOLD, TextDecoration.State.TRUE)); assertNull(c1.clickEvent()); assertEquals(c0, c1.decoration(TextDecoration.BOLD, TextDecoration.State.NOT_SET)); }
@Test void testMergeColor() { final C c0 = this.builder().build(); assertNull(c0.color()); assertDecorations(c0, ImmutableMap.of()); assertNull(c0.clickEvent()); final C c1 = (C) c0.mergeColor(TextComponent.of("xyz", TextColor.RED, ImmutableSet.of(TextDecoration.BOLD)).clickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/foo"))); assertEquals(TextColor.RED, c1.color()); assertDecorations(c1, ImmutableMap.of()); assertNull(c1.clickEvent()); assertEquals(c0, c1.color(null)); }
@Test void testMergeEvents() { final C c0 = this.builder().build(); assertNull(c0.color()); assertDecorations(c0, ImmutableMap.of()); assertNull(c0.clickEvent()); final C c1 = (C) c0.mergeEvents(TextComponent.of("xyz", TextColor.RED, ImmutableSet.of(TextDecoration.BOLD)).clickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/foo"))); assertNull(c1.color()); assertDecorations(c1, ImmutableMap.of()); assertNotNull(c1.clickEvent()); }
@Test void testMergeStyle() { final C c0 = this.builder().build(); assertNull(c0.color()); assertDecorations(c0, ImmutableMap.of()); assertNull(c0.clickEvent()); final C c1 = (C) c0.mergeStyle(TextComponent.of("xyz", TextColor.RED, ImmutableSet.of(TextDecoration.BOLD)).clickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/foo"))); assertEquals(TextColor.RED, c1.color()); assertDecorations(c1, ImmutableMap.of(TextDecoration.BOLD, TextDecoration.State.TRUE)); assertNotNull(c1.clickEvent()); assertEquals(c0, c1.color(null).decoration(TextDecoration.BOLD, TextDecoration.State.NOT_SET).clickEvent(null)); }
@Test void testResetStyle() { final C c0 = this.builder() .color(TextColor.RED) .decoration(TextDecoration.BOLD, true) .clickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/foo")) .build(); final C c1 = (C) c0.resetStyle(); assertNull(c1.color()); assertDecorations(c1, ImmutableMap.of()); assertNull(c1.clickEvent()); assertEquals(c1, c0.color(null).decoration(TextDecoration.BOLD, TextDecoration.State.NOT_SET).clickEvent(null)); }
@Override public @NonNull B applyDeep(final @NonNull Consumer<Builder<?, ?>> consumer) { this.apply(consumer); if(this.children == EMPTY_COMPONENT_LIST) { return (B) this; } final ListIterator<Component> it = this.children.listIterator(); while(it.hasNext()) { final Component child = it.next(); if(!(child instanceof BuildableComponent)) { continue; } final Builder<?, ?> childBuilder = ((BuildableComponent) child).toBuilder(); childBuilder.applyDeep(consumer); it.set(childBuilder.build()); } return (B) this; }
@Test void testClickEvent() { final C c0 = this.builder().build(); assertNull(c0.clickEvent()); final C c1 = (C) c0.clickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "foo")); assertNotNull(c1.clickEvent()); assertEquals(c0, c1.clickEvent(null)); }
@Test void testHoverEvent() { final C c0 = this.builder().build(); assertNull(c0.hoverEvent()); final C c1 = (C) c0.hoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.of("hover"))); assertNotNull(c1.hoverEvent()); assertEquals(c0, c1.hoverEvent(null)); }
@Test void testDecoration() { final C c0 = this.builder().build(); assertDecorations(c0, ImmutableMap.of()); final C c1 = (C) c0.decoration(TextDecoration.BOLD, true); assertDecorations(c1, ImmutableMap.of(TextDecoration.BOLD, TextDecoration.State.TRUE)); assertEquals(c0, c1.decoration(TextDecoration.BOLD, TextDecoration.State.NOT_SET)); }
@Override @SuppressWarnings("unchecked") public @NonNull B applyDeep(final @NonNull Consumer<Builder<?, ?>> consumer) { this.apply(consumer); if(this.children == EMPTY_COMPONENT_LIST) { return (B) this; } final ListIterator<Component> it = this.children.listIterator(); while(it.hasNext()) { final Component child = it.next(); if(!(child instanceof BuildableComponent)) { continue; } final Builder<?, ?> childBuilder = ((BuildableComponent) child).toBuilder(); childBuilder.applyDeep(consumer); it.set(childBuilder.build()); } return (B) this; }
protected AbstractBuilder(final @NonNull C component) { this.children = new ArrayList<>(component.children()); this.color = component.color(); this.obfuscated = component.decoration(TextDecoration.OBFUSCATED); this.bold = component.decoration(TextDecoration.BOLD); this.strikethrough = component.decoration(TextDecoration.STRIKETHROUGH); this.underlined = component.decoration(TextDecoration.UNDERLINED); this.italic = component.decoration(TextDecoration.ITALIC); this.clickEvent = Optional.ofNullable(component.clickEvent()).map(ClickEvent::copy).orElse(null); this.hoverEvent = Optional.ofNullable(component.hoverEvent()).map(HoverEvent::copy).orElse(null); this.insertion = component.insertion(); }
@Override @SuppressWarnings({"rawtypes", "unchecked"}) public @NonNull B mapChildrenDeep(final @NonNull Function<BuildableComponent<? ,?>, BuildableComponent<? ,?>> function) { if(this.children == EMPTY_COMPONENT_LIST) { return (B) this; } final ListIterator<Component> it = this.children.listIterator(); while(it.hasNext()) { final Component child = it.next(); if(!(child instanceof BuildableComponent)) { continue; } final BuildableComponent mappedChild = function.apply((BuildableComponent) child); if(mappedChild.children().isEmpty()) { if(child == mappedChild) { continue; } it.set(mappedChild); } else { final Builder<?, ?> builder = mappedChild.toBuilder(); builder.mapChildrenDeep(function); it.set(builder.build()); } } return (B) this; }
@Override @SuppressWarnings("unchecked") public @NonNull B applyDeep(final @NonNull Consumer<Builder<?, ?>> consumer) { this.apply(consumer); if(this.children == EMPTY_COMPONENT_LIST) { return (B) this; } final ListIterator<Component> it = this.children.listIterator(); while(it.hasNext()) { final Component child = it.next(); if(!(child instanceof BuildableComponent)) { continue; } final Builder<?, ?> childBuilder = ((BuildableComponent) child).toBuilder(); childBuilder.applyDeep(consumer); it.set(childBuilder.build()); } return (B) this; }
protected AbstractBuilder(final @NonNull C component) { this.children = new ArrayList<>(component.children()); this.color = component.color(); this.obfuscated = component.decoration(TextDecoration.OBFUSCATED); this.bold = component.decoration(TextDecoration.BOLD); this.strikethrough = component.decoration(TextDecoration.STRIKETHROUGH); this.underlined = component.decoration(TextDecoration.UNDERLINE); this.italic = component.decoration(TextDecoration.ITALIC); this.clickEvent = Optional.ofNullable(component.clickEvent()).map(ClickEvent::copy).orElse(null); this.hoverEvent = Optional.ofNullable(component.hoverEvent()).map(HoverEvent::copy).orElse(null); this.insertion = component.insertion(); }
@Override @SuppressWarnings({"rawtypes", "unchecked"}) public @NonNull B mapChildrenDeep(final @NonNull Function<BuildableComponent<? ,?>, BuildableComponent<? ,?>> function) { if(this.children == EMPTY_COMPONENT_LIST) { return (B) this; } final ListIterator<Component> it = this.children.listIterator(); while(it.hasNext()) { final Component child = it.next(); if(!(child instanceof BuildableComponent)) { continue; } final BuildableComponent mappedChild = function.apply((BuildableComponent) child); if(mappedChild.children().isEmpty()) { if(child == mappedChild) { continue; } it.set(mappedChild); } else { final Builder<?, ?> builder = mappedChild.toBuilder(); builder.mapChildrenDeep(function); it.set(builder.build()); } } return (B) this; }
final BuildableComponent<?, ?> child = this.deserialize(childElement, childElement.getClass(), context); if(parent == null) { parent = child.toBuilder(); } else { parent.append(child);