- Notifications
You must be signed in to change notification settings - Fork 14
JavaScript placeholders
To add a script to the plugin, navigate to the config/placeholderapi/javascript folder, then add your scripts there. Scripts will be loaded so long as the file ends with the .js file extension.
PlaceholderAPI will parse JavaScript using the Nashorn JavaScript engine. Because the plugin loads off of Sponge, all SpongeAPI classes are available for use. For example, you can create a simple function such as this:
varpName=player.getName();//Java type player worksvarout="Hello, "+pName;out=out+"!";This will return "Hello, <name>!" when called.
player - the Player object that the placeholder is parsing for
server - the Server the plugin is running on
args - a string array that holds all tokens provided to the variable.
service - a function that takes in a string and parses it for placeholders. JavaScript placeholders are excluded.
Example using all provided objects:
varpName=player.getName();//Java type providedvarout="Hello, "+pName+"!";varonline=server.getOnlinePlayers().size();out=out+" There are "+online+" players online!";out=out+(args!=null&&args.length>0 ? " "+args[0] : "");When provided %javascript_<script>_Hi!%, the output is "Hello, <name>! There are <online> players online! Hi!"
To create a Text object, you can use JavaImporter and import SpongeAPI packages.
varmain=function(){varimports=newJavaImporter(org.spongepowered.api.text,org.spongepowered.api.text.action,org.spongepowered.api.text.format)with(imports){varname=service.get("%player_displayname%","(%)([^ %]+)(%)");// Get returns a 'Text' object - the second parameter is a Regular Expression pattern for parsing the placeholders, and is optional.varpl=service.value("%server_online%");// Value returns a 'String' object. The second parameter, like 'get', is optional and is a Regular Expression.// NOTE: JavaScript placeholders cannot be parsed with service.get(...) or service.value(...).varout=Text.of(TextColors.GREEN,"Hello! ",TextActions.showText(Text.of(TextColors.AQUA,"Click me, ",name,TextColors.AQUA,"!")),TextActions.runCommand("I clicked the message!"),TextColors.AQUA,"[Click]");returnout;}}main();You can use this style of importing for other parts of the SpongeAPI. NOTE: Sub-packages are not automatically imported, so they must be included in the importer.
TODO: Document service.value(...)