Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
org.bukkit.plugin.java.JavaPlugin Class Referenceabstract
Inheritance diagram for org.bukkit.plugin.java.JavaPlugin:
org.bukkit.plugin.PluginBase org.bukkit.plugin.Plugin org.bukkit.command.TabExecutor org.bukkit.command.TabCompleter org.bukkit.command.CommandExecutor

Public Member Functions

final File getDataFolder ()
 
final PluginLoader getPluginLoader ()
 
final Server getServer ()
 
final boolean isEnabled ()
 
final PluginDescriptionFile getDescription ()
 
FileConfiguration getConfig ()
 
void reloadConfig ()
 
void saveConfig ()
 
void saveDefaultConfig ()
 
void saveResource (String resourcePath, boolean replace)
 
InputStream getResource (String filename)
 
List< Class<?> > getDatabaseClasses ()
 
final boolean isInitialized ()
 
boolean onCommand (CommandSender sender, Command command, String label, String[] args)
 
List< String > onTabComplete (CommandSender sender, Command command, String alias, String[] args)
 
PluginCommand getCommand (String name)
 
void onLoad ()
 
void onDisable ()
 
void onEnable ()
 
ChunkGenerator getDefaultWorldGenerator (String worldName, String id)
 
final boolean isNaggable ()
 
final void setNaggable (boolean canNag)
 
EbeanServer getDatabase ()
 
final Logger getLogger ()
 
String toString ()
 
- Public Member Functions inherited from org.bukkit.plugin.PluginBase
final int hashCode ()
 
final boolean equals (Object obj)
 
final String getName ()
 

Static Public Member Functions

static< TextendsJavaPlugin > T getPlugin (Class< T > clazz)
 
static JavaPlugin getProvidingPlugin (Class<?> clazz)
 

Protected Member Functions

 JavaPlugin (final PluginLoader loader, final Server server, final PluginDescriptionFile description, final File dataFolder, final File file)
 
 JavaPlugin (final JavaPluginLoader loader, final PluginDescriptionFile description, final File dataFolder, final File file)
 
File getFile ()
 
final Reader getTextResource (String file)
 
final ClassLoader getClassLoader ()
 
final void setEnabled (final boolean enabled)
 
final void initialize (PluginLoader loader, Server server, PluginDescriptionFile description, File dataFolder, File file, ClassLoader classLoader)
 
void installDDL ()
 
void removeDDL ()
 

Detailed Description

Represents a Java plugin

Definition at line 47 of file JavaPlugin.java.

Constructor & Destructor Documentation

org.bukkit.plugin.java.JavaPlugin.JavaPlugin ( final PluginLoader  loader,
final Server  server,
final PluginDescriptionFile  description,
final File  dataFolder,
final File  file 
)
protected
Deprecated:
This method is intended for unit testing purposes when the other #JavaPlugin(JavaPluginLoader, PluginDescriptionFile, File, File) constructor} cannot be used.

Its existence may be temporary.

Definition at line 77 of file JavaPlugin.java.

77  {
78  final ClassLoader classLoader = this.getClass().getClassLoader();
79  if (classLoader instanceof PluginClassLoader) {
80  throw new IllegalStateException("Cannot use initialization constructor at runtime");
81  }
82  init(loader, server, description, dataFolder, file, classLoader);
83  }

Member Function Documentation

final ClassLoader org.bukkit.plugin.java.JavaPlugin.getClassLoader ( )
protected

Returns the ClassLoader which holds this plugin

Returns
ClassLoader holding this plugin

Definition at line 302 of file JavaPlugin.java.

Referenced by org.bukkit.plugin.java.JavaPluginLoader.disablePlugin(), org.bukkit.plugin.java.JavaPlugin.getProvidingPlugin(), and org.bukkit.plugin.java.JavaPlugin.getResource().

302  {
303  return classLoader;
304  }
PluginCommand org.bukkit.plugin.java.JavaPlugin.getCommand ( String  name)

