Менеджер плагинов
Предупреждение
Эти документы были написаны для SpongeAPI 7 и, вероятно, устаревшие. Если вы чувствуете, что вы можете помочь обновить их, пожалуйста, отправьте PR!
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 Plugin 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.
The PluginManager Class
Public methods inside the PluginManager are used to grab information about the current collection of loaded
plugins, alongside their instances. The plugins are stored inside a PluginContainer (discussed in next
section) to allow for an easy center of information about the specific plugin. As an example, you can use the
PluginManager
to communicate with another plugin, grabbing its instance and using the methods it offers to provide
compatibility or extended features by means of your calling plugin.
Получение Менеджера плагинов
Вы можете получить экземпляр PluginManager
несколькими способами.
1. Внедрение зависимостей
Совет
See the Внедрение зависимостей guide for help on using dependency injection.
The PluginManager
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 PluginManager
instance and simply annotate it with @Inject
.
import com.google.inject.Inject;
import org.spongepowered.api.plugin.PluginManager;
@Inject
private PluginManager pluginManager;
2. Менеджер сервисов (Service Manager)
Совет
Смотрите Сервисы для подробного руководства по менеджеру сервисов.
The service manager also holds an instance of the server’s PluginManager
. Simply use the method
ServiceManager#provide(Class), passing the PluginManager
„s class (PluginManager.class
) as a
parameter.
private PluginManager pluginManager = serviceManager.provideUnchecked(PluginManager.class);
3. The Game Instance
Совет
See the JavaDocs for Game for full information about the class, as well as its methods and their usage.
A game instance can provide a reference to the server’s PluginManager
as well for convenience.
private PluginManager pluginManager = game.getPluginManager();
Now that you have an instance to the plugin manager, let’s use it.
4. Using the Sponge Class
The Sponge class works similarly to Game
, with the exception that since Sponge
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 Game
.
import org.spongepowered.api.Sponge;
private PluginManager pluginManager = Sponge.pluginManager();
Использование Менеджера плагинов (Plugin Manager)
Менеджер плагинов предоставляет несколько методов для работы с плагинами.
A lot of methods return plugin containers, which will be discussed in the next section. Plugin containers are pretty much self-explanatory «containers» of the actual plugin instance.
С помощью менеджера плагинов можно получить список всех плагинов, которые были загружены через менеджер плагинов:
import org.spongepowered.plugin.PluginContainer;
import java.util.Collection;
private Collection<PluginContainer> plugins = pluginManager.getPlugins();
Or, it is possible to obtain an instance to a plugin container directly, by the example shown below:
private PluginContainer myOtherPlugin = pluginManager.getPlugin("myOtherPluginId").orElse(null);
Класс PluginContainer
When grabbing a plugin from the PluginManager
, you’ll notice very quickly that you are not given an immediate
instance of the requested plugin. Instead, you’ll be greeted by a PluginContainer
containing information about the
plugin attained from its @Plugin
annotation in its main class, as well as the loaded instance.
The PluginContainer
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’ve used the method PluginContainer#getName() to get
its name.
private PluginContainer myOtherPlugin = pluginManager.getPlugin("myOtherPluginId").orElse(null);
private MyOtherPlugin pluginInstance = (MyOtherPlugin) myOtherPlugin.getInstance().orElse(null);
Примечание
PluginContainer#getInstance() will return as an Object
. You need to cast it as the target plugin
after obtaining it from the container.