From 32046972b7ca81aa4e08d512b1a6b36949475b60 Mon Sep 17 00:00:00 2001 From: p4535992 Date: Sat, 11 Feb 2023 12:21:05 +0100 Subject: [PATCH] start re-developing --- src/languages/en.json | 1 + src/scripts/lib/configurations.ts | 25 +++++++++++++++++-------- src/scripts/lib/injectConfig.ts | 28 ++++++++++++++-------------- src/scripts/lib/lib.ts | 21 ++++++++++----------- src/scripts/mountHud.ts | 14 +++++++------- src/scripts/mountManager.ts | 4 +++- src/scripts/tokenAttacherHelper.ts | 18 +++++++++--------- 7 files changed, 61 insertions(+), 50 deletions(-) diff --git a/src/languages/en.json b/src/languages/en.json index b610ce7..56c8060 100644 --- a/src/languages/en.json +++ b/src/languages/en.json @@ -104,6 +104,7 @@ "confirmationDialogTitle": "Drop Mount Up!", "confirmationDialogChoiceYes": "Yes", "confirmationDialogChoiceNo": "No", + "isNotAMount": "The token '{mount}' is not checked as mount on the token configuration", "tokenConfig": { "isamount": { "name": "Is a Mount ?", diff --git a/src/scripts/lib/configurations.ts b/src/scripts/lib/configurations.ts index 36dc078..b5c8a58 100644 --- a/src/scripts/lib/configurations.ts +++ b/src/scripts/lib/configurations.ts @@ -2,6 +2,7 @@ import CONSTANTS from "../constants"; import { SettingsForm } from "../settings-form"; import { MountUpFlags } from "../utils"; import { injectConfig } from "./injectConfig"; +import { info, warn } from "./lib"; /** * Handler called when token configuration window is opened. Injects custom form html and deals @@ -13,15 +14,23 @@ import { injectConfig } from "./injectConfig"; * @param {JQuery} html */ export async function renderTokenConfigHandler(app, html, data) { + // if (!game.user?.isGM) { + // info('Only GM can edit the Mount Up Token Configuration'); + // return; + // } + if (app.object.flags["token-attacher"]?.parent) { + warn("Detach token before editing token config.", true); + return; + } const objectOri = app.object; let object = objectOri; // MOD 4535992 let noActorDataFlagsOnToken = false; //@ts-ignore - // if (objectOri instanceof TokenDocument && objectOri.actorData) { - // //@ts-ignore - // object = objectOri.actorData; - // } + if (objectOri instanceof TokenDocument && objectOri.actorData) { + //@ts-ignore + object = objectOri.actorData; + } if (!hasProperty(object, "flags")) { object.flags = {}; noActorDataFlagsOnToken = true; @@ -47,13 +56,13 @@ export async function renderTokenConfigHandler(app, html, data) { type: "checkbox", label: game.i18n.localize(`${CONSTANTS.MODULE_NAME}.tokenConfig.${MountUpFlags.IsAMount}.name`), notes: game.i18n.localize(`${CONSTANTS.MODULE_NAME}.tokenConfig.${MountUpFlags.IsAMount}.hint`), - default: object?.flags?.[CONSTANTS.MODULE_NAME][MountUpFlags.IsAMount] ?? false, + default: object?.flags[CONSTANTS.MODULE_NAME][MountUpFlags.IsAMount] ?? false, }, [MountUpFlags.LockRider]: { type: "checkbox", label: game.i18n.localize(`${CONSTANTS.MODULE_NAME}.tokenConfig.${MountUpFlags.LockRider}.name`), notes: game.i18n.localize(`${CONSTANTS.MODULE_NAME}.tokenConfig.${MountUpFlags.LockRider}.hint`), - default: object?.flags?.[CONSTANTS.MODULE_NAME][MountUpFlags.LockRider] ?? false, + default: object?.flags[CONSTANTS.MODULE_NAME][MountUpFlags.LockRider] ?? false, }, [MountUpFlags.IconHud]: { type: "select", @@ -68,7 +77,7 @@ export async function renderTokenConfigHandler(app, html, data) { 5: "Handshake", }, default: - object?.flags?.[CONSTANTS.MODULE_NAME]?.[MountUpFlags.IconHud] ?? + object?.flags[CONSTANTS.MODULE_NAME]?.[MountUpFlags.IconHud] ?? game.settings.get(CONSTANTS.MODULE_NAME, "icon"), }, }; @@ -97,7 +106,7 @@ export async function renderTokenConfigHandler(app, html, data) { if (tokenConfig.options.sheetConfig) { isAMount = tokenConfig.object.getFlag(CONSTANTS.MODULE_NAME, MountUpFlags.IsAMount) ? "checked" : ""; } else { - isAMount = tokenConfig.token.flags?.[CONSTANTS.MODULE_NAME][MountUpFlags.IsAMount] ? "checked" : ""; + isAMount = tokenConfig.token.actor.flags[CONSTANTS.MODULE_NAME][MountUpFlags.IsAMount] ? "checked" : ""; } let data = { diff --git a/src/scripts/lib/injectConfig.ts b/src/scripts/lib/injectConfig.ts index 29b6529..e47f0b1 100644 --- a/src/scripts/lib/injectConfig.ts +++ b/src/scripts/lib/injectConfig.ts @@ -6,15 +6,15 @@ export var injectConfig = { inject: function injectConfig(app, html, data, objectOri) { this._generateTabStruct(app, html, data, objectOri); const tabSize = data.tab?.width ?? 100; - let object = objectOri || app.object; + // let object = objectOri || app.object; // MOD 4535992 - // objectOri = objectOri || app.object; - // let object = objectOri; - // //@ts-ignore - // if (objectOri instanceof TokenDocument && objectOri.actorData) { - // //@ts-ignore - // object = objectOri.actorData; - // } + objectOri = objectOri || app.object; + let object = objectOri; + //@ts-ignore + if (objectOri instanceof TokenDocument && objectOri.actorData) { + //@ts-ignore + object = objectOri.actorData; + } // END MOD 4535992 const moduleId = data.moduleId; let injectPoint; @@ -37,13 +37,13 @@ export var injectConfig = { const elemData = data[k]; let flag = "flags." + moduleId + "." + (k || ""); // MOD 4535992 - // //@ts-ignore - // if (objectOri instanceof TokenDocument && objectOri.actorData) { - // flag = "actorData.flags." + moduleId + "." + (k || ""); - // } - // const flagValue = object?.flags[moduleId][k] ?? elemData.default ?? getDefaultFlag(k); + //@ts-ignore + if (objectOri instanceof TokenDocument && objectOri.actorData) { + flag = "actorData.flags." + moduleId + "." + (k || ""); + } + const flagValue = object?.flags[moduleId][k] ?? elemData.default ?? getDefaultFlag(k); // END MOD 4535992 - const flagValue = object?.getFlag(moduleId, k) ?? elemData.default ?? getDefaultFlag(k); + // const flagValue = object?.getFlag(moduleId, k) ?? elemData.default ?? getDefaultFlag(k); const notes = v.notes ? `

${v.notes}

` : ""; v.label = v.units ? v.label + ` (${v.units})` : v.label; switch (elemData.type) { diff --git a/src/scripts/lib/lib.ts b/src/scripts/lib/lib.ts index 77b7985..dfb98bd 100644 --- a/src/scripts/lib/lib.ts +++ b/src/scripts/lib/lib.ts @@ -7,7 +7,6 @@ import { MountupEffectDefinitions } from "../mountup-effect-definition"; import { mountUpTA } from "../tokenAttacherHelper"; import { ActiveTokenMountUpData, MountUpFlags, findTokenById } from "../utils"; import { SettingsForm } from "../settings-form"; -import { isEmptyObject } from "jquery"; // ============================= // Module Generic function @@ -613,15 +612,15 @@ export async function renderDialogDropMountUp( `; - const mount: Token = findTokenById(mountTokenId); - if (!mount) { + const mountToken: Token = findTokenById(mountTokenId); + if (!mountToken) { warn(`No mount with reference '${mountTokenId}' is been found`, true); return; } const classIconIndex = //@ts-ignore - mount?.flags?.[CONSTANTS.MODULE_NAME]?.[MountUpFlags.IconHud] ?? + mountToken?.actor.flags[CONSTANTS.MODULE_NAME]?.[MountUpFlags.IconHud] ?? game.settings.get(CONSTANTS.MODULE_NAME, "icon"); const classIcon = SettingsForm.getIconClass(classIconIndex); const d = new Dialog({ @@ -649,10 +648,10 @@ export async function renderDialogDropMountUp( d.render(true); } -export function retrieveFlagFromToken(token: Token, key: string) { - let currentFlag = getProperty(token, `flags.${CONSTANTS.MODULE_NAME}.${key}`); - if (isEmptyObject(currentFlag) && token.actor) { - currentFlag = getProperty(token.actor, `flags.${CONSTANTS.MODULE_NAME}.${key}`); - } - return currentFlag; -} +// export function retrieveFlagFromToken(token: Token, key: string) { +// let currentFlag = getProperty(token, `flags.${CONSTANTS.MODULE_NAME}.${key}`); +// if (isEmptyObject(currentFlag) && token.actor) { +// currentFlag = getProperty(token.actor, `flags.${CONSTANTS.MODULE_NAME}.${key}`); +// } +// return currentFlag; +// } diff --git a/src/scripts/mountHud.ts b/src/scripts/mountHud.ts index e5261e3..c724725 100644 --- a/src/scripts/mountHud.ts +++ b/src/scripts/mountHud.ts @@ -71,7 +71,7 @@ export class MountHud { const classIconIndex = //@ts-ignore - hudToken?.flags?.[CONSTANTS.MODULE_NAME]?.[MountUpFlags.IconHud] ?? + hudToken?.actor?.flags[CONSTANTS.MODULE_NAME]?.[MountUpFlags.IconHud] ?? game.settings.get(CONSTANTS.MODULE_NAME, "icon"); const classIcon = SettingsForm.getIconClass(classIconIndex); @@ -101,19 +101,19 @@ export class MountHud { return; } const mountId = rider.actor?.getFlag(CONSTANTS.MODULE_NAME, MountUpFlags.Mount); - const mount = findTokenById(mountId); - if (!mount) { + const mountToken = findTokenById(mountId); + if (!mountToken) { warn(`No mount with reference '${mountId}' is been found`, true); return; } const classIconIndex = //@ts-ignore - mount?.flags?.[CONSTANTS.MODULE_NAME]?.[MountUpFlags.IconHud] ?? + mountToken?.actor.flags[CONSTANTS.MODULE_NAME]?.[MountUpFlags.IconHud] ?? game.settings.get(CONSTANTS.MODULE_NAME, "icon"); const classIcon = SettingsForm.getIconClass(classIconIndex); - let button = this.buildButton(html, hudToken, `Dismount ${hudToken.name} from ${mount.name}`, classIcon); + let button = this.buildButton(html, hudToken, `Dismount ${hudToken.name} from ${mountToken.name}`, classIcon); button = this.addSlash(button); button.find("i").on("click", async (ev) => { @@ -147,7 +147,7 @@ export class MountHud { if (token && game.user?.isGM) { const classIconIndex = //@ts-ignore - hudToken?.flags?.[CONSTANTS.MODULE_NAME]?.[MountUpFlags.IconHud] ?? + hudToken?.actor?.flags[CONSTANTS.MODULE_NAME]?.[MountUpFlags.IconHud] ?? game.settings.get(CONSTANTS.MODULE_NAME, "icon"); const classIcon = SettingsForm.getIconClass(classIconIndex); @@ -170,7 +170,7 @@ export class MountHud { } const classIconIndex = //@ts-ignore - hudToken?.flags?.[CONSTANTS.MODULE_NAME]?.[MountUpFlags.IconHud] ?? + hudToken?.actor?.flags[CONSTANTS.MODULE_NAME]?.[MountUpFlags.IconHud] ?? game.settings.get(CONSTANTS.MODULE_NAME, "icon"); const classIcon = SettingsForm.getIconClass(classIconIndex); diff --git a/src/scripts/mountManager.ts b/src/scripts/mountManager.ts index 27af769..8e97da8 100644 --- a/src/scripts/mountManager.ts +++ b/src/scripts/mountManager.ts @@ -1,7 +1,7 @@ import { SettingsForm } from "./settings-form"; import { dismountDropAllTA, dismountDropTargetTA, mountUpTA } from "./tokenAttacherHelper"; import { findTokenById, MountUpFlags, getTokenCenter, riderLock, riderX, riderY, socketAction } from "./utils"; -import { error, log, warn } from "./lib/lib"; +import { error, i18nFormat, log, warn } from "./lib/lib"; import CONSTANTS from "./constants"; /** @@ -16,9 +16,11 @@ export class MountManager { static async mountUpHud(hudToken) { const mountToken = canvas.tokens?.controlled.find((t) => t.id === hudToken._id); if (!mountToken) { + warn(`Token mount is not present on the canvas`, true); return; } if (!(String(mountToken.actor?.getFlag(CONSTANTS.MODULE_NAME, MountUpFlags.IsAMount)) === "true")) { + warn(i18nFormat(`${CONSTANTS.MODULE_NAME}.isNotAMount`,{mount: hudToken.name}), true); return; } const tokensToCheck = canvas.tokens?.controlled || []; diff --git a/src/scripts/tokenAttacherHelper.ts b/src/scripts/tokenAttacherHelper.ts index 224f339..adcba06 100644 --- a/src/scripts/tokenAttacherHelper.ts +++ b/src/scripts/tokenAttacherHelper.ts @@ -14,16 +14,16 @@ export const mountUpTA = async function (riderToken: Token, mountToken: Token, n return ui.notifications?.error("Can't mount more then one token!"); } - const mount = targets[0]; + const mountToken = targets[0]; const newMountCoords = { //@ts-ignore - x: mount.x, + x: mountToken.x, //@ts-ignore - y: mount.y, + y: mountToken.y, //@ts-ignore - w: mount.w, + w: mountToken.w, //@ts-ignore - h: mount.h, + h: mountToken.h, }; const newRiderCoords = { //@ts-ignore @@ -92,13 +92,13 @@ export const mountUpTA = async function (riderToken: Token, mountToken: Token, n let message = game.settings.get(CONSTANTS.MODULE_NAME, "mount-message") ? game.settings.get(CONSTANTS.MODULE_NAME, "mount-message") - : `I mount this ${mount?.name}`; + : `I mount this ${mountToken?.name}`; message = message.replace("{rider}", riderToken.name); - message = message.replace("{mount}", mount?.name); + message = message.replace("{mount}", mountToken?.name); const classIconIndex = //@ts-ignore - mount?.flags?.[CONSTANTS.MODULE_NAME]?.[MountUpFlags.IconHud] ?? + mountToken?.actor.flags[CONSTANTS.MODULE_NAME]?.[MountUpFlags.IconHud] ?? game.settings.get(CONSTANTS.MODULE_NAME, "icon"); const classIcon = SettingsForm.getIconClass(classIconIndex); @@ -120,7 +120,7 @@ export const mountUpTA = async function (riderToken: Token, mountToken: Token, n //@ts-ignore ChatMessage.create(chatData); - await window["tokenAttacher"].attachElementToToken(riderToken, mount, true); + await window["tokenAttacher"].attachElementToToken(riderToken, mountToken, true); const isLocked = false; await window["tokenAttacher"].setElementsLockStatus(riderToken, isLocked, true); const canMoveConstrained =