From 9d72b31d5fb02e223caab8542f2ed7c824ffbc37 Mon Sep 17 00:00:00 2001 From: Byroks Date: Sat, 20 Jan 2024 12:37:05 +0100 Subject: [PATCH] Add Items on Character Sheet Changes: + add button to add all different kind of items directly onto the character sheet + fix #1 by correctly updating the item --- source/module/M5Base.ts | 12 +++ source/module/actors/M5Character.ts | 14 ++- source/module/sheets/M5CharacterSheet.ts | 107 +++++++++++++++++++++-- templates/sheets/character/combat.hbs | 2 +- templates/sheets/character/effects.hbs | 3 +- templates/sheets/character/gear.hbs | 13 +-- templates/sheets/character/spells.hbs | 2 +- 7 files changed, 137 insertions(+), 16 deletions(-) diff --git a/source/module/M5Base.ts b/source/module/M5Base.ts index 1ab5161..72e3358 100644 --- a/source/module/M5Base.ts +++ b/source/module/M5Base.ts @@ -49,6 +49,18 @@ export interface M5RollResult extends M5RollTemplate { css: string; } +export enum M5ItemType { + SKILL = "skill", + ITEM = "item", + WEAPON = "weapon", + DEFENSIVE_WEAPON = "defensiveWeapon", + ARMOR = "armor", + CONTAINER = "container", + SPELL = "spell", + KAMPFKUNST = "kampfkunst", + EFFECT = "effect", +} + export enum M5EwResult { TBD = "", FUMBLE = "roll-ew-result-fumble", diff --git a/source/module/actors/M5Character.ts b/source/module/actors/M5Character.ts index e0bda3c..0ce24d6 100644 --- a/source/module/actors/M5Character.ts +++ b/source/module/actors/M5Character.ts @@ -1,5 +1,5 @@ import { M5Item } from "../items/M5Item"; -import { M5Attribute, M5CharacterCalculatedData, M5ItemMod, M5ModOperation, M5ModResult, M5RollData, M5Skill, M5SkillCalculated, M5SkillLearned } from "../M5Base"; +import { M5Attribute, M5CharacterCalculatedData, M5ItemMod, M5ItemType, M5ModOperation, M5ModResult, M5RollData, M5Skill, M5SkillCalculated, M5SkillLearned } from "../M5Base"; import M5ModAggregate from "./M5ModAggregate"; export class M5Character extends Actor { // constructor( @@ -522,6 +522,18 @@ export class M5Character extends Actor { }); } + createItem(itemName: string, itemType: M5ItemType): Promise { + const itemData = { + name: itemName, + type: itemType, + }; + + return (this as any).createEmbeddedDocuments("Item", [itemData]).then((docs) => { + const item = docs[0]; + return item; + }); + } + getItem(itemId: string): any { if (!(this as any).items) return null; return (this as any).getEmbeddedDocument("Item", itemId); diff --git a/source/module/sheets/M5CharacterSheet.ts b/source/module/sheets/M5CharacterSheet.ts index 2990225..d2ffa4c 100644 --- a/source/module/sheets/M5CharacterSheet.ts +++ b/source/module/sheets/M5CharacterSheet.ts @@ -1,7 +1,7 @@ import Logger from "../../utils/Logger"; import { M5Character } from "../actors/M5Character"; import { M5Item } from "../items/M5Item"; -import { M5SkillLearned, M5SkillUnlearned } from "../M5Base"; +import { M5ItemType, M5SkillLearned, M5SkillUnlearned } from "../M5Base"; import { M5Roll } from "../rolls/M5Roll"; export default class M5CharacterSheet extends ActorSheet { @@ -111,7 +111,11 @@ export default class M5CharacterSheet extends ActorSheet { if (!item.system.quantity) { item.system.quantity = 0; } - item.system.quantity += 1; + item.update({ + data: { + quantity: item.system.quantity + 1, + }, + }); this.render(); }); @@ -127,7 +131,11 @@ export default class M5CharacterSheet extends ActorSheet { const context = this.actor as any; const item = context.items.get(itemId); if (item.system.quantity > 0) { - item.system.quantity -= 1; + item.update({ + data: { + quantity: item.system.quantity - 1, + }, + }); } this.render(); }); @@ -144,7 +152,11 @@ export default class M5CharacterSheet extends ActorSheet { const context = this.actor as any; const item = context.items.get(itemId); if (item.system.quantity > 0) { - item.system.quantity -= 1; + item.update({ + data: { + quantity: item.system.quantity - 1, + }, + }); } await item.roll(); @@ -260,13 +272,96 @@ export default class M5CharacterSheet extends ActorSheet { const item = context.items.get(itemId); if (item.system.equipped === true) { item.system.equipped = false; - } - else { + } else { item.system.equipped = true; } this.render(); }); + html.find(".add-item").on("click", async (event) => { + const data = this.actor.system; + + const character = this.actor as M5Character; + character.createItem((game as Game).i18n.localize("TYPES.Item.item"), M5ItemType.ITEM).then((i) => { + const item = i as any; + item.update({ + data: { + quantity: 1, + }, + }); + }); + }); + + html.find(".add-weapon").on("click", async (event) => { + const data = this.actor.system; + + const character = this.actor as M5Character; + character.createItem((game as Game).i18n.localize("TYPES.Item.weapon"), M5ItemType.WEAPON); + }); + + html.find(".add-defensiveWeapon").on("click", async (event) => { + const data = this.actor.system; + + const character = this.actor as M5Character; + character.createItem((game as Game).i18n.localize("TYPES.Item.defensiveWeapon"), M5ItemType.DEFENSIVE_WEAPON); + }); + + html.find(".add-armor").on("click", async (event) => { + const data = this.actor.system; + + const character = this.actor as M5Character; + character.createItem((game as Game).i18n.localize("TYPES.Item.armor"), M5ItemType.ARMOR); + }); + + html.find(".add-container").on("click", async (event) => { + const data = this.actor.system; + + const character = this.actor as M5Character; + character.createItem((game as Game).i18n.localize("TYPES.Item.container"), M5ItemType.CONTAINER).then((i) => { + const item = i as any; + item.update({ + data: { + quantity: 1, + }, + }); + }); + }); + + html.find(".add-spell").on("click", async (event) => { + const data = this.actor.system; + + const character = this.actor as M5Character; + character.createItem((game as Game).i18n.localize("TYPES.Item.spell"), M5ItemType.SPELL).then((i) => { + const item = i as any; + item.update({ + data: { + process: "none", + }, + }); + }); + }); + + html.find(".add-kampfkunst").on("click", async (event) => { + const data = this.actor.system; + + const character = this.actor as M5Character; + character.createItem((game as Game).i18n.localize("TYPES.Item.kampfkunst"), M5ItemType.KAMPFKUNST).then((i) => { + const item = i as any; + item.update({ + data: { + type: "angriff", + variante: "anstuermen", + }, + }); + }); + }); + + html.find(".add-effect").on("click", async (event) => { + const data = this.actor.system; + + const character = this.actor as M5Character; + character.createItem((game as Game).i18n.localize("TYPES.Item.effect"), M5ItemType.EFFECT); + }); // Drag & Drop const dragDrop = new DragDrop({ diff --git a/templates/sheets/character/combat.hbs b/templates/sheets/character/combat.hbs index 3f495e3..e26b12a 100644 --- a/templates/sheets/character/combat.hbs +++ b/templates/sheets/character/combat.hbs @@ -48,7 +48,7 @@ {{localize "midgard5.kampfkunst-variante-short"}} {{localize "midgard5.ew"}} - + diff --git a/templates/sheets/character/effects.hbs b/templates/sheets/character/effects.hbs index 986629d..7af454d 100644 --- a/templates/sheets/character/effects.hbs +++ b/templates/sheets/character/effects.hbs @@ -2,7 +2,8 @@ {{localize "TYPES.Item.effect"}} - + + diff --git a/templates/sheets/character/gear.hbs b/templates/sheets/character/gear.hbs index a0d01ab..6f88e2a 100644 --- a/templates/sheets/character/gear.hbs +++ b/templates/sheets/character/gear.hbs @@ -43,8 +43,8 @@ {{localize "midgard5.item-quantity"}} {{localize "midgard5.item-value"}} - -   + + @@ -90,7 +90,8 @@ {{localize "TYPES.Item.weapon"}} {{localize "midgard5.item-value"}} -   + + @@ -124,7 +125,7 @@ {{localize "TYPES.Item.defensiveWeapon"}} {{localize "midgard5.item-value"}} -   + @@ -158,7 +159,7 @@ {{localize "TYPES.Item.armor"}} {{localize "midgard5.item-value"}} -   + @@ -196,7 +197,7 @@ {{localize "midgard5.item-value"}} -   + diff --git a/templates/sheets/character/spells.hbs b/templates/sheets/character/spells.hbs index 2bf55a7..ff5db64 100644 --- a/templates/sheets/character/spells.hbs +++ b/templates/sheets/character/spells.hbs @@ -4,7 +4,7 @@ {{localize "TYPES.Item.spell"}} {{localize "midgard5.ew"}} - +