Gets the command with the given name, specific to this plugin. Commands need to be registered in the PluginDescriptionFile to exist at runtime.

Parameters
namename or alias of the command
Returns
the plugin command if found, otherwise null

Definition at line 418 of file JavaPlugin.java.

References org.bukkit.plugin.PluginDescriptionFile.getName(), org.bukkit.command.PluginCommand.getPlugin(), org.bukkit.Server.getPluginCommand(), and org.bukkit.plugin.java.JavaPlugin.getServer().

418  {
419  String alias = name.toLowerCase();
420  PluginCommand command = getServer().getPluginCommand(alias);
421 
422  if (command == null || command.getPlugin() != this) {
423  command = getServer().getPluginCommand(description.getName().toLowerCase() + ":" + alias);
424  }
425 
426  if (command != null && command.getPlugin() == this) {
427  return command;
428  } else {
429  return null;
430  }
431  }
PluginCommand getPluginCommand(String name)
FileConfiguration org.bukkit.plugin.java.JavaPlugin.getConfig ( )

Gets a FileConfiguration 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

Implements org.bukkit.plugin.Plugin.

Definition at line 155 of file JavaPlugin.java.

References org.bukkit.plugin.java.JavaPlugin.reloadConfig().

Referenced by org.bukkit.plugin.java.JavaPlugin.saveConfig().

155  {
156  if (newConfig == null) {
157  reloadConfig();
158  }
159  return newConfig;
160  }
EbeanServer org.bukkit.plugin.java.JavaPlugin.getDatabase ( )

Gets the EbeanServer tied to this plugin. This will only be available if enabled in the PluginDescriptionFile#isDatabaseEnabled()

For more information on the use of Avaje Ebeans ORM, see Avaje Ebeans Documentation

For an example using Ebeans ORM, see Bukkit's Homebukkit Plugin

Returns
ebean server instance or null if not enabled

Implements org.bukkit.plugin.Plugin.

Definition at line 458 of file JavaPlugin.java.

458  {
459  return ebean;
460  }
List<Class<?> > org.bukkit.plugin.java.JavaPlugin.getDatabaseClasses ( )

Provides a list of all classes that should be persisted in the database

Returns
List of Classes that are Ebeans

Definition at line 372 of file JavaPlugin.java.

372  {
373  return new ArrayList<Class<?>>();
374  }
final File org.bukkit.plugin.java.JavaPlugin.getDataFolder ( )

Returns the folder that the plugin data's files are located in. The folder may not yet exist.

Returns
The folder.

Implements org.bukkit.plugin.Plugin.

Definition at line 100 of file JavaPlugin.java.

100  {
101  return dataFolder;
102  }
ChunkGenerator org.bukkit.plugin.java.JavaPlugin.getDefaultWorldGenerator ( String  worldName,
String  id 
)

Gets a ChunkGenerator for use in a default world, as specified in the server configuration

Parameters
worldNameName of the world that this will be applied to
idUnique ID, if any, that was specified to indicate which generator was requested
Returns
ChunkGenerator for use in the default world generation

Implements org.bukkit.plugin.Plugin.

Definition at line 443 of file JavaPlugin.java.

443  {
444  return null;
445  }
final PluginDescriptionFile org.bukkit.plugin.java.JavaPlugin.getDescription ( )

Returns the plugin.yaml file containing the details for this plugin

Returns
Contents of the plugin.yaml file

Implements org.bukkit.plugin.Plugin.

Definition at line 150 of file JavaPlugin.java.

Referenced by org.bukkit.plugin.java.JavaPluginLoader.disablePlugin().

150  {
151  return description;
152  }
File org.bukkit.plugin.java.JavaPlugin.getFile ( )
protected

Returns the file which contains this plugin

Returns
File containing this plugin

Definition at line 140 of file JavaPlugin.java.

140  {
141  return file;
142  }
final Logger org.bukkit.plugin.java.JavaPlugin.getLogger ( )

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

Implements org.bukkit.plugin.Plugin.

Definition at line 477 of file JavaPlugin.java.

