149 lines
6.3 KiB
JavaScript
149 lines
6.3 KiB
JavaScript
import { ASCII, SYSTEM, SYSTEM_ID, localizeSystemConfig } from "./module/config/system.mjs"
|
|
import * as models from "./module/models/_module.mjs"
|
|
import * as documents from "./module/documents/_module.mjs"
|
|
import * as applications from "./module/applications/_module.mjs"
|
|
import MGNERoll from "./module/documents/roll.mjs"
|
|
|
|
Hooks.once("init", () => {
|
|
console.info(ASCII)
|
|
console.info(`${SYSTEM_ID} | Initializing system`)
|
|
|
|
game.mgne = {
|
|
SYSTEM,
|
|
applications,
|
|
documents,
|
|
models,
|
|
}
|
|
|
|
CONFIG.Actor.documentClass = documents.MGNEActor
|
|
CONFIG.Actor.dataModels = {
|
|
character: models.MGNECharacter,
|
|
creature: models.MGNECreature,
|
|
companion: models.MGNECompanion,
|
|
}
|
|
|
|
CONFIG.Combat.documentClass = documents.MGNECombat
|
|
CONFIG.Item.documentClass = documents.MGNEItem
|
|
CONFIG.Item.dataModels = {
|
|
weapon: models.MGNEWeapon,
|
|
armor: models.MGNEArmor,
|
|
shield: models.MGNEShield,
|
|
equipment: models.MGNEEquipment,
|
|
"resonance-core": models.MGNEResonanceCore,
|
|
artifact: models.MGNEArtifact,
|
|
feature: models.MGNEFeature,
|
|
}
|
|
|
|
foundry.applications.sheets.ActorSheetV2 && foundry.documents.collections.Actors.unregisterSheet(
|
|
"core",
|
|
foundry.applications.sheets.ActorSheetV2,
|
|
{ types: Object.keys(CONFIG.Actor.dataModels) }
|
|
)
|
|
foundry.appv1?.sheets?.ActorSheet && foundry.documents.collections.Actors.unregisterSheet("core", foundry.appv1.sheets.ActorSheet)
|
|
foundry.documents.collections.Actors.registerSheet(SYSTEM_ID, applications.MGNECharacterSheet, { types: ["character"], makeDefault: true, label: SYSTEM.actorTypes.character.label })
|
|
foundry.documents.collections.Actors.registerSheet(SYSTEM_ID, applications.MGNECreatureSheet, { types: ["creature"], makeDefault: true, label: SYSTEM.actorTypes.creature.label })
|
|
foundry.documents.collections.Actors.registerSheet(SYSTEM_ID, applications.MGNECompanionSheet, { types: ["companion"], makeDefault: true, label: SYSTEM.actorTypes.companion.label })
|
|
|
|
foundry.applications.sheets.ItemSheetV2 && foundry.documents.collections.Items.unregisterSheet(
|
|
"core",
|
|
foundry.applications.sheets.ItemSheetV2,
|
|
{ types: Object.keys(CONFIG.Item.dataModels) }
|
|
)
|
|
foundry.appv1?.sheets?.ItemSheet && foundry.documents.collections.Items.unregisterSheet("core", foundry.appv1.sheets.ItemSheet)
|
|
foundry.documents.collections.Items.registerSheet(SYSTEM_ID, applications.MGNEWeaponSheet, { types: ["weapon"], makeDefault: true, label: SYSTEM.itemTypes.weapon.label })
|
|
foundry.documents.collections.Items.registerSheet(SYSTEM_ID, applications.MGNEArmorSheet, { types: ["armor"], makeDefault: true, label: SYSTEM.itemTypes.armor.label })
|
|
foundry.documents.collections.Items.registerSheet(SYSTEM_ID, applications.MGNEShieldSheet, { types: ["shield"], makeDefault: true, label: SYSTEM.itemTypes.shield.label })
|
|
foundry.documents.collections.Items.registerSheet(SYSTEM_ID, applications.MGNEEquipmentSheet, { types: ["equipment"], makeDefault: true, label: SYSTEM.itemTypes.equipment.label })
|
|
foundry.documents.collections.Items.registerSheet(SYSTEM_ID, applications.MGNEResonanceCoreSheet, { types: ["resonance-core"], makeDefault: true, label: SYSTEM.itemTypes["resonance-core"].label })
|
|
foundry.documents.collections.Items.registerSheet(SYSTEM_ID, applications.MGNEArtifactSheet, { types: ["artifact"], makeDefault: true, label: SYSTEM.itemTypes.artifact.label })
|
|
foundry.documents.collections.Items.registerSheet(SYSTEM_ID, applications.MGNEFeatureSheet, { types: ["feature"], makeDefault: true, label: SYSTEM.itemTypes.feature.label })
|
|
|
|
Handlebars.registerHelper("isEqual", (left, right) => left === right)
|
|
})
|
|
|
|
Hooks.once("setup", () => {
|
|
localizeSystemConfig()
|
|
})
|
|
|
|
Hooks.once("ready", () => {
|
|
console.info(`${SYSTEM_ID} | Ready`)
|
|
})
|
|
|
|
Hooks.on("renderCombatTracker", (_app, element) => {
|
|
const root = element instanceof HTMLElement ? element : element?.[0]
|
|
if (!root) return
|
|
|
|
const footer = root.querySelector(".combat-controls")
|
|
if (!footer || footer.querySelector(".mgne-flee-control")) return
|
|
|
|
const button = document.createElement("button")
|
|
button.type = "button"
|
|
button.className = "combat-control-lg mgne-flee-control"
|
|
button.dataset.action = "mgneFlee"
|
|
button.innerHTML = `<i class="fa-solid fa-person-running" inert></i><span>${game.i18n.localize("MGNE.Combat.Flee")}</span>`
|
|
button.disabled = !game.combat
|
|
button.addEventListener("click", event => {
|
|
event.preventDefault()
|
|
game.combat?.rollFlee()
|
|
})
|
|
|
|
footer.append(button)
|
|
})
|
|
|
|
Hooks.on("renderChatMessageHTML", (message, element) => {
|
|
const root = element instanceof HTMLElement ? element : element?.[0]
|
|
if (!root) return
|
|
|
|
root.querySelectorAll(".mgne-roll-damage-btn").forEach(btn => {
|
|
btn.addEventListener("click", async () => {
|
|
const actorId = btn.dataset.actorId
|
|
const itemId = btn.dataset.itemId
|
|
const actor = game.actors.get(actorId)
|
|
const item = actor?.items.get(itemId)
|
|
if (!actor || !item) {
|
|
ui.notifications.warn(game.i18n.localize("MGNE.Notification.ActorOrItemNotFound"))
|
|
return
|
|
}
|
|
await MGNERoll.rollDamage({ actor, item })
|
|
})
|
|
})
|
|
|
|
root.querySelectorAll(".mgne-apply-damage-select").forEach(select => {
|
|
const isAllowed = game.user.isGM || message.isAuthor
|
|
if (!isAllowed) {
|
|
select.closest(".chat-apply-actions")?.remove()
|
|
return
|
|
}
|
|
|
|
const card = select.closest(".mgne-chat-card")
|
|
const damageTotal = parseInt(card?.dataset.damageTotal ?? "0", 10) || 0
|
|
const damageCritical = card?.dataset.damageCritical === "true"
|
|
|
|
const tokens = canvas.scene?.tokens.contents ?? []
|
|
for (const token of tokens) {
|
|
if (!token.actor) continue
|
|
const opt = document.createElement("option")
|
|
opt.value = token.id
|
|
opt.textContent = token.name
|
|
select.appendChild(opt)
|
|
}
|
|
|
|
if (tokens.length === 0) {
|
|
const opt = document.createElement("option")
|
|
opt.disabled = true
|
|
opt.textContent = game.i18n.localize("MGNE.Roll.NoTargetSelected")
|
|
select.appendChild(opt)
|
|
}
|
|
|
|
select.addEventListener("change", async event => {
|
|
const tokenId = event.target.value
|
|
if (!tokenId) return
|
|
const token = canvas.scene?.tokens.get(tokenId)
|
|
const targetActor = token?.actor
|
|
if (!targetActor) return
|
|
select.value = ""
|
|
await targetActor.applyDamage(damageTotal, { critical: damageCritical, chat: true })
|
|
})
|
|
})
|
|
})
|