From dfd94129cd0478641c81189a739cb61a47a313fa Mon Sep 17 00:00:00 2001 From: TheLastScrub <67403321+TheLastScrub@users.noreply.github.com> Date: Sun, 13 Jun 2021 15:21:44 -0700 Subject: [PATCH] added function to auto-add unarmed attack item --- module/actor/actor.js | 45 +++++++++++++++++++++++++++++++++++++++++++ module/deltagreen.js | 31 +++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/module/actor/actor.js b/module/actor/actor.js index 8e42a50..554651a 100644 --- a/module/actor/actor.js +++ b/module/actor/actor.js @@ -163,4 +163,49 @@ export class DeltaGreenActor extends Actor { return super.create(data, options); } + async AddUnarmedAttackItemIfMissing(){ + try{ + + let alreadyAdded = false; + + for(let item of this.data.items){ + + let flag = await item.getFlag('deltagreen', 'SystemName'); + + if(flag === 'unarmed-attack'){ + alreadyAdded = true; + } + + } + + if(alreadyAdded === true){ return; }; + + let handToHandPack = await game.packs.get('deltagreen.hand-to-hand-weapons'); + let itemIndex = await handToHandPack.getIndex(); + let toAdd = []; // createEmbeddedDocument expects an array + + for(let idx of itemIndex){ + let _temp = await handToHandPack.getDocument(idx._id); + + if(_temp.name === 'Unarmed Attack'){ + toAdd.push(_temp.data); + } + } + + let newItems = await this.createEmbeddedDocuments("Item", toAdd); + + for(let item of newItems){ + await item.setFlag('deltagreen','AutoAdded', true); + + if(item.name === 'Unarmed Attack'){ + await item.setFlag('deltagreen','SystemName', 'unarmed-attack'); + } + } + } + catch(ex){ + console.log('Error adding unarmed strike item to Actor.') + console.log(ex); + } + } + } \ No newline at end of file diff --git a/module/deltagreen.js b/module/deltagreen.js index ccbab95..70924e2 100644 --- a/module/deltagreen.js +++ b/module/deltagreen.js @@ -217,6 +217,37 @@ Hooks.on("ready", ()=> { }); +// Note - this event is fired on ALL connected clients... +Hooks.on('createActor', async function(actor, options, userId){ + try{ + + // user this to trap on if this hook is firing for the same user that triggered the create + // can put logic specific to a particular user session below + if (userId != game.user.id) { return; }; + + + if(actor.data.type === 'agent'){ + // update the default type skill of Art - Painting's labels to try to be localized + // since I really backed myself into a corner on this with my implementation of it... + let artLabel = game.i18n.translations.DG?.TypeSkills?.Art ?? "Art"; + let paintingLabel = game.i18n.translations.DG?.TypeSkills?.Subskills?.Painting ?? "Painting"; + + let updatedData = duplicate(actor.data.data); + updatedData.typedSkills.tskill_01.group = artLabel; + updatedData.typedSkills.tskill_01.label = paintingLabel; + + actor.update({"data": updatedData}); + + // throw on an unarmed strike item for convenience + actor.AddUnarmedAttackItemIfMissing(); + } + + } + catch(ex){ + console.log(ex); + } +}); + /* -------------------------------------------- */ /* Hotbar Macros */ /* -------------------------------------------- */