Visitors

This page explains how to use visitors, which enable modular, reusable configurations for Lamp and Lamp.Builder through customizable registration and hooks.

Modular Visitors in Lamp

The Lamp library allows for modular and reusable configurations through the use of visitors. These visitors can be used to customize the behavior of both the Lamp.Builder and the Lamp instances themselves. This document provides an overview of how to use the LampBuilderVisitor and LampVisitor interfaces to create modular visitors that can register or add specific features to your command framework.

LampBuilderVisitor

Overview

LampBuilderVisitor is a functional interface that operates on a Lamp.Builder object. It allows for modular registration of additional features during the builder phase, making it easy to add reusable configurations across different parts of your application.

Creating a LampBuilderVisitor

Here's how you can create a LampBuilderVisitor to register common parameter types for Bukkit:

/**
 * Registers the following parameter types:
 * <ul>
 *     <li>{@link Player}</li>
 *     <li>{@link OfflinePlayer}</li>
 *     <li>{@link World}</li>
 *     <li>{@link EntitySelector}</li>
 * </ul>
 *
 * @param <A> The actor type
 * @return The visitor
 */
public static <A extends BukkitCommandActor> @NotNull LampBuilderVisitor<A> bukkitParameterTypes() {
    return builder -> {
        builder.parameterTypes()
                .addParameterTypeLast(Player.class, new PlayerParameterType())
                .addParameterTypeLast(OfflinePlayer.class, new OfflinePlayerParameterType())
                .addParameterTypeLast(World.class, new WorldParameterType())
                .addParameterTypeFactoryLast(new EntitySelectorParameterTypeFactory());
        if (BukkitVersion.isBrigadierSupported())
            builder.parameterTypes()
                    .addParameterTypeLast(Entity.class, new EntityParameterType());
    };
}

Using a LampBuilderVisitor

To apply this visitor to your Lamp.Builder, simply call the accept method:

Lamp.Builder<BukkitCommandActor> builder = Lamp.builder();
builder.accept(bukkitParameterTypes());
var lamp = builder.build();

This approach allows you to reuse the visitor across multiple builders, ensuring consistency and reducing boilerplate code.

LampVisitor

Overview

LampVisitor is a functional interface designed to operate on an instantiated Lamp object. It allows for modular modifications or additions to the Lamp instance after it has been built.

Creating a LampVisitor

You can create a LampVisitor to perform specific actions on the Lamp instance, such as registering additional commands or setting up event listeners.

public static <A extends CommandActor> @NotNull LampVisitor<A> myCustomLampVisitor() {
    return lamp -> {
        // Example: Register additional commands or listeners
        lamp.register(new MyCustomCommand());
    };
}

Using a LampVisitor

To apply this visitor to your Lamp instance, use the accept method:

var lamp = Lamp.builder().build();
lamp.accept(myCustomLampVisitor());

This approach makes it easy to extend the functionality of the Lamp instance without directly modifying its construction logic.

Last updated