From f042d061253d77f36a027fd807704c19d0c6a3b7 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Sun, 26 Feb 2023 06:59:52 -0800 Subject: [PATCH] Fix NamespacedKey#equals not accepting other Key types (#8919) --- patches/api/Adventure.patch | 69 +++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/patches/api/Adventure.patch b/patches/api/Adventure.patch index 07c4f0403c..271aaafbb0 100644 --- a/patches/api/Adventure.patch +++ b/patches/api/Adventure.patch @@ -1061,6 +1061,38 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 /** * The namespace representing all inbuilt keys. @@ -0,0 +0,0 @@ public final class NamespacedKey { + + @Override + public int hashCode() { +- int hash = 5; +- hash = 47 * hash + this.namespace.hashCode(); +- hash = 47 * hash + this.key.hashCode(); +- return hash; ++ // Paper start ++ int result = this.namespace.hashCode(); ++ result = (31 * result) + this.key.hashCode(); ++ return result; ++ // Paper end + } + + @Override +@@ -0,0 +0,0 @@ public final class NamespacedKey { + if (obj == null) { + return false; + } +- if (getClass() != obj.getClass()) { +- return false; +- } +- final NamespacedKey other = (NamespacedKey) obj; +- return this.namespace.equals(other.namespace) && this.key.equals(other.key); ++ // Paper start ++ if (!(obj instanceof net.kyori.adventure.key.Key key)) return false; ++ return this.namespace.equals(key.namespace()) && this.key.equals(key.value()); ++ // Paper end + } + + @Override +@@ -0,0 +0,0 @@ public final class NamespacedKey { public static NamespacedKey fromString(@NotNull String key) { return fromString(key, null); } @@ -4675,6 +4707,43 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 void setColor(@NotNull ChatColor color); /** +diff --git a/src/test/java/io/papermc/paper/adventure/KeyTest.java b/src/test/java/io/papermc/paper/adventure/KeyTest.java +new file mode 100644 +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 +--- /dev/null ++++ b/src/test/java/io/papermc/paper/adventure/KeyTest.java +@@ -0,0 +0,0 @@ ++package io.papermc.paper.adventure; ++ ++import java.util.HashSet; ++import java.util.Set; ++import net.kyori.adventure.key.Key; ++import org.bukkit.NamespacedKey; ++import org.junit.Test; ++ ++import static org.junit.Assert.assertEquals; ++import static org.junit.Assert.assertTrue; ++ ++public class KeyTest { ++ ++ @Test ++ public void equalsTest() { ++ Key key = new NamespacedKey("test", "key"); ++ Key key1 = Key.key("test", "key"); ++ assertEquals(key, key1); ++ assertEquals(key1, key); ++ } ++ ++ @Test ++ public void setTest() { ++ Key key = new NamespacedKey("test", "key"); ++ Key key1 = Key.key("test", "key"); ++ Set set = new HashSet<>(Set.of(key)); ++ Set set1 = new HashSet<>(Set.of(key1)); ++ assertTrue(set.contains(key1)); ++ assertTrue(set1.contains(key)); ++ } ++} diff --git a/src/test/java/org/bukkit/AnnotationTest.java b/src/test/java/org/bukkit/AnnotationTest.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/test/java/org/bukkit/AnnotationTest.java