Skip to content

Commit

Permalink
Limit stackGroup property to consumables and treasure
Browse files Browse the repository at this point in the history
  • Loading branch information
stwlam committed Dec 9, 2023
1 parent 474cf1f commit d89dc23
Show file tree
Hide file tree
Showing 21 changed files with 79 additions and 47 deletions.
4 changes: 3 additions & 1 deletion build/lib/extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,9 @@ class PackExtractor {

if (isPhysicalData(source)) {
delete (source.system as { identification?: unknown }).identification;

if ("stackGroup" in source.system && !source.system.stackGroup) {
delete (source.system as { stackGroup?: unknown }).stackGroup;
}
if (source.type === "consumable" && !source.system.spell) {
delete (source.system as { spell?: unknown }).spell;
}
Expand Down
2 changes: 2 additions & 0 deletions build/run-migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { Migration902DuskwoodDawnsilver } from "@module/migration/migrations/902
import { Migration903PhysicalNumericData } from "@module/migration/migrations/903-physical-numeric-data.ts";
import { Migration904UndercommonToSakvroth } from "@module/migration/migrations/904-undercommon-to-sakvroth.ts";
import { Migration905UnpersistUsage } from "@module/migration/migrations/905-unpersist-usage.ts";
import { Migration906LimitStackGroup } from "@module/migration/migrations/906-limit-stack-group.ts";
// ^^^ don't let your IDE use the index in these imports. you need to specify the full path ^^^

const { window } = new JSDOM();
Expand Down Expand Up @@ -68,6 +69,7 @@ const migrations: MigrationBase[] = [
new Migration903PhysicalNumericData(),
new Migration904UndercommonToSakvroth(),
new Migration905UnpersistUsage(),
new Migration906LimitStackGroup(),
];

const packsDataPath = path.resolve(process.cwd(), "packs");
Expand Down
1 change: 1 addition & 0 deletions src/module/item/armor/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ interface ArmorSystemData
runes: ArmorRuneData;
/** Armor is always worn in the "armor" slot. */
usage: WornUsage;
stackGroup: null;
}

interface ArmorTraits extends PhysicalItemTraits<ArmorTrait> {
Expand Down
7 changes: 5 additions & 2 deletions src/module/item/consumable/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
PhysicalSystemSource,
} from "@item/physical/data.ts";
import { SpellSource } from "@item/spell/data.ts";
import type { ConsumableTrait, OtherConsumableTag } from "./types.ts";
import type { AmmoStackGroup, ConsumableTrait, OtherConsumableTag } from "./types.ts";

type ConsumableSource = BasePhysicalItemSource<"consumable", ConsumableSystemSource>;

Expand Down Expand Up @@ -34,13 +34,16 @@ interface ConsumableSystemSource extends PhysicalSystemSource {
spell: SpellSource | null;

usage: { value: string };
stackGroup: AmmoStackGroup | null;
}

interface ConsumableSystemData
extends Omit<
ConsumableSystemSource,
"bulk" | "hp" | "identification" | "material" | "price" | "temporary" | "usage"
>,
Omit<PhysicalSystemData, "traits"> {}
Omit<PhysicalSystemData, "traits"> {
stackGroup: AmmoStackGroup | null;
}

export type { ConsumableCategory, ConsumableSource, ConsumableSystemData, ConsumableSystemSource, ConsumableTrait };
12 changes: 10 additions & 2 deletions src/module/item/consumable/sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,23 @@ export class ConsumableSheetPF2e extends PhysicalItemSheetPF2e<ConsumablePF2e> {
...sheetData,
consumableTypes: CONFIG.PF2E.consumableTypes,
materialEffects: createSheetTags(CONFIG.PF2E.materialDamageEffects, item.system.material.effects),
stackGroups: this.item.isAmmunition ? R.omit(CONFIG.PF2E.stackGroups, ["coins", "gems", "sacks"]) : null,
stackGroups: this.item.isAmmunition ? R.omit(CONFIG.PF2E.stackGroups, ["coins", "gems"]) : null,
otherTags: createSheetTags(CONFIG.PF2E.otherConsumableTags, item.system.traits.otherTags),
};
}

