So i'm pretty new to modding, but i want to add a feature to an item, that gives the player another item into the inventory whenever he uses it on a specific block.
I already have a mecanism that detects the block and stuff but the player.inventory.addItemStackToInventory(new ItemStack(Items.ITEM)); function is not working. Is it because of 1.18?
Or am I using it wrong?
This is the code I use, partially copied from the internet XD
@Override
public InteractionResultHolder<ItemStack> use(Level world, Player player, InteractionHand hand) {
if (!world.isClientSide()) {
System.out.println(player.getName().getString() + " used Item lul.");
boolean isFluid = false;
HitResult block = player.pick(20.0D, 0.0F, isFluid);
if(block.getType() == HitResult.Type.BLOCK)
{
BlockPos blockpos = ((BlockHitResult)block).getBlockPos();
BlockState blockstate = player.level.getBlockState(blockpos);
System.out.println("Looking at: "+blockstate.getBlock()+"\nIs Fluid: "+isFluid+"\nPosition= "+ blockpos.getX() + ", " + blockpos.getY() + ", " + blockpos.getZ());
if (blockstate.getBlock() == Blocks.OBSIDIAN)
{
player.inventory.addItemStackToInventory(new ItemStack(Items.BOWL));
}
}
}
return super.use(world, player, hand);
}