🛖
Fox Hut
  • Introduction
    • Setting up
    • CommandActor, @Command and @Subcommand
    • Creating your first command
    • Improving our greet command
  • Platforms
    • Bukkit / Spigot / Paper
    • BungeeCord
    • Velocity
    • Sponge
    • Fabric
    • Brigadier
    • Minestom
    • JDA
    • Command line
  • How-to
    • Creating variants of /teleport
    • Custom parameter types
    • Suggestions and auto-completion
    • Context parameters
    • Command permissions
    • Parameter validators
    • Command conditions
    • Response handlers
    • Cooldowns
    • Help commands
    • Annotation replacers
    • Orphan command
    • Exception handling
    • Hooks
    • Dependency injection
    • Visitors
    • Customizing the dispatcher and failure behavior
Powered by GitBook
On this page
  • Modular Visitors in Lamp
  • LampBuilderVisitor
  • LampVisitor

Was this helpful?

Edit on GitHub
  1. How-to

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());
    };
}
/**
 * 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
 */
fun <A : BukkitCommandActor> bukkitParameterTypes(): LampBuilderVisitor<A> {
    return LampBuilderVisitor { builder ->
        builder.parameterTypes()
            .addParameterTypeLast(Player::class.java, PlayerParameterType())
            .addParameterTypeLast(OfflinePlayer::class.java, OfflinePlayerParameterType())
            .addParameterTypeLast(World::class.java, WorldParameterType())
            .addParameterTypeFactoryLast(EntitySelectorParameterTypeFactory())
        if (BukkitVersion.isBrigadierSupported())
            builder.parameterTypes()
                .addParameterTypeLast(Entity::class.java, 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();
val builder: Lamp.Builder<BukkitCommandActor> = Lamp.builder()
builder.accept(bukkitParameterTypes())
val lamp: Lamp<BukkitCommandActor> = 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());
    };
}
fun <A : CommandActor> myCustomLampVisitor(): LampVisitor<A> {
    return LampVisitor { lamp ->
        // Example: Register additional commands or listeners
        lamp.commandHandler.register(MyCustomCommand())
        lamp.eventHandler.register(MyCustomEventListener())
    }
}

Using a LampVisitor

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

var lamp = Lamp.builder().build();
lamp.accept(myCustomLampVisitor());
val 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.

PreviousDependency injectionNextCustomizing the dispatcher and failure behavior

Last updated 9 months ago

Was this helpful?