Skip to content

Commit

Permalink
add optional feature tool
Browse files Browse the repository at this point in the history
  • Loading branch information
AngryBeaver committed Oct 24, 2022
1 parent b682b9b commit 902d30e
Show file tree
Hide file tree
Showing 18 changed files with 359 additions and 15 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ Default is "Create New Item" obviously you need to adapt if you have a different
### Configure subtype Recipe
![img.png](pictures/configure.png)

#### tool: (0.5.x)
you may add a tool the crafting process requires.
#### cost:
you may add costs to the crafting process
#### Ingredients:


You may add Items via drag and drop as Ingredients.
#### skill:
you may add a skill that is required in the crafting process.
Expand Down Expand Up @@ -67,9 +67,16 @@ You do not need to import those just drag and drop them into your recipes or use

You need to import them and then grant permission to the users you want to have access to it.
The recipecompendium will only show the recipes the user has access to.
### Settings
![img.png](pictures/toolconfig.png)

- You can enable or disable tools for recipes feature 0.5.x default it is disabled.
- You can configure the tool list your recipes can select from.
(however if you do you might risk incompatibility to others, if the list is missing some default dnd5e tools tell me so)

## latest features:
### 0.5.x feature tool
you now can use tools, if you do not have the tool you dont get a check you simple fail and your ingredients won't vanish.
### 0.4.x feature compendiums,
you now can use items directly from compendium,
module comes with 4 compendiums ingredients,rolltables,potions and recipes
Expand Down
4 changes: 4 additions & 0 deletions css/crafting.css
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
cursor: pointer;
}

.beavers-crafting .recipe .action.result {
padding:5px;
}

