added change to item prices.

This commit is contained in:
grimsace
2026-03-09 13:11:32 -05:00
parent 5629103a7d
commit 007ab89f22
2 changed files with 98 additions and 1 deletions
+2 -1
View File
@@ -27,7 +27,8 @@
"scripts/update-compendium.js", "scripts/update-compendium.js",
"scripts/convert-prices-to-credits.js", "scripts/convert-prices-to-credits.js",
"scripts/add-coins-dialog.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"] "styles": ["styles/sf2e-silver-to-credits.css"]
} }
+96
View File
@@ -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");
}
}