Skip to content

Adding a Custom Function

Jack William Kerr edited this page May 1, 2026 · 1 revision

Hopplet exposes a developer API that allows you to create custom functions for any of your players to use.


Creating a Custom Function

You can create a function that filters items by any criteria you desire.

Below is an example of an actual function in Hopplet.

public final class BookAuthorFunction implements Function<Set<String>> {

    @Override
    public @NonNull String name() {
        return "book_author";
    }

    @Override
    public @NonNull Set<String> aliases() {
        return Set.of("author");
    }

    @Override
    public @NonNull Component description() {
        return Component.text("Filters an item by the name of a book's author.");
    }

    @Override
    public @NonNull Plugin plugin() {
        return Hopplet.instance();
    }

    @Override
    public @NonNull Set<String> compile(@NonNull List<String> arguments) throws FilterCompileException {
        if (arguments.isEmpty()) throw new FilterCompileException(Component.text("No arguments provided to function " + name() + "."))
        
        return new HashSet<>(arguments);
    }

    @Override
    public boolean test(Filter.@NonNull Context context, @NonNull Set<String> arguments) {
        ItemStack stack = context.stack();
        if (!(stack.getItemMeta() instanceof BookMeta meta)) return false;

        String author = meta.getAuthor();
        if (author == null) return false;

        return arguments.contains(author);
    }
}

classes implementing Function require implementations of:

  • name(): a name to identify this function.
  • plugin(): used to identify this function and distinguish it from other functions that may have the same name.
  • compile(List<String>): this is where the strings provided by the user writing a filter are parsed into a more usable form if necessary. If your argument does not have any arguments, you can return Function.NO_ARGUMENTS.
  • test(Filter.Context, Set<String>): this is where a function determines whether an item is allowed to pass this filter.

To register your new function:

Function.register(new BookAuthorFunction());

Clone this wiki locally