Skip to content

Commit

Permalink
Merge pull request #235 from RabidOwlbear/main
Browse files Browse the repository at this point in the history
party sheet/container fix
  • Loading branch information
anthonyronda authored Sep 21, 2022
2 parents 80c013b + 2316182 commit a3b4d3c
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 25 deletions.
127 changes: 113 additions & 14 deletions src/module/actor/actor-sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ export class OseActorSheet extends ActorSheet {
}

_onDragStart(event) {
const v10 = isNewerVersion(game.version, "10.264");
const li = event.currentTarget;
let itemIdsArray = [];
if (event.target.classList.contains("content-link")) return;
Expand All @@ -242,10 +243,10 @@ export class OseActorSheet extends ActorSheet {
if (li.dataset.itemId) {
const item = this.actor.items.get(li.dataset.itemId);
dragData.type = "Item";
dragData.data = item.data;
if (item.data.type === "container" && item.data.data.itemIds.length) {
dragData.data = v10 ? item : item.data;
if (item.type === "container" && item.system.itemIds.length) {
//otherwise JSON.stringify will quadruple stringify for some reason
itemIdsArray = item.data.data.itemIds;
itemIdsArray = v10 ? item.system.itemIds : item.data.data.itemIds;
}
}

Expand All @@ -269,23 +270,121 @@ export class OseActorSheet extends ActorSheet {
})
);
}
async _onDropItem(event, data){
const v10 = isNewerVersion(game.version, "10.264");
const item = await Item.implementation.fromDropData(data);
const itemContainer = v10 ? this.object.items.get(item?.system?.containerId) : this.object.items.get(item?.data?.data?.containerId);
const itemData = item.toObject();
const targetEl = event.target.closest('.item')
const targetItem = this.object.items.get(targetEl?.dataset?.itemId)
let targetIsContainer
if(v10) targetIsContainer = targetItem?.type == 'container' ? true : false;
if(v10) targetIsContainer = targetItem?.data?.type == 'container' ? true : false;
const exists = this.object.items.get(item.id) ? true : false;
console.log(exists)


if(targetIsContainer){
return this._onContainerItemAdd(item, targetItem)
} else if (itemContainer){
this._onContainerItemRemove(item, itemContainer)

} else{
if(!exists){
return this._onDropItemCreate([itemData])
}
}



}
async _onContainerItemRemove(item, container){
const v10 = isNewerVersion(game.version, "10.264");
let newList = v10 ? container.system.itemIds.filter(i=>i.id != item.id) : container.data.data.itemIds.filter(i=>i.data,id != item.data.id);
const itemObj = v10 ? this.object.items.get(item.id) : this.object.items.get(item.data.id);
if(v10){
await container.update({system: {itemIds: newList}});
await itemObj.update({system:{containerId: ''}});
}
if(!v10){
await container.update({data:{data:{itemIds: newList}}});
await itemObj.update({data:{data:{containerId: ''}}});
}

async _onDropItemCreate(itemData) {
}
async _onContainerItemAdd(item, target){
const v10 = isNewerVersion(game.version, "10.264");
const itemData = item.toObject();
const container = v10 ? this.object.items.get(target.id) :this.object.items.get(target.data.id);

const containerId = v10 ? container.id : container.data.id;
const itemObj = v10 ? this.object.items.get(item.id) : this.object.items.get(item.data.id);
const alreadyExists = v10 ? container.system.itemIds.find(i=>i.id == item.id) :container.data.data.itemIds.find(i=>i.data.id == item.data.id);
if(!alreadyExists){
if(v10){
const newList = [...container.system.itemIds];
newList.push(item.id);
await container.update({system:{itemIds: newList}})
await itemObj.update({system:{containerId: container.id}})
}
if(!v10){
const newList = [...container.data.data.itemIds];
newList.push(item.data.id);
await container.update({data:{ data: {itemIds: newList}}})
await itemObj.update({data:{ data: {containerId: container.id}}})
}
}

}
async _onDropItemCreate(itemData, container = false) {
const v10 = isNewerVersion(game.version, "10.264");
//override to fix hidden items because their original containers don't exist on this actor
itemData = itemData instanceof Array ? itemData : [itemData];
itemData.forEach((item) => {
if (item.data.containerId && item.data.containerId !== "")

if(v10){
if (item.system.containerId && item.system.containerId !== "")
item.system.containerId = "";}
if (item.type === "container" && typeof item.system.itemIds === "string") {
//itemIds was double stringified to fix strange behavior with stringify blanking our Arrays
const containedItems = JSON.parse(item.system.itemIds);
containedItems.forEach((containedItem) => {
containedItem.system.containerId = "";
});
itemData.push(...containedItems);
}
if(!v10){
if (item.data.containerId && item.data.containerId !== "")
item.data.containerId = "";
if (item.type === "container" && typeof item.data.itemIds === "string") {
//itemIds was double stringified to fix strange behavior with stringify blanking our Arrays
const containedItems = JSON.parse(item.data.itemIds);
containedItems.forEach((containedItem) => {
containedItem.data.containerId = "";
});
itemData.push(...containedItems);
if (item.type === "container" && typeof item.data.data.itemIds === "string") {
//itemIds was double stringified to fix strange behavior with stringify blanking our Arrays
const containedItems = JSON.parse(item.data.data.itemIds);
containedItems.forEach((containedItem) => {
containedItem.data.data.containerId = "";
});
itemData.push(...containedItems);
}
};


})
if (!container) {
return this.actor.createEmbeddedDocuments("Item", itemData);
}
if (container){
let itemIds = v10 ? container.system.itemIds : container.data.data.itemIds;
itemIds.push(itemData.id);
const item = this.actor.items.get(itemData[0]._id);
if(v10){
await item.update({system:{containerId: container.id}});
await container.update({system:{itemIds: itemIds}});
}
});
return this.actor.createEmbeddedDocuments("Item", itemData);
if(!v10){
await item.update({data:{data:{containerId: container.id}}});
await container.update({data:{data:{itemIds: itemIds}}});
}

}
}

