Package net.risingworld.api
Class Plugin
java.lang.Object
net.risingworld.api.Plugin
Plugin
is the base class for all plugins. Extend this class to create your own plugin.-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionfinal void
"Enqueues" a task (Runnable).final void
executeDelayed
(float delay, Runnable runnable) Executes a task after a given delay.final Collection<Plugin>
Returns a collection of all plugins which are currently loaded.getDescription
(String name) Gets the description for this plugin (which is defined in the plugin.yml file).final String
Gets the current version of the game/server.final int
getID()
Gets the internal id of this plugin.int
Gets the load order for this plugin.final Database
getMySQLConnection
(String database, String ip, int port, String username, String password) Creates a new MySQL database connection.getName()
Gets the name of this plugin.getPath()
Gets the path to the plugin folder (for example: "C:/Server/plugins/MyPlugin"), without a slash at the end.final Plugin
getPluginByID
(int pluginID) Finds a plugin by id.final Plugin
getPluginByName
(String pluginName) Finds a plugin with the according name and returns the instance.final float
Gets the total running time of the game/server in seconds.final Database
getSQLiteConnection
(String database) Creates a new SQLite database connection (in thread-safe mode).final WorldDatabase
Gets an interface providing access to the world database.final boolean
Checks whether or not the currently executing thread is the main thread.abstract void
This method is called when the plugin will be disabled and unloaded.abstract void
onEnable()
This method is called when the plugin will be enabled.void
onLoad()
This method is called when the plugin will be loaded (beforeonEnable()
is called).final void
registerEventListener
(Listener listener) Registers an eventListener
.final String
sendHttpRequest
(String url) Sends a http request to the target url.final String
sendHttpRequest
(String url, HashMap<String, String> postData) Sends a http request with POST data to the target url.final void
triggerEvent
(Event event) Calls an event.final void
unregisterEventListener
(Listener listener) Unregisters an eventListener
, i.e this listener will no longer be called when an event triggers.
-
Constructor Details
-
Plugin
protected Plugin()
-
-
Method Details
-
onLoad
public void onLoad()This method is called when the plugin will be loaded (beforeonEnable()
is called). Other plugins may not be loaded at this stage. Override this method if necessary, otherwise just use theonEnable()
method for initialization purposes. -
onEnable
public abstract void onEnable()This method is called when the plugin will be enabled. At this point all other plugins are already loaded. You have to override this method when extending this class.- Example:
-
onDisable
public abstract void onDisable()This method is called when the plugin will be disabled and unloaded. You have to override this method when extending this class. -
getID
public final int getID()Gets the internal id of this plugin. Once the game / server is started, it assigns a unique id to every plugin, but this id is only valid during runtime (i.e the plugin may get a different id after restart).- Returns:
- the internal id of this plugin.
-
getName
Gets the name of this plugin.- Returns:
- the plugin name.
-
getDescription
Gets the description for this plugin (which is defined in the plugin.yml file).- Parameters:
name
- the actual property you want to get. Valid names:name, version, author, team, description, main, license, website, contact, git
- Returns:
- the property, or "N/A" when no such property was found.
-
getLoadOrder
public int getLoadOrder()Gets the load order for this plugin. The lower the value, the earlier the plugin gets loaded (when the API gets initialized). Default value is 0.- Returns:
- the load order for this plugin.
-
getPath
Gets the path to the plugin folder (for example: "C:/Server/plugins/MyPlugin"), without a slash at the end.- Returns:
- the path to the plugin folder (the subfolder of this particular plugin).
- Example: Access text file "config.txt" in plugin folder
-
getGameVersion
Gets the current version of the game/server.- Returns:
- the current version of the game/server as a String.
-
getPluginByID
Finds a plugin by id. Please bear in mind that plugin ids are not persistent and they're only valid during the session!- Parameters:
pluginID
- the id of the plugin you're looking for.- Returns:
- the plugin instance, or null if no plugin with this id was found.
-
getPluginByName
Finds a plugin with the according name and returns the instance.- Parameters:
pluginName
- the name of the plugin you're looking for (which is specified in the plugin.yml file)- Returns:
- the plugin instance, or null if no such plugin was found.
- Example: Access another plugin called "PluginB"
-
getAllPlugins
Returns a collection of all plugins which are currently loaded. This method creates a new collection, so modifying it has no effect on the plugins or the server.- Returns:
- a newly created collection containing all currently loaded plugins.
-
getWorldDatabase
Gets an interface providing access to the world database.- Parameters:
db
- the world database type / target (i.e which world database you want to access).- Returns:
- an interface which provides access to the desired world database.
-
getSQLiteConnection
Creates a new SQLite database connection (in thread-safe mode). If the provided database file does not exists, it will be created. Instead of providing a file path, you can also create a memory database (by providing null as parameter).
Note: Calling this method always creates a new connection. It's recommendable to create a connection to a database only once and reuse that database object. If you no longer need the connection, callDatabase.close()
to free the resources.- Parameters:
database
- the path to the database (to get the relative plugin path, callgetPath()
). Set to null if you want to create a temporary memory database (which only exists while the game is running).- Returns:
- a
Database
object which represents this database connection, or null if an error occurred. - Example: Create connection to "MyDatabase.db" in plugin folder and create new table if not exists
- Example: Create connection to "MyDatabase.db" in subfolder "config"
- Example: Create temporary memory database
-
getMySQLConnection
public final Database getMySQLConnection(String database, String ip, int port, String username, String password) Creates a new MySQL database connection.
Note: Calling this method always creates a new connection. It's recommendable to create a connection to a database only once and reuse that database object. If you no longer need the connection, callDatabase.close()
to free the resources.- Parameters:
database
- the database name.ip
- the ip of the MySQL server.port
- the port of the MySQL server (by default 3306).username
- the login username, e.g "root"password
- the login password.- Returns:
- a
Database
object which represents this database connection, or null if an error occurred (access denied, wrong ip etc.) - Example: Create connection to "rwdb" database on local server
-
sendHttpRequest
Sends a http request to the target url. Returns the content as a string.- Parameters:
url
- the target url- Returns:
- the content of the target page.
-
sendHttpRequest
Sends a http request with POST data to the target url. Returns the content as a string.- Parameters:
url
- the target urlpostData
- the POST data as a hash map- Returns:
- the content of the target page.
-
registerEventListener
Registers an eventListener
. The particular methods of this event will be called when an event was triggered (depending on the parameter of the method - an event method is supposed to have a singleEvent
parameter). Also make sure to use the@EventMethod
annotation for these methods.- Parameters:
listener
- the event listener you want to register.- Example: An example class for an event listener
- Example: Register the "MyListener" event listener
-
unregisterEventListener
Unregisters an eventListener
, i.e this listener will no longer be called when an event triggers.- Parameters:
listener
- the event listener you want to unregister.
-
triggerEvent
Calls an event. This is useful if you create your own custom events and want to call them through the event pipeline.- Parameters:
event
- the event you want to trigger.- Example: Create a custom event and an according listener
- Example: Trigger the "CustomEvent"
-
enqueue
"Enqueues" a task (Runnable). This runnable will be processed the next tick. All runnables that are "enqueued" will always be processed from the same thread, so this might be useful to achieve a thread-safety.- Parameters:
runnable
- the Runnable that will be processed in the next tick.- Example: Enqueue a runnable, using lambda expression
- Example: Same as example #1, but without lamda expressions, instead passing the anonymous class as parameter (old style)
-
executeDelayed
Executes a task after a given delay. Task will be executed from the main server thread (this is always the same thread), similar toenqueue(java.lang.Runnable)
.- Parameters:
delay
- the delay in seconds.runnable
- the task you want to execute delayed.- Example: Execute a task (runnable) after 5 seconds (using lambda expression)
-
isMainThread
public final boolean isMainThread()Checks whether or not the currently executing thread is the main thread.- Returns:
- true if this method was called from the main thread, false if not.
-
getRunningTime
public final float getRunningTime()Gets the total running time of the game/server in seconds. Same asServer.getRunningTime()
.- Returns:
- the total running time in seconds.
-