Crafting and recipes
Register recipes from onEnable() through PluginContext::recipes(). Bedriox
owns matching, inventory consumption, output placement, rollback, and client
synchronization.
Shaped and shapeless recipes
A shaped recipe declares a one-to-three-cell-wide grid in row-major order and
supplies immutable output ItemStack values. A shapeless recipe declares the
same ingredients without fixed slots.
use Bedriox\Api\Crafting\RecipeIngredient;
use Bedriox\Api\Crafting\ShapedRecipe;
use Bedriox\Api\Crafting\ShapelessRecipe;
use Bedriox\Api\Inventory\ItemStack;
public function onEnable(): void
{
$grassFromDirt = new ShapelessRecipe(
id: 'myplugin:grass_from_dirt',
ingredients: [RecipeIngredient::exact('minecraft:dirt')],
outputs: [new ItemStack('minecraft:grass_block', 1)],
);
$this->context()->recipes()->register($grassFromDirt);
$diamond = RecipeIngredient::exact('minecraft:diamond');
$stick = RecipeIngredient::exact('minecraft:stick');
$diamondSword = new ShapedRecipe(
id: 'myplugin:diamond_sword',
width: 1,
height: 3,
ingredients: [$diamond, $diamond, $stick],
outputs: [new ItemStack('minecraft:diamond_sword', 1)],
allowMirror: false,
);
$this->context()->recipes()->register($diamondSword);
}
$grassFromDirt is the complete shapeless recipe object passed to the
registrar. Its ingredients may appear anywhere in the crafting grid.
$diamondSword is the complete shaped recipe object. Because its grid is one
cell wide and three cells tall, ingredients lists the top diamond, bottom
diamond, and stick in that exact order. $diamond and $stick are reusable
ingredient descriptions; each ItemStack describes an output and its count.
Use null for an intentionally empty shaped-grid cell. For example, a two by
two diagonal layout uses [$diamond, null, null, $stick]. Ingredient counts
must be between 1 and 64, shaped dimensions between 1 and 3, and a recipe may
produce one to four output stacks.
Inspect or cancel a craft
The event parameter is supplied by Bedriox. Its recipe property is the
matched ShapedRecipe or ShapelessRecipe; craftCount is the number of
times the client requested that recipe in this transaction.
use Bedriox\Api\Event\EventHandler;
use Bedriox\Api\Event\Player\PlayerCraftItemEvent;
#[EventHandler]
public function limitGrassRecipe(PlayerCraftItemEvent $event): void
{
if ($event->recipe->identifier() !== 'myplugin:grass_from_dirt') {
return;
}
if ($event->craftCount > 16) {
$event->cancel();
$event->player->sendMessage('Craft at most 16 grass blocks at once.');
}
}
Recipe identifiers must belong to your plugin namespace. Listen to
PlayerCraftItemEvent to inspect or cancel an accepted craft before commit and
PlayerCraftedItemEvent to observe the committed result. Do not remove inputs
or grant outputs manually from the event handler.