/* -------------------------------------------- */
Expand Down
4 changes: 2 additions & 2 deletions src/module/actor/character-sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class OseActorSheetCharacter extends OseActorSheet {
...Object.values(data.owned),
...Object.values(data.spells),
data.abilities,
].forEach((o) => o.sort((a, b) => (a.data.sort || 0) - (b.data.sort || 0)));
].forEach((o) => o.sort((a, b) => (a.sort || 0) - (b.sort || 0)));
}

generateScores() {
Expand Down Expand Up @@ -297,7 +297,7 @@ export class OseActorSheetCharacter extends OseActorSheet {
const item = this.actor.items.get(li.data("itemId"));
await item.update({
data: {
equipped: !item.data.data.equipped,
equipped: !item.system.equipped,
},
});
});
Expand Down
9 changes: 6 additions & 3 deletions src/module/party/party-sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ export class OsePartySheet extends FormApplication {
user: game.user,
settings: settings,
};

return data;
}

async _addActorToParty(actor) {

if (actor.type !== "character") {
return;
}
Expand All @@ -83,14 +85,14 @@ export class OsePartySheet extends FormApplication {
/* - Adding to the Party Sheet -*/
_onDrop(event) {
event.preventDefault();

// WIP Drop Items
let data;
try {
data = JSON.parse(event.dataTransfer.getData("text/plain"));

switch (data.type) {
case "Actor":

return this._onDropActor(event, data);
case "Folder":
return this._onDropFolder(event, data);
Expand All @@ -100,13 +102,14 @@ export class OsePartySheet extends FormApplication {
}
}

_onDropActor(event, data) {
async _onDropActor(event, data) {

if (data.type !== "Actor") {
return;
}

const actors = game.actors;
let droppedActor = actors.find((actor) => actor.id === data.id);
let droppedActor = await fromUuid(data.uuid);

this._addActorToParty(droppedActor);
}
Expand Down
20 changes: 14 additions & 6 deletions src/module/party/party.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
export class OseParty {
static get currentParty() {
const characters = game.actors.filter(
(act) =>
act.data.type === "character" &&
act.data.flags.ose &&
act.data.flags.ose.party === true);

const v10 = isNewerVersion(game.version, "10.264");
const systemName = game.system.id == 'ose' ?game.system.id : 'ose-dev'
const characters = v10 ?
game.actors.filter(
(act) =>
act.type === "character" &&
act.flags[systemName] &&
act.flags[systemName].party === true) :
game.actors.filter(
(act) =>
act.data.type === "character" &&
act.data.flags[systemName] &&
act.data.flags[systemName].party === true);
console.log(characters)
return characters;
}
}

0 comments on commit a3b4d3c

Please sign in to comment.