Skip to content

Commit

Permalink
v1.6.4 release
Browse files Browse the repository at this point in the history
  • Loading branch information
drewg13 committed May 29, 2021
1 parent bcacd86 commit a4bca93
Show file tree
Hide file tree
Showing 10 changed files with 394 additions and 474 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@ v1.6.2 Fix tooltip bugs

v1.6.3 Fix Vice rolls

v1.6.4 Fix createItem hook issues in 0.8.x, switch XP triggers to tooltip on hover
19 changes: 10 additions & 9 deletions module/sav-helpers.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
export class SaVHelpers {

/**
* 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 {Document} actor
* @returns {Array}
*
*/
static removeDuplicatedItemType(item_data, actor) {

let dupe_list = [];
let distinct_types = ["crew_reputation", "class", "background", "vice", "heritage", "ship_size", "crew_type"];
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.
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)) {
if( game.majorVersion > 7 ) {
actor.deleteEmbeddedDocuments("Item", [i.id]);
} else {
actor.deleteOwnedItem(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);
}
});

return dupe_list;
}

/**
Expand Down
61 changes: 61 additions & 0 deletions module/sav-item.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,70 @@
import { SaVHelpers } from "./sav-helpers.js";

/**
* Extend the basic Item
* @extends {Item}
*/
export class SaVItem 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( ( game.majorVersion > 7 ) && ( actor?.documentName === "Actor" ) ) {
removeItems = SaVHelpers.removeDuplicatedItemType( data, actor );
}
if( removeItems ) {
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( ( game.majorVersion > 7 ) && ( actor?.documentName === "Actor" ) && ( actor?.permission >= CONST.ENTITY_PERMISSIONS.OWNER ) ) {
await SaVHelpers.callItemLogic( data, actor );

if( ( game.majorVersion > 7 ) && ( ( data.type === "class" ) || ( data.type === "crew_type" ) ) && ( data.data.def_abilities !== "" ) ) {
await SaVHelpers.addDefaultAbilities( data, actor );
}

if( ( game.majorVersion > 7 ) && ( ( data.type === "class" ) || ( data.type === "crew_type" ) ) && ( ( actor.img.slice( 0, 46 ) === "systems/scum-and-villainy/styles/assets/icons/" ) || ( actor.img === "icons/svg/mystery-man.svg" ) ) ) {
const icon = data.img;
const icon_update = {
img: icon,
token: {
img: icon
}
};
await actor.update( icon_update );
}
}
}
}

/* -------------------------------------------- */

/** @override */
async _onDelete( options, userId ) {
super._onDelete( options, userId );

let actor = this.parent ? this.parent : null;
let data = this.data;
if( ( game.majorVersion > 7 ) && ( actor?.documentName === "Actor" ) && ( actor?.permission >= CONST.ENTITY_PERMISSIONS.OWNER ) ) {
await SaVHelpers.undoItemLogic( data, actor );
}
}

/* override */
prepareData() {
super.prepareData();
Expand Down
2 changes: 1 addition & 1 deletion module/sav-sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ async _onFlagAddClick(event) {

if ( perms >= CONST.ENTITY_PERMISSIONS.OWNER ) {
let dialog = new Dialog({
title: `${game.i18n.localize('BITD.Add')} ${item_type}`,
title: `${game.i18n.localize('BITD.Add')} ${game.i18n.localize('BITD.' + SaVHelpers.getProperCase(item_type) )}`,
content: html,
buttons: {
one: {
Expand Down
56 changes: 1 addition & 55 deletions module/sav.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,44 +284,9 @@ Hooks.once("ready", async function() {
* Hooks
*/


Hooks.on("preCreateItem", async (item, data, options, userId) => {

let actor = item.parent ? item.parent : null;
if ( ( game.majorVersion > 7 ) && ( actor?.documentName === "Actor" ) ) {
await SaVHelpers.removeDuplicatedItemType(data, actor);

if ( ( ( data.type === "class" ) || ( data.type === "crew_type" ) ) && !( data.data.def_abilities === "" ) ) {
await SaVHelpers.addDefaultAbilities( data, actor );
}

if ( ( ( data.type === "class" ) || ( data.type === "crew_type" ) ) && ( ( actor.img.slice( 0, 46 ) === "systems/scum-and-villainy/styles/assets/icons/" ) || ( actor.img === "icons/svg/mystery-man.svg" ) ) ) {
const icon = data.img;
const icon_update = {
img: icon,
token: {
img: icon
}
};
await actor.update( icon_update );
/** code to replace all attached tokens as well
const tokens = actor.getActiveTokens();
let token_update;
if ( tokens ) {
token_update = {
img: icon
};
tokens.forEach( t => t.update( token_update ) );
};
*/
}
}
return true;
});

Hooks.on("preCreateOwnedItem", async (parent_entity, child_data, options, userId) => {
if( game.majorVersion === 7 ) {
await SaVHelpers.removeDuplicatedItemType(child_data, parent_entity);
await parent_entity.deleteOwnedItem( await SaVHelpers.removeDuplicatedItemType(child_data, parent_entity) );

if ( ( ( child_data.type === "class" ) || ( child_data.type === "crew_type" ) ) && !( child_data.data.def_abilities === "" ) ) {
await SaVHelpers.addDefaultAbilities( child_data, parent_entity );
Expand All @@ -341,32 +306,13 @@ Hooks.on("preCreateOwnedItem", async (parent_entity, child_data, options, userId
return true;
});

Hooks.on("createItem", async (item, options, userId) => {

let actor = item.parent ? item.parent : null;
let data = item.data;
if ( ( game.majorVersion > 7 ) && (actor?.documentName === "Actor") && (actor?.permission >= CONST.ENTITY_PERMISSIONS.OWNER) ) {
await SaVHelpers.callItemLogic(data, actor);
}
return true;
});

Hooks.on("createOwnedItem", async (parent_entity, child_data, options, userId) => {
if ( ( game.majorVersion === 7 ) && (parent_entity.entity === "Actor") && (parent_entity.permission >= CONST.ENTITY_PERMISSIONS.OWNER) ) {
await SaVHelpers.callItemLogic(child_data, parent_entity);
}
return true;
});

Hooks.on("deleteItem", async (item, options, userId) => {
let actor = item.parent ? item.parent : null;
let data = item.data;
if ( ( game.majorVersion > 7 ) && (actor?.documentName === "Actor") && (actor?.permission >= CONST.ENTITY_PERMISSIONS.OWNER) ) {
await SaVHelpers.undoItemLogic(data, actor);
}
return true;
});

Hooks.on("deleteOwnedItem", async (parent_entity, child_data, options, userId) => {
if ( ( game.majorVersion === 7 ) && (parent_entity.entity === "Actor") && (parent_entity.permission >= CONST.ENTITY_PERMISSIONS.OWNER) ) {
await SaVHelpers.undoItemLogic(child_data, parent_entity);
Expand Down
Loading

0 comments on commit a4bca93

Please sign in to comment.