I recently learned how to use this and its very helpful so lets get started.
1. Locating The Script Folder
___
First thing you need to know is the location of the scripts folder.
Within this folder you will find a plugin Graham placed in there when he released the source of course it isn't invoked and its not really useful it just a hello world plugin. I suggest you delete it as it isn't needed.
2. The Plugins Bare Format
___
This is how you will set up a plugin using a Javascript function which is similar to a void.
Code:
function myPlugin() {
}
3. Importing Java Classes
___
To import a java class to use in your plugin you will need to use "importClass(#classPath.#className);" which you place at the top of your class.
Ex.
Code:
importClass(org.hyperion.rs2.model.player.Player);
function myPlugin() {
}
4. Using Parameters And Class Methods.
___
Okay so this part is a little different from java but not much it is still pretty simple. First off parameters say your want to create a plugin for a player action you would need to import the Player class and add the Player parameter which will look like this.
Code:
importClass(org.hyperion.rs2.model.player.Player);
function myPlugin(Player) {
}
If you take notice unlike in java parameters don't have labels which means you will only be using the class name. Now lets call upon the the "sendMessage(String message)" packet for this plugin.
Code:
importClass(org.hyperion.rs2.model.player.Player);
function myPlugin(Player) {
Player.getActionSender().sendMessage("You just triggered a plugin.");
}
Pretty simple : D.
5. Statements
___
Using simple statements are just as easy as using them in java. So for this instance we will be making a plugin that will let a player with a certain combat level get some gold upon logging in.
Code:
importClass(org.hyperion.rs2.model.player.Player);
importClass(org.hyperion.rs2.model.item.Item);
function myPlugin(Player) {
if(Player.getSkills().getCombatLevel() > 5) {
Player.getInventory().add(new Item(995, 100));
} else {
Player.getActionSender().sendMessage("You aren't a high enough level to obtain a login gold bonus.");
}
}
And seeing as you used the Item type you will need to import that class as well.
6. Invoking your script.
___
The process of invoking your plugin is what will make it run. Lets break down the invoke method.
Code:
public void invoke(String identifier, Object... args) {
Invocable invEngine = (Invocable) jsEngine;
try {
invEngine.invokeFunction(identifier, args);
} catch (NoSuchMethodException ex) {
logger.log(Level.WARNING, "No such method: " + identifier, ex);
} catch (ScriptException ex) {
logger.log(Level.WARNING, "ScriptException thrown!", ex);
}
}
When declaring the method you will need to first fill in the String identifier which is the name of the javascript function() so in this particular instance it would be "myPlugin".
Code:
ScriptManager.getScriptManager().invoke("myPlugin", Object... args);
Now we need to add are arguments or the parameters you used. In this instance we used "Player" so we need to create an instance of "Player player" in the java class you are declaring the invoke method and add that instance label to the args parameter of the invoke method like so.
Code:
ScriptManager.getScriptManager().invoke("myPlugin", player);
Now when you use more then one argument you can simple add a comma after the first delared argument so on and so fourth like so.
Code:
ScriptManager.getScriptManager().invoke("myPlugin", player, item);
with
Code:
importClass(org.hyperion.rs2.model.player.Player);
importClass(org.hyperion.rs2.model.item.Item);
function myPlugin(Player, Item) {
if(Player.getSkills().getCombatLevel() > 5) {
Player.getInventory().add(new Item(995, 100));
} else {
Player.getActionSender().sendMessage("You aren't a high enough level to obtain a login gold bonus.");
}
}
These are just the basics I'm still learning my way around I though this may be helpful to anyone who would like to start using the plugin system or didn't have an idea of where to start.