Search in sources :

Example 1 with CatalogMappingData

use of org.lanternpowered.server.game.registry.CatalogMappingData in project LanternServer by LanternPowered.

the class LanternGameRegistry method getCatalogMappingData.

@SuppressWarnings("unchecked")
private List<CatalogMappingData> getCatalogMappingData(RegistryModule module) {
    Map<String, ?> mappings = null;
    if (module instanceof AlternateCatalogRegistryModule) {
        mappings = checkNotNull(((AlternateCatalogRegistryModule) module).provideCatalogMap());
    }
    final List<CatalogMappingData> data = new ArrayList<>();
    for (Field field : module.getClass().getDeclaredFields()) {
        RegisterCatalog annotation = field.getAnnotation(RegisterCatalog.class);
        if (annotation != null) {
            if (mappings == null) {
                try {
                    field.setAccessible(true);
                    mappings = (Map<String, ?>) field.get(module);
                    checkState(!mappings.isEmpty(), "The registered module: " + module.getClass().getSimpleName() + " cannot have an empty mapping during registration!");
                } catch (Exception e) {
                    this.game.getLogger().error("Failed to retrieve a registry field from module: " + module.getClass().getCanonicalName(), e);
                }
            }
            data.add(new CatalogMappingData(annotation, mappings));
        }
    }
    if (module instanceof CatalogMappingDataHolder) {
        data.addAll(((CatalogMappingDataHolder) module).getCatalogMappings());
    }
    return data;
}
Also used : Field(java.lang.reflect.Field) AlternateCatalogRegistryModule(org.spongepowered.api.registry.AlternateCatalogRegistryModule) ArrayList(java.util.ArrayList) RegisterCatalog(org.spongepowered.api.registry.util.RegisterCatalog) CatalogMappingData(org.lanternpowered.server.game.registry.CatalogMappingData) CyclicGraphException(org.lanternpowered.server.util.graph.CyclicGraphException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CatalogMappingDataHolder(org.lanternpowered.server.game.registry.CatalogMappingDataHolder)

Example 2 with CatalogMappingData

use of org.lanternpowered.server.game.registry.CatalogMappingData in project LanternServer by LanternPowered.

the class AccessoryRegistryModule method getCatalogMappings.

@Override
public List<CatalogMappingData> getCatalogMappings() {
    final ImmutableList.Builder<CatalogMappingData> mappingData = ImmutableList.builder();
    mappingData.addAll(super.getCatalogMappings());
    final ImmutableMap.Builder<String, Accessory> topHatMappings = ImmutableMap.builder();
    getAll().stream().filter(accessory -> accessory instanceof TopHat).forEach(accessory -> topHatMappings.put(accessory.getName().replace("_top_hat", ""), accessory));
    mappingData.add(new CatalogMappingData(TopHats.class, topHatMappings.build()));
    return mappingData.build();
}
Also used : List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) PluginCatalogRegistryModule(org.lanternpowered.server.game.registry.PluginCatalogRegistryModule) DyeColors(org.spongepowered.api.data.type.DyeColors) ImmutableMap(com.google.common.collect.ImmutableMap) TopHats(org.lanternpowered.server.extra.accessory.TopHats) RegistrationDependency(org.spongepowered.api.registry.util.RegistrationDependency) CatalogMappingData(org.lanternpowered.server.game.registry.CatalogMappingData) TopHat(org.lanternpowered.server.extra.accessory.TopHat) DyeColorRegistryModule(org.lanternpowered.server.game.registry.type.data.DyeColorRegistryModule) LanternTopHat(org.lanternpowered.server.extra.accessory.LanternTopHat) Accessory(org.lanternpowered.server.extra.accessory.Accessory) TopHat(org.lanternpowered.server.extra.accessory.TopHat) LanternTopHat(org.lanternpowered.server.extra.accessory.LanternTopHat) Accessory(org.lanternpowered.server.extra.accessory.Accessory) ImmutableList(com.google.common.collect.ImmutableList) TopHats(org.lanternpowered.server.extra.accessory.TopHats) CatalogMappingData(org.lanternpowered.server.game.registry.CatalogMappingData) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with CatalogMappingData

use of org.lanternpowered.server.game.registry.CatalogMappingData in project LanternServer by LanternPowered.

the class LanternGameRegistry method tryModulePhaseRegistration.

private void tryModulePhaseRegistration(RegistryModule module) {
    try {
        final Set<Method> methods = getCustomRegistrations(module);
        methods.stream().filter(this::isProperPhase).forEach(method -> invokeCustomRegistration(module, method));
        if (isProperPhase(module)) {
            module.registerDefaults();
            for (CatalogMappingData data : getCatalogMappingData(module)) {
                final Map<String, ?> mappings = data.getMappings();
                if (mappings.isEmpty()) {
                    return;
                }
                RegistryHelper.mapFields(data.getTarget(), mappings, data.getIgnoredFields());
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Error trying to initialize module: " + module.getClass().getCanonicalName(), e);
    }
}
Also used : Method(java.lang.reflect.Method) CatalogMappingData(org.lanternpowered.server.game.registry.CatalogMappingData) CyclicGraphException(org.lanternpowered.server.util.graph.CyclicGraphException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

CatalogMappingData (org.lanternpowered.server.game.registry.CatalogMappingData)3 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 CyclicGraphException (org.lanternpowered.server.util.graph.CyclicGraphException)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Accessory (org.lanternpowered.server.extra.accessory.Accessory)1 LanternTopHat (org.lanternpowered.server.extra.accessory.LanternTopHat)1 TopHat (org.lanternpowered.server.extra.accessory.TopHat)1 TopHats (org.lanternpowered.server.extra.accessory.TopHats)1 CatalogMappingDataHolder (org.lanternpowered.server.game.registry.CatalogMappingDataHolder)1 PluginCatalogRegistryModule (org.lanternpowered.server.game.registry.PluginCatalogRegistryModule)1 DyeColorRegistryModule (org.lanternpowered.server.game.registry.type.data.DyeColorRegistryModule)1 DyeColors (org.spongepowered.api.data.type.DyeColors)1 AlternateCatalogRegistryModule (org.spongepowered.api.registry.AlternateCatalogRegistryModule)1 RegisterCatalog (org.spongepowered.api.registry.util.RegisterCatalog)1