SpongeJavaScript
SpongeJavaScript is a plugin that allow anyone who know JavaScript to create their own plugins. The project is under the MIT license.
How SpongeJavaScript works
It use Nashorn, a JavaScript engine in Java. This engine give the possibility to the developers to code in JavaScript to interact with the Java objects. To do this, you have to do something like this.
var player;//Java object of a player
player.setDisplayName("Djxy");
With SpongeJavaScript you have the possibility to do the same thing, but with the JavaScript syntax.
var player;//Same Java object, but wrapped by the plugin
player.setDisplayName("Djxy");
//or
player.displayName = "Djxy";
The power of this plugin is that you can interact with any Java objects that are wrapped by the plugin. That mean you can interact with the Sponge API in JavaScript.
Possibilites
- Interact with every objects.
- Handle every events.
- Create your commands and interact with the arguments when executed.
- Simple interface to interact with the economy service.
- Your own logger for the console.
- A file manager to ease the creation of your config file.
Plugin example
Click here for more information about how to create a JavaScript plugin.
var blocks = {};
function onGameInitializationEvent(event){
fileManager.create("blocks.json");
var fileJSON = JSON.parse(fileManager.read("blocks.json"));
if(fileJSON != null)
blocks = fileJSON;
console.log("First plugin loaded!");
commandManager.register(createCommand());
eventManager.register(createEventListenerBlockBreak());
}
function onGameStoppingServerEvent(event){
fileManager.write("blocks.json", JSON.stringify(blocks));
}
function createEventListenerBlockBreak(){
var eventListener = {
listener: function(event, player) {
//Name of the block break
var name = event.transactions[0].original.state.type.name+"";
//If the player is defined and the block is defined in the list of blocks, it send a message to the player that inform him how much money he received.
if(player && blocks[name]){
player.sendMessage(Text.of("You received ", TextColors.GREEN, blocks[name]+" "+economyService.currency.symbol.toPlain));
economyService.addMoney(player, parseFloat(blocks[name]));
}
},
event: org.spongepowered.api.event.block.ChangeBlockEvent.Break.class
};
return eventListener;
}
function createCommand(){
var command = {
description: "Set the amount of money to receive when you break this block.",
permission: "job.set.amount",
executor: function(commandSource, commandContext) {
//If the commandSource is a player, it set the price of the block and informs the player the price has been set.
if(typeof commandSource == typeof Player.class){
commandSource.sendMessage(Text.of("You set the amount to receive for ", TextColors.GREEN, commandContext.one("blockId").get+"", TextColors.WHITE, " to ", TextColors.GREEN, commandContext.one("amount").get+""+economyService.currency.symbol.toPlain));
blocks[commandContext.one("blockId").get+""] = Javascript.convertJSObjectToObject(commandContext.one("amount").get);
}
},
commands: ["job", "jsJob"],
arguments: [
{
format: "onlyOne",
name: "blockId"
},
{
format: "onlyOne",
type: "doubleNum",
name: "amount"
}
]
};
return command;
}