<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Plugin Manager &mdash; Sponge 6.0.0 documentation</title>

    <link rel="shortcut icon" href="../_static/favicon.ico"/>




    <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />

    <link rel="stylesheet" href="../_static/spongedocs.css" type="text/css" />

        <link rel="index" title="Index"
              href="../genindex.html"/>        <link rel="search" title="Search" href="../search.html"/>    <link rel="top" title="Sponge 6.0.0 documentation" href="../index.html"/>        <link rel="up" title="Creating a Plugin" href="index.html"/>        <link rel="next" title="Game Profile Manager" href="game-profile-manager.html"/>        <link rel="prev" title="Modifying World Generation" href="wgen/customwgen.html"/>
    <!-- Google Analytics -->
    <script>
        (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
        m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
        })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

        ga('create', 'UA-59476017-2', 'auto');
        ga('send', 'pageview');
    </script>

  <script src="../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav" role="document">

  <div class="wy-grid-for-nav">

    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
    <div id="sp-logo-container" class="page-scroll">
        <a class="logo" href="../index.html">
            <img src="../_static/spongie-mark-dark.svg">
            <span>Sponge</span>
            <i class="fa fa-fw fa-chevron-down"></i>
        </a>
        <div id="sp-logo-menu">
            <ul id="sp-logo-dropdown">
                <li><a href="https://www.spongepowered.org"><i class="fa-fw fa fa-home"></i>Homepage</a></li>
                <li><a href="https://forums.spongepowered.org"><i class="fa-fw fa fa-comments"></i>Forums</a></li>
                <li><a href="https://github.com/SpongePowered"><i class="fa-fw fa fa-code"></i>Code</a></li>
                <li class="active"><a href="https://docs.spongepowered.org"><i class="fa-fw fa fa-book"></i>Docs</a></li>
                <li><a href="https://jd.spongepowered.org"><i class="fa-fw fa fa-graduation-cap"></i>Javadocs</a></li>
                <li><a href="https://forums.spongepowered.org/c/plugins/plugin-releases"><i class="fa-fw fa fa-plug"></i>Plugins</a></li>
                <li><a href="https://www.spongepowered.org/downloads"><i class="fa-fw fa fa-download"></i>Downloads</a></li>
                <li><a href="https://www.spongepowered.org/chat"><i class="fa-fw fa fa-comment"></i>Chat</a></li>
            </ul>
        </div>
    </div>

<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
                <ul>
<li class="toctree-l1"><a class="reference internal" href="../server/index.html">Creating a Server</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../preparing/index.html">Preparing for Development</a></li>
</ul>
<ul class="current">
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Creating a Plugin</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="buildsystem.html">Build Systems</a></li>
<li class="toctree-l2"><a class="reference internal" href="workspace/index.html">Setting Up Your Workspace</a></li>
<li class="toctree-l2"><a class="reference internal" href="project/index.html">Setting Up Your Project</a></li>
<li class="toctree-l2"><a class="reference internal" href="plugin-identifier.html">Plugin Identifiers</a></li>
<li class="toctree-l2"><a class="reference internal" href="plugin-class.html">Main Plugin Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="lifecycle.html">Plugin Lifecycle</a></li>
<li class="toctree-l2"><a class="reference internal" href="injection.html">Dependency Injection</a></li>
<li class="toctree-l2"><a class="reference internal" href="practices/index.html">Practices</a></li>
<li class="toctree-l2"><a class="reference internal" href="optional/index.html">Optionals</a></li>
<li class="toctree-l2"><a class="reference internal" href="logging.html">Logging and Debugging</a></li>
<li class="toctree-l2"><a class="reference internal" href="commands/index.html">Commands</a></li>
<li class="toctree-l2"><a class="reference internal" href="event/index.html">Events</a></li>
<li class="toctree-l2"><a class="reference internal" href="assets.html">The Asset API</a></li>
<li class="toctree-l2"><a class="reference internal" href="configuration/index.html">Configuring Plugins</a></li>
<li class="toctree-l2"><a class="reference internal" href="text/index.html">Text</a></li>
<li class="toctree-l2"><a class="reference internal" href="data/index.html">The Data API</a></li>
<li class="toctree-l2"><a class="reference internal" href="blocks/index.html">Blocks</a></li>
<li class="toctree-l2"><a class="reference internal" href="entities/index.html">Entities</a></li>
<li class="toctree-l2"><a class="reference internal" href="items/index.html">Items</a></li>
<li class="toctree-l2"><a class="reference internal" href="trade-offers.html">Trade-Offers</a></li>
<li class="toctree-l2"><a class="reference internal" href="effects.html">Effects</a></li>
<li class="toctree-l2"><a class="reference internal" href="scheduler.html">Scheduler</a></li>
<li class="toctree-l2"><a class="reference internal" href="services.html">Services</a></li>
<li class="toctree-l2"><a class="reference internal" href="database.html">Databases</a></li>
<li class="toctree-l2"><a class="reference internal" href="permissions.html">Permissions</a></li>
<li class="toctree-l2"><a class="reference internal" href="bans.html">Bans</a></li>
<li class="toctree-l2"><a class="reference internal" href="bookview.html">Book Views</a></li>
<li class="toctree-l2"><a class="reference internal" href="economy/index.html">Economy</a></li>
<li class="toctree-l2"><a class="reference internal" href="wgen/index.html">World Generation</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Plugin Manager</a></li>
<li class="toctree-l2"><a class="reference internal" href="game-profile-manager.html">Game Profile Manager</a></li>
<li class="toctree-l2"><a class="reference internal" href="offline-userplayer-data.html">Offline Player Data</a></li>
<li class="toctree-l2"><a class="reference internal" href="debugging.html">Plugin Debugging</a></li>
<li class="toctree-l2"><a class="reference internal" href="tab-lists.html">Tab Lists</a></li>
<li class="toctree-l2"><a class="reference internal" href="plugin-meta.html">Plugin Metadata</a></li>
<li class="toctree-l2"><a class="reference internal" href="ray-tracing.html">Ray Tracing</a></li>
<li class="toctree-l2"><a class="reference internal" href="tutorials.html">Tutorials</a></li>
<li class="toctree-l2"><a class="reference internal" href="internals/index.html">Implementation-dependent Plugins</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../ore/index.html">Ore Documentation</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../contributing/index.html">Contributing to Sponge</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../about/index.html">About the Sponge Project</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
        <a href="../index.html">Sponge</a>
      </nav>


      <div class="wy-nav-content">
        <div class="rst-content">

 

