From 2de449cd7e92436717ede4ec0db06e6ffd698c1c Mon Sep 17 00:00:00 2001 From: Megastruktur Date: Wed, 20 Jan 2021 12:45:38 +0300 Subject: [PATCH] Fixes all Tokens to be force actor linked. Adds migration. --- CHANGELOG.txt | 3 ++ module/blades-actor.js | 16 +++++++++++ module/blades.js | 2 +- module/migration.js | 63 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index d77076f..15b2dc5 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,3 +1,6 @@ +v2.15 +- Fixes all Tokens to be force "actor linked" + v2.14 - Fixes Dice-so-Nice integration diff --git a/module/blades-actor.js b/module/blades-actor.js index 8a1df31..0ae5776 100644 --- a/module/blades-actor.js +++ b/module/blades-actor.js @@ -7,6 +7,22 @@ import { BladesHelpers } from "./blades-helpers.js"; */ export class BladesActor extends Actor { + /** @override */ + static async create(data, options={}) { + + data.token = data.token || {}; + + // For Crew and Character set the Token to sync with charsheet. + switch (data.type) { + case 'character': + case 'crew': + data.token.actorLink = true; + break; + } + + + return super.create(data, options); + } /** @override */ getRollData() { diff --git a/module/blades.js b/module/blades.js index 15fc099..1e81438 100644 --- a/module/blades.js +++ b/module/blades.js @@ -269,7 +269,7 @@ Hooks.once("ready", function() { // Determine whether a system migration is required const currentVersion = game.settings.get("bitd", "systemMigrationVersion"); - const NEEDS_MIGRATION_VERSION = 2.0; + const NEEDS_MIGRATION_VERSION = 2.15; let needMigration = (currentVersion < NEEDS_MIGRATION_VERSION) || (currentVersion === null); diff --git a/module/migration.js b/module/migration.js index 3fc4852..a474e34 100644 --- a/module/migration.js +++ b/module/migration.js @@ -18,6 +18,33 @@ export const migrateWorld = async function() { console.error(err); } } + + // Migrate Token Link for Character and Crew + if (a.data.type === 'character' || a.data.type === 'crew') { + try { + const updateData = _migrateTokenLink(a.data); + if ( !isObjectEmpty(updateData) ) { + console.log(`Migrating Token Link for ${a.name}`); + await a.update(updateData, {enforceTypes: false}); + } + } catch(err) { + console.error(err); + } + } + + } + + // Migrate Actor Link + for ( let s of game.scenes.entities ) { + try { + const updateData = _migrateSceneData(s.data); + if ( !isObjectEmpty(updateData) ) { + console.log(`Migrating Scene entity ${s.name}`); + await s.update(updateData, {enforceTypes: false}); + } + } catch(err) { + console.error(err); + } } // Set the migration as complete @@ -25,6 +52,26 @@ export const migrateWorld = async function() { ui.notifications.info(`BITD System Migration to version ${game.system.data.version} completed!`, {permanent: true}); }; + +/* -------------------------------------------- */ + +/** + * Migrate a single Scene entity to incorporate changes to the data model of it's actor data overrides + * Return an Object of updateData to be applied + * @param {Object} scene The Scene data to Update + * @return {Object} The updateData to apply + */ +export const _migrateSceneData = function(scene) { + const tokens = duplicate(scene.tokens); + return { + tokens: tokens.map(t => { + t.actorLink = true; + t.actorData = {}; + return t; + }) + }; +}; + /* -------------------------------------------- */ /* -------------------------------------------- */ @@ -93,4 +140,20 @@ function _migrateActor(actor) { // } } +/* -------------------------------------------- */ + + +/** + * Make Token be an Actor link. + * @param {Actor} actor The actor to Update + * @return {Object} The updateData to apply + */ +function _migrateTokenLink(actor) { + + let updateData = {} + updateData['token.actorLink'] = true; + + return updateData; +} + /* -------------------------------------------- */ \ No newline at end of file