Move item add logic from hooks to methods (#119)

* Move item add logic from hooks to methods

* Bug fix for item logic
This commit is contained in:
drewg13 2021-06-01 02:56:25 -04:00 committed by GitHub
parent 95a34349e7
commit 372ce2408f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 33 deletions

View file

@ -1,31 +1,34 @@
export class BladesHelpers {
/**
* Removes a duplicate item type from charlist.
* Identifies duplicate items by type and returns a array of item ids to remove
*
* @param {Object} item_data
* @param {Entity} actor
* @param {Document} actor
* @returns {Array}
*
*/
static removeDuplicatedItemType(item_data, actor) {
let dupe_list = [];
let distinct_types = ["crew_reputation", "class", "vice", "background", "heritage"];
let allowed_types = ["item"];
let should_be_distinct = distinct_types.includes(item_data.type);
// If the Item has the exact same name - remove it from list.
// Remove Duplicate items from the array.
let update = [];
actor.items.forEach(i => {
actor.items.forEach( i => {
let has_double = (item_data.type === i.data.type);
if (i.data.name === item_data.name || (should_be_distinct && has_double)) {
update.push(i.id);
if ( ( ( i.name === item_data.name ) || ( should_be_distinct && has_double ) ) && !( allowed_types.includes( item_data.type ) ) && ( item_data._id !== i.id ) ) {
dupe_list.push (i.id);
}
});
actor.deleteEmbeddedDocuments("Item", update);
return dupe_list;
}
/**
* Add item modification if logic exists.
* @param {Object} item_data
* @param {Entity} entity
* @param {Document} entity
*/
static async callItemLogic(item_data, entity) {
@ -77,7 +80,7 @@ export class BladesHelpers {
* - Remove all items and then Add them back to
* sustain the logic mods
* @param {Object} item_data
* @param {Entity} entity
* @param {Document} entity
*/
static async undoItemLogic(item_data, entity) {
@ -101,7 +104,7 @@ export class BladesHelpers {
case "addition":
foundry.utils.mergeObject(
logic_update,
{[expression.attribute]: Number(BladesHelpers.getNestedProperty(entity, prefix + expression.attribute)) - expression.value},
{[expression.attribute]: Number(BladesHelpers.getNestedProperty(entity, expression.attribute)) - expression.value},
{insertKeys: true}
);
break;
@ -191,7 +194,7 @@ export class BladesHelpers {
let attribute_labels = {};
const attributes = game.system.model.Actor.character.attributes;
for (var attribute_name in attributes) {
for (attribute_name in attributes) {
attribute_labels[attribute_name] = attributes[attribute_name].label;
for (var skill_name in attributes[attribute_name].skills) {
attribute_labels[skill_name] = attributes[attribute_name].skills[skill_name].label;
@ -206,10 +209,9 @@ export class BladesHelpers {
* Returns true if the attribute is an action
*
* @param {string} attribute_name
* @returns {bool}
* @returns {Boolean}
*/
static isAttributeAction(attribute_name) {
let attribute_labels = {};
const attributes = game.system.model.Actor.character.attributes;
return !(attribute_name in attributes);

View file

@ -1,9 +1,57 @@
import { BladesHelpers } from "./blades-helpers.js";
/**
* Extend the basic Item
* @extends {Item}
*/
export class BladesItem extends Item {
/** @override */
async _preCreate( data, options, user ) {
await super._preCreate( data, options, user );
let removeItems = [];
if( user.id === game.user.id ) {
let actor = this.parent ? this.parent : null;
if( actor?.documentName === "Actor" ) {
removeItems = BladesHelpers.removeDuplicatedItemType( data, actor );
}
if( removeItems.length !== 0 ) {
await actor.deleteEmbeddedDocuments( "Item", removeItems );
}
}
}
/* -------------------------------------------- */
/** @override */
async _onCreate( data, options, userId ) {
super._onCreate( data, options, userId );
if( userId === game.user.id ) {
let actor = this.parent ? this.parent : null;
if( ( actor?.documentName === "Actor" ) && ( actor?.permission >= CONST.ENTITY_PERMISSIONS.OWNER ) ) {
await BladesHelpers.callItemLogic( data, actor );
}
}
}
/* -------------------------------------------- */
/** @override */
async _onDelete( options, userId ) {
super._onDelete( options, userId );
let actor = this.parent ? this.parent : null;
let data = this.data;
if ( ( actor?.documentName === "Actor" ) && ( actor?.permission >= CONST.ENTITY_PERMISSIONS.OWNER ) ) {
await BladesHelpers.undoItemLogic( data, actor );
}
}
/* -------------------------------------------- */
/* override */
prepareData() {

View file

@ -284,29 +284,12 @@ Hooks.once("ready", function() {
/*
* Hooks
*/
Hooks.on("preCreateItem", (item, data, options, userId) => {
BladesHelpers.removeDuplicatedItemType(data, item.parent);
return true;
});
Hooks.on("createItem", async (item, options, userId) => {
await BladesHelpers.callItemLogic(item.data, item.parent);
return true;
});
Hooks.on("deleteItem", async (item, options, userId) => {
await BladesHelpers.undoItemLogic(item.data, item.parent);
return true;
});
// getSceneControlButtons
Hooks.on("renderSceneControls", async (app, html) => {
let dice_roller = $('<li class="scene-control" title="Dice Roll"><i class="fas fa-dice"></i></li>');
dice_roller.click(function() {
simpleRollPopup();
dice_roller.click( async function() {
await simpleRollPopup();
});
html.append(dice_roller);
});