From 7186461a55ac0b9c312e8964a8fd3a568ffe4e83 Mon Sep 17 00:00:00 2001 From: Zhell Date: Wed, 24 Jan 2024 16:52:17 +0100 Subject: [PATCH] Update loot creator --- README.md | 6 +- dnd5e_utils/loot_creator.js | 113 ++++++++++++++++++------------------ 2 files changed, 60 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index 6e5811f..901e67e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -Interested in following along with development of any of my modules (or need macro help)? Join the [Discord server](https://discord.gg/QAG8eWABGT). +Interested in following along with development of any of my modules (or need macro help)? Join the [Discord server](https://discord.gg/QAG8eWABGT). # Macro Repo -Some macros I have made. +Some macros I have made. Modules required are listed in each macro. -Modules required are listed in each macro. +This repo is not licensed under MIT or similar. You may use any script found here for personal use. Please ask for permission first if you wish to use a script in a module or system made available for public use. diff --git a/dnd5e_utils/loot_creator.js b/dnd5e_utils/loot_creator.js index c103263..0e4c557 100644 --- a/dnd5e_utils/loot_creator.js +++ b/dnd5e_utils/loot_creator.js @@ -1,89 +1,90 @@ -// Credit to @LukasPrism for the warpgate targeting. +/** + * Macro to create loot, either in an item pile or on an actor. + * Can additionally add to the sidebar on creation. + * Required modules: item-piles, warpgate. + * Credit to @LukasPrism for the warpgate targeting. + */ -// macro to create loot, either in an item pile or on an actor. -// can additionally add to the sidebar on creation. +/* --------------------------- */ -// required modules: item-piles, warpgate - -// get a random image from the treasurs folder. const {files} = await FilePicker.browse("public", "icons/commodities/treasure"); const img = files[Math.floor(Math.random() * files.length)]; -// set up input fields. -const nameField = ``; -const priceField = ``; -const weightField = ``; -const quantityField = ``; -const descriptionField = ``; -const sidebarCheck = ``; - -// set up html. const content = ` -

+

+ +


-
${nameField}
+
+ +
-
${priceField}
+
+ +
-
${weightField}
+
+ +
-
${quantityField}
+
+ +
-
${descriptionField}
+
+ +
-
${sidebarCheck}
+
+ +
`; -// create and render dialog. -new Dialog({ - content, +Dialog.prompt({ title: "Create Loot", - buttons: { - go: { - icon: "", - label: "Create Loot!", - callback: async (html) => { - // construct item data. - const data = new FormDataExtended(html[0].querySelector("form")).object; - const itemData = foundry.utils.mergeObject({img, type: "loot", "system.rarity": "common"}, data); - // pick the target or location. - const crosshairs = await warpgate.crosshairs.show({ - label: "Select recipient or location", - drawIcon: false, - size: 1 - }); - const tokenDocs = !crosshairs.cancelled ? warpgate.crosshairs.collect(crosshairs) : []; - // pop it in the sidebar - if (data.sidebar) await Item.createDocuments([itemData]); + rejectClose: false, + label: "Create Loot!", + content: content, + callback: async ([html]) => { + const itemData = foundry.utils.mergeObject({ + img, type: "loot", "system.rarity": "common" + }, new FormDataExtended(html.querySelector("FORM")).object); + const sidebar = itemData.sidebar; + delete itemData.sidebar; - // if no token was targeted, add the item to a new item pile, initially hidden. - if (!tokenDocs.length) { - const updates = { - embedded: {Item: {[data.name]: itemData}}, - token: {hidden: true} - } - return warpgate.spawnAt(crosshairs, "Default Item Pile", updates); - } - // if a token was targeted, add to their inventory instead. - await tokenDocs[0].actor.createEmbeddedDocuments("Item", [itemData]); - // if single item pile it's probably named as that item, so fix that - const isItemPile = tokenDocs[0].getFlag("item-piles", "data.enabled"); - if (isItemPile) return tokenDocs[0].update({name: "Pile of Loot"}); + const crosshairs = await warpgate.crosshairs.show({ + label: "Select recipient or location", drawIcon: false, size: 1 + }); + const tokenDocs = crosshairs.cancelled ? [] : warpgate.crosshairs.collect(crosshairs); + if (sidebar) await Item.createDocuments([itemData]); + + // if no token was targeted, add the item to a new item pile, initially hidden. + if (!tokenDocs.length) { + const updates = { + embedded: {Item: {[itemData.name]: itemData}}, + token: {hidden: true} } + return warpgate.spawnAt(crosshairs, "Default Item Pile", updates); } + + const [token] = tokenDocs; + const parent = token.actor; + await Item.create(itemData, {parent: parent}); + const isItemPile = token.getFlag("item-piles", "data.enabled"); + if (isItemPile) return token.update({name: "Pile of Loot"}); } -}).render(true); +});