protected override _updateObject(event: Event, formData: Record<string, unknown>): Promise<void> {
if (formData["system.stackGroup"] === "") {
formData["system.stackGroup"] = null;
}

return super._updateObject(event, formData);
}
}

interface ConsumableSheetData extends PhysicalItemSheetData<ConsumablePF2e> {
consumableTypes: ConfigPF2e["PF2E"]["consumableTypes"];
materialEffects: SheetOptions;
stackGroups: Omit<typeof CONFIG.PF2E.stackGroups, "coins" | "gems" | "sacks"> | null;
stackGroups: Omit<typeof CONFIG.PF2E.stackGroups, "coins" | "gems"> | null;
otherTags: SheetOptions;
}
14 changes: 13 additions & 1 deletion src/module/item/consumable/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
type ConsumableTrait = keyof typeof CONFIG.PF2E.consumableTraits;
type OtherConsumableTag = "herbal";

export type { ConsumableTrait, OtherConsumableTag };
type AmmoStackGroup =
| "arrows"
| "blowgunDarts"
| "bolts"
| "coins"
| "gems"
| "rounds5"
| "rounds10"
| "slingBullets"
| "sprayPellets"
| "woodenTaws";

export type { AmmoStackGroup, ConsumableTrait, OtherConsumableTag };
17 changes: 2 additions & 15 deletions src/module/item/container/sheet.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
import { ItemSheetOptions } from "@item/base/sheet/sheet.ts";
import { PhysicalItemSheetData, PhysicalItemSheetPF2e } from "@item/physical/index.ts";
import * as R from "remeda";
import { PhysicalItemSheetPF2e } from "@item/physical/index.ts";
import type { ContainerPF2e } from "./document.ts";

export class ContainerSheetPF2e extends PhysicalItemSheetPF2e<ContainerPF2e> {
override async getData(options?: Partial<ItemSheetOptions>): Promise<ContainerSheetData> {
return {
...(await super.getData(options)),
stackGroups: R.pick(CONFIG.PF2E.stackGroups, ["sacks"]),
};
}
}

interface ContainerSheetData extends PhysicalItemSheetData<ContainerPF2e> {
stackGroups: { sacks: string };
}
export class ContainerSheetPF2e extends PhysicalItemSheetPF2e<ContainerPF2e> {}
1 change: 1 addition & 0 deletions src/module/item/equipment/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ interface EquipmentSystemData
attribute: AttributeString;
selected: boolean;
};
stackGroup: null;
}

