dh-ikonis/scripts/main.js

169 lines
5.5 KiB
JavaScript

import { patchDHWeapon, patchIkonisLogic, patchDhCharacter, loadIkonisFeatures, DEFAULT_AUGMENTS } from './ikonis-data.js';
import { patchIkonisSheet } from './ikonis-sheet.js';
import { IkonisAugmentConfig } from './ikonis-config.js';
const MODULE_ID = 'dh-ikonis';
Hooks.once('init', () => {
console.log(`${MODULE_ID} | Initializing Ikonis Module`);
// Global Augments List
game.settings.register(MODULE_ID, "augmentsList", {
scope: "world",
config: false,
type: Array,
default: DEFAULT_AUGMENTS
});
// Default Bonded Feature
game.settings.register(MODULE_ID, "defaultBondedUuid", {
scope: "world",
config: false,
type: String,
default: ""
});
// Slot Scaling for Tiers 1, 2, 3, 4
[1, 2, 3, 4].forEach(tier => {
game.settings.register(MODULE_ID, `slotsTier${tier}`, {
name: `Slots at Tier ${tier}`,
hint: `How many augment slots a weapon gets at Tier ${tier}.`,
scope: "world",
config: true,
type: Number,
default: tier >= 2 ? 3 : 2
});
});
// Configuration Menu
game.settings.registerMenu(MODULE_ID, "augmentsMenu", {
name: "Manage Ikonis Augments",
label: "Open Augment Manager",
hint: "Add, remove, or edit the global list of Ikonis augments.",
icon: "fa-solid fa-microchip",
type: class extends foundry.applications.api.ApplicationV2 {
render() {
IkonisAugmentConfig.open();
return this;
}
},
restricted: true
});
});
Hooks.on('setup', () => {
patchDHWeapon();
patchIkonisLogic();
patchIkonisSheet();
});
// Watch for Tier/Ikonis changes and force a refresh
Hooks.on('updateItem', (item, changes, options, userId) => {
const isTierUpdate = foundry.utils.hasProperty(changes, "system.tier");
const isIkonisUpdate = foundry.utils.hasProperty(changes, "flags.dh-ikonis");
const isEquipUpdate = foundry.utils.hasProperty(changes, "system.equipped");
if (isTierUpdate || isIkonisUpdate || isEquipUpdate) {
console.log(`DH-Ikonis | Update detected for ${item.name}.`);
// Refresh sheets
Object.values(item.apps || {}).forEach(app => {
if (app.render) app.render(true);
});
// If it's on an actor, we need to force the actor to recalculate its effects
if (item.actor) {
console.log(`DH-Ikonis | Resetting actor data for ${item.actor.name}...`);
item.actor.prepareData();
// Force sheet re-render for the actor
item.actor.sheet?.render(false);
}
}
});
Hooks.once('ready', async () => {
console.log("DH-Ikonis | Ready hook triggered.");
// Sync features from compendium if needed
if (game.user.isGM) {
await syncIkonisFeatures();
await overrideCurrency();
}
// Load features into memory for sync getters
await loadIkonisFeatures();
// Patch Character Data Model
const DhCharacter = game.system.api?.models?.actors?.DhCharacter || CONFIG.Actor.dataModels?.character;
if (DhCharacter) {
patchDhCharacter(DhCharacter);
} else {
console.warn("DH-Ikonis | Could not find DhCharacter class for patching visual features.");
}
// 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);
});
});
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...");
// We must work with a plain object for settings updates
const newHomebrew = homebrew.toObject();
newHomebrew.currency = {
title: "Quantum",
coins: {
enabled: true,
label: "Quantum",
icon: "fa-solid fa-atom"
},
handfuls: {
enabled: false,
label: "Handfuls",
icon: "fa-solid fa-coins"
},
bags: {
enabled: false,
label: "Bags",
icon: "fa-solid fa-sack"
},
chests: {
enabled: false,
label: "Chests",
icon: "fa-solid fa-treasure-chest"
}
};
await game.settings.set('daggerheart', 'homebrew', newHomebrew);
}
}
async function syncIkonisFeatures() {
const pack = game.packs.get("dh-ikonis.ikonis-features");
if (!pack) return;
const index = await pack.getIndex();
const currentAugs = game.settings.get(MODULE_ID, 'augmentsList') || [];
let updates = false;
const newAugs = currentAugs.map(aug => {
if (!aug.featureUuid) {
const match = index.find(i => i.name === aug.name);
if (match) {
aug.featureUuid = match.uuid;
updates = true;
}
}
return aug;
});
if (updates) await game.settings.set(MODULE_ID, 'augmentsList', newAugs);
let bondedUuid = game.settings.get(MODULE_ID, 'defaultBondedUuid');
if (!bondedUuid) {
const bondMatch = index.find(i => i.name === "Ikonis Bond");
if (bondMatch) await game.settings.set(MODULE_ID, 'defaultBondedUuid', bondMatch.uuid);
}
}