Referenced by org.bukkit.plugin.java.JavaPlugin.initialize(), and org.bukkit.plugin.java.JavaPlugin.reloadConfig().

477  {
478  return logger;
479  }
static <TextendsJavaPlugin> T org.bukkit.plugin.java.JavaPlugin.getPlugin ( Class< T >  clazz)
static

This method provides fast access to the plugin that has provided 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.

Parameters
clazzthe class desired
Returns
the plugin that provides and implements said class
Exceptions
IllegalArgumentExceptionif clazz is null
IllegalArgumentExceptionif clazz does not extend JavaPlugin
IllegalStateExceptionif clazz was not provided by a plugin, for example, if called with JavaPlugin.getPlugin(JavaPlugin.class)
IllegalStateExceptionif called from the static initializer for given JavaPlugin
ClassCastExceptionif plugin that provided the class does not extend the class

Definition at line 508 of file JavaPlugin.java.

508  {
509  Validate.notNull(clazz, "Null class cannot have a plugin");
510  if (!JavaPlugin.class.isAssignableFrom(clazz)) {
511  throw new IllegalArgumentException(clazz + " does not extend " + JavaPlugin.class);
512  }
513  final ClassLoader cl = clazz.getClassLoader();
514  if (!(cl instanceof PluginClassLoader)) {
515  throw new IllegalArgumentException(clazz + " is not initialized by " + PluginClassLoader.class);
516  }
517  JavaPlugin plugin = ((PluginClassLoader) cl).plugin;
518  if (plugin == null) {
519  throw new IllegalStateException("Cannot get plugin for " + clazz + " from a static initializer");
520  }
521  return clazz.cast(plugin);
522  }
final PluginLoader org.bukkit.plugin.java.JavaPlugin.getPluginLoader ( )

Gets the associated PluginLoader responsible for this plugin

Returns
PluginLoader that controls this plugin

Implements org.bukkit.plugin.Plugin.

Definition at line 110 of file JavaPlugin.java.

110  {
111  return loader;
112  }
static JavaPlugin org.bukkit.plugin.java.JavaPlugin.getProvidingPlugin ( Class<?>  clazz)
static

This method provides fast access to the plugin that has provided the given class.

Exceptions
IllegalArgumentExceptionif the class is not provided by a JavaPlugin
IllegalArgumentExceptionif class is null
IllegalStateExceptionif called from the static initializer for given JavaPlugin

Definition at line 534 of file JavaPlugin.java.

References org.bukkit.plugin.java.JavaPlugin.getClassLoader().

534  {
535  Validate.notNull(clazz, "Null class cannot have a plugin");
536  final ClassLoader cl = clazz.getClassLoader();
537  if (!(cl instanceof PluginClassLoader)) {
538  throw new IllegalArgumentException(clazz + " is not provided by " + PluginClassLoader.class);
539  }
540  JavaPlugin plugin = ((PluginClassLoader) cl).plugin;
541  if (plugin == null) {
542  throw new IllegalStateException("Cannot get plugin for " + clazz + " from a static initializer");
543  }
544  return plugin;
545  }
InputStream org.bukkit.plugin.java.JavaPlugin.getResource ( String  filename)

Gets an embedded resource in this plugin

Parameters
filenameFilename of the resource
Returns
File if found, otherwise null

Implements org.bukkit.plugin.Plugin.

Definition at line 277 of file JavaPlugin.java.

References org.bukkit.plugin.java.JavaPlugin.getClassLoader().

Referenced by org.bukkit.plugin.java.JavaPlugin.getTextResource(), org.bukkit.plugin.java.JavaPlugin.reloadConfig(), and org.bukkit.plugin.java.JavaPlugin.saveResource().

277  {
278  if (filename == null) {
279  throw new IllegalArgumentException("Filename cannot be null");
280  }
281 
282  try {
283  URL url = getClassLoader().getResource(filename);
284 
285  if (url == null) {
286  return null;
287  }
288 
289  URLConnection connection = url.openConnection();
290  connection.setUseCaches(false);
291  return connection.getInputStream();
292  } catch (IOException ex) {
293  return null;
294  }
295  }
final ClassLoader getClassLoader()
final Server org.bukkit.plugin.java.JavaPlugin.getServer ( )

