Recipe arguments

A recipe argument command with the suggestions for Minecraft items

The RecipeArgument class lets you retrieve Bukkit's ComplexRecipe object.

Example - Giving a player the result of a recipe

Say we want to give yourself the result of a specific recipe. Since Bukkit's Recipe class contains the getResult() method, we will use that in our example. We want to create the following command:

/giverecipe <recipe>

As such, we easily implement it by specifying the RecipeArgument, casting it and adding it to the player's inventory:

new CommandAPICommand("giverecipe")
    .withArguments(new RecipeArgument("recipe"))
    .executesPlayer((player, args) -> {
        ComplexRecipe recipe = (ComplexRecipe) args.get("recipe");
        player.getInventory().addItem(recipe.getResult());
    })
    .register();
CommandAPICommand("giverecipe")
    .withArguments(RecipeArgument("recipe"))
    .executesPlayer(PlayerCommandExecutor { player, args ->
        val recipe = args["recipe"] as ComplexRecipe
        player.inventory.addItem(recipe.result)
    })
    .register()
commandAPICommand("giverecipe") {
    recipeArgument("recipe")
    playerExecutor { player, args ->
        val recipe = args["recipe"] as ComplexRecipe
        player.inventory.addItem(recipe.result)
    }
}

Example - Unlocking a recipe for a player

In this example, we'll use the ComplexRecipe's getKey() method to write an example to to unlock a recipe for a player. For this command, we'll use the following syntax:

/unlockrecipe <player> <recipe>

This is then implemented trivially as follows:

new CommandAPICommand("unlockrecipe")
    .withArguments(new PlayerArgument("player"))
    .withArguments(new RecipeArgument("recipe"))
    .executes((sender, args) -> {
        Player target = (Player) args.get("player");
        ComplexRecipe recipe = (ComplexRecipe) args.get("recipe");

        target.discoverRecipe(recipe.getKey());
    })
    .register();
CommandAPICommand("unlockrecipe")
    .withArguments(PlayerArgument("player"))
    .withArguments(RecipeArgument("recipe"))
    .executes(CommandExecutor { _, args ->
        val target = args["player"] as Player
        val recipe = args["recipe"] as ComplexRecipe

        target.discoverRecipe(recipe.key)
    })
    .register()
commandAPICommand("unlockrecipe") {
    playerArgument("player")
    recipeArgument("recipe")
    anyExecutor { _, args ->
        val target = args["player"] as Player
        val recipe = args["recipe"] as ComplexRecipe

        target.discoverRecipe(recipe.key)
    }
}