@Override KeybindComponent.Builder builder() { return KeybindComponent.builder("key.jump"); }
Builder(final @NonNull KeybindComponent component) { super(component); this.keybind = component.keybind(); }
/** * Creates a keybind component with content, and optional color. * * @param keybind the keybind * @param color the color * @return the keybind component */ public static KeybindComponent of(final @NonNull String keybind, final @Nullable TextColor color) { return of(keybind, color, Collections.emptySet()); }
@Override public @NonNull KeybindComponent append(final @NonNull Component component) { this.detectCycle(component); // detect cycle before modifying final List<Component> children = new ArrayList<>(this.children.size() + 1); children.addAll(this.children); children.add(component); return new KeybindComponent(children, this.color, this.obfuscated, this.bold, this.strikethrough, this.underlined, this.italic, this.clickEvent, this.hoverEvent, this.insertion, this.keybind); }
@Test void testOf_color_decorations() { final KeybindComponent component = KeybindComponent.of("key.jump", TextColor.GREEN, ImmutableSet.of(TextDecoration.BOLD)); assertEquals("key.jump", component.keybind()); assertEquals(TextColor.GREEN, component.color()); assertEquals(TextDecoration.State.NOT_SET, component.decoration(TextDecoration.OBFUSCATED)); assertEquals(TextDecoration.State.TRUE, component.decoration(TextDecoration.BOLD)); assertEquals(TextDecoration.State.NOT_SET, component.decoration(TextDecoration.STRIKETHROUGH)); assertEquals(TextDecoration.State.NOT_SET, component.decoration(TextDecoration.UNDERLINED)); assertEquals(TextDecoration.State.NOT_SET, component.decoration(TextDecoration.ITALIC)); }
@Override public @NonNull KeybindComponent decoration(final @NonNull TextDecoration decoration, final TextDecoration.@NonNull State state) { switch(decoration) { case BOLD: return new KeybindComponent(this.children, this.color, this.obfuscated, checkNotNull(state, "flag"), this.strikethrough, this.underlined, this.italic, this.clickEvent, this.hoverEvent, this.insertion, this.keybind); case ITALIC: return new KeybindComponent(this.children, this.color, this.obfuscated, this.bold, this.strikethrough, this.underlined, checkNotNull(state, "flag"), this.clickEvent, this.hoverEvent, this.insertion, this.keybind); case UNDERLINE: return new KeybindComponent(this.children, this.color, this.obfuscated, this.bold, this.strikethrough, checkNotNull(state, "flag"), this.italic, this.clickEvent, this.hoverEvent, this.insertion, this.keybind); case STRIKETHROUGH: return new KeybindComponent(this.children, this.color, this.obfuscated, this.bold, checkNotNull(state, "flag"), this.underlined, this.italic, this.clickEvent, this.hoverEvent, this.insertion, this.keybind); case OBFUSCATED: return new KeybindComponent(this.children, this.color, checkNotNull(state, "flag"), this.bold, this.strikethrough, this.underlined, this.italic, this.clickEvent, this.hoverEvent, this.insertion, this.keybind); default: throw new IllegalArgumentException(String.format("unknown decoration '%s'", decoration)); } }
@Override public @NonNull Component render(final @NonNull Component component, final @NonNull C context) { if(component instanceof TranslatableComponent) { return this.render((TranslatableComponent) component, context); } else if(component instanceof TextComponent) { final TextComponent.Builder builder = TextComponent.builder(((TextComponent) component).content()); return this.deepRender(component, builder, context).build(); } else if(component instanceof KeybindComponent) { final KeybindComponent.Builder builder = KeybindComponent.builder(((KeybindComponent) component).keybind()); return this.deepRender(component, builder, context).build(); } else if(component instanceof ScoreComponent) { final ScoreComponent sc = (ScoreComponent) component; final ScoreComponent.Builder builder = ScoreComponent.builder() .name(sc.name()) .objective(sc.objective()) .value(sc.value()); return this.deepRender(component, builder, context).build(); } else if(component instanceof SelectorComponent) { final SelectorComponent.Builder builder = SelectorComponent.builder(((SelectorComponent) component).pattern()); return this.deepRender(component, builder, context).build(); } else { return component; } }
@Test void testOf_color() { final KeybindComponent component = KeybindComponent.of("key.jump", TextColor.GREEN); assertEquals("key.jump", component.keybind()); assertEquals(TextColor.GREEN, component.color()); for(final TextDecoration decoration : TextDecoration.values()) { assertEquals(TextDecoration.State.NOT_SET, component.decoration(decoration)); } }
@Override public @NonNull KeybindComponent decoration(final @NonNull TextDecoration decoration, final TextDecoration.@NonNull State state) { switch(decoration) { case BOLD: return new KeybindComponent(this.children, this.color, this.obfuscated, requireNonNull(state, "flag"), this.strikethrough, this.underlined, this.italic, this.clickEvent, this.hoverEvent, this.insertion, this.keybind); case ITALIC: return new KeybindComponent(this.children, this.color, this.obfuscated, this.bold, this.strikethrough, this.underlined, requireNonNull(state, "flag"), this.clickEvent, this.hoverEvent, this.insertion, this.keybind); case UNDERLINED: return new KeybindComponent(this.children, this.color, this.obfuscated, this.bold, this.strikethrough, requireNonNull(state, "flag"), this.italic, this.clickEvent, this.hoverEvent, this.insertion, this.keybind); case STRIKETHROUGH: return new KeybindComponent(this.children, this.color, this.obfuscated, this.bold, requireNonNull(state, "flag"), this.underlined, this.italic, this.clickEvent, this.hoverEvent, this.insertion, this.keybind); case OBFUSCATED: return new KeybindComponent(this.children, this.color, requireNonNull(state, "flag"), this.bold, this.strikethrough, this.underlined, this.italic, this.clickEvent, this.hoverEvent, this.insertion, this.keybind); default: throw new IllegalArgumentException(String.format("unknown decoration '%s'", decoration)); } }
@Override public @NonNull Component render(final @NonNull Component component, final @NonNull C context) { if(component instanceof TranslatableComponent) { return this.render((TranslatableComponent) component, context); } else if(component instanceof TextComponent) { final TextComponent.Builder builder = TextComponent.builder(((TextComponent) component).content()); return this.deepRender(component, builder, context).build(); } else if(component instanceof KeybindComponent) { final KeybindComponent.Builder builder = KeybindComponent.builder(((KeybindComponent) component).keybind()); return this.deepRender(component, builder, context).build(); } else if(component instanceof ScoreComponent) { final ScoreComponent sc = (ScoreComponent) component; final ScoreComponent.Builder builder = ScoreComponent.builder() .name(sc.name()) .objective(sc.objective()) .value(sc.value()); return this.deepRender(component, builder, context).build(); } else if(component instanceof SelectorComponent) { final SelectorComponent.Builder builder = SelectorComponent.builder(((SelectorComponent) component).pattern()); return this.deepRender(component, builder, context).build(); } else { return component; } }
@Override public @NonNull KeybindComponent append(final @NonNull Component component) { this.detectCycle(component); // detect cycle before modifying final List<Component> children = new ArrayList<>(this.children.size() + 1); children.addAll(this.children); children.add(component); return new KeybindComponent(children, this.color, this.obfuscated, this.bold, this.strikethrough, this.underlined, this.italic, this.clickEvent, this.hoverEvent, this.insertion, this.keybind); }
@Test void testOf() { final KeybindComponent component = KeybindComponent.of("key.jump"); assertEquals("key.jump", component.keybind()); assertNull(component.color()); for(final TextDecoration decoration : TextDecoration.values()) { assertEquals(TextDecoration.State.NOT_SET, component.decoration(decoration)); } }
/** * Creates a keybind component by applying configuration from {@code consumer}. * * @param consumer the builder configurator * @return the keybind component */ public static KeybindComponent make(final @NonNull Consumer<Builder> consumer) { final Builder builder = builder(); consumer.accept(builder); return builder.build(); }
@Override public @NonNull KeybindComponent decoration(final @NonNull TextDecoration decoration, final TextDecoration.@NonNull State state) { switch(decoration) { case BOLD: return new KeybindComponent(this.children, this.color, this.obfuscated, requireNonNull(state, "flag"), this.strikethrough, this.underlined, this.italic, this.clickEvent, this.hoverEvent, this.insertion, this.keybind); case ITALIC: return new KeybindComponent(this.children, this.color, this.obfuscated, this.bold, this.strikethrough, this.underlined, requireNonNull(state, "flag"), this.clickEvent, this.hoverEvent, this.insertion, this.keybind); case UNDERLINED: return new KeybindComponent(this.children, this.color, this.obfuscated, this.bold, this.strikethrough, requireNonNull(state, "flag"), this.italic, this.clickEvent, this.hoverEvent, this.insertion, this.keybind); case STRIKETHROUGH: return new KeybindComponent(this.children, this.color, this.obfuscated, this.bold, requireNonNull(state, "flag"), this.underlined, this.italic, this.clickEvent, this.hoverEvent, this.insertion, this.keybind); case OBFUSCATED: return new KeybindComponent(this.children, this.color, requireNonNull(state, "flag"), this.bold, this.strikethrough, this.underlined, this.italic, this.clickEvent, this.hoverEvent, this.insertion, this.keybind); default: throw new IllegalArgumentException(String.format("unknown decoration '%s'", decoration)); } }
@Override public @NonNull Component render(final @NonNull Component component, final @NonNull C context) { if(component instanceof TranslatableComponent) { return this.render((TranslatableComponent) component, context); } else if(component instanceof TextComponent) { final TextComponent.Builder builder = TextComponent.builder(((TextComponent) component).content()); return this.deepRender(component, builder, context).build(); } else if(component instanceof KeybindComponent) { final KeybindComponent.Builder builder = KeybindComponent.builder(((KeybindComponent) component).keybind()); return this.deepRender(component, builder, context).build(); } else if(component instanceof ScoreComponent) { final ScoreComponent sc = (ScoreComponent) component; final ScoreComponent.Builder builder = ScoreComponent.builder() .name(sc.name()) .objective(sc.objective()) .value(sc.value()); return this.deepRender(component, builder, context).build(); } else if(component instanceof SelectorComponent) { final SelectorComponent.Builder builder = SelectorComponent.builder(((SelectorComponent) component).pattern()); return this.deepRender(component, builder, context).build(); } else { return component; } }
@Override public @NonNull KeybindComponent append(final @NonNull Component component) { this.detectCycle(component); // detect cycle before modifying final List<Component> children = new ArrayList<>(this.children.size() + 1); children.addAll(this.children); children.add(component); return new KeybindComponent(children, this.color, this.obfuscated, this.bold, this.strikethrough, this.underlined, this.italic, this.clickEvent, this.hoverEvent, this.insertion, this.keybind); }