Search in sources :

Example 11 with CatalogType

use of org.spongepowered.api.CatalogType in project SpongeCommon by SpongePowered.

the class SpongeArgumentType method getConverter.

@SuppressWarnings("unchecked")
static <T> Function<String, T> getConverter(final Class<T> type, String converterKey) {
    if (!converters.containsKey(converterKey)) {
        try {
            final Method valueOf = type.getMethod("valueOf", String.class);
            converters.put(converterKey, SpongeSelectorFactory.<String, T>methodAsFunction(valueOf, true));
        } catch (NoSuchMethodException ignored) {
            if (CatalogType.class.isAssignableFrom(type)) {
                final Class<? extends CatalogType> type2 = type.asSubclass(CatalogType.class);
                converters.put(converterKey, (Function<String, T>) input -> {
                    return (T) SpongeImpl.getGame().getRegistry().getType(type2, input).get();
                });
            } else {
                throw new IllegalStateException("can't convert " + type);
            }
        } catch (SecurityException e) {
            SpongeImpl.getLogger().catching(e);
        }
    }
    return (Function<String, T>) converters.get(converterKey);
}
Also used : Function(java.util.function.Function) CatalogType(org.spongepowered.api.CatalogType) Method(java.lang.reflect.Method)

Example 12 with CatalogType

use of org.spongepowered.api.CatalogType in project SpongeCommon by SpongePowered.

the class CatalogTypeClassesTest method testCatalogCanBeRetrieved.

@Test
public void testCatalogCanBeRetrieved() throws Exception {
    assumeFalse(this.isDummy);
    final CatalogType o = (CatalogType) this.targetedField.get(null);
    this.registryModule.getById(o.getId()).orElseThrow(() -> new RegistryException("Could not locate " + this.fieldName + " in the registry: " + this.registryModule));
}
Also used : CatalogType(org.spongepowered.api.CatalogType) RegistryException(org.spongepowered.api.registry.RegistryException) Test(org.junit.Test)

Example 13 with CatalogType

use of org.spongepowered.api.CatalogType in project SpongeCommon by SpongePowered.

the class RegistryTestUtil method generateCatalogContainerTestObjects.

static Iterable<Object[]> generateCatalogContainerTestObjects() {
    final SpongeGameRegistry registry = (SpongeGameRegistry) Sponge.getGame().getRegistry();
    final ArrayList<Object[]> objects = new ArrayList<>();
    for (Map.Entry<Class<? extends CatalogType>, CatalogRegistryModule<?>> entry : registry.catalogRegistryMap.entrySet()) {
        final Class<? extends CatalogType> key = entry.getKey();
        final CatalogedBy catalogedBy = key.getAnnotation(CatalogedBy.class);
        if (catalogedBy != null) {
            for (Class<?> containerClass : catalogedBy.value()) {
                for (Field field : containerClass.getFields()) {
                    if (Modifier.isStatic(field.getModifiers())) {
                        objects.add(new Object[] { field.getName(), entry.getKey(), containerClass, entry.getValue(), field });
                    }
                }
            }
        }
    }
    return objects;
}
Also used : Field(java.lang.reflect.Field) CatalogType(org.spongepowered.api.CatalogType) CatalogRegistryModule(org.spongepowered.api.registry.CatalogRegistryModule) CatalogedBy(org.spongepowered.api.util.annotation.CatalogedBy) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 14 with CatalogType

use of org.spongepowered.api.CatalogType in project SpongeCommon by SpongePowered.

the class RegistryTestUtil method generateRegistryTestObjects.

static Iterable<Object[]> generateRegistryTestObjects() {
    final SpongeGameRegistry registry = (SpongeGameRegistry) Sponge.getGame().getRegistry();
    final ArrayList<Object[]> array = new ArrayList<>();
    for (Map.Entry<Class<? extends CatalogType>, CatalogRegistryModule<?>> entry : registry.catalogRegistryMap.entrySet()) {
        for (CatalogType catalogType : entry.getValue().getAll()) {
            array.add(new Object[] { entry.getKey().getSimpleName(), entry.getKey(), entry.getValue(), catalogType, catalogType.getId() });
        }
    }
    return array;
}
Also used : CatalogType(org.spongepowered.api.CatalogType) CatalogRegistryModule(org.spongepowered.api.registry.CatalogRegistryModule) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 15 with CatalogType

use of org.spongepowered.api.CatalogType in project SpongeCommon by SpongePowered.

the class RegistryTestUtil method generateCatalogTypeMethodTestObjects.

static Iterable<Object[]> generateCatalogTypeMethodTestObjects() {
    final SpongeGameRegistry registry = (SpongeGameRegistry) Sponge.getGame().getRegistry();
    final ArrayList<Object[]> array = new ArrayList<>();
    for (Map.Entry<Class<? extends CatalogType>, CatalogRegistryModule<?>> entry : registry.catalogRegistryMap.entrySet()) {
        for (CatalogType catalogType : entry.getValue().getAll()) {
            for (Method method : getTestableApiMethods(getApplicableApiCatalogTypeInterfaces(catalogType))) {
                array.add(new Object[] { entry.getKey().getSimpleName(), entry.getKey(), catalogType, catalogType.getId(), method, method.getDeclaringClass().getSimpleName() + "#" + method.getName() + "()", catalogType.getClass().getName() });
            }
        }
    }
    return array;
}
Also used : CatalogType(org.spongepowered.api.CatalogType) CatalogRegistryModule(org.spongepowered.api.registry.CatalogRegistryModule) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Map(java.util.Map)

Aggregations

CatalogType (org.spongepowered.api.CatalogType)26 Map (java.util.Map)12 ArrayList (java.util.ArrayList)8 ItemDataNode (io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode)7 DataView (org.spongepowered.api.data.DataView)6 Nullable (javax.annotation.Nullable)5 CatalogRegistryModule (org.spongepowered.api.registry.CatalogRegistryModule)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 Collection (java.util.Collection)4 List (java.util.List)4 Optional (java.util.Optional)4 Sponge (org.spongepowered.api.Sponge)4 DataContainer (org.spongepowered.api.data.DataContainer)4 Text (org.spongepowered.api.text.Text)4 ImmutableList (com.google.common.collect.ImmutableList)3 IOException (java.io.IOException)3 Method (java.lang.reflect.Method)3 IdentityHashMap (java.util.IdentityHashMap)3 BlockState (org.spongepowered.api.block.BlockState)3 DataSerializable (org.spongepowered.api.data.DataSerializable)3