improve feature resolution, robustness of currency settings, and initialization logic

This commit is contained in:
CPTN Cosmo 2026-04-26 17:59:02 +02:00
parent 8975a13f99
commit e12ac35855
3 changed files with 72 additions and 18 deletions

View file

@ -43,6 +43,15 @@ export async function loadIkonisFeatures() {
console.log(`DH-Ikonis | Cache size: ${_featureCache.size}`);
}
/**
* Robust feature fetching. Tries cache first, then async fromUuid.
*/
export async function getAttachedFeature(uuid) {
if (!uuid) return null;
if (_featureCache.has(uuid)) return _featureCache.get(uuid);
return await fromUuid(uuid);
}
export function getSlotCount(item) {
const flags = item.getFlag('dh-ikonis') || {};
if (typeof flags.slotOverride === "number") return flags.slotOverride;
@ -121,15 +130,27 @@ export function patchDhCharacter(DhCharacter) {
if (!this.parent) return lists;
for (const item of this.parent.items) {
if (item.type !== 'weapon' || !item.system.equipped) continue;
const weapons = this.parent.items.filter(i => i.type === 'weapon');
if (weapons.length > 0) {
console.log(`DH-Ikonis | Checking ${weapons.length} weapons on ${this.parent.name} for augments...`);
}
for (const item of weapons) {
if (!item.system.equipped) continue;
const installedIds = item.getFlag('dh-ikonis', 'installedAugments') || [];
const bondedUuid = item.getFlag('dh-ikonis', 'bondedFeatureUuid');
if (bondedUuid) {
const feature = _featureCache.get(bondedUuid) || fromUuidSync(bondedUuid);
if (feature) ikonisFeatures.push(feature);
if (feature) {
ikonisFeatures.push(feature);
console.log(`DH-Ikonis | Resolved bonded feature: ${feature.name}`);
}
}
if (installedIds.length > 0) {
console.log(`DH-Ikonis | Found ${installedIds.length} installed augments on ${item.name}`);
}
const allAugs = getAugments();
@ -138,7 +159,10 @@ export function patchDhCharacter(DhCharacter) {
if (!aug?.featureUuid) continue;
const feature = _featureCache.get(aug.featureUuid) || fromUuidSync(aug.featureUuid);
if (feature) ikonisFeatures.push(feature);
if (feature) {
ikonisFeatures.push(feature);
console.log(`DH-Ikonis | Resolved augment feature: ${feature.name}`);
}
}
}

View file

@ -49,12 +49,24 @@ Hooks.once('init', () => {
},
restricted: true
});
// Try to patch character model early if available
const DhCharacter = CONFIG.Actor.dataModels?.character;
if (DhCharacter) {
patchDhCharacter(DhCharacter);
}
});
Hooks.on('setup', () => {
patchDHWeapon();
patchIkonisLogic();
patchIkonisSheet();
// Ensure character model is patched if missed in init
const DhCharacter = CONFIG.Actor.dataModels?.character;
if (DhCharacter) {
patchDhCharacter(DhCharacter);
}
});
// Watch for Tier/Ikonis changes and force a refresh
@ -93,7 +105,7 @@ Hooks.once('ready', async () => {
// Load features into memory for sync getters
await loadIkonisFeatures();
// Patch Character Data Model
// Final check for Character Data Model patch
const DhCharacter = game.system.api?.models?.actors?.DhCharacter || CONFIG.Actor.dataModels?.character;
if (DhCharacter) {
patchDhCharacter(DhCharacter);
@ -103,18 +115,35 @@ Hooks.once('ready', async () => {
// Force re-render of open character sheets to show newly patched features
Object.values(ui.windows).forEach(w => {
if (w.document?.type === 'character') w.render(true);
if (w.document?.type === 'character') {
console.log(`DH-Ikonis | Forcing refresh of character sheet: ${w.document.name}`);
w.render(true);
}
});
if (game.user.isGM) {
ui.notifications.info("DH-Ikonis | Ikonis Module initialized and features synchronized.");
}
});
async function overrideCurrency() {
const homebrew = game.settings.get('daggerheart', 'homebrew');
// Only override if not already set to Quantum to avoid spamming updates
if (homebrew.currency?.title !== "Quantum") {
console.log("DH-Ikonis | Overriding currency settings to Quantum...");
// Robust key check for Daggerheart Homebrew settings
let key = 'Homebrew';
if (!game.settings.settings.has('daggerheart.Homebrew')) {
if (game.settings.settings.has('daggerheart.homebrew')) key = 'homebrew';
else return; // Setting not found
}
const homebrew = game.settings.get('daggerheart', key);
if (!homebrew) return;
// Check if we already have Quantum to avoid spamming updates
const currentTitle = homebrew.currency?.title;
if (currentTitle !== "Quantum") {
console.log(`DH-Ikonis | Overriding currency settings to Quantum (using key: ${key})...`);
// We must work with a plain object for settings updates
const newHomebrew = homebrew.toObject();
// Handle both DataModel and plain object
const newHomebrew = (typeof homebrew.toObject === 'function') ? homebrew.toObject() : foundry.utils.deepClone(homebrew);
newHomebrew.currency = {
title: "Quantum",
@ -140,7 +169,8 @@ async function overrideCurrency() {
}
};
await game.settings.set('daggerheart', 'homebrew', newHomebrew);
await game.settings.set('daggerheart', key, newHomebrew);
ui.notifications.info("DH-Ikonis | Currency system updated to Quantum Credits.");
}
}