diff --git a/module.json b/module.json index d1f5b33..3c2a487 100755 --- a/module.json +++ b/module.json @@ -27,7 +27,8 @@ "scripts/update-compendium.js", "scripts/convert-prices-to-credits.js", "scripts/add-coins-dialog.js", - "scripts/party-sheet-prices.js" + "scripts/party-sheet-prices.js", + "scripts/item-sheet-prices.js" ], "styles": ["styles/sf2e-silver-to-credits.css"] } diff --git a/scripts/item-sheet-prices.js b/scripts/item-sheet-prices.js new file mode 100644 index 0000000..d539f84 --- /dev/null +++ b/scripts/item-sheet-prices.js @@ -0,0 +1,96 @@ +Hooks.on("renderItemSheet", (app, html, data) => { + convertItemSheetPricesToCredits(html); +}); + +Hooks.on("updateItem", (item, data) => { + const sheets = item.apps; + sheets.forEach((sheet) => { + if (sheet.element) { + convertItemSheetPricesToCredits(sheet.element); + } + }); +}); + +// MutationObserver to catch item sheet updates +const itemSheetObserver = new MutationObserver((mutations) => { + mutations.forEach((mutation) => { + if (mutation.type === "attributes" && mutation.attributeName === "value") { + const $element = $(mutation.target); + + // Check if this is a price input field + if ( + $element.attr("data-property") === "system.price.value" || + $element.attr("id")?.includes("price") + ) { + convertPriceInput($element); + } + } + }); +}); + +itemSheetObserver.observe(document.body, { + subtree: true, + attributes: true, + attributeFilter: ["value"], +}); + +function convertItemSheetPricesToCredits(html) { + console.log("Converting item sheet prices to credits"); + + // Find all price input fields + const priceInputs = html.find('input[data-property="system.price.value"]'); + + priceInputs.each(function () { + convertPriceInput($(this)); + }); + + // Also check for any input with "price" in the id + const priceInputsById = html.find('input[id*="price"]'); + priceInputsById.each(function () { + const $input = $(this); + + // Make sure it's actually a price field + if ( + $input.attr("data-property")?.includes("price") || + $input.attr("id")?.includes("price") + ) { + convertPriceInput($input); + } + }); +} + +function convertPriceInput($input) { + let value = $input.val(); + const originalValue = value; + + if (!value) return; + + // Convert pp to cr (1 pp = 100 cr) + value = value.replace(/(\d+(?:\.\d+)?)\s*pp/gi, (match, amount) => { + const creditAmount = parseFloat(amount) * 100; + return `${creditAmount} cr`; + }); + + // Convert gp to cr (1 gp = 10 cr) + value = value.replace(/(\d+(?:\.\d+)?)\s*gp/gi, (match, amount) => { + const creditAmount = parseFloat(amount) * 10; + return `${creditAmount} cr`; + }); + + // Convert cp to cr (1 cp = 0.1 cr) + value = value.replace(/(\d+(?:\.\d+)?)\s*cp/gi, (match, amount) => { + const creditAmount = parseFloat(amount) / 10; + return `${creditAmount} cr`; + }); + + // Replace sp with cr (no conversion needed, 1 sp = 1 cr) + value = value.replace(/(\d+(?:\.\d+)?)\s*sp/gi, "$1 cr"); + + // Only update if value changed + if (value !== originalValue) { + console.log("Converting item price:", originalValue, "to:", value); + $input.val(value); + // Trigger change event to update the sheet + $input.trigger("change"); + } +}