CraftItemEvent?

Hey guys :slightly_smiling:

I need an event, that fires everytime, when a player crafts a new item / block.
It should fire, no matter if the player crafts in his own inventory, or somewhere else (crafting table).

I have to get the result (Itemstack?) of the crafting process, then I have to check, if itā€™s an item or a block.
I know, that I have to work with itemstacks, but how can i get the BlockState, if the result is a block?

When I use following code, I only can get the DefaultStateā€¦

if (stack.getItem().getBlock().isPresent()) {

     BlockState blockState = stack.getItem().getBlock().get().getDefaultState();

}

Thanks :slightly_smiling:

Thereā€™s no one single blockstate for a given item - which is why you can only directly get the default state. The blockstate which is actually used when placing down an item can vary, depending on the environment you place it down in.

For example, take redstone dust. When placed down, the BlockState used depends on the surrounding blocks. If another piece of redstone dust is adjacent to it on the north side, then the NORTH property will have its value set to SIDE. Had the redstone been placed in a different environment, the BlockState would have been different.

Without knowing anything else, the default BlockState is what youā€™d most likely want to use. You can of course obtain a new BlockState by using BlockState#withTrait, or similar methods.

1 Like

thanks :slightly_smiling: do you also know, how I can solve the other problem i have?

I need an event, that fires everytime, when a player crafts a new item / block.
It should fire, no matter if the player crafts in his own inventory, or somewhere else (crafting table).

I have to get the result (Itemstack?) of the crafting process, then I have to check, if itā€™s an item or a block.

Is there no solution?

At the moment, there isnā€™t one. However, @kashike is working on some improvements to the recipe API:

An event might be added there.

For now you might be able to listen to ClickInventoryEvent and filter out the event slots so that itā€™s an instance of CraftingOutput in the CraftingInventory (Not all is likely to be implemented yet, but the API is there)

1 Like