introduces socketlib as hard requirement enabling players to loot and buy even when they lack the permission to update items they don't own (yet).

pull/65/head
macniel 2025-11-16 12:36:19 +01:00
parent e0e70d126f
commit 4f4446d327
6 changed files with 70 additions and 12 deletions

View File

@ -2,14 +2,23 @@ import {XmlImportDialog} from "./module/dialog/xmlImportDialog.mjs";
import {initGlobalSettings, initUserSettings} from "./module/settings/global-settings.mjs"; import {initGlobalSettings, initUserSettings} from "./module/settings/global-settings.mjs";
import {setUpActorSheets, setUpItemSheets} from "./module/setup/sheets.mjs"; import {setUpActorSheets, setUpItemSheets} from "./module/setup/sheets.mjs";
import {loadPartials} from "./module/setup/partials.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"; import {initHandlebarHelpers} from "./module/handlebar-helpers/index.mjs";
Hooks.once("init", () => { Hooks.once("init", () => {
console.log("DSA 4.1 is ready for development!") console.log("DSA 4.1 is ready for development!")
game.DSA41 = initGlobalAccess() game.DSA41 = {
...game.DSA41,
...initGlobalAccess()
}
initDocumentClasses(CONFIG) initDocumentClasses(CONFIG)
initUserSettings(game.settings) initUserSettings(game.settings)
@ -27,6 +36,9 @@ Hooks.once("init", () => {
initHandlebarHelpers(Handlebars) initHandlebarHelpers(Handlebars)
}) })
game.DSA41 = {}
initSocketLib(game.DSA41)
Hooks.on("getActorContextOptions", (application, menuItems) => { Hooks.on("getActorContextOptions", (application, menuItems) => {
menuItems.push({ menuItems.push({
name: "Import from XML", name: "Import from XML",

View File

@ -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 { export {
initSocketLib,
initGlobalAccess, initGlobalAccess,
initDocumentClasses, initDocumentClasses,
initDataModels, initDataModels,

View File

@ -280,7 +280,7 @@ export default {
name: "Aus dem Inventar entfernen", name: "Aus dem Inventar entfernen",
icon: '<i class="fa-solid fa-trash"></i>', icon: '<i class="fa-solid fa-trash"></i>',
callback: (target) => { callback: (target) => {
thisObject.document.deleteEmbeddedDocuments('Item', [target.dataset.itemId]) game.DSA41.socket.executeAsGM("removeFromLootTable", thisObject.document.id, target.dataset.itemId)
}, },
condition: (target) => { condition: (target) => {
const {itemId} = target.dataset const {itemId} = target.dataset

View File

@ -743,7 +743,7 @@ class CharacterSheet extends HandlebarsApplicationMixin(ActorSheetV2) {
} else { } else {
if (document.parent && document.parent !== this.actor) { 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) await this._onDropDocument(event, document)

View File

@ -109,13 +109,8 @@ export class MerchantSheet extends HandlebarsApplicationMixin(ActorSheetV2) {
actor.createEmbeddedDocuments('Item', [item]).then(documents => { actor.createEmbeddedDocuments('Item', [item]).then(documents => {
documents[0].update({'system.quantity': 1}) documents[0].update({'system.quantity': 1})
}) })
if (item.system.quantity != -1) { // -1 means infinite
if (item.system.quantity > 1) { game.DSA41.socket.executeAsGM("buyFromLootTable", this.document.id, item.id)
item.update({'system.quantity': item.system.quantity - 1})
} else {
this.document.deleteEmbeddedDocuments('Item', [item._id]) // delete when the quantity is equal to 0
}
}
ChatMessage.create({ ChatMessage.create({
user: game.user._id, user: game.user._id,
speaker: {actor}, speaker: {actor},

View File

@ -7,6 +7,7 @@
"minimum": 12, "minimum": 12,
"verified": 13 "verified": 13
}, },
"dependencies": [],
"authors": [ "authors": [
{ {
"name": "GrandpaPoppy" "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", "initiative": "1d6",
"grid": { "grid": {
"distance": 1, "distance": 1,