interface EquipmentTraits extends PhysicalItemTraits<EquipmentTrait> {
Expand Down
8 changes: 0 additions & 8 deletions src/module/item/physical/bulk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,6 @@ const STACK_DEFINITIONS: StackDefinitions = {
size: 10,
lightBulk: 1,
},
rations: {
size: 7,
lightBulk: 1,
},
sacks: {
size: 5,
lightBulk: 1,
},
coins: {
size: 1000,
lightBulk: 10,
Expand Down
2 changes: 1 addition & 1 deletion src/module/item/physical/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ interface PhysicalSystemSource extends ItemSystemSource {
price: PartialPrice;
equipped: EquippedData;
identification: IdentificationSource;
stackGroup: string | null;
containerId: string | null;
material: ItemMaterialSource;
size: Size;
Expand Down Expand Up @@ -61,6 +60,7 @@ interface PhysicalSystemData extends PhysicalSystemSource, Omit<ItemSystemData,
temporary: boolean;
identification: IdentificationData;
usage: UsageDetails;
stackGroup: string | null;
}

type Investable<TData extends PhysicalSystemData | PhysicalSystemSource> = TData & {
Expand Down
12 changes: 2 additions & 10 deletions src/module/item/physical/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { MystifiedTraits } from "@item/base/data/values.ts";
import { isCycle } from "@item/container/helpers.ts";
import { Rarity, Size, ZeroToTwo } from "@module/data.ts";
import type { UserPF2e } from "@module/user/document.ts";
import { ErrorPF2e, isObject, sluggify, sortBy } from "@util";
import { ErrorPF2e, isObject, sortBy } from "@util";
import * as R from "remeda";
import { getUnidentifiedPlaceholderImage } from "../identification.ts";
import { Bulk } from "./bulk.ts";
Expand Down Expand Up @@ -220,8 +220,7 @@ abstract class PhysicalItemPF2e<TParent extends ActorPF2e | null = ActorPF2e | n
this.system.material.type ||= null;
this.system.material.grade ||= null;
this.system.material.effects ??= [];
this.system.stackGroup ||= null;
this.system.baseItem ??= sluggify(this.system.stackGroup ?? "") || null;
this.system.stackGroup ??= null;
this.system.hp.brokenThreshold = Math.floor(this.system.hp.max / 2);

// Temporary: prevent noise from items pre migration 746
Expand Down Expand Up @@ -536,13 +535,6 @@ abstract class PhysicalItemPF2e<TParent extends ActorPF2e | null = ActorPF2e | n

handleHPChange(this, changed);

// Avoid setting a `baseItem` or `stackGroup` to an empty string
for (const key of ["baseItem", "stackGroup"] as const) {
if (typeof changed.system[key] === "string") {
changed.system[key] = String(changed.system[key]).trim() || null;
}
}

// Clear 0 price denominations and per fields with values 0 or 1
if (isObject<Record<string, unknown>>(changed.system.price)) {
const price: Record<string, unknown> = changed.system.price;
Expand Down
1 change: 1 addition & 0 deletions src/module/item/shield/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ interface ShieldSystemData
traits: ShieldTraits;
/** Shields are always held. */
usage: HeldUsage;
stackGroup: null;
}

interface IntegratedWeaponData extends IntegratedWeaponSource {
Expand Down
3 changes: 3 additions & 0 deletions src/module/item/treasure/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ type TreasureSource = BasePhysicalItemSource<"treasure", TreasureSystemSource>;
type TreasureSystemSource = Omit<PhysicalSystemSource, "usage"> & {
/** Usage for treasure isn't stored. */
readonly usage?: never;
stackGroup: "coins" | "gems" | null;
};

interface TreasureSystemData extends Omit<PhysicalSystemData, "equipped"> {
equipped: TreasureEquippedData;
/** Treasure need only be on one's person. */
usage: CarriedUsage;

stackGroup: "coins" | "gems" | null;
}

interface TreasureEquippedData extends EquippedData {
Expand Down
8 changes: 8 additions & 0 deletions src/module/item/treasure/sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ export class TreasureSheetPF2e extends PhysicalItemSheetPF2e<TreasurePF2e> {
stackGroups: R.pick(CONFIG.PF2E.stackGroups, ["coins", "gems"]),
};
}

protected override _updateObject(event: Event, formData: Record<string, unknown>): Promise<void> {
if (formData["system.stackGroup"] === "") {
formData["system.stackGroup"] = null;
}

return super._updateObject(event, formData);
}
}

interface TreasureSheetData extends PhysicalItemSheetData<TreasurePF2e> {
Expand Down
1 change: 1 addition & 0 deletions src/module/item/weapon/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ interface WeaponSystemData
usage: WeaponUsageDetails;
graspingAppendage: boolean;
meleeUsage?: Required<ComboWeaponMeleeUsage>;
stackGroup: null;
}

type WeaponUsageDetails = UsageDetails & Required<WeaponSystemSource["usage"]>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ItemSourcePF2e } from "@item/base/data/index.ts";
import { isPhysicalData } from "@item/base/data/helpers.ts";
import { ItemSourcePF2e } from "@item/base/data/index.ts";
import { Size, SIZES, ValueAndMax } from "@module/data.ts";
import { MigrationBase } from "../base.ts";

Expand Down Expand Up @@ -104,7 +104,7 @@ type MaybeOldSystemSource = {
hp: ValueAndMax & { brokenThreshold?: number };
quantity: number | { value: number };
hardness: number | { value: number };
stackGroup: string | null | { value: string | null };
stackGroup?: string | null | { value: string | null };
containerId: string | null | { value: string | null };
size: Size | { value: Size };
temporary?: boolean | { value: boolean };
Expand Down
19 changes: 19 additions & 0 deletions src/module/migration/migrations/906-limit-stack-group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ItemSourcePF2e, isPhysicalData } from "@item/base/data/index.ts";
import { itemIsOfType } from "@item/helpers.ts";
import { MigrationBase } from "../base.ts";

/** Limit `stackGroup` property to consumables and treasure */
export class Migration906LimitStackGroup extends MigrationBase {
static override version = 0.906;

override async updateItem(source: MaybeWithToBeDeletedStackGroup): Promise<void> {
const toDelete = !isPhysicalData(source) || !itemIsOfType(source, "consumable", "treasure");
if (toDelete && "stackGroup" in source.system) {
source.system["-=stackGroup"] = null;
} else if (itemIsOfType(source, "consumable", "treasure")) {
source.system.stackGroup = source.system.slug === "rations" ? null : source.system.stackGroup || null;
}
}
}

type MaybeWithToBeDeletedStackGroup = ItemSourcePF2e & { system: { "-=stackGroup"?: null } };
1 change: 1 addition & 0 deletions src/module/migration/migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,4 @@ export { Migration902DuskwoodDawnsilver } from "./902-duskwood-dawnsilver.ts";
export { Migration903PhysicalNumericData } from "./903-physical-numeric-data.ts";
export { Migration904UndercommonToSakvroth } from "./904-undercommon-to-sakvroth.ts";
export { Migration905UnpersistUsage } from "./905-unpersist-usage.ts";
export { Migration906LimitStackGroup } from "./906-limit-stack-group.ts";
2 changes: 1 addition & 1 deletion src/module/migration/runner/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface CollectionDiff<T extends foundry.documents.ActiveEffectSource | ItemSo
export class MigrationRunnerBase {
migrations: MigrationBase[];

static LATEST_SCHEMA_VERSION = 0.905;
static LATEST_SCHEMA_VERSION = 0.906;

static MINIMUM_SAFE_VERSION = 0.634;

Expand Down
2 changes: 0 additions & 2 deletions src/scripts/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,6 @@ export const PF2ECONFIG = {
gems: "PF2E.StackGroupGems",
rounds5: "PF2E.StackGroupRounds5",
rounds10: "PF2E.StackGroupRounds10",
rations: "PF2E.StackGroupRations",
sacks: "PF2E.StackGroupSacks",
slingBullets: "PF2E.StackGroupSlingBullets",
sprayPellets: "PF2E.StackGroupSprayPellets",
woodenTaws: "PF2E.StackGroupWoodenTaws",
Expand Down
5 changes: 3 additions & 2 deletions static/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,6 @@
"equipped": {
"carryType": "worn"
},
"stackGroup": null,
"containerId": null,
"size": "med",
"material": {
Expand Down Expand Up @@ -657,7 +656,8 @@
"templates": [
"common",
"physical"
]
],
"stackGroup": null
},
"weapon": {
"templates": [
Expand Down Expand Up @@ -796,6 +796,7 @@
"usage": {
"value": "held-in-one-hand"
},
"stackGroup": null,
"spell": null
},
"equipment": {
Expand Down

0 comments on commit d89dc23

Please sign in to comment.