/** * Prevents a cycle between this component and the provided component. * * @param that the other component */ default void detectCycle(final @NonNull Component that) { if(that.contains(this)) { throw new IllegalStateException("Component cycle detected between " + this + " and " + that); } }
/** * Prevents a cycle between this component and the provided component. * * @param that the other component */ default void detectCycle(final @NonNull Component that) { if(that.contains(this)) { throw new IllegalStateException("Component cycle detected between " + this + " and " + that); } }
/** * Prevents a cycle between this component and the provided component. * * @param that the other component */ default void detectCycle(final @NonNull Component that) { if(that.contains(this)) { throw new IllegalStateException("Component cycle detected between " + this + " and " + that); } }
/** * Checks if this component contains a component. * * @param that the other component * @return {@code true} if this component contains the provided * component, {@code false} otherwise */ default boolean contains(final @NonNull Component that) { if(this == that) return true; for(final Component child : this.children()) { if(child.contains(that)) return true; } if(this.hoverEvent() != null) { final Component hover = this.hoverEvent().value(); if(that == hover) return true; for(final Component child : hover.children()) { if(child.contains(that)) return true; } } return false; }
/** * Checks if this component contains a component. * * @param that the other component * @return {@code true} if this component contains the provided * component, {@code false} otherwise */ default boolean contains(final @NonNull Component that) { if(this == that) return true; for(final Component child : this.children()) { if(child.contains(that)) return true; } if(this.hoverEvent() != null) { final Component hover = this.hoverEvent().value(); if(that == hover) return true; for(final Component child : hover.children()) { if(child.contains(that)) return true; } } return false; }
/** * Checks if this component contains a component. * * @param that the other component * @return {@code true} if this component contains the provided * component, {@code false} otherwise */ default boolean contains(final @NonNull Component that) { if(this == that) return true; for(final Component child : this.children()) { if(child.contains(that)) return true; } if(this.hoverEvent() != null) { final Component hover = this.hoverEvent().value(); if(that == hover) return true; for(final Component child : hover.children()) { if(child.contains(that)) return true; } } return false; }
@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 testContains() { final Component child = TranslatableComponent.of("multiplayer.player.left"); final Component component = TranslatableComponent.builder() .key("multiplayer.player.left") .append(child) .build(); assertTrue(component.contains(child)); }