<div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
    <li><a href="../index.html">Docs</a> &raquo;</li>
          <li><a href="index.html">Creating a Plugin</a> &raquo;</li>
    <li>Plugin Manager</li>
      <li class="wy-breadcrumbs-aside">
            <a href="https://github.com/SpongePowered/SpongeDocs/blob/BestPractises/source/plugin/manager.rst" class="fa fa-github"> Edit on GitHub</a>
      </li>
  </ul>
  <hr/>
</div>          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
  <div class="section" id="plugin-manager">
<h1>Plugin Manager<a class="headerlink" href="#plugin-manager" title="Permalink to this headline">¶</a></h1>
<p>The Plugin Manager is what your plugin gets sent to after being loaded by the server at startup. The server loads
your plugin by finding its main class, annotated by the <a class="reference external" href="https://jd.spongepowered.org/6.0.0/org/spongepowered/api/plugin/Plugin.html">Plugin</a> annotation that holds its general information,
and sends a new instance of it to the manager. The manager then keeps that instance in its own collection that you can
look into and pull from using methods provided by itself, thus allowing you to easily interact with another loaded
plugin if you so desire.</p>
<div class="section" id="the-pluginmanager-class">
<h2>The PluginManager Class<a class="headerlink" href="#the-pluginmanager-class" title="Permalink to this headline">¶</a></h2>
<p>Public methods inside the <a class="reference external" href="https://jd.spongepowered.org/6.0.0/org/spongepowered/api/plugin/PluginManager.html">PluginManager</a> are used to grab information about the current collection of loaded
plugins, alongside their instances. The plugins are stored inside a <a class="reference external" href="https://jd.spongepowered.org/6.0.0/org/spongepowered/api/plugin/PluginContainer.html">PluginContainer</a> (discussed in next
section) to allow for an easy center of information about the specific plugin. As an example, you can use the
<code class="docutils literal"><span class="pre">PluginManager</span></code> to communicate with another plugin, grabbing its instance and using the methods it offers to provide
compability or extended features by means of your calling plugin.</p>
</div>
<div class="section" id="obtaining-the-plugin-manager">
<h2>Obtaining the Plugin Manager<a class="headerlink" href="#obtaining-the-plugin-manager" title="Permalink to this headline">¶</a></h2>
<p>You can get an instance of the server&#8217;s <code class="docutils literal"><span class="pre">PluginManager</span></code> using a few different ways.</p>
<div class="section" id="dependency-injection">
<h3>1. Dependency Injection<a class="headerlink" href="#dependency-injection" title="Permalink to this headline">¶</a></h3>
<div class="admonition tip">
<p class="first admonition-title">Tip</p>
<p class="last">See the <a class="reference internal" href="injection.html"><span class="doc">Dependency Injection</span></a> guide for help on using dependency injection.</p>
</div>
<p>The <code class="docutils literal"><span class="pre">PluginManager</span></code> is one of the few API instances that are injected into the main class upon being loaded. To ask
for a reference, create a new variable to hold the <code class="docutils literal"><span class="pre">PluginManager</span></code> instance and simply annotate it with <code class="docutils literal"><span class="pre">&#64;Inject</span></code>.</p>
<div class="highlight-java"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">com.google.inject.Inject</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.spongepowered.api.plugin.PluginManager</span><span class="o">;</span>

