Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Haxxer committed Jan 2, 2024
1 parent c0db38d commit 99bed2f
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 66 deletions.
3 changes: 2 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## Version 2.8.15

- Fixed players not being able to sort vaults
- Fixed non-stackable items being fully deleted when added to vaults even though they may have quantity in the origin character inventory
- Fixed players not being able to sort vaults when they have permission to organize the vault

## Version 2.8.14

Expand Down
50 changes: 25 additions & 25 deletions src/API/private-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ export default class PrivateAPI {
*/
static async _dropItems({
userId, sceneId, sourceUuid = false, targetUuid = false, itemData = false, position = false, elevation = false
} = {}) {
} = {}) {

let itemsDropped;

Expand Down Expand Up @@ -988,16 +988,16 @@ export default class PrivateAPI {
}

static async _createItemPile({
sceneId = null,
position = false,
actor = false,
createActor = false,
items = false,
tokenOverrides = {},
actorOverrides = {},
itemPileFlags = {},
folders = false,
} = {}) {
sceneId = null,
position = false,
actor = false,
createActor = false,
items = false,
tokenOverrides = {},
actorOverrides = {},
itemPileFlags = {},
folders = false,
} = {}) {

let returns = {};

Expand Down Expand Up @@ -1643,7 +1643,7 @@ export default class PrivateAPI {
foundry.utils.setProperty(flagData, "x", dropData.gridPosition?.x ?? 0);
foundry.utils.setProperty(flagData, "y", dropData.gridPosition?.y ?? 0);
}
foundry.utils.setProperty(dropData.itemData, CONSTANTS.FLAGS.ITEM, flagData);
foundry.utils.setProperty(dropData.itemData, "flags", flagData);

if (sourceActor) {
return game.itempiles.API.transferItems(sourceActor, targetActor, [dropData.itemData], { interactionId: dropData.interactionId });
Expand Down Expand Up @@ -1708,8 +1708,8 @@ export default class PrivateAPI {
if (!hotkeyActionState.forceDropOneItem) {

if (!dropData.skipCheck) {
const item = await Item.implementation.create(dropData.itemData.item, {temporary: true});
itemQuantity = await DropItemDialog.show(item, dropData.target, {unlimitedQuantity: !dropData.source && game.user.isGM});
const item = await Item.implementation.create(dropData.itemData.item, { temporary: true });
itemQuantity = await DropItemDialog.show(item, dropData.target, { unlimitedQuantity: !dropData.source && game.user.isGM });
if (!itemQuantity) return;
}

Expand Down Expand Up @@ -2277,17 +2277,17 @@ export default class PrivateAPI {
}

static async _rollItemTable({
table = "",
timesToRoll = "1",
resetTable = true,
normalizeTable = false,
displayChat = false,
rollData = {},
customCategory = false,
targetActor = false,
removeExistingActorItems = false,
userId = false,
} = {}) {
table = "",
timesToRoll = "1",
resetTable = true,
normalizeTable = false,
displayChat = false,
rollData = {},
customCategory = false,
targetActor = false,
removeExistingActorItems = false,
userId = false,
} = {}) {

let items = await PileUtilities.rollTable({
tableUuid: table,
Expand Down
4 changes: 3 additions & 1 deletion src/helpers/pile-utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -1719,7 +1719,9 @@ export function getNewItemsVaultPosition(item, gridData, { position = null } = {
const { width, height } = getVaultItemDimensions(item, { ...itemFlagData, flipped });
for (let w = 0; w < width; w++) {
for (let h = 0; h < height; h++) {
fitsInPosition = position.x + w < enabledCols && position.y + h < enabledRows && !grid[position.x + w][position.y + h];
fitsInPosition = (position.x + w) < enabledCols
&& (position.y + h) < enabledRows
&& !grid[position.x + w][position.y + h];
if (!fitsInPosition) break;
}
if (!fitsInPosition) break;
Expand Down
77 changes: 40 additions & 37 deletions src/helpers/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import ItemPileSocket from "../socket.js";
import PrivateAPI from "../API/private-api.js";
import { SYSTEMS } from "../systems.js";
import CONSTANTS from "../constants/constants.js";
import { isItemStackable } from "./utilities.js";

export default class Transaction {

Expand Down Expand Up @@ -36,11 +35,8 @@ export default class Transaction {

type = PileUtilities.isItemCurrency(item) ? "currency" : type;

let flags = data.item ? data.flags ?? false : false;
let flags = data.flags ?? false;
let itemData = item instanceof Item ? item.toObject() : foundry.utils.deepClone(item);
if (flags) {
setProperty(itemData, "flags", flags);
}
if (SYSTEMS.DATA.ITEM_TRANSFORMER && !remove) {
itemData = await SYSTEMS.DATA.ITEM_TRANSFORMER(itemData);
}
Expand Down Expand Up @@ -68,68 +64,75 @@ export default class Transaction {
actorExistingItem = actorHasItem || Utilities.findSimilarItem(this.actor.items, itemData);
}

let canItemStack = PileUtilities.canItemStack(actorExistingItem || itemData, this.actor);
const canItemStack = PileUtilities.canItemStack(actorExistingItem || itemData, this.actor);

if (!canItemStack) {
if (remove && (keepIfZero || type === "currency")) {
this.itemsToNotDelete.add(item.id);
}

if (remove && actorExistingItem) {
this.itemTypeMap.set(actorExistingItem.id, type);
if (!onlyDelta) {
this.itemsToForceDelete.add(actorExistingItem.id);
}
this.itemDeltas.set(actorExistingItem.id, -1);
} else {
if (!itemId) {
itemId = randomID();
}
Utilities.setItemQuantity(itemData, incomingQuantity);
this.itemTypeMap.set(itemId, type)
this.itemsToCreate.push(itemData);
}
if (flags) {
this.itemFlagMap.set(itemId, flags);
}

} else if (actorExistingItem) {
if (actorExistingItem) {

const itemQuantity = Utilities.getItemQuantity(actorExistingItem);

if (itemQuantity > 1 || canItemStack) {

const newQuantity = itemQuantity + incomingQuantity;

const existingItemUpdate = remove
? this.itemsToUpdate.find(item => item._id === itemId)
: Utilities.findSimilarItem(this.itemsToUpdate, itemData);

const existingItemUpdate = remove ? this.itemsToUpdate.find(item => item._id === itemId) : Utilities.findSimilarItem(this.itemsToUpdate, itemData);
if (keepIfZero || type === "currency") {
this.itemsToNotDelete.add(item.id);
}
if (!onlyDelta) {
if (existingItemUpdate) {
const newQuantity = Utilities.getItemQuantity(existingItemUpdate) + incomingQuantity;
Utilities.setItemQuantity(existingItemUpdate, newQuantity);
if (keepIfZero && type !== "currency") {
setProperty(existingItemUpdate, CONSTANTS.FLAGS.ITEM + ".notForSale", newQuantity === 0);
}
} else {
const newQuantity = Utilities.getItemQuantity(actorExistingItem) + incomingQuantity;
const update = Utilities.setItemQuantity(actorExistingItem.toObject(), newQuantity);
if (keepIfZero && type !== "currency") {
setProperty(update, CONSTANTS.FLAGS.ITEM + ".notForSale", newQuantity === 0);
}
this.itemTypeMap.set(actorExistingItem.id, type)
this.itemsToUpdate.push(update);
}

} else if (remove) {

this.itemsToForceDelete.add(actorExistingItem.id);

} else {

itemData._id = randomID();
Utilities.setItemQuantity(itemData, incomingQuantity);
this.itemsToCreate.push(itemData);
this.itemTypeMap.set(itemData._id, type)

}

this.itemDeltas.set(actorExistingItem.id, (this.itemDeltas.has(actorExistingItem.id) ? this.itemDeltas.get(actorExistingItem.id) : 0) + incomingQuantity);

} else {
if (!itemData._id) {

if (!itemData._id || !canItemStack) {
itemData._id = randomID();
}

const existingItemCreation = Utilities.findSimilarItem(this.itemsToCreate, itemData);
if (existingItemCreation) {
if (existingItemCreation && canItemStack) {
const newQuantity = Utilities.getItemQuantity(existingItemCreation) + incomingQuantity;
Utilities.setItemQuantity(existingItemCreation, newQuantity);
} else {
Utilities.setItemQuantity(itemData, incomingQuantity);
this.itemsToCreate.push(itemData);
this.itemTypeMap.set(itemData._id, type)
}
}

if (flags) {
this.itemFlagMap.set(itemData._id, flags);
}

}
}

Expand Down Expand Up @@ -179,9 +182,9 @@ export default class Transaction {

this.itemDeltas = Array.from(this.itemDeltas).map(([id, quantity]) => {
const item = this.actor.items.get(id).toObject();
const itemFlagData = getItemFlagData(item);
const existingFlagData = this.itemFlagMap.get(id) ? getProperty(this.itemFlagMap.get(id), CONSTANTS.SIMPLE_FLAGS.ITEM) : {};
setProperty(item, CONSTANTS.FLAGS.ITEM, foundry.utils.mergeObject(itemFlagData, existingFlagData));
const existingFlagData = getItemFlagData(item);
const newFlagData = this.itemFlagMap.get(id) ?? {};
setProperty(item, CONSTANTS.FLAGS.ITEM, foundry.utils.mergeObject(existingFlagData, newFlagData));
const type = this.itemTypeMap.get(id);
Utilities.setItemQuantity(item, quantity, true);
return { item, quantity, type };
Expand Down
4 changes: 2 additions & 2 deletions src/stores/vault-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ export class VaultStore extends ItemPileStore {

return PrivateAPI._depositWithdrawItem({
source, target, itemData: {
item: itemData, quantity: 1
item: itemData,
}, gridPosition: validPosition
});

Expand Down Expand Up @@ -544,7 +544,7 @@ export class VaultItem extends PileItem {
setProperty(flags, "x", x);
setProperty(flags, "y", y);
setProperty(flags, "flipped", flipped);
setProperty(itemData, CONSTANTS.FLAGS.ITEM, flags);
setProperty(itemData, "flags", flags);

await game.itempiles.API.addItems(this.store.actor, [{
item: itemData, quantity
Expand Down

0 comments on commit 99bed2f

Please sign in to comment.