#1340: Centralize the conversion from and to Minecraft / Bukkit registry items even more and add a test case for them
By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package org.bukkit;
|
||||
package org.bukkit.registry;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import com.google.common.collect.Lists;
|
||||
@@ -6,6 +6,9 @@ import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.stream.Stream;
|
||||
import org.bukkit.Keyed;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.Registry;
|
||||
import org.bukkit.craftbukkit.CraftRegistry;
|
||||
import org.bukkit.support.AbstractTestingBase;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
@@ -0,0 +1,50 @@
|
||||
package org.bukkit.registry;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import com.google.common.base.Joiner;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import org.bukkit.Registry;
|
||||
import org.bukkit.support.AbstractTestingBase;
|
||||
import org.bukkit.support.DummyServer;
|
||||
import org.bukkit.support.provider.RegistriesArgumentProvider;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
|
||||
/**
|
||||
* This class tests, if all default registries present in {@link Registry} are added to {@link RegistriesArgumentProvider}
|
||||
*/
|
||||
public class RegistryArgumentAddedTest extends AbstractTestingBase {
|
||||
|
||||
@Test
|
||||
public void testPresent() throws ClassNotFoundException {
|
||||
// Make sure every registry is created
|
||||
Class.forName(Registry.class.getName());
|
||||
|
||||
Set<Class<?>> loadedRegistries = new HashSet<>(DummyServer.registers.keySet());
|
||||
Set<Class<?>> notFound = new HashSet<>();
|
||||
|
||||
RegistriesArgumentProvider
|
||||
.getData()
|
||||
.map(Arguments::get)
|
||||
.map(array -> array[0])
|
||||
.map(clazz -> (Class<?>) clazz)
|
||||
.forEach(clazz -> {
|
||||
if (!loadedRegistries.remove(clazz)) {
|
||||
notFound.add(clazz);
|
||||
}
|
||||
});
|
||||
|
||||
assertTrue(loadedRegistries.isEmpty(), String.format("""
|
||||
There are registries present, which are not registered in RegistriesArgumentProvider.
|
||||
|
||||
Add the following registries to the RegistriesArgumentProvider class, so that they can be tested.
|
||||
%s""", Joiner.on('\n').join(loadedRegistries)));
|
||||
|
||||
assertTrue(notFound.isEmpty(), String.format("""
|
||||
There are more registries present in RegistriesArgumentProvider then loaded by Registry class.
|
||||
|
||||
Remove the following registries from the RegistriesArgumentProvider class.
|
||||
%s""", Joiner.on('\n').join(notFound)));
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.bukkit;
|
||||
package org.bukkit.registry;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import java.lang.reflect.Field;
|
||||
@@ -9,6 +9,9 @@ import net.minecraft.core.IRegistry;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.resources.MinecraftKey;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import org.bukkit.Keyed;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.Registry;
|
||||
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
|
||||
import org.bukkit.inventory.meta.trim.TrimMaterial;
|
||||
import org.bukkit.inventory.meta.trim.TrimPattern;
|
||||
@@ -0,0 +1,306 @@
|
||||
package org.bukkit.registry;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assumptions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import com.google.common.base.Joiner;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import net.minecraft.core.IRegistry;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Keyed;
|
||||
import org.bukkit.Registry;
|
||||
import org.bukkit.craftbukkit.util.Handleable;
|
||||
import org.bukkit.support.AbstractTestingBase;
|
||||
import org.bukkit.support.provider.RegistryArgumentProvider;
|
||||
import org.bukkit.support.test.RegistriesTest;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
public class RegistryConversionTest extends AbstractTestingBase {
|
||||
|
||||
private static final String MINECRAFT_TO_BUKKIT = "minecraftToBukkit";
|
||||
private static final String BUKKIT_TO_MINECRAFT = "bukkitToMinecraft";
|
||||
|
||||
private static final Map<Class<? extends Keyed>, Method> MINECRAFT_TO_BUKKIT_METHODS = new HashMap<>();
|
||||
private static final Map<Class<? extends Keyed>, Method> BUKKIT_TO_MINECRAFT_METHODS = new HashMap<>();
|
||||
|
||||
private static final Set<Class<? extends Keyed>> IMPLEMENT_HANDLE_ABLE = new HashSet<>();
|
||||
|
||||
@Order(1)
|
||||
@RegistriesTest
|
||||
public void testHandleableImplementation(Class<? extends Keyed> clazz) {
|
||||
Set<Class<? extends Keyed>> notImplemented = new HashSet<>();
|
||||
Registry<? extends Keyed> registry = Bukkit.getRegistry(clazz);
|
||||
|
||||
for (Keyed item : registry) {
|
||||
if (!(item instanceof Handleable<?>)) {
|
||||
notImplemented.add(item.getClass());
|
||||
}
|
||||
}
|
||||
|
||||
assertTrue(notImplemented.isEmpty(), String.format("""
|
||||
Not all implementations of the registry from the class %s have the Handleable interface implemented.
|
||||
Every Implementation should implement the Handleable interface.
|
||||
|
||||
The following implementation do not implement Handleable:
|
||||
%s""", clazz.getName(), Joiner.on('\n').join(notImplemented)));
|
||||
|
||||
IMPLEMENT_HANDLE_ABLE.add(clazz);
|
||||
}
|
||||
|
||||
@Order(2)
|
||||
@RegistriesTest
|
||||
public void testMinecraftToBukkitPresent(Class<? extends Keyed> clazz, ResourceKey<IRegistry<?>> registryKey,
|
||||
Class<? extends Keyed> craftClazz, Class<?> minecraftClazz) {
|
||||
Method method = null;
|
||||
try {
|
||||
method = craftClazz.getDeclaredMethod(MINECRAFT_TO_BUKKIT, minecraftClazz);
|
||||
} catch (NoSuchMethodException e) {
|
||||
fail(String.format("""
|
||||
The class %s does not have a public static method to convert a minecraft value to a bukkit value.
|
||||
|
||||
Following method should be add which, returns the bukkit value based on the minecraft value.
|
||||
%s
|
||||
""", craftClazz, buildMinecraftToBukkitMethod(clazz, minecraftClazz)));
|
||||
}
|
||||
|
||||
assertTrue(Modifier.isPublic(method.getModifiers()), String.format("""
|
||||
The method %s in class %s is not public.
|
||||
|
||||
The method should be made public, method structure:
|
||||
%s
|
||||
""", MINECRAFT_TO_BUKKIT, craftClazz, buildMinecraftToBukkitMethod(clazz, minecraftClazz)));
|
||||
|
||||
assertTrue(Modifier.isStatic(method.getModifiers()), String.format("""
|
||||
The method %s in class %s is not static.
|
||||
|
||||
The method should be made static, method structure:
|
||||
%s
|
||||
""", MINECRAFT_TO_BUKKIT, craftClazz, buildMinecraftToBukkitMethod(clazz, minecraftClazz)));
|
||||
|
||||
assertSame(clazz, method.getReturnType(), String.format("""
|
||||
The method %s in class %s has the wrong return value.
|
||||
|
||||
The method should have the correct return value, method structure:
|
||||
%s
|
||||
""", MINECRAFT_TO_BUKKIT, craftClazz, buildMinecraftToBukkitMethod(clazz, minecraftClazz)));
|
||||
|
||||
MINECRAFT_TO_BUKKIT_METHODS.put(clazz, method);
|
||||
}
|
||||
|
||||
private String buildMinecraftToBukkitMethod(Class<? extends Keyed> clazz, Class<?> minecraftClazz) {
|
||||
return String.format("""
|
||||
public static %s minecraftToBukkit(%s minecraft) {
|
||||
[...]
|
||||
}
|
||||
""", clazz.getSimpleName(), minecraftClazz.getName());
|
||||
}
|
||||
|
||||
@Order(2)
|
||||
@RegistriesTest
|
||||
public void testBukkitToMinecraftPresent(Class<? extends Keyed> clazz, ResourceKey<IRegistry<?>> registryKey,
|
||||
Class<? extends Keyed> craftClazz, Class<?> minecraftClazz) {
|
||||
Method method = null;
|
||||
try {
|
||||
method = craftClazz.getDeclaredMethod(BUKKIT_TO_MINECRAFT, clazz);
|
||||
} catch (NoSuchMethodException e) {
|
||||
fail(String.format("""
|
||||
The class %s does not have a public static method to convert a bukkit value to a minecraft value.
|
||||
|
||||
Following method should be add which, returns the minecraft value based on the bukkit value.
|
||||
%s
|
||||
""", craftClazz, buildBukkitToMinecraftMethod(clazz, minecraftClazz)));
|
||||
}
|
||||
|
||||
assertTrue(Modifier.isPublic(method.getModifiers()), String.format("""
|
||||
The method %s in class %s is not public.
|
||||
|
||||
The method should be made public, method structure:
|
||||
%s
|
||||
""", BUKKIT_TO_MINECRAFT, craftClazz, buildBukkitToMinecraftMethod(clazz, minecraftClazz)));
|
||||
|
||||
assertTrue(Modifier.isStatic(method.getModifiers()), String.format("""
|
||||
The method %s in class %s is not static.
|
||||
|
||||
The method should be made static, method structure:
|
||||
%s
|
||||
""", BUKKIT_TO_MINECRAFT, craftClazz, buildBukkitToMinecraftMethod(clazz, minecraftClazz)));
|
||||
|
||||
assertSame(minecraftClazz, method.getReturnType(), String.format("""
|
||||
The method %s in class %s has the wrong return value.
|
||||
|
||||
The method should have the correct return value, method structure:
|
||||
%s
|
||||
""", BUKKIT_TO_MINECRAFT, craftClazz, buildBukkitToMinecraftMethod(clazz, minecraftClazz)));
|
||||
|
||||
BUKKIT_TO_MINECRAFT_METHODS.put(clazz, method);
|
||||
}
|
||||
|
||||
private String buildBukkitToMinecraftMethod(Class<? extends Keyed> clazz, Class<?> minecraftClazz) {
|
||||
return String.format("""
|
||||
public static %s bukkitToMinecraft(%s bukkit) {
|
||||
[...]
|
||||
}
|
||||
""", minecraftClazz.getName(), clazz.getSimpleName());
|
||||
}
|
||||
|
||||
@Order(2)
|
||||
@RegistriesTest
|
||||
public void testMinecraftToBukkitNullValue(Class<? extends Keyed> clazz) throws IllegalAccessException {
|
||||
checkValidMinecraftToBukkit(clazz);
|
||||
|
||||
try {
|
||||
Object result = MINECRAFT_TO_BUKKIT_METHODS.get(clazz).invoke(null, (Object) null);
|
||||
fail(String.format("""
|
||||
Method %s in class %s should not accept null values and should throw a IllegalArgumentException.
|
||||
Got '%s' as return object.
|
||||
""", MINECRAFT_TO_BUKKIT, clazz.getName(), result));
|
||||
} catch (InvocationTargetException e) {
|
||||
// #invoke wraps the error in a InvocationTargetException, so we need to check it this way
|
||||
assertSame(IllegalArgumentException.class, e.getCause().getClass(), String.format("""
|
||||
Method %s in class %s should not accept null values and should throw a IllegalArgumentException.
|
||||
""", MINECRAFT_TO_BUKKIT, clazz.getName()));
|
||||
}
|
||||
}
|
||||
|
||||
@Order(3)
|
||||
@RegistriesTest
|
||||
public void testBukkitToMinecraftNullValue(Class<? extends Keyed> clazz) throws IllegalAccessException {
|
||||
checkValidBukkitToMinecraft(clazz);
|
||||
|
||||
try {
|
||||
Object result = BUKKIT_TO_MINECRAFT_METHODS.get(clazz).invoke(null, (Object) null);
|
||||
fail(String.format("""
|
||||
Method %s in class %s should not accept null values and should throw a IllegalArgumentException.
|
||||
Got '%s' as return object.
|
||||
""", BUKKIT_TO_MINECRAFT, clazz.getName(), result));
|
||||
} catch (InvocationTargetException e) {
|
||||
// #invoke wraps the error in a InvocationTargetException, so we need to check it this way
|
||||
assertSame(IllegalArgumentException.class, e.getCause().getClass(), String.format("""
|
||||
Method %s in class %s should not accept null values and should throw a IllegalArgumentException.
|
||||
""", BUKKIT_TO_MINECRAFT, clazz.getName()));
|
||||
}
|
||||
}
|
||||
|
||||
@Order(3)
|
||||
@RegistriesTest
|
||||
public void testMinecraftToBukkit(Class<? extends Keyed> clazz) {
|
||||
checkValidMinecraftToBukkit(clazz);
|
||||
checkValidHandle(clazz);
|
||||
|
||||
Map<Object, Object> notMatching = new HashMap<>();
|
||||
Method method = MINECRAFT_TO_BUKKIT_METHODS.get(clazz);
|
||||
|
||||
RegistryArgumentProvider.getValues(clazz).map(Arguments::get).forEach(arguments -> {
|
||||
Keyed bukkit = (Keyed) arguments[0];
|
||||
Object minecraft = arguments[1];
|
||||
|
||||
try {
|
||||
Object otherBukkit = method.invoke(null, minecraft);
|
||||
if (bukkit != otherBukkit) {
|
||||
notMatching.put(bukkit, otherBukkit);
|
||||
}
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
|
||||
assertTrue(notMatching.isEmpty(), String.format("""
|
||||
The method %s in class %s does not match all registry items correctly.
|
||||
|
||||
Following registry items where match not correctly:
|
||||
%s""", MINECRAFT_TO_BUKKIT, clazz.getName(),
|
||||
Joiner.on('\n').withKeyValueSeparator(" got: ").join(notMatching)));
|
||||
}
|
||||
|
||||
@Order(3)
|
||||
@RegistriesTest
|
||||
public void testBukkitToMinecraft(Class<? extends Keyed> clazz) {
|
||||
checkValidBukkitToMinecraft(clazz);
|
||||
checkValidHandle(clazz);
|
||||
|
||||
Map<Object, Object> notMatching = new HashMap<>();
|
||||
Method method = BUKKIT_TO_MINECRAFT_METHODS.get(clazz);
|
||||
|
||||
RegistryArgumentProvider.getValues(clazz).map(Arguments::get).forEach(arguments -> {
|
||||
Keyed bukkit = (Keyed) arguments[0];
|
||||
Object minecraft = arguments[1];
|
||||
|
||||
try {
|
||||
Object otherMinecraft = method.invoke(null, bukkit);
|
||||
if (minecraft != otherMinecraft) {
|
||||
notMatching.put(minecraft, otherMinecraft);
|
||||
}
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
|
||||
assertTrue(notMatching.isEmpty(), String.format("""
|
||||
The method %s in class %s does not match all registry items correctly.
|
||||
|
||||
Following registry items where match not correctly:
|
||||
%s""", BUKKIT_TO_MINECRAFT, clazz.getName(),
|
||||
Joiner.on('\n').withKeyValueSeparator(" got: ").join(notMatching)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Minecraft registry can return a default key / value
|
||||
* when the passed minecraft value is not registry in this case, we want it to throw an error.
|
||||
*/
|
||||
@Order(3)
|
||||
@RegistriesTest
|
||||
public void testMinecraftToBukkitNoValidMinecraft(Class<? extends Keyed> clazz, ResourceKey<IRegistry<?>> registryKey,
|
||||
Class<? extends Keyed> craftClazz, Class<?> minecraftClazz) throws IllegalAccessException {
|
||||
checkValidMinecraftToBukkit(clazz);
|
||||
|
||||
try {
|
||||
|
||||
Object minecraft = mock(minecraftClazz);
|
||||
Object result = MINECRAFT_TO_BUKKIT_METHODS.get(clazz).invoke(null, minecraft);
|
||||
fail(String.format("""
|
||||
Method %s in class %s should not accept a none registered value and should throw a IllegalStateException.
|
||||
Got '%s' as return object.
|
||||
""", MINECRAFT_TO_BUKKIT, clazz.getName(), result));
|
||||
} catch (InvocationTargetException e) {
|
||||
// #invoke wraps the error in a InvocationTargetException, so we need to check it this way
|
||||
assertSame(IllegalStateException.class, e.getCause().getClass(), String.format("""
|
||||
Method %s in class %s should not accept a none registered value and should throw a IllegalStateException.
|
||||
""", MINECRAFT_TO_BUKKIT, clazz.getName()));
|
||||
}
|
||||
}
|
||||
|
||||
private void checkValidBukkitToMinecraft(Class<? extends Keyed> clazz) {
|
||||
assumeTrue(BUKKIT_TO_MINECRAFT_METHODS.containsKey(clazz), String.format("""
|
||||
Cannot test class %s, because it does not have a valid %s method.
|
||||
|
||||
Check test results of testBukkitToMinecraftPresent for more information.
|
||||
""", clazz.getName(), BUKKIT_TO_MINECRAFT));
|
||||
}
|
||||
|
||||
private void checkValidMinecraftToBukkit(Class<? extends Keyed> clazz) {
|
||||
assumeTrue(MINECRAFT_TO_BUKKIT_METHODS.containsKey(clazz), String.format("""
|
||||
Cannot test class %s, because it does not have a valid %s method.
|
||||
|
||||
Check test results of testMinecraftToBukkitPresent for more information.
|
||||
""", clazz.getName(), MINECRAFT_TO_BUKKIT));
|
||||
}
|
||||
|
||||
private void checkValidHandle(Class<? extends Keyed> clazz) {
|
||||
assumeTrue(IMPLEMENT_HANDLE_ABLE.contains(clazz), String.format("""
|
||||
Cannot test class %s, because it does not implement Handleable.
|
||||
|
||||
Check test results of testHandleableImplementation for more information.
|
||||
""", clazz.getName()));
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.bukkit;
|
||||
package org.bukkit.registry;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import com.mojang.serialization.Lifecycle;
|
||||
@@ -9,6 +9,9 @@ import net.minecraft.core.IRegistry;
|
||||
import net.minecraft.core.RegistryMaterials;
|
||||
import net.minecraft.resources.MinecraftKey;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import org.bukkit.Keyed;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.Registry;
|
||||
import org.bukkit.craftbukkit.CraftRegistry;
|
||||
import org.bukkit.support.AbstractTestingBase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -1,9 +1,13 @@
|
||||
package org.bukkit.support;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Logger;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Keyed;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Registry;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.craftbukkit.CraftLootTable;
|
||||
import org.bukkit.craftbukkit.CraftRegistry;
|
||||
@@ -12,9 +16,12 @@ import org.bukkit.craftbukkit.inventory.CraftItemFactory;
|
||||
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
|
||||
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
|
||||
import org.bukkit.craftbukkit.util.Versioning;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
public final class DummyServer {
|
||||
|
||||
public static final Map<Class<?>, Registry<?>> registers = new HashMap<>();
|
||||
|
||||
static {
|
||||
try {
|
||||
Server instance = mock(withSettings().stubOnly());
|
||||
@@ -36,7 +43,10 @@ public final class DummyServer {
|
||||
when(instance.getLootTable(any())).then(mock -> new CraftLootTable(mock.getArgument(0),
|
||||
AbstractTestingBase.DATA_PACK.getLootData().getLootTable(CraftNamespacedKey.toMinecraft(mock.getArgument(0)))));
|
||||
|
||||
when(instance.getRegistry(any())).then(mock -> CraftRegistry.createRegistry(mock.getArgument(0), AbstractTestingBase.REGISTRY_CUSTOM));
|
||||
when(instance.getRegistry(any())).then((Answer<Registry<?>>) mock -> {
|
||||
Class<? extends Keyed> aClass = mock.getArgument(0);
|
||||
return registers.computeIfAbsent(aClass, key -> CraftRegistry.createRegistry(aClass, AbstractTestingBase.REGISTRY_CUSTOM));
|
||||
});
|
||||
|
||||
Bukkit.setServer(instance);
|
||||
} catch (Throwable t) {
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package org.bukkit.support.provider;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.world.effect.MobEffectList;
|
||||
import net.minecraft.world.item.Instrument;
|
||||
import org.bukkit.GameEvent;
|
||||
import org.bukkit.MusicInstrument;
|
||||
import org.bukkit.craftbukkit.CraftGameEvent;
|
||||
import org.bukkit.craftbukkit.CraftMusicInstrument;
|
||||
import org.bukkit.craftbukkit.enchantments.CraftEnchantment;
|
||||
import org.bukkit.craftbukkit.generator.structure.CraftStructure;
|
||||
import org.bukkit.craftbukkit.generator.structure.CraftStructureType;
|
||||
import org.bukkit.craftbukkit.inventory.trim.CraftTrimMaterial;
|
||||
import org.bukkit.craftbukkit.inventory.trim.CraftTrimPattern;
|
||||
import org.bukkit.craftbukkit.potion.CraftPotionEffectType;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.generator.structure.Structure;
|
||||
import org.bukkit.generator.structure.StructureType;
|
||||
import org.bukkit.inventory.meta.trim.TrimMaterial;
|
||||
import org.bukkit.inventory.meta.trim.TrimPattern;
|
||||
import org.bukkit.potion.PotionEffectType;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.ArgumentsProvider;
|
||||
|
||||
public class RegistriesArgumentProvider implements ArgumentsProvider {
|
||||
|
||||
private static final List<Arguments> DATA = Lists.newArrayList();
|
||||
|
||||
static {
|
||||
// Order: Bukkit class, Minecraft Registry key, CraftBukkit class, Minecraft class
|
||||
DATA.add(Arguments.of(Enchantment.class, Registries.ENCHANTMENT, CraftEnchantment.class, net.minecraft.world.item.enchantment.Enchantment.class));
|
||||
DATA.add(Arguments.of(GameEvent.class, Registries.GAME_EVENT, CraftGameEvent.class, net.minecraft.world.level.gameevent.GameEvent.class));
|
||||
DATA.add(Arguments.of(MusicInstrument.class, Registries.INSTRUMENT, CraftMusicInstrument.class, Instrument.class));
|
||||
DATA.add(Arguments.of(PotionEffectType.class, Registries.MOB_EFFECT, CraftPotionEffectType.class, MobEffectList.class));
|
||||
DATA.add(Arguments.of(Structure.class, Registries.STRUCTURE, CraftStructure.class, net.minecraft.world.level.levelgen.structure.Structure.class));
|
||||
DATA.add(Arguments.of(StructureType.class, Registries.STRUCTURE_TYPE, CraftStructureType.class, net.minecraft.world.level.levelgen.structure.StructureType.class));
|
||||
DATA.add(Arguments.of(TrimMaterial.class, Registries.TRIM_MATERIAL, CraftTrimMaterial.class, net.minecraft.world.item.armortrim.TrimMaterial.class));
|
||||
DATA.add(Arguments.of(TrimPattern.class, Registries.TRIM_PATTERN, CraftTrimPattern.class, net.minecraft.world.item.armortrim.TrimPattern.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) throws Exception {
|
||||
return getData();
|
||||
}
|
||||
|
||||
public static Stream<? extends Arguments> getData() {
|
||||
return DATA.stream();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.bukkit.support.provider;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Keyed;
|
||||
import org.bukkit.Registry;
|
||||
import org.bukkit.craftbukkit.util.Handleable;
|
||||
import org.bukkit.support.test.RegistryTest;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.ArgumentsProvider;
|
||||
import org.junit.jupiter.params.support.AnnotationConsumer;
|
||||
|
||||
public class RegistryArgumentProvider implements ArgumentsProvider, AnnotationConsumer<RegistryTest> {
|
||||
|
||||
private Class<? extends Keyed> registryType;
|
||||
|
||||
@Override
|
||||
public void accept(RegistryTest registryTest) {
|
||||
registryType = registryTest.value();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) throws Exception {
|
||||
return getValues(registryType);
|
||||
}
|
||||
|
||||
public static Stream<? extends Arguments> getValues(Class<? extends Keyed> registryType) {
|
||||
Registry<?> registry = Bukkit.getRegistry(registryType);
|
||||
return registry.stream().map(keyed -> (Handleable<?>) keyed)
|
||||
.map(handleAble -> Arguments.of(handleAble, handleAble.getHandle()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.bukkit.support.test;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import org.bukkit.support.provider.RegistriesArgumentProvider;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ArgumentsSource;
|
||||
|
||||
/**
|
||||
* Order: Bukkit class, Minecraft Registry key, CraftBukkit class, Minecraft class
|
||||
*/
|
||||
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@ArgumentsSource(RegistriesArgumentProvider.class)
|
||||
@ParameterizedTest
|
||||
public @interface RegistriesTest {
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.bukkit.support.test;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import org.bukkit.Keyed;
|
||||
import org.bukkit.support.provider.RegistryArgumentProvider;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ArgumentsSource;
|
||||
|
||||
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@ArgumentsSource(RegistryArgumentProvider.class)
|
||||
@ParameterizedTest
|
||||
public @interface RegistryTest {
|
||||
|
||||
Class<? extends Keyed> value();
|
||||
}
|
||||
Reference in New Issue
Block a user