Skip to content

Commit

Permalink
Upgrade Spigot dependency to 1.21.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Jikoo committed Dec 18, 2024
1 parent ccabd2a commit d06ae79
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
<artifactId>spigot-api</artifactId>
<groupId>org.spigotmc</groupId>
<scope>provided</scope>
<version>1.20.6-R0.1-SNAPSHOT</version>
<version>1.21.4-R0.1-SNAPSHOT</version>
</dependency>
<!-- Optional runtime dependencies not exposed to dependents -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -40,7 +41,8 @@ public final class ServerMocks {
registers.computeIfAbsent(invocationGetRegistry.getArgument(0), clazz -> {
Registry<?> registry = mock();
Map<NamespacedKey, Keyed> cache = new HashMap<>();
doAnswer(invocationGetEntry -> {

Answer<Keyed> 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.
Expand All @@ -49,18 +51,30 @@ public final class ServerMocks {
try {
//noinspection unchecked
constantClazz = (Class<? extends Keyed>) 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 -> {
Keyed keyed = mock(constantClazz);
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());
Expand Down

0 comments on commit d06ae79

Please sign in to comment.