Returns the Server instance currently running this plugin

Returns
Server running this plugin

Implements org.bukkit.plugin.Plugin.

Definition at line 120 of file JavaPlugin.java.

Referenced by org.bukkit.plugin.java.JavaPlugin.getCommand().

120  {
121  return server;
122  }
final Reader org.bukkit.plugin.java.JavaPlugin.getTextResource ( String  file)
protected

Provides a reader for a text file located inside the jar. The behavior of this method adheres to PluginAwareness.Flags#UTF8, or if not defined, uses UTF8 if FileConfiguration#UTF8_OVERRIDE is specified, or system default otherwise.

Parameters
filethe filename of the resource to load
Returns
null if getResource(String) returns null
Exceptions
IllegalArgumentExceptionif file is null
See also
ClassLoader::getResourceAsStream(String)

Definition at line 174 of file JavaPlugin.java.

References org.bukkit.plugin.java.JavaPlugin.getResource(), and org.bukkit.configuration.file.FileConfiguration.UTF8_OVERRIDE.

174  {
175  final InputStream in = getResource(file);
176 
177  return in == null ? null : new InputStreamReader(in, isStrictlyUTF8() || FileConfiguration.UTF8_OVERRIDE ? Charsets.UTF_8 : Charset.defaultCharset());
178  }
InputStream getResource(String filename)
final void org.bukkit.plugin.java.JavaPlugin.initialize ( PluginLoader  loader,
Server  server,
PluginDescriptionFile  description,
File  dataFolder,
File  file,
ClassLoader  classLoader 
)
protected
Deprecated:
This method is legacy and will be removed - it must be replaced by the specially provided constructor(s).

Definition at line 328 of file JavaPlugin.java.

References org.bukkit.Warning.WarningState.DEFAULT, org.bukkit.plugin.java.JavaPlugin.getLogger(), org.bukkit.plugin.PluginBase.getName(), org.bukkit.Server.getWarningState(), and org.bukkit.Warning.WarningState.OFF.

328  {
329  if (server.getWarningState() == WarningState.OFF) {
330  return;
331  }
332  getLogger().log(Level.WARNING, getClass().getName() + " is already initialized", server.getWarningState() == WarningState.DEFAULT ? null : new AuthorNagException("Explicit initialization"));
333  }
WarningState getWarningState()
final boolean org.bukkit.plugin.java.JavaPlugin.isEnabled ( )

Returns a value indicating whether or not this plugin is currently enabled

Returns
true if this plugin is enabled, otherwise false

Implements org.bukkit.plugin.Plugin.

Definition at line 131 of file JavaPlugin.java.

131  {
132  return isEnabled;
133  }
final boolean org.bukkit.plugin.java.JavaPlugin.isInitialized ( )

Gets the initialization status of this plugin

Returns
true if this plugin is initialized, otherwise false
Deprecated:
This method cannot return false, as JavaPlugin is now initialized in the constructor.

Definition at line 390 of file JavaPlugin.java.

390  {
391  return true;
392  }
final boolean org.bukkit.plugin.java.JavaPlugin.isNaggable ( )

Simple boolean if we can still nag to the logs about things

Returns
boolean whether we can nag

Implements org.bukkit.plugin.Plugin.

Definition at line 448 of file JavaPlugin.java.

448  {
449  return naggable;
450  }
boolean org.bukkit.plugin.java.JavaPlugin.onCommand ( CommandSender  sender,
Command  command,
String  label,
String[]  args 
)

Executes the given command, returning its success

Parameters
senderSource of the command
commandCommand which was executed
labelAlias of the command which was used
argsPassed command arguments
Returns
true if a valid command, otherwise false

Implements org.bukkit.command.CommandExecutor.

