[Discontinued] ✏ Create a plugin in JavaScript [v2.0.2]

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;
}

Links

7 Likes

Update 0.1.1

  • Four functions added
  • setInterval
  • setTimeout
  • clearInterval
  • clearTimeout

Links

Tested on:

  • Plugin:
  • Spongeforge-1.8.9-1694-3.1.0-BETA-1090
  • Forge-1.8.9-11.15.0.1715-universal

Update 0.2

  • Optimization: Some objects from Sponge are stock in memory to reuse it later.

Links

Tested on:

  • Plugin:
  • Spongeforge-1.8.9-1694-3.1.0-BETA-1090
  • Forge-1.8.9-11.15.0.1715-universal

Hacks. 110% hacks.

1 Like

Hack? I’m not sure to understand

Plugins in Javascript. Crazy. Haxor.

1 Like

#Update 0.3.1

  • Update to Sponge API 4.0.1
  • Refactor some packages
  • Added the MIT license in the files

Links

Could this be modified to create a Skript-style language? So other developers could allow admins to script different situations, like when a vote is received?

What do you mean by Skript-style language?

GitHub - Njol/Skript: Skript is a Bukkit plugin which allows server admins to customize their server easily, but without the hassle of programming a plugin or asking/paying someone to program a plugin for them. So allow other plugins to interpret and execute the JavaScript.

If I understand, you want to add your own variables to the SpongeJavaScript environment to allow the admins to use your variables?

Sounds close enough. I’d want it to execute as a part of my plugin, not a standalone, and act as an extention.

Not only variables, but functions, objects, etc.

Yes I could do that. I will create an API to let you add your variables, objects and functions.

I will add the possibility to execute JavaScript as a part of your plugin too.

#Update 1.0

  • API
  • Create your own JavaScript environment.
  • Interact with the scripts loaded by the plugin.
  • Add your variables and functions to let the JavaScript developers interact with your plugin.
  • Wiki updated with new tutorials.
  • Gradle/Maven support

Links

100% will follow you :smiley_cat:

1 Like

Sorry for bumping.

Is there any way to live edit scripts ? Or should I reboot the server ?

Yes, you can add functions and variables in the runtime.

Great ! What is its behaviour ? Refreshing every X times ? Or detecting file changes ?

I am comfortable with live edit but I like to have some control like “my script is done let’s try now, /spongeJavaScript reload”

As a web developer, I am really thankful for your plugin :blush:

No, there is no command to reload a script in runtime. Currently, the only way to edit a script in runtime is with Java. I will try to do an update tonight to update a script in runtime with a command.