Fixes and enhancements, from issue list
This commit is contained in:
@@ -104,24 +104,45 @@ Hooks.once("init", function () {
|
||||
Hooks.once("ready", async function () {
|
||||
console.info("Oath Hammer | System Ready")
|
||||
|
||||
// Migration: remove orphaned items with removed types (lineage → actor field, ability → trait)
|
||||
const removedTypes = new Set(["lineage", "ability"])
|
||||
// Migration: remove orphaned items with removed types (lineage → actor field, ability → trait, regiment → actor type)
|
||||
const removedTypes = new Set(["lineage", "ability", "regiment"])
|
||||
for (const actor of game.actors) {
|
||||
const invalidItems = actor._source.items?.filter(i => removedTypes.has(i.type)) ?? []
|
||||
if (invalidItems.length) {
|
||||
console.info(`Oath Hammer | Migrating ${actor.name}: removing ${invalidItems.length} obsolete item(s)`)
|
||||
await actor.deleteEmbeddedDocuments("Item", invalidItems.map(i => i._id))
|
||||
const toDelete = []
|
||||
|
||||
// Catch items that failed validation and landed in invalidDocumentIds
|
||||
for (const id of (actor.items.invalidDocumentIds ?? [])) {
|
||||
const raw = actor.items.getInvalid(id)
|
||||
if (raw && removedTypes.has(raw._source?.type)) toDelete.push(id)
|
||||
}
|
||||
|
||||
// Also catch any that slipped through in _source (belt-and-suspenders)
|
||||
for (const src of (actor._source.items ?? [])) {
|
||||
if (removedTypes.has(src.type) && !toDelete.includes(src._id)) toDelete.push(src._id)
|
||||
}
|
||||
|
||||
if (toDelete.length) {
|
||||
console.info(`Oath Hammer | Migrating ${actor.name}: removing ${toDelete.length} obsolete item(s)`)
|
||||
await actor.deleteEmbeddedDocuments("Item", toDelete)
|
||||
}
|
||||
}
|
||||
|
||||
// Purge invalid world items of removed types
|
||||
for (const id of game.items.invalidDocumentIds) {
|
||||
const item = game.items.getInvalid(id)
|
||||
if (item && removedTypes.has(item._source.type)) {
|
||||
if (item && removedTypes.has(item._source?.type)) {
|
||||
console.info(`Oath Hammer | Deleting world item: ${item._source.name} (${item._source.type})`)
|
||||
await item.delete()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Auto-link regiment (and army) actor tokens so they can be added to garrisons/armies
|
||||
Hooks.on("preCreateActor", (actor, _data, _options, _userId) => {
|
||||
if (actor.type === "regiment" || actor.type === "army") {
|
||||
actor.updateSource({ "prototypeToken.actorLink": true })
|
||||
}
|
||||
})
|
||||
|
||||
// Handle "Roll Damage" button in weapon attack chat cards
|
||||
Hooks.on("renderChatMessageHTML", (message, html) => {
|
||||
const btn = html.querySelector("[data-action=\"rollWeaponDamage\"]")
|
||||
|
||||
Reference in New Issue
Block a user