Package org.bukkit.plugin.java
Class JavaPlugin
- java.lang.Object
-
- org.bukkit.plugin.PluginBase
-
- org.bukkit.plugin.java.JavaPlugin
-
- All Implemented Interfaces:
CommandExecutor
,TabCompleter
,TabExecutor
,Plugin
public abstract class JavaPlugin extends PluginBase
Represents a Java plugin
-
-
Constructor Summary
Constructors Modifier Constructor Description JavaPlugin()
protected
JavaPlugin(@NotNull JavaPluginLoader loader, @NotNull PluginDescriptionFile description, @NotNull File dataFolder, @NotNull File file)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected @NotNull ClassLoader
getClassLoader()
Returns the ClassLoader which holds this plugin@Nullable PluginCommand
getCommand(@NotNull String name)
Gets the command with the given name, specific to this plugin.@NotNull FileConfiguration
getConfig()
Gets aFileConfiguration
for this plugin, read through "config.yml"@NotNull File
getDataFolder()
Returns the folder that the plugin data's files are located in.@Nullable ChunkGenerator
getDefaultWorldGenerator(@NotNull String worldName, @Nullable String id)
Gets aChunkGenerator
for use in a default world, as specified in the server configuration@NotNull PluginDescriptionFile
getDescription()
Returns the plugin.yaml file containing the details for this pluginprotected @NotNull File
getFile()
Returns the file which contains this plugin@NotNull Logger
getLogger()
Returns the plugin logger associated with this server's logger.static <T extends JavaPlugin>
TgetPlugin(@NotNull Class<T> clazz)
This method provides fast access to the plugin that hasprovided
the given plugin class, which is usually the plugin that implemented it.@NotNull PluginLoader
getPluginLoader()
Gets the associated PluginLoader responsible for this pluginstatic @NotNull JavaPlugin
getProvidingPlugin(@NotNull Class<?> clazz)
This method provides fast access to the plugin that has provided the given class.@Nullable InputStream
getResource(@NotNull String filename)
Gets an embedded resource in this plugin@NotNull Server
getServer()
Returns the Server instance currently running this pluginprotected @Nullable Reader
getTextResource(@NotNull String file)
Provides a reader for a text file located inside the jar.boolean
isEnabled()
Returns a value indicating whether or not this plugin is currently enabledboolean
isNaggable()
Simple boolean if we can still nag to the logs about thingsboolean
onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args)
Executes the given command, returning its success.void
onDisable()
Called when this plugin is disabledvoid
onEnable()
Called when this plugin is enabledvoid
onLoad()
Called after a plugin is loaded but before it has been enabled.@Nullable List<String>
onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args)
Requests a list of possible completions for a command argument.void
reloadConfig()
Discards any data inPlugin.getConfig()
and reloads from disk.void
saveConfig()
Saves theFileConfiguration
retrievable byPlugin.getConfig()
.void
saveDefaultConfig()
Saves the raw contents of the default config.yml file to the location retrievable byPlugin.getConfig()
.void
saveResource(@NotNull String resourcePath, boolean replace)
Saves the raw contents of any resource embedded with a plugin's .jar file assuming it can be found usingPlugin.getResource(String)
.protected void
setEnabled(boolean enabled)
Sets the enabled state of this pluginvoid
setNaggable(boolean canNag)
Set naggable state@NotNull String
toString()
-
Methods inherited from class org.bukkit.plugin.PluginBase
equals, getName, hashCode
-
-
-
-
Constructor Detail
-
JavaPlugin
public JavaPlugin()
-
JavaPlugin
protected JavaPlugin(@NotNull @NotNull JavaPluginLoader loader, @NotNull @NotNull PluginDescriptionFile description, @NotNull @NotNull File dataFolder, @NotNull @NotNull File file)
-
-
Method Detail
-
getDataFolder
@NotNull public final @NotNull File getDataFolder()
Returns the folder that the plugin data's files are located in. The folder may not yet exist.- Returns:
- The folder.
-
getPluginLoader
@NotNull public final @NotNull PluginLoader getPluginLoader()
Gets the associated PluginLoader responsible for this plugin- Returns:
- PluginLoader that controls this plugin
-
getServer
@NotNull public final @NotNull Server getServer()
Returns the Server instance currently running this plugin- Returns:
- Server running this plugin
-
isEnabled
public final boolean isEnabled()
Returns a value indicating whether or not this plugin is currently enabled- Returns:
- true if this plugin is enabled, otherwise false
-
getFile
@NotNull protected @NotNull File getFile()
Returns the file which contains this plugin- Returns:
- File containing this plugin
-
getDescription
@NotNull public final @NotNull PluginDescriptionFile getDescription()
Returns the plugin.yaml file containing the details for this plugin- Returns:
- Contents of the plugin.yaml file
-
getConfig
@NotNull public @NotNull FileConfiguration getConfig()
Description copied from interface:Plugin
Gets aFileConfiguration
for this plugin, read through "config.yml"If there is a default config.yml embedded in this plugin, it will be provided as a default for this Configuration.
- Returns:
- Plugin configuration
-
getTextResource
@Nullable protected final @Nullable Reader getTextResource(@NotNull @NotNull String file)
Provides a reader for a text file located inside the jar.The returned reader will read text with the UTF-8 charset.
- Parameters:
file
- the filename of the resource to load- Returns:
- null if
getResource(String)
returns null - Throws:
IllegalArgumentException
- if file is null- See Also:
ClassLoader.getResourceAsStream(String)
-
reloadConfig
public void reloadConfig()
Description copied from interface:Plugin
Discards any data inPlugin.getConfig()
and reloads from disk.
-
saveConfig
public void saveConfig()
Description copied from interface:Plugin
Saves theFileConfiguration
retrievable byPlugin.getConfig()
.
-
saveDefaultConfig
public void saveDefaultConfig()
Description copied from interface:Plugin
Saves the raw contents of the default config.yml file to the location retrievable byPlugin.getConfig()
.This should fail silently if the config.yml already exists.
-
saveResource
public void saveResource(@NotNull @NotNull String resourcePath, boolean replace)
Description copied from interface:Plugin
Saves the raw contents of any resource embedded with a plugin's .jar file assuming it can be found usingPlugin.getResource(String)
.The resource is saved into the plugin's data folder using the same hierarchy as the .jar file (subdirectories are preserved).
- Parameters:
resourcePath
- the embedded resource path to look for within the plugin's .jar file. (No preceding slash).replace
- if true, the embedded resource will overwrite the contents of an existing file.
-
getResource
@Nullable public @Nullable InputStream getResource(@NotNull @NotNull String filename)
Description copied from interface:Plugin
Gets an embedded resource in this plugin- Parameters:
filename
- Filename of the resource- Returns:
- File if found, otherwise null
-
getClassLoader
@NotNull protected final @NotNull ClassLoader getClassLoader()
Returns the ClassLoader which holds this plugin- Returns:
- ClassLoader holding this plugin
-
setEnabled
protected final void setEnabled(boolean enabled)
Sets the enabled state of this plugin- Parameters:
enabled
- true if enabled, otherwise false
-
onCommand
public boolean onCommand(@NotNull @NotNull CommandSender sender, @NotNull @NotNull Command command, @NotNull @NotNull String label, @NotNull @NotNull String[] args)
Executes the given command, returning its success.
If false is returned, then the "usage" plugin.yml entry for this command (if defined) will be sent to the player.- Parameters:
sender
- Source of the commandcommand
- Command which was executedlabel
- Alias of the command which was usedargs
- Passed command arguments- Returns:
- true if a valid command, otherwise false
-
onTabComplete
@Nullable public @Nullable List<String> onTabComplete(@NotNull @NotNull CommandSender sender, @NotNull @NotNull Command command, @NotNull @NotNull String alias, @NotNull @NotNull String[] args)
Requests a list of possible completions for a command argument.- Parameters:
sender
- Source of the command. For players tab-completing a command inside of a command block, this will be the player, not the command block.command
- Command which was executedalias
- The alias usedargs
- The arguments passed to the command, including final partial argument to be completed and command label- Returns:
- A List of possible completions for the final argument, or null to default to the command executor
-
getCommand
@Nullable public @Nullable PluginCommand getCommand(@NotNull @NotNull String name)
Gets the command with the given name, specific to this plugin. Commands need to be registered in thePluginDescriptionFile
to exist at runtime.- Parameters:
name
- name or alias of the command- Returns:
- the plugin command if found, otherwise null
-
onLoad
public void onLoad()
Description copied from interface:Plugin
Called after a plugin is loaded but before it has been enabled.When multiple plugins are loaded, the onLoad() for all plugins is called before any onEnable() is called.
-
onDisable
public void onDisable()
Description copied from interface:Plugin
Called when this plugin is disabled
-
onEnable
public void onEnable()
Description copied from interface:Plugin
Called when this plugin is enabled
-
getDefaultWorldGenerator
@Nullable public @Nullable ChunkGenerator getDefaultWorldGenerator(@NotNull @NotNull String worldName, @Nullable @Nullable String id)
Description copied from interface:Plugin
Gets aChunkGenerator
for use in a default world, as specified in the server configuration- Parameters:
worldName
- Name of the world that this will be applied toid
- Unique ID, if any, that was specified to indicate which generator was requested- Returns:
- ChunkGenerator for use in the default world generation
-
isNaggable
public final boolean isNaggable()
Description copied from interface:Plugin
Simple boolean if we can still nag to the logs about things- Returns:
- boolean whether we can nag
-
setNaggable
public final void setNaggable(boolean canNag)
Description copied from interface:Plugin
Set naggable state- Parameters:
canNag
- is this plugin still naggable?
-
getLogger
@NotNull public @NotNull Logger getLogger()
Description copied from interface:Plugin
Returns the plugin logger associated with this server's logger. The returned logger automatically tags all log messages with the plugin's name.- Returns:
- Logger associated with this plugin
-
getPlugin
@NotNull public static <T extends JavaPlugin> T getPlugin(@NotNull @NotNull Class<T> clazz)
This method provides fast access to the plugin that hasprovided
the given plugin class, which is usually the plugin that implemented it.An exception to this would be if plugin's jar that contained the class does not extend the class, where the intended plugin would have resided in a different jar / classloader.
- Type Parameters:
T
- a class that extends JavaPlugin- Parameters:
clazz
- the class desired- Returns:
- the plugin that provides and implements said class
- Throws:
IllegalArgumentException
- if clazz is nullIllegalArgumentException
- if clazz does not extendJavaPlugin
IllegalStateException
- if clazz was not provided by a plugin, for example, if called withJavaPlugin.getPlugin(JavaPlugin.class)
IllegalStateException
- if called from the static initializer for given JavaPluginClassCastException
- if plugin that provided the class does not extend the class
-
getProvidingPlugin
@NotNull public static @NotNull JavaPlugin getProvidingPlugin(@NotNull @NotNull Class<?> clazz)
This method provides fast access to the plugin that has provided the given class.- Parameters:
clazz
- a class belonging to a plugin- Returns:
- the plugin that provided the class
- Throws:
IllegalArgumentException
- if the class is not provided by a JavaPluginIllegalArgumentException
- if class is nullIllegalStateException
- if called from the static initializer for given JavaPlugin
-
-