Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions 5e/Recover Item Uses Macro
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const token = canvas.tokens.controlled[0];
const actor = token.actor;
const item = actor.items.find(i => i.name === "Throwing Cards"); // item name


const config = {
Actor: {
items: {
name: "Throwing Cards" // Item name
},
system: {
activities: {
uses: {
spent: item.system.uses.spent || 0,
max: 52 // item max uses
}
}
}
}
};


const currentSpent = config.Actor.system.activities.uses.spent;

const max = config.Actor.system.activities.uses.max;

// change to skill or ability that makes sense

const perceptionBonus = actor.system.skills.prc.mod;

// update to the roll you want to make and the skill or ability

async function calculateIncreaseAmount(perceptionBonus) {
const roll = new Roll("1d5");
await roll.evaluate(); // Await the roll evaluation
return roll.total + perceptionBonus;
}

// Calculate the increase amount

const increaseAmount = await calculateIncreaseAmount(perceptionBonus);

// update for your items max uses

const currentUses = currentSpent || 0;
const maxUses = parseInt(max) || 52;

// Calculate new uses

const newUses = Math.min(currentUses - increaseAmount, maxUses);

async function updateSpent(newSpent) {
try {
await item.update({ "system.uses.spent": newSpent });
console.log("Item updated successfully.");
} catch (updateError) {
console.error("Error updating item:", updateError);
}
}

// Update the spent value

await updateSpent(newUses);

ChatMessage.create({
content: `${actor.name} searched around and found ${increaseAmount} undamaged ${item.name}.`,
speaker: { alias: actor.name },
});

// Check the updated value

console.log("Updated spent value:", config.Actor.system.activities.use);