Search in sources :

Example 1 with InfoPluginContainer

use of org.lanternpowered.server.plugin.InfoPluginContainer in project LanternServer by LanternPowered.

the class AbstractAssetRepository method registerAsset.

Asset registerAsset(Object plugin, String id, URL url, Path file) {
    final Optional<Asset> optAsset = this.loadedAssets.get(id);
    if (optAsset != null) {
        return optAsset.get();
    }
    PluginContainer pluginContainer;
    if (plugin instanceof String) {
        final String pluginId = (String) plugin;
        // Attempt to find a plugin container that is assigned to the id,
        // if not, create a plugin container that represents the plugin
        // with the id.
        pluginContainer = this.pluginManager.getPlugin(pluginId).orElse(null);
        if (pluginContainer == null) {
            // don't register a plugin in this case, just return a asset with a dummy one.
            if (INFO_FILE_PATTERN.matcher(id).matches()) {
                return new LanternAsset(new SimplePluginContainer(pluginId), id, url, file);
            }
            // Attempt to get plugin info from the repository, and use
            // that to define the plugin container
            final URL infoUrl = getAssetURL(Paths.get(DEFAULT_ASSET_DIR).resolve(pluginId).resolve("plugin.info"));
            if (infoUrl != null) {
                try {
                    final PluginMetadata pluginMetadata = InfoPluginContainer.readPluginInfo(pluginId, infoUrl);
                    // Construct a plugin container
                    pluginContainer = new InfoPluginContainer(pluginId, pluginMetadata);
                } catch (IOException e) {
                    Lantern.getLogger().error("Failed to read plugin.info");
                }
            }
            if (pluginContainer == null) {
                // Generate a simple plugin container
                pluginContainer = new SimplePluginContainer(pluginId);
            }
            // Register the plugin container
            this.pluginManager.registerPlugin(pluginContainer);
            Lantern.getLogger().info("Registered data pack plugin: {} {}", pluginContainer.getName(), pluginContainer.getVersion().orElse("unknown"));
        }
    } else {
        // Search for the plugin container based on the instance
        pluginContainer = this.pluginManager.fromInstance(plugin).get();
    }
    checkNotNull(pluginContainer);
    final LanternAsset asset = new LanternAsset(pluginContainer, id, url, file);
    this.loadedAssets.put(id, Optional.of(asset));
    return asset;
}
Also used : SimplePluginContainer(org.lanternpowered.server.plugin.SimplePluginContainer) PluginContainer(org.spongepowered.api.plugin.PluginContainer) InfoPluginContainer(org.lanternpowered.server.plugin.InfoPluginContainer) SimplePluginContainer(org.lanternpowered.server.plugin.SimplePluginContainer) Asset(org.lanternpowered.api.asset.Asset) PluginMetadata(org.spongepowered.plugin.meta.PluginMetadata) IOException(java.io.IOException) URL(java.net.URL) InfoPluginContainer(org.lanternpowered.server.plugin.InfoPluginContainer)

Aggregations

IOException (java.io.IOException)1 URL (java.net.URL)1 Asset (org.lanternpowered.api.asset.Asset)1 InfoPluginContainer (org.lanternpowered.server.plugin.InfoPluginContainer)1 SimplePluginContainer (org.lanternpowered.server.plugin.SimplePluginContainer)1 PluginContainer (org.spongepowered.api.plugin.PluginContainer)1 PluginMetadata (org.spongepowered.plugin.meta.PluginMetadata)1