4 JavaScript API
Jesse Boyd edited this page 2018-08-23 13:49:18 +10:00

Requirements

  • FAWE for 1.13
  • Java 9 (Or nashorn with es6 support)

Introduction

  • JavaScript can be used to register commands, patterns, masks, and brushes
  • Scripts should be placed in plugins/FastAsyncWorldEdit/commands (can be configured in the config.yml)
  • Use /fawe reload to reload the scripts.
  • Nashorn Tutorial | Oracle Nashorn Tutorial

Creating normal commands

Create a new .js file in commands/, or commands/<alias>/

Example file:

  • All functions with a description (desc) will be registered.
load("nashorn:mozilla_compat.js") // Allow package imports
importPackage(Packages.java.io);
importPackage(Packages.java.lang);
importPackage(Packages.org.bukkit);
importPackage(Packages.com.sk89q.worldedit); // Import whatever packages you need

hello.desc = "Some description"; // this is the only required field
hello.aliases = ["hello", "helloworld"];  // Defaults to the function name
hello.usage = "[blah=3]"; // Defaults to the parameters
hello.flags = "f"; // A string with the characters that are flag parameters (defaults to empty string)
hello.permission = "fawe.use"; // The permission (defaults to fawe.use)
function hello(player = Player, blah = Double = "3", f) {
    // Parameters are in the form: param = type = default
    // Type defaults to String (flags default to Boolean)
    // To make a value optional use null as the default (flags are optional)
    
    player.print("Hello World: " + blah + " | " + f);
}

Creating a custom pattern

Create a new .js file in commands/patterns
(Similar thing with masks, except in commands/masks, and returns a Mask)

load("nashorn:mozilla_compat.js")
importPackage(Packages.java.io);
importPackage(Packages.java.lang);
importPackage(Packages.org.bukkit);
importPackage(Packages.com.sk89q.worldedit);
importPackage(Packages.com.sk89q.worldedit.blocks);

test.desc = "Using a pattern  from javascript";
test.aliases = ["#blah"]; 
function test(player = Player) {
    // Return some pattern here - idk stone?
    return new BaseBlock(1, 0);
}

Registering a brush

Create a new .js file in commands/brush

// Some standard imports, half of which aren't used, lol
load("nashorn:mozilla_compat.js")
importPackage(Packages.java.io);
importPackage(Packages.java.lang);
importPackage(Packages.org.bukkit);
importPackage(Packages.com.boydti.fawe);
importPackage(Packages.com.sk89q.worldedit);
importPackage(Packages.com.sk89q.worldedit.entity);
importPackage(Packages.com.sk89q.worldedit.blocks);
importPackage(Packages.com.sk89q.worldedit.patterns);
importPackage(Packages.com.sk89q.worldedit.vector);
importPackage(Packages.com.sk89q.worldedit.regions);
importPackage(Packages.com.sk89q.worldedit.regions.region);
importPackage(Packages.com.sk89q.minecraft.util.commands)
importPackage(Packages.com.sk89q.worldedit.tools);
importPackage(Packages.com.sk89q.worldedit.command);
importPackage(Packages.com.sk89q.worldedit.tools.brushes);
importPackage(Packages.com.sk89q.worldedit.world.biome);
importPackage(Packages.com.sk89q.worldedit.command.tool);
importPackage(Packages.com.sk89q.worldedit.command.tool.brush);
importPackage(Packages.com.sk89q.worldedit.function.operation);
importPackage(Packages.com.sk89q.worldedit.function.pattern);
importPackage(Packages.com.sk89q.worldedit.extension.platform.permission);

// Brush commands expect a BrushSettings to be returned
// We can use the BrushProcessor class to help construct one
var worldEdit = WorldEdit.getInstance();
var processor = new BrushProcessor(worldEdit);

spherejs.desc = "Sphere brush from javascript"; // The description (required)
spherejs.aliases = ["spherejs"]; // The aliases for the command (defaults to the name of the function)
spherejs.usage = "<pattern> [radius=2]"; // The usage (defaults to the function parameters)
spherejs.flags = "hf"; // The flag parameters (defaults to empty string)
spherejs.permission = "worldedit.brush.spherejs"; // The permission (defaults to fawe.use)
function spherejs(player = Player, editSession = EditSession, session = LocalSession, fill = Pattern, radius = Expression = "5", h, f, context = CommandContext) {
    // Parameters are in the form: param = type = default
    // Type defaults to String (flags default to Boolean)
    // To make a value optional use null as the default
    
    worldEdit.checkMaxBrushRadius(radius);
    player.print("h flag is " + h); // TODO do something with the flags?
    player.print("f flag is " + f);
    // TODO return some custom brush instead of sphere
    return processor.set(session, context, new SphereBrush()).setSize(radius).setFill(fill);
}