3 Registering custom brushes or commands
Jesse Boyd edited this page 2018-07-07 07:45:39 +10:00

With FAWE you can register your own brushes and commands:

/**
 * Create a command with the provided aliases and register all methods of the class as sub commands.<br>
 *  - You should try to register commands during startup
 *  - If no aliases are specified, all methods become root commands
 * @param clazz The class containing all the sub command methods
 * @param aliases The aliases to give the command (or none)
 */
FaweAPI.registerCommands(Object clazz, String... aliases);
// Note: This is just an alias for `CommandManager.getInstance().registerCommands(...)`

A snippet from the BrushCommands class which registers the cylinder brush

@Command(
        aliases = { "cylinder", "cyl", "c" },
        usage = "<block> [radius] [height]",
        flags = "h",
        desc = "Choose the cylinder brush",
        help =
                "Chooses the cylinder brush.\n" +
                        "The -h flag creates hollow cylinders instead.",
        min = 1,
        max = 3
)
@CommandPermissions("worldedit.brush.cylinder")
public void cylinderBrush(Player player, LocalSession session, Pattern fill,
                          @Optional("2") double radius, @Optional("1") int height, @Switch('h') boolean hollow) throws WorldEditException {
    worldEdit.checkMaxBrushRadius(radius);
    worldEdit.checkMaxBrushRadius(height);
    BrushTool tool = session.getBrushTool(player);
    tool.setFill(fill);
    tool.setSize(radius);
    if (hollow) {
        tool.setBrush(new HollowCylinderBrush(height), "worldedit.brush.cylinder", player);
    } else {
        tool.setBrush(new CylinderBrush(height), "worldedit.brush.cylinder", player);
    }
    player.print(BBC.getPrefix() + BBC.BRUSH_SPHERE.f(radius, height));
}

And the cylinder brush:

public class CylinderBrush implements Brush {

    private int height;

    public CylinderBrush(int height) {
        this.height = height;
    }

    @Override
    public void build(EditSession editSession, Vector position, Pattern pattern, double size) throws MaxChangedBlocksException {
        if (pattern == null) {
            pattern = new BlockPattern(new BaseBlock(BlockID.COBBLESTONE));
        }
        editSession.makeCylinder(position, Patterns.wrap(pattern), size, size, height, true);
    }

}

Bindings

The bindings are the types that can be used in your commands (e.g. int, Pattern, Vector etc) By default these are the bindings available to you:

To register a custom binding use CommandManger.getInstance().getBuilder().addBinding(yourCustomBinding)

Command Processing

A command processor is used to process/transform a command class. For instance, BrushCommands returns a BrushSettings class, and the BrushProcessor (implements CallableProcessor<BrushSettings>) takes this brush, sets it for the player and sends them a message.

You can either implement CallableProcessor<T> in the command class you are registering (recommended) or pass it as an additional argument with CommandManager.getInstance().getBuilder().registerMethodsAsCommands(...)