API OpenAI DEPRECATED - Drowbe/coffee-pub-blacksmith GitHub Wiki
Blacksmith Toolbar API Documentation
Audience: Developers integrating with Blacksmith and leveraging the exposed API.
Overview
The Blacksmith Toolbar API allows external modules to register custom tools with the Blacksmith toolbar system. This provides a unified interface for adding functionality to both the Blacksmith Utilities toolbar and FoundryVTT's token control toolbar.
Getting Started
1. Access the API
// Get the Blacksmith module API
const blacksmith = game.modules.get('coffee-pub-blacksmith')?.api;
// Check if API is available
if (blacksmith?.registerToolbarTool) {
// API is ready to use
} else {
// Wait for API to load
Hooks.once('ready', () => {
// API should be available now
});
}
2. Register a Tool
// Register a custom tool (minimal example - many properties have defaults)
const success = blacksmith.registerToolbarTool('my-custom-tool', {
icon: "fa-solid fa-dice-d20", // Optional: defaults to "fa-solid fa-square-question"
name: "my-custom-tool", // Optional: defaults to toolId if not provided
title: "My Custom Tool", // Optional: defaults to name or toolId
button: true, // Optional: defaults to true (v13 requirement)
visible: true, // Optional: defaults to true
zone: "rolls", // Optional: defaults to "general"
order: 5, // Optional: defaults to 999
moduleId: "my-module", // Optional: defaults to "blacksmith-core"
gmOnly: false, // Optional: defaults to false
leaderOnly: false, // Optional: defaults to false
onCoffeePub: true, // Optional: defaults to true (can be boolean or function)
onFoundry: false, // Optional: defaults to false (can be boolean or function)
onClick: () => {
// Your tool logic here
console.log("My custom tool clicked!");
}
});
if (success) {
console.log("Tool registered successfully!");
} else {
console.log("Failed to register tool");
}
Note for FoundryVTT v13: The button property defaults to true and is required for toolbar display. The name property defaults to toolId if not provided, which is especially important for external modules to ensure proper tool identification.
API Reference
Tool Registration
registerToolbarTool(toolId, toolData)
Registers a new tool with the Blacksmith toolbar system.
Parameters:
toolId(string): Unique identifier for the tooltoolData(Object): Tool configuration object
Returns: boolean - Success status
Tool Data Properties:
icon(string, optional): FontAwesome icon class (e.g., "fa-solid fa-dice-d20"). Defaults to"fa-solid fa-square-question"if not provided.name(string, optional): Tool name (used for data-tool attribute). Defaults totoolIdif not provided - this is especially important for external modules to ensure proper tool identification in FoundryVTT v13.title(string, optional): Tooltip text displayed on hover. Defaults totoolData.nameortoolIdif not provided.onClick(Function, required): Function to execute when tool is clickedbutton(boolean, optional): Whether to show as button. Defaults totrue- FoundryVTT v13 requiresbutton: truefor toolbar display. Must betruefor tools to appear in toolbars.toggle(boolean, optional): Automatically set tofalsefor all tools. Not user-configurable.visible(boolean|Function, optional): Whether tool is visible. Defaults totrueif not provided. Can be a function that returns a boolean for dynamic visibility.zone(string, optional): Zone for organization (default: "general")order(number, optional): Order within zone (default: 999)moduleId(string, optional): Module identifier (default: "blacksmith-core")gmOnly(boolean, optional): Whether tool is GM-only (default: false)leaderOnly(boolean, optional): Whether tool is leader-only (default: false)onCoffeePub(boolean|Function, optional): Whether to show in Blacksmith toolbar. Can be a boolean or a function that returns a boolean for dynamic visibility. Defaults totruefor backward compatibility.onFoundry(boolean|Function, optional): Whether to show in FoundryVTT native toolbar. Can be a boolean or a function that returns a boolean for dynamic visibility. Defaults tofalse.
unregisterToolbarTool(toolId)
Removes a tool from the Blacksmith toolbar system.
Parameters:
toolId(string): Unique identifier for the tool
Returns: boolean - Success status
Tool Querying
getRegisteredTools()
Gets all registered tools.
Returns: Map - Map of all registered tools (toolId -> toolData)
getToolsByModule(moduleId)
Gets all tools registered by a specific module.
Parameters:
moduleId(string): Module identifier
Returns: Array - Array of tools registered by the module
isToolRegistered(toolId)
Checks if a tool is registered.
Parameters:
toolId(string): Unique identifier for the tool
Returns: boolean - Whether the tool is registered
Settings Management
getToolbarSettings()
Gets current toolbar settings.
Returns: Object - Current toolbar settings
{
displayStyle: string // "none", "dividers", or "labels"
}
setToolbarSettings(settings)
Updates toolbar settings.
Parameters:
settings(Object): Settings objectdisplayStyle(string, optional): "none", "dividers", or "labels"
Toolbar Targeting
The Blacksmith toolbar system supports two different toolbar locations:
Toolbar Types:
onCoffeePub: true: Shows in the Blacksmith Utilities toolbar (custom toolbar)onFoundry: true: Shows in FoundryVTT's native token control toolbar
Usage Examples:
// Tool only in Blacksmith toolbar (default behavior)
blacksmith.registerToolbarTool('blacksmith-only', {
// ... other properties
onCoffeePub: true,
onFoundry: false
});
// Tool only in FoundryVTT toolbar
blacksmith.registerToolbarTool('foundry-only', {
// ... other properties
onCoffeePub: false,
onFoundry: true
});
// Tool in both toolbars
blacksmith.registerToolbarTool('both-toolbars', {
// ... other properties
onCoffeePub: true,
onFoundry: true
});
Default Behavior:
onCoffeePub:true(backward compatibility) - Can be a boolean or functiononFoundry:false(current behavior) - Can be a boolean or function
Dynamic Visibility:
Both onCoffeePub and onFoundry support function values for dynamic visibility:
// Tool that only appears in CoffeePub toolbar when a setting is enabled
blacksmith.registerToolbarTool('conditional-tool', {
// ... other properties
onCoffeePub: () => {
return game.settings.get('my-module', 'enableToolbarTool');
},
onFoundry: false
});
Tool Zones
The toolbar system organizes tools into predefined zones:
Zone Order (appearance order):
general- Default zone for tools that don't fit other categoriesrolls- Roll-related tools, dice rollers, random generatorscommunication- Chat, messaging, and communication toolsutilities- General utility tools, helpers, calculatorsleadertools- Leadership and management toolsgmtools- GM-specific tools, admin functions, management tools
Zone Guidelines:
general: Default zone for tools that don't fit other categoriesrolls: Dice rollers, random generators, roll-related toolscommunication: Chat tools, messaging, communication featuresutilities: General helpers, calculators, utility functionsleadertools: Party leadership tools, vote managementgmtools: GM-only tools, admin functions, management tools
Visibility System
The toolbar uses a three-tier visibility system:
User Types:
- GM: Sees all tools (including GM tools and leader tools)
- LEADER: Sees all tools except GM tools, plus leader tools
- PLAYER: Sees all tools except GM tools and leader tools
Tool Properties:
gmOnly: true: Only visible to Game MastersleaderOnly: true: Visible to party leaders and GMs- Default: Visible to all users
Ordering Guidelines
Tools within each zone are ordered by their order property:
- Lower numbers appear first within each zone
- Recommended ranges:
1-10: Core/primary tools11-50: Secondary tools51-100: Utility tools101+: Optional/advanced tools
Example Usage
Minimal Tool Registration (v13)
// Minimal example - many properties have defaults
// Only onClick is truly required
blacksmith.registerToolbarTool('my-utility', {
icon: "fa-solid fa-calculator", // Optional: has default
onClick: () => {
// Your utility logic
ui.notifications.info("Utility tool activated!");
}
// name defaults to 'my-utility' (toolId)
// title defaults to name or toolId
// button defaults to true (v13 requirement)
// visible defaults to true
// zone defaults to "general"
// order defaults to 999
// onCoffeePub defaults to true
// onFoundry defaults to false
});
Basic Tool Registration
// Register a simple utility tool (Blacksmith toolbar only)
blacksmith.registerToolbarTool('my-utility', {
icon: "fa-solid fa-calculator",
name: "my-utility", // Optional: defaults to toolId
title: "My Utility Tool", // Optional: defaults to name or toolId
button: true, // Optional: defaults to true (v13 requirement)
visible: true, // Optional: defaults to true
zone: "utilities",
order: 10,
moduleId: "my-module",
onCoffeePub: true, // Optional: defaults to true
onFoundry: false, // Optional: defaults to false
onClick: () => {
// Your utility logic
ui.notifications.info("Utility tool activated!");
}
});
GM-Only Tool
// Register a GM-only admin tool
blacksmith.registerToolbarTool('my-admin-tool', {
icon: "fa-solid fa-cog",
name: "my-admin-tool",
title: "Admin Tool",
button: true, // REQUIRED for toolbar display
visible: true, // REQUIRED for visibility
zone: "gmtools",
order: 5,
moduleId: "my-module",
gmOnly: true,
onClick: () => {
// Admin functionality
console.log("Admin tool used by GM");
}
});
Leader Tool
// Register a leader-only tool
blacksmith.registerToolbarTool('my-leader-tool', {
icon: "fa-solid fa-crown",
name: "my-leader-tool",
title: "Leader Tool",
button: true, // REQUIRED for toolbar display
visible: true, // REQUIRED for visibility
zone: "leadertools",
order: 1,
moduleId: "my-module",
leaderOnly: true,
onClick: () => {
// Leader functionality
ui.notifications.info("Leader tool activated!");
}
});
FoundryVTT Toolbar Integration
// Register a tool for FoundryVTT's token toolbar
blacksmith.registerToolbarTool('my-token-tool', {
icon: "fa-solid fa-dice-d20",
name: "my-token-tool",
title: "Token Tool",
button: true,
visible: true,
zone: "rolls",
order: 5,
moduleId: "my-module",
onCoffeePub: false, // Don't show in Blacksmith toolbar
onFoundry: true, // Show in FoundryVTT toolbar
gmOnly: true, // Only GMs can use this tool
onClick: () => {
// Tool logic for token operations
ui.notifications.info("Token tool activated!");
}
});
Tool in Both Toolbars
// Register a tool that appears in both toolbars
blacksmith.registerToolbarTool('my-universal-tool', {
icon: "fa-solid fa-star",
name: "my-universal-tool",
title: "Universal Tool",
button: true,
visible: true,
zone: "utilities",
order: 10,
moduleId: "my-module",
onCoffeePub: true, // Show in Blacksmith toolbar
onFoundry: true, // Show in FoundryVTT toolbar
onClick: () => {
// Tool logic
ui.notifications.info("Universal tool activated!");
}
});
Dynamic Visibility
// Register a tool with dynamic visibility
blacksmith.registerToolbarTool('my-conditional-tool', {
icon: "fa-solid fa-eye",
name: "my-conditional-tool",
title: "Conditional Tool",
button: true,
visible: true,
zone: "utilities",
order: 20,
moduleId: "my-module",
onCoffeePub: true,
onFoundry: false,
visible: () => {
// Only show if certain conditions are met
return game.user.isGM || game.settings.get('my-module', 'enableFeature');
},
onClick: () => {
// Tool logic
}
});
Module Cleanup
// Unregister all tools when module is disabled
Hooks.once('disableModule', (moduleId) => {
if (moduleId === 'my-module') {
const tools = blacksmith.getToolsByModule('my-module');
tools.forEach(tool => {
blacksmith.unregisterToolbarTool(tool.name);
});
}
});
Error Handling
The API includes robust error handling:
- Invalid tool data: Returns
falseand logs error - Duplicate tool IDs: Returns
false(tools must be unique) - Missing required properties: Returns
falseand logs error - API not available: Check for API availability before use
Best Practices
- Unique Tool IDs: Use descriptive, unique tool identifiers
- Proper Zone Selection: Choose the most appropriate zone for your tool
- Consistent Ordering: Use consistent order values within your module
- Module Cleanup: Unregister tools when your module is disabled
- Error Handling: Always check return values and handle errors gracefully
- API Availability: Check if the API is available before using it
Troubleshooting
Tool Not Appearing
- Check if tool is registered:
blacksmith.isToolRegistered('tool-id') - Verify visibility settings (gmOnly, leaderOnly, visible function)
- For FoundryVTT v13: Ensure
nameproperty is set (defaults totoolIdif not provided) - For FoundryVTT v13:
buttonproperty defaults totruebut must betruefor toolbar display - Verify
onCoffeePuboronFoundryfunctions returntrueif using function values - Check console for error messages
- Ensure API is loaded:
blacksmith?.registerToolbarTool - If players don't see newly registered tools, make sure a controls refresh runs on their client (e.g., after registration or when settings change)
API Not Available
- Use correct API path:
blacksmith.registerToolbarTool()notblacksmith.api.registerToolbarTool() - Wait for
readyhook, notblacksmithUpdatedhook - Check if module is active:
game.modules.get('coffee-pub-blacksmith')?.active
Tool in Wrong Zone
- Verify
zoneproperty is set correctly - Check zone spelling (must match predefined zones exactly)
- Default zone is "general" if not specified
Tool Not Clickable
- Verify
onClickfunction is provided and valid - Check for JavaScript errors in onClick function
- Ensure tool is not disabled by visibility logic
Support
For issues or questions about the Blacksmith Toolbar API:
- Check this documentation first
- Review console logs for error messages
- Test with a simple tool registration
- Contact the Blacksmith development team
Version History
-
v13.0.8: FoundryVTT v13 compatibility updates
- Property defaults: Added automatic defaults for
name(defaults totoolId),title,icon, andbutton(defaults totrue) - Dynamic visibility:
onCoffeePubandonFoundrynow support function values for dynamic visibility evaluation - v13 requirements:
button: trueis now required for toolbar display (defaults totrueif not provided) - External module support: Improved defaults ensure external modules work correctly even if properties are missing
- Updated API documentation to reflect v13 requirements and defaults
- Property defaults: Added automatic defaults for
-
v12.1.3: Enhanced toolbar targeting
- Added
onCoffeePubandonFoundryparameters for toolbar targeting - Support for FoundryVTT native toolbar integration
- Backward compatibility maintained
- Updated API documentation
- Added
-
v12.1.2: Initial toolbar API release
- Basic tool registration/unregistration
- Zone-based organization
- Three-tier visibility system
- Settings management