.beavers-crafting .drop-area {
/* Center the content */
align-items: center;
Expand Down
15 changes: 15 additions & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"ingredients": "Ingredients",
"quantity": "Quantity",
"skills": "Skill",
"tools": "Tool",
"dc": "DC",
"results": "Results",
"dropItem": "drop item",
Expand Down Expand Up @@ -44,7 +45,21 @@
"displayIngredients": {
"name": "Display ingredient sheet",
"hint": "Show ItemSheet when clicking on ingredient items of a recipe."
},
"useTool": {
"name": "Recipes use tools",
"hint": "A Recipe can require to have a tool present on actor."
},
"toolButton": {
"name": "Configrue tools available",
"hint": "Configure list of tools that are available in tool dropdown when creating recipes",
"label": "configure"
}
},
"tool-config": {
"title": "Tool Config",
"dropItem": "drop tool",
"warn": "Be warned if you manipulate items from this list it will reduce your compatibility to imported/exported recipes. E.g. if you delete tools imported recipes that require this tool might break."
}
}
}
2 changes: 1 addition & 1 deletion module.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"title": "Beaver's Crafting System",
"description": "A Crafting Module for DnD",
"id": "beavers-crafting",
"version": "0.4.1",
"version": "0.5.0",
"authors": [
{
"name": "angryBeaver",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "beavers-crafting",
"title": "Beaver's Crafting",
"version": "0.4.1",
"version": "0.5.0",
"description": "Crafting",
"devDir": "C:\\Users\\User\\AppData\\Local\\FoundryVTT\\Data\\modules",
"main": "src/main.js",
Expand Down
Binary file modified pictures/configure.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/toolconfig.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 14 additions & 2 deletions src/Crafting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export class Crafting {
}

async craft(): Promise<Result> {
const result = await this.checkSkill();
const result = await this.checkTool();
await this.checkSkill(result);
await this.evaluateAnyOf();
RecipeCompendium.validateRecipeToItemList(Object.values(this.recipe.ingredients), this.actor.items, result);
this.checkCurrency(result);
Expand All @@ -55,6 +56,12 @@ export class Crafting {
return result;
}

async checkTool(result?: Result): Promise<Result> {
if(!result) result = new DefaultResult();
if (result.hasErrors) return result;
return await RecipeCompendium.validateTool(this.recipe,this.actor.items,result);
}

async evaluateAnyOf(){
const toDelete:string[] = [];
const toAdd:Component[] = [];
Expand Down Expand Up @@ -137,7 +144,12 @@ export class Crafting {

async _sendToChat(result: Result) {
let content = await renderTemplate(`modules/${Settings.NAMESPACE}/templates/crafting-chat.hbs`,
{recipe: this.recipe, result: result, roll: this.roll})
{
recipe: this.recipe,
result: result,
roll: this.roll,
displayTool: Settings.get(Settings.USE_TOOL)
})
content = TextEditor.enrichHTML(content);
await ChatMessage.create({
content: content,
Expand Down
22 changes: 19 additions & 3 deletions src/Recipe.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import {Settings} from "./Settings.js";
import {DefaultCurrency} from "./Exchange.js";
import {sanitizeUuid} from "./helpers/Utility.js";
import {getItem, sanitizeUuid} from "./helpers/Utility.js";
import {getToolConfig} from "./apps/ToolConfig.js";

export class Recipe implements RecipeStoreData{
export class Recipe {
id:string;
name:string;
img:string;
ingredients:Map<string,Component>;
results:Map<string,Component>;
skill?:Skill;
currency?:Currency;
tool?:string;
_trash:Trash;

static fromItem(item):Recipe{
Expand All @@ -27,6 +29,7 @@ export class Recipe implements RecipeStoreData{
this.results = data.results || {}
this.skill = data.skill;
this.currency = data.currency;
this.tool = data.tool;
this._trash = {
ingredients:{},
results:{},
Expand All @@ -38,7 +41,11 @@ export class Recipe implements RecipeStoreData{
ingredients: this.serializeIngredients(),
skill: this.skill,
results: this.serializeResults(),
currency: this.currency
currency: this.currency,
tool: this.tool,
}
if(!this.tool){
serialized["-=tool"] = null;
}
if(!this.skill){
serialized["-=skill"] = null;
Expand Down Expand Up @@ -92,6 +99,14 @@ export class Recipe implements RecipeStoreData{
removeCurrency(){
delete this.currency;
}
async addTool(){
const config = await getToolConfig()
this.tool = config[0].uuid;
}
removeTool(){
delete this.tool;
}

}
interface Trash {
ingredients:{};
Expand Down Expand Up @@ -137,4 +152,5 @@ interface RecipeStoreData {
results:Map<string,Component>;
skill?:Skill;
currency?:Currency;
tool?:string;
}
63 changes: 61 additions & 2 deletions src/Settings.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
import {ToolConfig} from "./apps/ToolConfig.js";

export class Settings {

static NAMESPACE = "beavers-crafting";
static CREATE_ITEM_TITLE = "createItemTitle";
static DISPLAY_RESULTS = "displayResults";
static DISPLAY_INGREDIENTS = "displayIngredients";
static USE_TOOL = "useTool";
static TOOL_CONFIG_BUTTON= "toolConfigButton";
static TOOL_CONFIG = "toolConfig;"
static RECIPE_SUBTYPE = "Recipe";
static ANYOF_SUBTYPE = "AnyOf";

static init() {
game.settings.register(this.NAMESPACE, this.USE_TOOL, {
name: game.i18n.localize('beaversCrafting.settings.useTool.name'),
hint: game.i18n.localize('beaversCrafting.settings.useTool.hint'),
scope: "world",
config: true,
default: false,
type: Boolean,
});
game.settings.register(this.NAMESPACE, this.CREATE_ITEM_TITLE, {
name: game.i18n.localize('beaversCrafting.settings.createItemTitle.name'),
hint: game.i18n.localize('beaversCrafting.settings.createItemTitle.hint'),
Expand All @@ -32,12 +45,58 @@ export class Settings {
default: true,
type: Boolean,
});
}
game.settings.register(this.NAMESPACE, this.TOOL_CONFIG,{
name: "ToolConfig",
scope: "world",
config: false,
default: defaultToolConfig,
type: Object

});
game.settings.registerMenu(this.NAMESPACE, this.TOOL_CONFIG_BUTTON, {
name: game.i18n.localize('beaversCrafting.settings.toolButton.name'),
label: game.i18n.localize("beaversCrafting.settings.toolButton.label"),
hint: game.i18n.localize('beaversCrafting.settings.toolButton.hint'),
scope: "world",
type: ToolConfig,
restricted: true
});
}

static get(key){
return game.settings.get(this.NAMESPACE, key);
};

static set(key,value){
game.settings.set(this.NAMESPACE,key,value);
}

}

}
const defaultToolConfig = [
"Compendium.dnd5e.items.8NS6MSOdXtUqD7Ib",
"Compendium.dnd5e.items.rTbVrNcwApnuTz5E",
"Compendium.dnd5e.items.fC0lFK8P4RuhpfaU",
"Compendium.dnd5e.items.YfBwELTgPFHmQdHh",
"Compendium.dnd5e.items.hM84pZnpCqKfi8XH",
"Compendium.dnd5e.items.PUMfwyVUbtyxgYbD",
"Compendium.dnd5e.items.skUih6tBvcBbORzA",
"Compendium.dnd5e.items.YHCmjsiXxZ9UdUhU",
"Compendium.dnd5e.items.hJS8yEVkqgJjwfWa",
"Compendium.dnd5e.items.woWZ1sO5IUVGzo58",
"Compendium.dnd5e.items.KndVe2insuctjIaj",
"Compendium.dnd5e.items.0d08g1i5WXnNrCNA",
"Compendium.dnd5e.items.ap9prThUB2y9lDyj",
"Compendium.dnd5e.items.xKErqkLo4ASYr5EP",
"Compendium.dnd5e.items.SztwZhbhZeCqyAes",
"Compendium.dnd5e.items.Y9S75go1hLMXUD48",
"Compendium.dnd5e.items.jhjo20QoiD5exf09",
"Compendium.dnd5e.items.ccm5xlWhx74d6lsK",
"Compendium.dnd5e.items.ugzwHl8vYaPu2GNd",
"Compendium.dnd5e.items.i89okN7GFTWHsvPy",
"Compendium.dnd5e.items.IBhDAr7WkhWPYLVn",
"Compendium.dnd5e.items.cG3m4YlHfbQlLEOx",
"Compendium.dnd5e.items.il2GNi8C0DvGLL9P",
"Compendium.dnd5e.items.V13fjV5oSmvbRdgP",
"Compendium.dnd5e.items.6rocoBx5jdzG1QQH",
];
7 changes: 6 additions & 1 deletion src/apps/CraftingApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {FilterType, RecipeCompendium} from "./RecipeCompendium.js";
import {Crafting} from "../Crafting.js";
import {getDataFrom, getItem} from "../helpers/Utility.js";
import {Settings} from "../Settings.js";
import {getToolConfig} from "./ToolConfig.js";

export class CraftingApp extends Application {
data: {
Expand Down Expand Up @@ -63,6 +64,8 @@ export class CraftingApp extends Application {
return null;
}
data.result = RecipeCompendium.validateRecipeToItemList(Object.values(data.recipe.ingredients), this.data.actor.items);
const crafting = await Crafting.from(this.data.actor.id, this.data.recipe.id);
data.result = await crafting.checkTool(data.result);
data.content = await renderTemplate('modules/beavers-crafting/templates/recipe-sheet.hbs',
{
recipe: data.recipe,
Expand All @@ -71,7 +74,9 @@ export class CraftingApp extends Application {
editable: false,
result: data.result,
displayResults:Settings.get(Settings.DISPLAY_RESULTS),
displayIngredients:Settings.get(Settings.DISPLAY_RESULTS)
displayIngredients:Settings.get(Settings.DISPLAY_RESULTS),
tools: await getToolConfig(),
displayTool: Settings.get(Settings.USE_TOOL)
});
return data;
}
Expand Down
21 changes: 20 additions & 1 deletion src/apps/RecipeCompendium.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//the firstdraft implementation will be kept simple stupid and not performant at all.
import {Recipe} from "../Recipe.js";
import {DefaultComponent, Recipe} from "../Recipe.js";
import {Settings} from "../Settings.js";
import {AnyOf} from "./AnyOfSheet.js";
import {getItem, sanitizeUuid} from "../helpers/Utility.js";
Expand Down Expand Up @@ -55,6 +55,7 @@ export class RecipeCompendium {
if (await this.isAnyAnyOfInList(listOfAnyOfIngredients, actor.items)) { //isAvailable or usable ! when any item matches anyOf has the given quantity
const listOfIngredientsWithoutAnyOf = Object.values(recipe.ingredients).filter(component => component.type !== Settings.ANYOF_SUBTYPE);
const result = RecipeCompendium.validateRecipeToItemList(listOfIngredientsWithoutAnyOf, actor.items);
await RecipeCompendium.validateTool(recipe,actor.items,result);
if ((filter == FilterType.usable && !result.hasErrors)
|| (filter == FilterType.available && result.isAvailable)) {
returnList.push(recipe);
Expand All @@ -80,6 +81,24 @@ export class RecipeCompendium {
return true;
}

static async validateTool(recipe,listOfItems,result ?: Result): Promise<Result>{
if (!result) result = new DefaultResult();
if( recipe.tool && Settings.get(Settings.USE_TOOL)) {
const item = await getItem(recipe.tool);
const component = new DefaultComponent(item, item.uuid, "Item");
result.tool = {
component: component,
isAvailable: false,
difference: 0,
};
result.tool.isAvailable = listOfItems.filter((i) => RecipeCompendium.isSame(i, component)).length > 0;
if(!result.tool.isAvailable){
result.hasErrors = true;
}
}
return result;
}

static validateRecipeToItemList(listOfIngredients: Component[], listOfItems, result ?: Result): Result {
if (!result) result = new DefaultResult();
result.isAvailable = listOfIngredients.length === 0;
Expand Down
20 changes: 19 additions & 1 deletion src/apps/RecipeSheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {getCurrencies, getSkills} from "../systems/dnd5e.js"
import {RecipeCompendium} from "./RecipeCompendium.js";
import {getDataFrom, getItem} from "../helpers/Utility.js";
import {isAnyOf} from "./AnyOfSheet.js";
import {getToolConfig} from "./ToolConfig.js";

const recipeSheets: { [key: string]: RecipeSheet } = {};

Expand Down Expand Up @@ -67,7 +68,16 @@ export class RecipeSheet {

async render(){
let template = await renderTemplate('modules/beavers-crafting/templates/recipe-sheet.hbs',
{recipe: this.recipe,currencies: getCurrencies(),skills: getSkills(),editable:this.editable,displayResults:Settings.get(Settings.DISPLAY_RESULTS),displayIngredients:Settings.get(Settings.DISPLAY_RESULTS)});
{
recipe: this.recipe,
currencies: getCurrencies(),
skills: getSkills(),
editable:this.editable,
displayResults:Settings.get(Settings.DISPLAY_RESULTS),
displayIngredients:Settings.get(Settings.DISPLAY_RESULTS),
tools: await getToolConfig(),
displayTool: Settings.get(Settings.USE_TOOL)
});
this.recipeElement.find('.recipe').remove();
this.recipeElement.append(template);
this.handleEvents();
Expand All @@ -90,6 +100,14 @@ export class RecipeSheet {
this.recipe.addSkill();
this.update();
});
this.recipeElement.find('.tools .item-add').click(e=>{
this.recipe.addTool()
.then(()=>this.update());
});
this.recipeElement.find('.tools .item-delete').click(e=>{
this.recipe.removeTool();
this.update();
});
this.recipeElement.find('.currencies .item-delete').click(e=>{
this.recipe.removeCurrency();
this.update();
Expand Down
Loading

0 comments on commit 902d30e

Please sign in to comment.