#1020: Cast instead of using #typed when getting BlockType and ItemType to better work with testing / mocks

By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
Bukkit/Spigot 2024-06-03 07:15:10 +10:00
parent f9b0675bdd
commit b0df4fc813
3 changed files with 21 additions and 8 deletions

View file

@ -3409,11 +3409,12 @@ public interface BlockType extends Keyed, Translatable {
//</editor-fold>
@NotNull
private static <B extends BlockData> BlockType.Typed<B> getBlockType(@NotNull String key) {
private static <B extends BlockType> B getBlockType(@NotNull String key) {
NamespacedKey namespacedKey = NamespacedKey.minecraft(key);
BlockType.Typed<?> blockType = Registry.BLOCK.get(namespacedKey).typed();
BlockType blockType = Registry.BLOCK.get(namespacedKey);
Preconditions.checkNotNull(blockType, "No BlockType found for %s. This is a bug.", namespacedKey);
return (BlockType.Typed<B>) blockType;
// Cast instead of using BlockType#typed, since block type can be a mock during testing and would return null
return (B) blockType;
}
/**

View file

@ -2257,11 +2257,12 @@ public interface ItemType extends Keyed, Translatable {
//</editor-fold>
@NotNull
private static <M extends ItemMeta> Typed<M> getItemType(@NotNull String key) {
private static <M extends ItemType> M getItemType(@NotNull String key) {
NamespacedKey namespacedKey = NamespacedKey.minecraft(key);
Typed<?> itemType = Registry.ITEM.get(namespacedKey).typed();
ItemType itemType = Registry.ITEM.get(namespacedKey);
Preconditions.checkNotNull(itemType, "No ItemType found for %s. This is a bug.", namespacedKey);
return (Typed<M>) itemType;
// Cast instead of using ItemType#typed, since item type can be a mock during testing and would return null
return (M) itemType;
}
/**

View file

@ -3,6 +3,7 @@ package org.bukkit.support;
import static org.mockito.Mockito.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Logger;
import java.util.stream.Stream;
@ -38,12 +39,22 @@ public final class TestServer {
when(instance.getBukkitVersion()).thenReturn("BukkitVersion_" + TestServer.class.getPackage().getImplementationVersion());
Map<Class<? extends Keyed>, Registry<?>> registers = new HashMap<>();
when(instance.getRegistry(any())).then(invocationOnMock -> registers.computeIfAbsent(invocationOnMock.getArgument(0), aClass -> new Registry<Keyed>() {
when(instance.getRegistry(any())).then(invocationOnMock -> registers.computeIfAbsent(invocationOnMock.getArgument(0), aClass -> new Registry<>() {
private final Map<NamespacedKey, Keyed> cache = new HashMap<>();
@Override
public Keyed get(NamespacedKey key) {
return cache.computeIfAbsent(key, key2 -> mock(aClass, withSettings().stubOnly()));
Class<? extends Keyed> theClass;
// Some registries have extra Typed classes such as BlockType and ItemType.
// To avoid class cast exceptions during init mock the Typed class.
// To get the correct class, we just use the field type.
try {
theClass = (Class<? extends Keyed>) aClass.getField(key.getKey().toUpperCase(Locale.ROOT).replace('.', '_')).getType();
} catch (ClassCastException | NoSuchFieldException e) {
throw new RuntimeException(e);
}
return cache.computeIfAbsent(key, key2 -> mock(theClass, withSettings().stubOnly()));
}
@NotNull