Definition at line 398 of file JavaPlugin.java.

398  {
399  return false;
400  }
void org.bukkit.plugin.java.JavaPlugin.onDisable ( )

Called when this plugin is disabled

Implements org.bukkit.plugin.Plugin.

Definition at line 437 of file JavaPlugin.java.

Referenced by org.bukkit.plugin.java.JavaPlugin.setEnabled().

437 {}
void org.bukkit.plugin.java.JavaPlugin.onEnable ( )

Called when this plugin is enabled

Implements org.bukkit.plugin.Plugin.

Definition at line 440 of file JavaPlugin.java.

Referenced by org.bukkit.plugin.java.JavaPlugin.setEnabled().

440 {}
void org.bukkit.plugin.java.JavaPlugin.onLoad ( )

Called after a plugin is loaded but before it has been enabled.

When mulitple plugins are loaded, the onLoad() for all plugins is called before any onEnable() is called.

Implements org.bukkit.plugin.Plugin.

Definition at line 434 of file JavaPlugin.java.

434 {}
List<String> org.bukkit.plugin.java.JavaPlugin.onTabComplete ( CommandSender  sender,
Command  command,
String  alias,
String[]  args 
)

Requests a list of possible completions for a command argument.

Parameters
senderSource of the command
commandCommand which was executed
aliasThe alias used
argsThe 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

Implements org.bukkit.command.TabCompleter.

Definition at line 406 of file JavaPlugin.java.

406  {
407  return null;
408  }
void org.bukkit.plugin.java.JavaPlugin.reloadConfig ( )

Discards any data in getConfig() and reloads from disk.

Implements org.bukkit.plugin.Plugin.

Definition at line 182 of file JavaPlugin.java.

References org.bukkit.plugin.java.JavaPlugin.getLogger(), org.bukkit.plugin.java.JavaPlugin.getResource(), org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(), org.bukkit.configuration.MemoryConfiguration.setDefaults(), and org.bukkit.configuration.file.FileConfiguration.UTF8_OVERRIDE.

Referenced by org.bukkit.plugin.java.JavaPlugin.getConfig().

182  {
183  newConfig = YamlConfiguration.loadConfiguration(configFile);
184 
185  final InputStream defConfigStream = getResource("config.yml");
186  if (defConfigStream == null) {
187  return;
188  }
189 
190  final YamlConfiguration defConfig;
191  if (isStrictlyUTF8() || FileConfiguration.UTF8_OVERRIDE) {
192  defConfig = YamlConfiguration.loadConfiguration(new InputStreamReader(defConfigStream, Charsets.UTF_8));
193  } else {
194  final byte[] contents;
195  defConfig = new YamlConfiguration();
196  try {
197  contents = ByteStreams.toByteArray(defConfigStream);
198  } catch (final IOException e) {
199  getLogger().log(Level.SEVERE, "Unexpected failure reading config.yml", e);
200  return;
201  }
202 
203  final String text = new String(contents, Charset.defaultCharset());
204  if (!text.equals(new String(contents, Charsets.UTF_8))) {
205  getLogger().warning("Default system encoding may have misread config.yml from plugin jar");
206  }
207 
208  try {
209  defConfig.loadFromString(text);
210  } catch (final InvalidConfigurationException e) {
211  getLogger().log(Level.SEVERE, "Cannot load configuration from jar", e);
212  }
213  }
214 
215  newConfig.setDefaults(defConfig);
216  }
InputStream getResource(String filename)
void org.bukkit.plugin.java.JavaPlugin.saveConfig ( )

Saves the FileConfiguration retrievable by getConfig().

Implements org.bukkit.plugin.Plugin.

Definition at line 223 of file JavaPlugin.java.

References org.bukkit.plugin.java.JavaPlugin.getConfig(), and org.bukkit.configuration.file.FileConfiguration.save().

223  {
224  try {
225  getConfig().save(configFile);
226  } catch (IOException ex) {
227  logger.log(Level.SEVERE, "Could not save config to " + configFile, ex);
228  }
229  }
void org.bukkit.plugin.java.JavaPlugin.saveDefaultConfig ( )

