Class Plugin
java.lang.Object
net.risingworld.api.Plugin
Base class of all plugins. Extend this class to create your own plugin.
Example: Very basic example plugin
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal void"Enqueues" a task (Runnable).final voidexecuteDelayed(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 StringGets the current version of the game/server.final intgetID()Gets the internal id of this plugin.intGets the load order for this plugin.final DatabasegetMySQLConnection(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 PlugingetPluginByID(int pluginID) Finds a plugin by id.final PlugingetPluginByName(String pluginName) Finds a plugin with the according name and returns the instance.final floatGets the total running time of the game/server in seconds.final DatabasegetSQLiteConnection(String database) Creates a new SQLite database connection (in thread-safe mode).final WorldDatabaseGets an interface providing access to the world database.final booleanChecks whether or not the currently executing thread is the main thread.abstract voidThis method is called when the plugin will be disabled and unloaded.abstract voidonEnable()This method is called when the plugin will be enabled.voidonLoad()This method is called when the plugin will be loaded (beforeonEnable()is called).final voidregisterEventListener(Listener listener) Registers an eventListener.final voidRegisters a webserver route handler for this plugin.final voidregisterWebserverHandler(String path, WebserverHandler handler) Registers a webserver route handler for this plugin, providing a callback which is invoked directly whenever a matching HTTP request is received (similar toregisterWebserverHandler(String), but without the need to register a separate event listener and filter the request path manually).final StringsendHttpRequest(String url) Sends a http request to the target url.final StringsendHttpRequest(String url, HashMap<String, String> postData) Sends a http request with POST data to the target url.final voidtriggerEvent(Event event) Calls an event.final voidunregisterEventListener(Listener listener) Unregisters an eventListener, i.e this listener will no longer be called when an event triggers.final voidUnregisters a previously registered webserver route handler (seeregisterWebserverHandler(String)andregisterWebserverHandler(String, WebserverHandler)).
-
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
-
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
Databaseobject 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
Databaseobject 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
-
sendHttpRequest
-
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 singleEventparameter). Also make sure to use the@EventMethodannotation 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"
-
registerWebserverHandler
Registers a webserver route handler for this plugin. Once registered, incoming HTTP requests targeting this route will trigger anHttpRequestEvent.
The provided path is relative to the plugin (it must not be null). The resulting route is always located under the reserved prefix/plugins/<pluginname>/, for example registering the path "status" for a plugin called "MyPlugin" results in the route/plugins/myplugin/status. This prevents collisions with built-in webserver routes and with other plugins.
Note: The Webserver is only active for the dedicated server! Calling this method in singleplayer or co-op (LAN, P2P) has no effect!- Parameters:
path- the route path (relative to the plugin), e.g. "status".- Example: Register a webserver route and respond with JSON
-
registerWebserverHandler
Registers a webserver route handler for this plugin, providing a callback which is invoked directly whenever a matching HTTP request is received (similar toregisterWebserverHandler(String), but without the need to register a separate event listener and filter the request path manually).
The provided path is relative to the plugin and follows the same rules asregisterWebserverHandler(String)(resulting route under/plugins/<pluginname>/). The givenWebserverHandlerwill only be called for requests targeting this particular route.
Note: The handler is invoked on a webserver worker thread, NOT on the main thread. Useenqueue(Runnable)to safely access game state. The Webserver is also only active for the dedicated server!- Parameters:
path- the route path (relative to the plugin), e.g. "status".handler- the handler to call for incoming requests.- Example: Register a webserver route with a callback, responding with JSON
-
unregisterWebserverHandler
Unregisters a previously registered webserver route handler (seeregisterWebserverHandler(String)andregisterWebserverHandler(String, WebserverHandler)).- Parameters:
path- the route path which was used to register the handler.
-
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
-
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.
-