@Override TranslatableComponent.Builder builder() { return TranslatableComponent.builder("multiplayer.player.left"); }
/** * Creates a translatable component with a translation key and arguments. * * @param key the translation key * @param args the translation arguments * @return the translatable component */ public static TranslatableComponent of(final @NonNull String key, final @NonNull List<Component> args) { return of(key, null, args); }
@Override public @NonNull TranslatableComponent 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 TranslatableComponent(children, this.color, this.obfuscated, this.bold, this.strikethrough, this.underlined, this.italic, this.clickEvent, this.hoverEvent, this.insertion, this.key, this.args); }
@Test void testOf_color_decorations() { final TranslatableComponent component = TranslatableComponent.of("multiplayer.player.left", TextColor.GREEN, ImmutableSet.of(TextDecoration.BOLD)); assertEquals("multiplayer.player.left", component.key()); 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 TranslatableComponent decoration(final @NonNull TextDecoration decoration, final TextDecoration.@NonNull State state) { switch(decoration) { case BOLD: return new TranslatableComponent(this.children, this.color, this.obfuscated, requireNonNull(state, "flag"), this.strikethrough, this.underlined, this.italic, this.clickEvent, this.hoverEvent, this.insertion, this.key, this.args); case ITALIC: return new TranslatableComponent(this.children, this.color, this.obfuscated, this.bold, this.strikethrough, this.underlined, requireNonNull(state, "flag"), this.clickEvent, this.hoverEvent, this.insertion, this.key, this.args); case UNDERLINED: return new TranslatableComponent(this.children, this.color, this.obfuscated, this.bold, this.strikethrough, requireNonNull(state, "flag"), this.italic, this.clickEvent, this.hoverEvent, this.insertion, this.key, this.args); case STRIKETHROUGH: return new TranslatableComponent(this.children, this.color, this.obfuscated, this.bold, requireNonNull(state, "flag"), this.underlined, this.italic, this.clickEvent, this.hoverEvent, this.insertion, this.key, this.args); case OBFUSCATED: return new TranslatableComponent(this.children, this.color, requireNonNull(state, "flag"), this.bold, this.strikethrough, this.underlined, this.italic, this.clickEvent, this.hoverEvent, this.insertion, this.key, this.args); default: throw new IllegalArgumentException(String.format("unknown decoration '%s'", decoration)); } }
@Test void testSerializeTranslatable() { final TranslatableComponent component = TranslatableComponent.of( "multiplayer.player.left", TextComponent.builder("kashike") .clickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/msg kashike ")) .hoverEvent(new HoverEvent(HoverEvent.Action.SHOW_ENTITY, TextComponent.of("{\"name\":\"kashike\",\"id\":\"eb121687-8b1a-4944-bd4d-e0a818d9dfe2\"}"))) .build() ).color(TextColor.YELLOW); assertEquals(component, GsonComponentSerializer.INSTANCE.deserialize(GsonComponentSerializer.INSTANCE.serialize(component))); } }
@Test void testOf() { final TranslatableComponent component = TranslatableComponent.of("multiplayer.player.left"); assertEquals("multiplayer.player.left", component.key()); assertNull(component.color()); for(final TextDecoration decoration : TextDecoration.values()) { assertEquals(TextDecoration.State.NOT_SET, component.decoration(decoration)); } }
@Test void testDeserializeTranslatable() { assertEquals(TranslatableComponent.of("multiplayer.player.left"), GsonComponentSerializer.INSTANCE.deserialize("{\"translate\":\"multiplayer.player.left\"}")); assertEquals(TranslatableComponent.builder("multiplayer.player.left") .args(TextComponent.of("kashike")) .build(), GsonComponentSerializer.INSTANCE.deserialize("{\"translate\":\"multiplayer.player.left\",\"with\":[{\"text\":\"kashike\"}]}")); }
@Override public @NonNull TranslatableComponent decoration(final @NonNull TextDecoration decoration, final TextDecoration.@NonNull State state) { switch(decoration) { case BOLD: return new TranslatableComponent(this.children, this.color, this.obfuscated, checkNotNull(state, "flag"), this.strikethrough, this.underlined, this.italic, this.clickEvent, this.hoverEvent, this.insertion, this.key, this.args); case ITALIC: return new TranslatableComponent(this.children, this.color, this.obfuscated, this.bold, this.strikethrough, this.underlined, checkNotNull(state, "flag"), this.clickEvent, this.hoverEvent, this.insertion, this.key, this.args); case UNDERLINE: return new TranslatableComponent(this.children, this.color, this.obfuscated, this.bold, this.strikethrough, checkNotNull(state, "flag"), this.italic, this.clickEvent, this.hoverEvent, this.insertion, this.key, this.args); case STRIKETHROUGH: return new TranslatableComponent(this.children, this.color, this.obfuscated, this.bold, checkNotNull(state, "flag"), this.underlined, this.italic, this.clickEvent, this.hoverEvent, this.insertion, this.key, this.args); case OBFUSCATED: return new TranslatableComponent(this.children, this.color, checkNotNull(state, "flag"), this.bold, this.strikethrough, this.underlined, this.italic, this.clickEvent, this.hoverEvent, this.insertion, this.key, this.args); default: throw new IllegalArgumentException(String.format("unknown decoration '%s'", decoration)); } }
@Override public @NonNull TranslatableComponent 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 TranslatableComponent(children, this.color, this.obfuscated, this.bold, this.strikethrough, this.underlined, this.italic, this.clickEvent, this.hoverEvent, this.insertion, this.key, this.args); }
@Test void testMake() { final TranslatableComponent component = TranslatableComponent.make(builder -> { builder.key("multiplayer.player.left"); builder.color(TextColor.DARK_PURPLE); }); assertEquals("multiplayer.player.left", component.key()); assertEquals(TextColor.DARK_PURPLE, component.color()); }
/** * Creates a translatable component by applying configuration from {@code consumer}. * * @param consumer the builder configurator * @return the translatable component */ public static TranslatableComponent make(final @NonNull Consumer<Builder> consumer) { final Builder builder = builder(); consumer.accept(builder); return builder.build(); }
@Test void testOf_color() { final TranslatableComponent component = TranslatableComponent.of("multiplayer.player.left", TextColor.GREEN); assertEquals("multiplayer.player.left", component.key()); assertEquals(TextColor.GREEN, component.color()); for(final TextDecoration decoration : TextDecoration.values()) { assertEquals(TextDecoration.State.NOT_SET, component.decoration(decoration)); } }
/** * Creates a translatable component with a translation key and arguments. * * @param key the translation key * @param args the translation arguments * @return the translatable component */ public static TranslatableComponent of(final @NonNull String key, final @NonNull Component... args) { return of(key, null, args); }