diff --git a/src/main.mjs b/src/main.mjs index 09d2c24a..f0da9309 100644 --- a/src/main.mjs +++ b/src/main.mjs @@ -2,14 +2,23 @@ import {XmlImportDialog} from "./module/dialog/xmlImportDialog.mjs"; import {initGlobalSettings, initUserSettings} from "./module/settings/global-settings.mjs"; import {setUpActorSheets, setUpItemSheets} from "./module/setup/sheets.mjs"; import {loadPartials} from "./module/setup/partials.mjs"; -import {initCombat, initDataModels, initDocumentClasses, initGlobalAccess} from "./module/setup/config.mjs"; +import { + initSocketLib, + initCombat, + initDataModels, + initDocumentClasses, + initGlobalAccess +} from "./module/setup/config.mjs"; import {initHandlebarHelpers} from "./module/handlebar-helpers/index.mjs"; Hooks.once("init", () => { console.log("DSA 4.1 is ready for development!") - game.DSA41 = initGlobalAccess() + game.DSA41 = { + ...game.DSA41, + ...initGlobalAccess() + } initDocumentClasses(CONFIG) initUserSettings(game.settings) @@ -27,6 +36,9 @@ Hooks.once("init", () => { initHandlebarHelpers(Handlebars) }) +game.DSA41 = {} +initSocketLib(game.DSA41) + Hooks.on("getActorContextOptions", (application, menuItems) => { menuItems.push({ name: "Import from XML", diff --git a/src/module/setup/config.mjs b/src/module/setup/config.mjs index e8778dd1..b603f42e 100644 --- a/src/module/setup/config.mjs +++ b/src/module/setup/config.mjs @@ -73,7 +73,48 @@ function initCombat(config) { } } +function initSocketLib() { + + + Hooks.on("socketlib.ready", () => { + console.log("Socketlib is registering") + let socket = socketlib.registerSystem("DSA_4-1") + socket.register("removeFromLootTable", removeFromLootTable) + socket.register("buyFromLootTable", buyFromLootTable) + if (!game.DSA41) { + game.DSA41 = {} + } + game.DSA41.socket = socket + }) + + + async function removeFromLootTable(actorId, itemId) { + if (actorId && game.actors.get(actorId)) { + const actor = game.actors.get(actorId) + + return await actor.deleteEmbeddedDocuments('Item', [itemId]) + } + } + + async function buyFromLootTable(actorId, itemId) { + if (actorId && game.actors.get(actorId)) { + const actor = game.actors.get(actorId) + const item = actor.items.find(p => p.id === itemId) + if (item.system.quantity != -1) { // -1 means infinite + if (item.system.quantity > 1) { + item.update({'system.quantity': item.system.quantity - 1}) + } else { + actor.deleteEmbeddedDocuments('Item', [item._id]) // delete when the quantity is equal to 0 + } + } + return true + } + } + +} + export { + initSocketLib, initGlobalAccess, initDocumentClasses, initDataModels, diff --git a/src/module/sheets/character/equipment.mjs b/src/module/sheets/character/equipment.mjs index bdb26eaa..8c34b1c8 100644 --- a/src/module/sheets/character/equipment.mjs +++ b/src/module/sheets/character/equipment.mjs @@ -280,7 +280,7 @@ export default { name: "Aus dem Inventar entfernen", icon: '', callback: (target) => { - thisObject.document.deleteEmbeddedDocuments('Item', [target.dataset.itemId]) + game.DSA41.socket.executeAsGM("removeFromLootTable", thisObject.document.id, target.dataset.itemId) }, condition: (target) => { const {itemId} = target.dataset diff --git a/src/module/sheets/characterSheet.mjs b/src/module/sheets/characterSheet.mjs index f25d4caf..f99cd01a 100644 --- a/src/module/sheets/characterSheet.mjs +++ b/src/module/sheets/characterSheet.mjs @@ -743,7 +743,7 @@ class CharacterSheet extends HandlebarsApplicationMixin(ActorSheetV2) { } else { if (document.parent && document.parent !== this.actor) { - document.parent.items.get(document._id).delete() + game.DSA41.socket.executeAsGM("removeFromLootTable", document.parent.id, document._id) } await this._onDropDocument(event, document) diff --git a/src/module/sheets/merchantSheet.mjs b/src/module/sheets/merchantSheet.mjs index bc9b2b63..2774656f 100644 --- a/src/module/sheets/merchantSheet.mjs +++ b/src/module/sheets/merchantSheet.mjs @@ -109,13 +109,8 @@ export class MerchantSheet extends HandlebarsApplicationMixin(ActorSheetV2) { actor.createEmbeddedDocuments('Item', [item]).then(documents => { documents[0].update({'system.quantity': 1}) }) - if (item.system.quantity != -1) { // -1 means infinite - if (item.system.quantity > 1) { - item.update({'system.quantity': item.system.quantity - 1}) - } else { - this.document.deleteEmbeddedDocuments('Item', [item._id]) // delete when the quantity is equal to 0 - } - } + + game.DSA41.socket.executeAsGM("buyFromLootTable", this.document.id, item.id) ChatMessage.create({ user: game.user._id, speaker: {actor}, diff --git a/src/system.json b/src/system.json index be6b6c09..2c5045b7 100644 --- a/src/system.json +++ b/src/system.json @@ -7,6 +7,7 @@ "minimum": 12, "verified": 13 }, + "dependencies": [], "authors": [ { "name": "GrandpaPoppy" @@ -340,7 +341,16 @@ } } }, - "socket": false, + "socket": true, + "relationships": { + "requires": [ + { + "id": "socketlib", + "type": "module", + "manifest": "https://github.com/farling42/foundryvtt-socketlib/releases/latest/download/module.json" + } + ] + }, "initiative": "1d6", "grid": { "distance": 1,