Saves the raw contents of the default config.yml file to the location retrievable by getConfig(). If there is no default config.yml embedded in the plugin, an empty config.yml file is saved. This should fail silently if the config.yml already exists.

Implements org.bukkit.plugin.Plugin.

Definition at line 232 of file JavaPlugin.java.

References org.bukkit.plugin.java.JavaPlugin.saveResource().

232  {
233  if (!configFile.exists()) {
234  saveResource("config.yml", false);
235  }
236  }
void saveResource(String resourcePath, boolean replace)
void org.bukkit.plugin.java.JavaPlugin.saveResource ( String  resourcePath,
boolean  replace 
)

Saves the raw contents of any resource embedded with a plugin's .jar file assuming it can be found using getResource(String).

The resource is saved into the plugin's data folder using the same hierarchy as the .jar file (subdirectories are preserved).

Parameters
resourcePaththe embedded resource path to look for within the plugin's .jar file. (No preceding slash).
replaceif true, the embedded resource will overwrite the contents of an existing file.
Exceptions
IllegalArgumentExceptionif the resource path is null, empty, or points to a nonexistent resource.

Implements org.bukkit.plugin.Plugin.

Definition at line 239 of file JavaPlugin.java.

References org.bukkit.plugin.java.JavaPlugin.getResource().

Referenced by org.bukkit.plugin.java.JavaPlugin.saveDefaultConfig().

239  {
240  if (resourcePath == null || resourcePath.equals("")) {
241  throw new IllegalArgumentException("ResourcePath cannot be null or empty");
242  }
243 
244  resourcePath = resourcePath.replace('\\', '/');
245  InputStream in = getResource(resourcePath);
246  if (in == null) {
247  throw new IllegalArgumentException("The embedded resource '" + resourcePath + "' cannot be found in " + file);
248  }
249 
250  File outFile = new File(dataFolder, resourcePath);
251  int lastIndex = resourcePath.lastIndexOf('/');
252  File outDir = new File(dataFolder, resourcePath.substring(0, lastIndex >= 0 ? lastIndex : 0));
253 
254  if (!outDir.exists()) {
255  outDir.mkdirs();
256  }
257 
258  try {
259  if (!outFile.exists() || replace) {
260  OutputStream out = new FileOutputStream(outFile);
261  byte[] buf = new byte[1024];
262  int len;
263  while ((len = in.read(buf)) > 0) {
264  out.write(buf, 0, len);
265  }
266  out.close();
267  in.close();
268  } else {
269  logger.log(Level.WARNING, "Could not save " + outFile.getName() + " to " + outFile + " because " + outFile.getName() + " already exists.");
270  }
271  } catch (IOException ex) {
272  logger.log(Level.SEVERE, "Could not save " + outFile.getName() + " to " + outFile, ex);
273  }
274  }
InputStream getResource(String filename)
final void org.bukkit.plugin.java.JavaPlugin.setEnabled ( final boolean  enabled)
protected

Sets the enabled state of this plugin

Parameters
enabledtrue if enabled, otherwise false

Definition at line 311 of file JavaPlugin.java.

References org.bukkit.plugin.java.JavaPlugin.onDisable(), and org.bukkit.plugin.java.JavaPlugin.onEnable().

Referenced by org.bukkit.plugin.java.JavaPluginLoader.disablePlugin().

311  {
312  if (isEnabled != enabled) {
313  isEnabled = enabled;
314 
315  if (isEnabled) {
316  onEnable();
317  } else {
318  onDisable();
319  }
320  }
321  }
final void org.bukkit.plugin.java.JavaPlugin.setNaggable ( boolean  canNag)

Set naggable state

Parameters
canNagis this plugin still naggable?

Implements org.bukkit.plugin.Plugin.

Definition at line 453 of file JavaPlugin.java.

453  {
454  this.naggable = canNag;
455  }

The documentation for this class was generated from the following file: