More WIP on 0.8.x migration

This commit is contained in:
Johannes Loher 2021-06-30 03:53:52 +02:00
parent ef01698178
commit 6b39284164
16 changed files with 427 additions and 321 deletions

View file

@ -5,12 +5,18 @@
import { ModifiableDataBaseTotal } from "../common/common-data";
import { DS4 } from "../config";
import { DS4Item } from "../item/item";
import { ItemType } from "../item/item-data";
import { DS4ArmorPreparedData, DS4ShieldPreparedData } from "../item/item-prepared-data";
import { ItemType } from "../item/item-data-source";
import { DS4ArmorDataProperties, DS4ShieldDataProperties } from "../item/item-data-properties";
import { createCheckRoll } from "../rolls/check-factory";
import { isAttribute, isTrait } from "./actor-data-source";
import { Check } from "./actor-data-properties";
import { DS4Item } from "../item/item";
declare global {
interface DocumentClassConfig {
Actor: typeof DS4Actor;
}
}
/**
* The Actor class for DS4
@ -84,9 +90,9 @@ export class DS4Actor extends Actor {
(changes: (foundry.data.ActiveEffectData["changes"][number] & { effect: ActiveEffect })[], e) => {
if (e.data.disabled) return changes;
const item = this.getOriginatingItemOfActiveEffect(e);
if (item?.isNonEquippedEuipable()) return changes; // TODO: DS4Item
if (item?.isNonEquippedEuipable()) return changes;
const factor = item?.activeEffectFactor ?? 1; // TODO: DS4Item
const factor = item?.activeEffectFactor ?? 1;
const newChanges = e.data.changes.filter(predicate).flatMap((c) => {
const changeSource = c.toObject();
@ -110,8 +116,7 @@ export class DS4Actor extends Actor {
this.overrides = expandObject({ ...flattenObject(this.overrides), ...overrides });
}
// TODO: returns DS4Item | undefined
protected getOriginatingItemOfActiveEffect(effect: ActiveEffect): Item | undefined {
protected getOriginatingItemOfActiveEffect(effect: ActiveEffect): DS4Item | undefined {
return this.items.find((item) => item.uuid === effect.data.origin);
}
@ -272,7 +277,6 @@ export class DS4Actor extends Actor {
* Handle how changes to a Token attribute bar are applied to the Actor.
* This only differs from the base implementation by also allowing negative values.
* @override
* TODO: Adjust return type
*/
async modifyTokenAttribute(
attribute: string,

View file

@ -9,7 +9,7 @@ import { ModifiableDataBaseTotal } from "../../common/common-data";
import { DS4 } from "../../config";
import { getCanvas } from "../../helpers";
import { DS4Item } from "../../item/item";
import { DS4ItemData } from "../../item/item-data";
import { DS4ItemData } from "../../item/item-data-source";
import { getDS4Settings } from "../../settings";
import notifications from "../../ui/notifications";
import { DS4Actor } from "../actor";
@ -18,13 +18,13 @@ import { isCheck } from "../actor-data-properties";
/**
* The base Sheet class for all DS4 Actors
*/
export class DS4ActorSheet extends ActorSheet<ActorSheet.Data<DS4Actor>> {
// TODO(types): Improve mergeObject in upstream so that it isn't necessary to provide all parameters (see https://github.com/League-of-Foundry-Developers/foundry-vtt-types/issues/272)
export class DS4ActorSheet extends ActorSheet<ActorSheet.Options> {
/** @override */
static get defaultOptions(): BaseEntitySheet.Options {
const superDefaultOptions = super.defaultOptions;
return mergeObject(superDefaultOptions, {
...superDefaultOptions,
static get defaultOptions(): ActorSheet.Options {
// TODO: Improve
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return foundry.utils.mergeObject(super.defaultOptions, {
classes: ["ds4", "sheet", "actor"],
height: 620,
scrollY: [
@ -105,7 +105,7 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Data<DS4Actor>> {
html.find(".item-edit").on("click", (ev) => {
const li = $(ev.currentTarget).parents(".item");
const id = li.data("itemId");
const item = this.actor.getOwnedItem(id);
const item = this.actor.getEmbeddedDocument("Item", id) as DS4Item; // TODO: Improve in upstream
if (!item) {
throw new Error(game.i18n.format("DS4.ErrorActorDoesNotHaveItem", { id, actor: this.actor.name }));
}
@ -118,7 +118,7 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Data<DS4Actor>> {
// Delete Inventory Item
html.find(".item-delete").on("click", (ev) => {
const li = $(ev.currentTarget).parents(".item");
this.actor.deleteOwnedItem(li.data("itemId"));
this.actor.deleteEmbeddedDocuments("Item", [li.data("itemId")]);
li.slideUp(200, () => this.render(false));
});
@ -133,23 +133,21 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Data<DS4Actor>> {
* Handle creating a new Owned Item for the actor using initial data defined in the HTML dataset
* @param event - The originating click event
*/
protected _onItemCreate(event: JQuery.ClickEvent): Promise<DS4ItemData> {
protected _onItemCreate(event: JQuery.ClickEvent): void {
event.preventDefault();
const header = event.currentTarget;
// Get the type of item to create.
// Grab any data associated with this control.
const { type, ...data } = duplicate(header.dataset);
// Initialize a default name.
const name = `New ${type.capitalize()}`;
// Prepare the item object.
const itemData = {
name: name,
type: type,
data: data,
};
// Finally, create the item!
return this.actor.createOwnedItem(itemData);
DS4Item.create(itemData, { parent: this.actor });
}
/**
@ -162,7 +160,8 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Data<DS4Actor>> {
ev.preventDefault();
const el: HTMLFormElement = $(ev.currentTarget).get(0);
const id = $(ev.currentTarget).parents(".item").data("itemId");
const item = duplicate(this.actor.getOwnedItem(id));
const item = this.actor.getEmbeddedDocument("Item", id) as DS4Item; // TODO: Improve in upstream
const itemObject = item.toObject();
const property: string | undefined = $(ev.currentTarget).data("property");
// Early return:
@ -175,8 +174,8 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Data<DS4Actor>> {
// Set new value
const newValue = this.getValue(el);
setProperty(item, property, newValue);
this.actor.updateOwnedItem(item);
setProperty(itemObject, property, newValue);
this.actor.updateEmbeddedDocuments("Item", [{ ...itemObject }]); // TODO: Improve in upstream
}
/**
@ -241,7 +240,7 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Data<DS4Actor>> {
protected _onRollItem(event: JQuery.ClickEvent): void {
event.preventDefault();
const id = $(event.currentTarget).parents(".item").data("itemId");
const item = this.actor.getOwnedItem(id);
const item = this.actor.getEmbeddedDocument("Item", id, { strict: true }) as DS4Item; // TODO: improve in upstream types
item.roll().catch((e) => notifications.error(e, { log: true }));
}
@ -277,14 +276,7 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Data<DS4Actor>> {
}
/** @override */
protected async _onDropItem(
event: DragEvent,
data: { type: "Item" } & (
| { data: DeepPartial<ActorSheet.OwnedItemData<DS4Actor>> }
| { pack: string }
| { id: string }
),
): Promise<boolean | undefined | ActorSheet.OwnedItemData<DS4Actor>> {
protected async _onDropItem(event: DragEvent, data: ActorSheet.DropData.Item): Promise<unknown> {
const item = await DS4Item.fromDropData(data);
if (item && !this.actor.canOwnItemType(item.data.type)) {
notifications.warn(