<span class="nd">@Inject</span>
<span class="kd">private</span> <span class="n">PluginManager</span> <span class="n">pluginManager</span><span class="o">;</span>
</pre></div>
</div>
</div>
<div class="section" id="the-service-manager">
<h3>2. The Service Manager<a class="headerlink" href="#the-service-manager" title="Permalink to this headline">¶</a></h3>
<div class="admonition tip">
<p class="first admonition-title">Tip</p>
<p class="last">See <a class="reference internal" href="services.html"><span class="doc">Services</span></a> for a full guide about the Service Manager.</p>
</div>
<p>The service manager also holds an instance of the server&#8217;s <code class="docutils literal"><span class="pre">PluginManager</span></code>. Simply use the method
<a class="reference external" href="https://jd.spongepowered.org/6.0.0/org/spongepowered/api/service/ServiceManager.html#provide-java.lang.Class-">ServiceManager#provide(Class)</a>, passing the <code class="docutils literal"><span class="pre">PluginManager</span></code>&#8216;s class (<code class="docutils literal"><span class="pre">PluginManager.class</span></code>) as a
parameter.</p>
<div class="highlight-java"><div class="highlight"><pre><span></span><span class="kd">private</span> <span class="n">PluginManager</span> <span class="n">pluginManager</span> <span class="o">=</span> <span class="n">serviceManager</span><span class="o">.</span><span class="na">provide</span><span class="o">(</span><span class="n">PluginManager</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>
</pre></div>
</div>
</div>
<div class="section" id="the-game-instance">
<h3>3. The Game Instance<a class="headerlink" href="#the-game-instance" title="Permalink to this headline">¶</a></h3>
<div class="admonition tip">
<p class="first admonition-title">Tip</p>
<p class="last">See the JavaDocs for <a class="reference external" href="https://jd.spongepowered.org/6.0.0/org/spongepowered/api/Game.html">Game</a> for full information about the class, as well as its methods and their usage.</p>
</div>
<p>A game instance can provide a reference to the server&#8217;s <code class="docutils literal"><span class="pre">PluginManager</span></code> as well for convenience.</p>
<div class="highlight-java"><div class="highlight"><pre><span></span><span class="kd">private</span> <span class="n">PluginManager</span> <span class="n">pluginManager</span> <span class="o">=</span> <span class="n">game</span><span class="o">.</span><span class="na">getPluginManager</span><span class="o">();</span>
</pre></div>
</div>
<p>Now that you have an instance to the plugin manager, let&#8217;s use it.</p>
</div>
<div class="section" id="using-the-sponge-class">
<h3>4. Using the Sponge Class<a class="headerlink" href="#using-the-sponge-class" title="Permalink to this headline">¶</a></h3>
<p>The <a class="reference external" href="https://jd.spongepowered.org/6.0.0/org/spongepowered/api/Sponge.html">Sponge</a> class works similarly to <code class="docutils literal"><span class="pre">Game</span></code>, with the exception that since <code class="docutils literal"><span class="pre">Sponge</span></code> contains static
methods. It can be accessed anywhere throughout your plugin. You also do not need to store an instance of it, as you
would need to do with <code class="docutils literal"><span class="pre">Game</span></code>.</p>
<div class="highlight-java"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">org.spongepowered.api.Sponge</span><span class="o">;</span>

<span class="kd">private</span> <span class="n">PluginManager</span> <span class="n">pluginManager</span> <span class="o">=</span> <span class="n">Sponge</span><span class="o">.</span><span class="na">getPluginManager</span><span class="o">();</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="using-the-plugin-manager">
<h2>Using the Plugin Manager<a class="headerlink" href="#using-the-plugin-manager" title="Permalink to this headline">¶</a></h2>
<p>The plugin manager provides several methods for working with plugins.</p>
<p>A lot of methods return plugin containers, which will be discussed in the next section. Plugin containers are pretty
much self-explanatory &#8220;containers&#8221; of the actual plugin instance.</p>
<p>With the plugin manager, it is possible to get all plugins currently loaded through the plugin manager:</p>
<div class="highlight-java"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">org.spongepowered.api.plugin.PluginContainer</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">java.util.List</span><span class="o">;</span>

<span class="kd">private</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">PluginContainer</span><span class="o">&gt;</span> <span class="n">plugins</span> <span class="o">=</span> <span class="n">pluginManager</span><span class="o">.</span><span class="na">getPlugins</span><span class="o">();</span>
</pre></div>
</div>
<p>Or, it is possible to obtain an instance to a plugin container directly, by the example shown below:</p>
<div class="highlight-java"><div class="highlight"><pre><span></span><span class="kd">private</span> <span class="n">PluginContainer</span> <span class="n">myOtherPlugin</span> <span class="o">=</span> <span class="n">pluginManager</span><span class="o">.</span><span class="na">getPlugin</span><span class="o">(</span><span class="s">&quot;myOtherPluginId&quot;</span><span class="o">).</span><span class="na">orNull</span><span class="o">();</span>
</pre></div>
</div>
</div>
<div class="section" id="the-plugincontainer-class">
<h2>The PluginContainer Class<a class="headerlink" href="#the-plugincontainer-class" title="Permalink to this headline">¶</a></h2>
<p>When grabbing a plugin from the <code class="docutils literal"><span class="pre">PluginManager</span></code>, you&#8217;ll notice very quickly that you are not given an immediate
instance of the requested plugin. Instead, you&#8217;ll be greeted by a <code class="docutils literal"><span class="pre">PluginContainer</span></code> containing information about the
plugin attained from its <code class="docutils literal"><span class="pre">&#64;Plugin</span></code> annotation in its main class, as well as the loaded instance.</p>
<p>The <code class="docutils literal"><span class="pre">PluginContainer</span></code> will hold any generic information about the plugin set by its owning developer. You can use
information from here instead of hard-coding what you know about it in your supporting plugin. An example scenario would
be if the owning developer changes the name of the plugin, references to the latter in the supporting plugin would not
become wrong as a result of this change, provided you&#8217;ve used the method <a class="reference external" href="https://jd.spongepowered.org/6.0.0/org/spongepowered/api/plugin/PluginContainer.html#getName--">PluginContainer#getName()</a> to get
its name.</p>
<div class="highlight-java"><div class="highlight"><pre><span></span><span class="kd">private</span> <span class="n">PluginContainer</span> <span class="n">myOtherPlugin</span> <span class="o">=</span> <span class="n">pluginManager</span><span class="o">.</span><span class="na">getPlugin</span><span class="o">(</span><span class="s">&quot;myOtherPluginId&quot;</span><span class="o">).</span><span class="na">orNull</span><span class="o">();</span>
<span class="kd">private</span> <span class="n">MyOtherPlugin</span> <span class="n">pluginInstance</span> <span class="o">=</span> <span class="o">(</span><span class="n">MyOtherPlugin</span><span class="o">)</span> <span class="n">myOtherPlugin</span><span class="o">.</span><span class="na">getInstance</span><span class="o">();</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><a class="reference external" href="https://jd.spongepowered.org/6.0.0/org/spongepowered/api/plugin/PluginContainer.html#getInstance--">PluginContainer#getInstance()</a> will return as an <code class="docutils literal"><span class="pre">Object</span></code>. You need to cast it as the target plugin
after obtaining it from the container.</p>
</div>
</div>
</div>

           </div>
          </div>
<footer>
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
        <a href="game-profile-manager.html" class="btn btn-neutral float-right" title="Game Profile Manager" accesskey="n">Next <span class="fa fa-arrow-circle-right"></span></a>
        <a href="wgen/customwgen.html" class="btn btn-neutral" title="Modifying World Generation" accesskey="p"><span class="fa fa-arrow-circle-left"></span> Previous</a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&copy; Copyright 2014-2017, Sponge Contributors.
    </p>
  </div>Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 
</footer>
        </div>
      </div>

    </section>

  </div>


<div class="rst-versions" data-toggle="rst-versions" role="note" aria-label="versions">
    <span class="rst-current-version" data-toggle="rst-current-version">
        <i class="fa fa-book"> <span>SpongeDocs</span></i>
        v: 6.0.0
        <span class="fa fa-caret-down"></span>
    </span>
    <div id="versions" class="rst-other-versions">




            <dl>
                <dt>Contribute</dt>
                    <dd><a href="https://github.com/SpongePowered/SpongeDocs/blob/BestPractises/source/plugin/manager.rst">Source</a></dd>
                    <dd><a href="https://github.com/SpongePowered/SpongeDocs/edit/BestPractises/source/plugin/manager.rst">Edit</a></dd>
            </dl>
    </div>
</div>

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../',
            VERSION:'6.0.0',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true
        };
    </script>      <script type="text/javascript" src="../_static/jquery.js"></script>      <script type="text/javascript" src="../_static/underscore.js"></script>      <script type="text/javascript" src="../_static/doctools.js"></script>      <script type="text/javascript" src="../_static/spongedocs.js"></script>

    <script type="text/javascript" src="../_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.StickyNav.enable();
      });
  </script>
 
</body>
</html>