Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: Make contains check for entity name #362

Merged
merged 3 commits into from
May 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions api/src/main/java/net/kyori/adventure/text/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -1313,10 +1313,16 @@ default boolean contains(final @NonNull Component that, final @NonNull BiPredica
}
final @Nullable HoverEvent<?> hoverEvent = this.hoverEvent();
if(hoverEvent != null) {
if(hoverEvent.action().type().isAssignableFrom(Component.class)) {
final Component hover = (Component) hoverEvent.value();
if(that == hover) return true;
for(final Component child : hover.children()) {
final Object value = hoverEvent.value();
Component component = null;
if(value instanceof Component) {
component = (Component) hoverEvent.value();
} else if(value instanceof HoverEvent.ShowEntity) {
component = ((HoverEvent.ShowEntity) value).name();
}
if(component != null) {
if(equals.test(that, component)) return true;
for(final Component child : component.children()) {
if(child.contains(that, equals)) return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2021 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.kyori.adventure.text;

import java.util.UUID;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.event.HoverEvent;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;

// https://github.com/KyoriPowered/adventure/issues/363
class ComponentContainsTest {
@Test
public void testOf() {
final Component c0 = Component.text("best!");
final Component c1 = Component.text()
.append(Component.text("Nero "))
.append(Component.text(" are the ").append(c0))
.build();

assertTrue(c1.contains(c0));
assertTrue(c1.contains(Component.text("best!"), Component.EQUALS));
}

@Test
public void testOfHoverShowText() {
final Component c0 = Component.text("A great EDM trio");
final Component c1 = Component.text()
.append(Component.text("Nero ").hoverEvent(c0))
.append(Component.text(" are the best!"))
.build();

assertTrue(c1.contains(c0));
assertTrue(c1.contains(Component.text("A great EDM trio"), Component.EQUALS));
}

@Test
public void testOfHoverShowEntity() {
final Component c0 = Component.text("Joe Ray");
final Component c1 = Component.text()
.append(Component.text("One member").hoverEvent(HoverEvent.showEntity(Key.key("minecraft:player"), UUID.randomUUID(), c0)))
.append(Component.text(" launched a solo project in 2017."))
.build();

assertTrue(c1.contains(c0));
assertTrue(c1.contains(Component.text("Joe Ray"), Component.EQUALS));
}
}
12 changes: 0 additions & 12 deletions api/src/test/java/net/kyori/adventure/text/TextComponentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,6 @@ void testContains() {
assertTrue(component.contains(child));
}

// https://github.com/KyoriPowered/adventure/issues/363
kezz marked this conversation as resolved.
Show resolved Hide resolved
@Test
void testContainsEquality() {
final Component c0 = Component.text("best!");
final Component c1 = Component.text()
.append(Component.text("Nero "))
.append(Component.text(" are the ").append(c0))
.build();

assertTrue(c1.contains(Component.text("best!"), Component.EQUALS));
}

@Test
void testContent() {
final TextComponent c0 = Component.text("foo");
Expand Down