diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..7130698 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +# SPDX-FileCopyrightText: 2022 Johannes Loher +# +# SPDX-License-Identifier: MIT + +root = true + +[*] +end_of_line = lf +insert_final_newline = true +indent_style = space +indent_size = 2 +charset = utf-8 +trim_trailing_whitespace = true diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..d0fedef --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,5 @@ +# SPDX-FileCopyrightText: 2022 Johannes Loher +# +# SPDX-License-Identifier: MIT + +ko_fi: ghostfvtt diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..c3a74a8 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,42 @@ +name: Release Creation + +on: + release: + types: [published] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Extract version number from tag + id: get_version + uses: battila7/get-version-action@v2 + + - name: Substitute placeholders in the manifest + uses: microsoft/variable-substitution@v1 + with: + files: "module.json" + env: + version: ${{steps.get_version.outputs.version-without-v}} + url: https://github.com/${{github.repository}} + bugs: https://github.com/${{github.repository}}/issues + license: https://raw.githubusercontent.com/${{github.repository}}/${{github.event.release.tag_name}}/LICENSE + readme: https://raw.githubusercontent.com/${{github.repository}}/${{github.event.release.tag_name}}/README.md + manifest: https://github.com/${{github.repository}}/releases/latest/download/module.json + download: https://github.com/${{github.repository}}/releases/download/${{github.event.release.tag_name}}/module.zip + - name: Create module.zip + run: zip -r ./module.zip module.json README.md LICENSE modules/ + + - name: Update Release with Files + uses: ncipollo/release-action@v1 + with: + allowUpdates: true + name: ${{ github.event.release.name }} + draft: ${{ github.event.release.unpublished }} + prerelease: ${{ github.event.release.prerelease }} + token: ${{ secrets.GITHUB_TOKEN }} + artifacts: "./module.json, ./module.zip" + tag: ${{ github.event.release.tag_name }} + body: ${{ github.event.release.body }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..91e2991 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +# SPDX-FileCopyrightText: 2022 Johannes Loher +# +# SPDX-License-Identifier: MIT + +foundry.js +client +common +.vscode/ +jsconfig.json diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a6c3962 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2022 Johannes Loher + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..fc6a0a5 --- /dev/null +++ b/README.md @@ -0,0 +1,41 @@ + + +# PF2 Shields Token Bar + +![Supported Foundry Versions](https://img.shields.io/endpoint?url=https://foundryshields.com/version?url=https://github.com/ghost-fvtt/pf2-shields-token-bar/releases/latest/download/module.json) +![Latest Release Download Count](https://img.shields.io/github/downloads/ghost-fvtt/pf2-shields-token-bar/latest/module.zip) +[![Forge Installs](https://img.shields.io/badge/dynamic/json?label=Forge%20Installs&query=package.installs&suffix=%25&url=https%3A%2F%2Fforge-vtt.com%2Fapi%2Fbazaar%2Fpackage%2Fpf2-shields-token-bar&colorB=4aa94a)](https://forge-vtt.com/bazaar#package=pf2-shields-token-bar) +[![Foundry Hub Endorsements](https://img.shields.io/endpoint?logoColor=white&url=https%3A%2F%2Fwww.foundryvtt-hub.com%2Fwp-json%2Fhubapi%2Fv1%2Fpackage%2Fpf2-shields-token-bar%2Fshield%2Fendorsements)](https://www.foundryvtt-hub.com/package/pf2-shields-token-bar/) +[![Ko-fi](https://img.shields.io/badge/Ko--fi-ghostfvtt-00B9FE?logo=kofi)](https://ko-fi.com/ghostfvtt) + +A module for [Foundry Virtual Tabletop] that makes the shield HP in [Pathfinder Second Edition] a properly editable +token resource. + +## Installation + +To install and use PF2 Shields Token Bar, simply paste the following URL into the +**Install Module** dialog on the Setup menu of Foundry Virtual Tabletop. + +https://github.com/ghost-fvtt/pf2-shields-token-bar/releases/latest/download/module.json + +## Usage + +Simply use `attributes.shield.hp` as a token resource. You can then edit it like any other token resource. Configuring +this with [Bar Brawl] is also supported. + +## Licensing + +This project is being developed under the terms of the +[LIMITED LICENSE AGREEMENT FOR MODULE DEVELOPMENT] for Foundry Virtual Tabletop. + +The project itself is licensed under the [MIT] license. + +[Foundry Virtual Tabletop]: https://foundryvtt.com/ +[Pathfinder Second Edition]: https://foundryvtt.com/packages/pf2e +[Bar Brawl]: https://foundryvtt.com/packages/barbrawl +[LIMITED LICENSE AGREEMENT FOR MODULE DEVELOPMENT]: https://foundryvtt.com/article/license/ +[MIT]: LICENSE diff --git a/module.json b/module.json new file mode 100644 index 0000000..bbab211 --- /dev/null +++ b/module.json @@ -0,0 +1,48 @@ +{ + "id": "pf2-shields-token-bar", + "title": "PF2 Shields Token Bar", + "description": "A module for Foundry Virtual Tabletop that makes the shield HP in Pathfinder Second Edition a properly editable token resource.", + "authors": [ + { + "name": "Johannes Loher", + "email": "johannes.loher@fg4f.de", + "discord": "ghost#2000", + "ko-fi": "ghostfvtt" + } + ], + "url": "this is auto replaced", + "license": "this is auto replaced", + "readme": "this is auto replaced", + "bugs": "this is auto replaced", + "version": "This is auto replaced", + "compatibility": { + "minimum": "10", + "verified": "10" + }, + "esmodules": ["modules/pf2-shields-token-bar.mjs"], + "relationships": { + "systems": [ + { + "id": "pf2e", + "type": "system", + "compatibility": { + "minimum": "4.2.5", + "verified": "4.2.5" + } + } + ], + "requires": [ + { + "id": "lib-wrapper", + "type": "module", + "compatibility": { + "minimum": "1.12.7.0", + "verified": "1.12.7.0" + } + } + ] + }, + "manifest": "this is auto replaced", + "download": "this is auto replaced", + "manifestPlusVersion": "1.2.0" +} diff --git a/modules/pf2-shields-token-bar.mjs b/modules/pf2-shields-token-bar.mjs new file mode 100644 index 0000000..c724d24 --- /dev/null +++ b/modules/pf2-shields-token-bar.mjs @@ -0,0 +1,86 @@ +// SPDX-FileCopyrightText: 2022 Johannes Loher +// +// SPDX-License-Identifier: MIT + +const packageId = "pf2-shields-token-bar"; + +Hooks.once("ready", () => { + if (!game.modules.get("lib-wrapper")?.active && game.user.isGM) + ui.notifications.error( + `Module ${packageId} requires the 'libWrapper' module. Please install and activate it.` + ); +}); + +Hooks.once("init", () => { + libWrapper?.register( + packageId, + "CONFIG.Actor.documentClass.prototype.modifyTokenAttribute", + modifyTokenAttribute, + "MIXED" + ); + + libWrapper?.register( + packageId, + "CONFIG.Token.documentClass.prototype.getBarAttribute", + getBarAttribute, + "MIXED" + ); +}); + +/** + * @param {Actor['modifyTokenAttribute']} wrapped + * @param {string} attribute + * @param {number} value + * @param {boolean} [isDelta=false] + * @param {boolean} [isBar=true] + */ +async function modifyTokenAttribute( + wrapped, + attribute, + value, + isDelta = false, + isBar = true +) { + if (attribute === "attributes.shield.hp") { + const { attributes } = this; + const actorShield = "shield" in attributes ? attributes.shield : null; + const shield = (() => { + const item = this.items.get(actorShield?.itemId ?? ""); + return item?.isOfType("armor") ? item : null; + })(); + if (!shield) return this; + + if (isDelta) + value = Math.clamped( + 0, + Number(shield.hitPoints.value) + value, + Number(shield.hitPoints.max) + ); + + await shield.update({ "system.hp.value": value }); + return this; + } + return wrapped.call(this, attribute, value, isDelta, isBar); +} + +/** + * @param {TokenDocument['getBarAttribute']} wrapped + * @param {string} barName + * @param {{alternative?: string}} [options={}] + */ +function getBarAttribute(wrapped, barName, { alternative } = {}) { + const attr = alternative || this[barName]?.attribute; + if (!attr || !this.actor) return null; + const data = foundry.utils.getProperty(this.actor.system, attr); + if (data === null || data === undefined) return null; + if (attr === "attributes.shield.hp") { + return { + type: "bar", + attribute: attr, + value: parseInt(data.value || 0), + max: parseInt(data.max || 0), + editable: true, + }; + } + return wrapped.call(this, barName, { alternative }); +}