In this short tutorial I will teach you how to use Hyperion's JScript engine (And if you don't use Hyperion how to implant it).
If you do not use Hyperion, add this in your server:
Also if you don't use Hyperion, add this where your server startup is done:
(data/scripts/ is the path where you scripts are being held.)
In this tutorial Ill commands as an example.
In your command packet add:
Now, look at this part of the code:
The string command is the name of the method that the script manager will call. (example: if you're command name is "pos", the script manager will look for a method named pos.)
The string args and the instance player is looked for in "( )" after the method name.
Example, using the command pos again:
As you see in that example, JScript is very similar to Java. Think of "function" to be "void" like in Java.
For some commands, you will need to import packages or classes. This is done fairly similar as its done in Java.
For that command, I need the class bank. To import that class, I have two options.
A:
I can import just that class:
Or B:
I can import the whole package. I only recommend this if you need two or more classes in that same package.
You can do almost anything you can do in Java in JScript. Loops, arrays, switch statements. You can even import packages and classes in the Java API!
An important difference between Java and JScript is how you declare integers. Instead of doing:
You would do:
This is the extreme basics and how to just set it up, Ill update this tutorial again .
If you do not use Hyperion, add this in your server:
Code: [Select]
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
/**
* Manages server scripts.
*
* @author blakeman8192
*
*/
public class ScriptManager {
/**
* The singleton of this class.
*/
private static final ScriptManager INSTANCE = new ScriptManager();
/**
* Gets the ScriptManager singleton.
*
* @return The ScriptManager singleton.
*/
public static ScriptManager getScriptManager() {
return INSTANCE;
}
/**
* The ScriptEngineManager.
*/
private ScriptEngineManager mgr;
/**
* The JavaScript Engine.
*/
private ScriptEngine jsEngine;
/**
* The logger for this manager.
*/
private final Logger logger = Logger.getLogger(this.toString());
/**
* Creates the script manager.
*/
private ScriptManager() {
mgr = new ScriptEngineManager();
jsEngine = mgr.getEngineByName("JavaScript");
logger.info("Loading scripts...");
}
/**
* Invokes a JavaScript function.
*
* @param identifier
* The identifier of the function.
* @param args
* The function arguments.
*/
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);
} catch (ScriptException ex) {
logger.log(Level.WARNING, "ScriptException thrown!", ex);
}
}
/**
* Loads JavaScript files into the JavaScript ScriptEngine from the argued
* path.
*
* @param dirPath
* The path of the directory to load the JavaScript source files
* from.
*/
public void loadScripts(String dirPath) {
File dir = new File(dirPath);
if (dir.exists() && dir.isDirectory()) {
File[] children = dir.listFiles();
for (File child : children) {
if (child.isFile() && child.getName().endsWith(".js"))
try {
jsEngine.eval(new InputStreamReader(
new FileInputStream(child)));
} catch (ScriptException ex) {
logger.log(Level.SEVERE, "Unable to load script!", ex);
} catch (FileNotFoundException ex) {
logger.log(Level.SEVERE, "Unable to find script!", ex);
}
else if (child.isDirectory())
loadScripts(child.getPath());
}
}
logger.info("Loaded scripts!");
}
}Also if you don't use Hyperion, add this where your server startup is done:
Code: [Select]
ScriptManager.getScriptManager().loadScripts("data/scripts/");(data/scripts/ is the path where you scripts are being held.)
In this tutorial Ill commands as an example.
In your command packet add:
Code: [Select]
ScriptManager.getScriptManager().invoke(command, (Object) args, player);Now, look at this part of the code:
Code: [Select]
(command, (Object) args, player)The string command is the name of the method that the script manager will call. (example: if you're command name is "pos", the script manager will look for a method named pos.)
The string args and the instance player is looked for in "( )" after the method name.
Example, using the command pos again:
Code: [Select]
function pos(args,player) {
player.getActionSender().sendMessage("You are at: " + player.getLocation() + ".");
}As you see in that example, JScript is very similar to Java. Think of "function" to be "void" like in Java.
For some commands, you will need to import packages or classes. This is done fairly similar as its done in Java.
Code: [Select]
function bank(args,player) {
Bank.open(player);
}For that command, I need the class bank. To import that class, I have two options.
A:
I can import just that class:
Code: [Select]
importClass(org.hyperion.rs2.model.container.Bank)Or B:
I can import the whole package. I only recommend this if you need two or more classes in that same package.
Code: [Select]
importPackage(org.hyperion.rs2.model.container)You can do almost anything you can do in Java in JScript. Loops, arrays, switch statements. You can even import packages and classes in the Java API!
An important difference between Java and JScript is how you declare integers. Instead of doing:
Code: [Select]
int maxAmount = Integer.MAX_VALUE;You would do:
Code: [Select]
var maxAmount = Integer.MAX_VALUE;This is the extreme basics and how to just set it up, Ill update this tutorial again .

















comments (0)
Post a Comment