97 lines
2.6 KiB
JavaScript
97 lines
2.6 KiB
JavaScript
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");
|
||
|
|
}
|
||
|
|
}
|