From d06ae79e8d7f4e9fc33e7ab2c3c27f375082049b Mon Sep 17 00:00:00 2001 From: Jikoo Date: Wed, 18 Dec 2024 08:25:29 -0500 Subject: [PATCH] Upgrade Spigot dependency to 1.21.4 --- pom.xml | 2 +- .../planarwrappers/mock/ServerMocks.java | 22 +++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index e3ebf29..fc5a595 100644 --- a/pom.xml +++ b/pom.xml @@ -92,7 +92,7 @@ spigot-api org.spigotmc provided - 1.20.6-R0.1-SNAPSHOT + 1.21.4-R0.1-SNAPSHOT diff --git a/src/test/java/com/github/jikoo/planarwrappers/mock/ServerMocks.java b/src/test/java/com/github/jikoo/planarwrappers/mock/ServerMocks.java index 3f50eb5..13beeb8 100644 --- a/src/test/java/com/github/jikoo/planarwrappers/mock/ServerMocks.java +++ b/src/test/java/com/github/jikoo/planarwrappers/mock/ServerMocks.java @@ -19,6 +19,7 @@ import org.bukkit.Server; import org.bukkit.Tag; import org.jetbrains.annotations.NotNull; +import org.mockito.stubbing.Answer; public final class ServerMocks { @@ -40,7 +41,8 @@ public final class ServerMocks { registers.computeIfAbsent(invocationGetRegistry.getArgument(0), clazz -> { Registry registry = mock(); Map cache = new HashMap<>(); - doAnswer(invocationGetEntry -> { + + Answer getOrThrow = invocationGetEntry -> { NamespacedKey key = invocationGetEntry.getArgument(0); // Some classes (like BlockType and ItemType) have extra generics that will be // erased during runtime calls. To ensure accurate typing, grab the constant's field. @@ -49,10 +51,8 @@ public final class ServerMocks { try { //noinspection unchecked constantClazz = (Class) clazz.getField(key.getKey().toUpperCase(Locale.ROOT).replace('.', '_')).getType(); - } catch (ClassCastException e) { + } catch (ClassCastException | NoSuchFieldException e) { throw new RuntimeException(e); - } catch (NoSuchFieldException e) { - return null; } return cache.computeIfAbsent(key, key1 -> { @@ -60,7 +60,21 @@ public final class ServerMocks { doReturn(key).when(keyed).getKey(); return keyed; }); + }; + + doAnswer(getOrThrow).when(registry).getOrThrow(notNull()); + // For get, return null for nonexistant constants. + doAnswer(invocation -> { + try { + return getOrThrow.answer(invocation); + } catch (RuntimeException e) { + if (e.getCause() instanceof NoSuchFieldException) { + return null; + } + throw e; + } }).when(registry).get(notNull()); + return registry; })) .when(mock).getRegistry(notNull());