withdrawItem
Take items from an open container in Minecraft by specifying the item name and quantity, enabling inventory management through natural language commands.
Instructions
Take items from an open container
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| itemName | Yes | Name of the item to withdraw | |
| amount | No | Amount of items to withdraw |
Implementation Reference
- The asynchronous handler function that implements the core logic of the 'withdrawItem' tool. It checks for connection and open container, finds matching items by name, withdraws the specified amount from container slots using container.withdraw(), and returns success or error responses.if (!botState.isConnected || !botState.bot) { return createNotConnectedResponse() } try { // Check if a container is open if (!botState.currentContainer) { return createSuccessResponse( 'No container is currently open. Use openContainer first.' ) } // Find the item in the container const container = botState.currentContainer const matchingItems = container.slots .filter((item): item is NonNullable<typeof item> => Boolean( item !== null && item.name && item.name.toLowerCase() === itemName.toLowerCase() ) ) .map((item, slotIndex) => ({ item, slotIndex })) if (matchingItems.length === 0) { return createSuccessResponse( `Item "${itemName}" not found in the container.` ) } // Calculate how many items to withdraw let remainingAmount = amount let withdrawnAmount = 0 // Withdraw items from each matching slot until we have enough for (const { item, slotIndex } of matchingItems) { if (remainingAmount <= 0) break const amountFromThisSlot = Math.min(remainingAmount, item.count) // In mineflayer, withdraw takes itemType, metadata, count await container.withdraw(item.type, null, amountFromThisSlot) remainingAmount -= amountFromThisSlot withdrawnAmount += amountFromThisSlot } return createSuccessResponse( `Withdrew ${withdrawnAmount} x ${itemName} from the container.` ) } catch (error) { return createErrorResponse(error) } } )
- Zod schema defining the input parameters for the 'withdrawItem' tool: 'itemName' (string, name of item to withdraw) and 'amount' (optional number, defaults to 1).itemName: z.string().describe('Name of the item to withdraw'), amount: z .number() .optional() .default(1) .describe('Amount of items to withdraw'), }, async ({ itemName, amount }) => {
- src/tools/containerInteraction.ts:94-161 (registration)Registration of the 'withdrawItem' tool via server.tool(), specifying the tool name, description 'Take items from an open container', input schema, and handler function.'withdrawItem', 'Take items from an open container', { itemName: z.string().describe('Name of the item to withdraw'), amount: z .number() .optional() .default(1) .describe('Amount of items to withdraw'), }, async ({ itemName, amount }) => { if (!botState.isConnected || !botState.bot) { return createNotConnectedResponse() } try { // Check if a container is open if (!botState.currentContainer) { return createSuccessResponse( 'No container is currently open. Use openContainer first.' ) } // Find the item in the container const container = botState.currentContainer const matchingItems = container.slots .filter((item): item is NonNullable<typeof item> => Boolean( item !== null && item.name && item.name.toLowerCase() === itemName.toLowerCase() ) ) .map((item, slotIndex) => ({ item, slotIndex })) if (matchingItems.length === 0) { return createSuccessResponse( `Item "${itemName}" not found in the container.` ) } // Calculate how many items to withdraw let remainingAmount = amount let withdrawnAmount = 0 // Withdraw items from each matching slot until we have enough for (const { item, slotIndex } of matchingItems) { if (remainingAmount <= 0) break const amountFromThisSlot = Math.min(remainingAmount, item.count) // In mineflayer, withdraw takes itemType, metadata, count await container.withdraw(item.type, null, amountFromThisSlot) remainingAmount -= amountFromThisSlot withdrawnAmount += amountFromThisSlot } return createSuccessResponse( `Withdrew ${withdrawnAmount} x ${itemName} from the container.` ) } catch (error) { return createErrorResponse(error) } } ) // Tool to deposit items to a container server.tool(