Skip to content

Commit

Permalink
Merge pull request #831 from kgar/dnd5e-4.1.x-compat-2nd-pass
Browse files Browse the repository at this point in the history
Dnd5e 4.1.x compat 2nd pass
  • Loading branch information
kgar authored Nov 12, 2024
2 parents 8dd3358 + c4a126c commit f12e0d4
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 55 deletions.
37 changes: 23 additions & 14 deletions src/foundry/foundry-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -861,8 +861,8 @@ export const FoundryAdapter = {
browseFilePicker(...args: any[]) {
return new FilePicker(...args).browse();
},
renderArmorConfig(actor: any) {
return new dnd5e.applications.actor.ActorArmorConfig(actor).render(true);
renderArmorConfig(document: any) {
return new dnd5e.applications.actor.ArmorClassConfig({ document }).render(true);
},
renderInitiativeConfig(document: any) {
return new dnd5e.applications.actor.InitiativeConfig({
Expand Down Expand Up @@ -1249,21 +1249,30 @@ export const FoundryAdapter = {
*/
onDropStackConsumablesForActor(
actor: Actor5e,
itemData: any
itemData: any,
{ container = null }: { container: any | null },
event: DragEvent
): Promise<Item5e> | null {
// TODO: Move this to the base actor sheet in app V2 when all actors go App V2.
const droppedSourceId =
itemData._stats?.compendiumSource ?? itemData.flags.core?.sourceId;
if (itemData.type !== 'consumable' || !droppedSourceId) return null;
const similarItem = actor.items.find((i: Item5e) => {
const sourceId = i.getFlag('core', 'sourceId');
return (
sourceId &&
sourceId === droppedSourceId &&
i.type === 'consumable' &&
i.name === itemData.name
);
});
if (!similarItem) return null;

if (itemData.type !== 'consumable' || !droppedSourceId) {
return null;
}

const similarItem = actor.sourcedItems
.get(droppedSourceId, { legacy: false })
?.filter(
(i: Item5e) =>
i.system.container === container && i.name === itemData.name
)
?.first();

if (!similarItem) {
return null;
}

return similarItem.update({
'system.quantity':
similarItem.system.quantity + Math.max(itemData.system.quantity, 1),
Expand Down
55 changes: 26 additions & 29 deletions src/mixins/Tidy5eActorSheetBaseMixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ export function Tidy5eActorSheetBaseMixin(BaseApplication: any) {
Tidy5eActorSheetBase.ACTOR_ACTIONS_AND_CONTROLS.configureToken.control
.action
);
if (configureTokenControl) {
configureTokenControl.label = this.token
? 'Token'
: 'TOKEN.TitlePrototype';
if (configureTokenControl && this.token) {
configureTokenControl.label = 'Token';
configureTokenControl.icon = 'far fa-user-circle';
} else {
configureTokenControl.label = 'TOKEN.TitlePrototype';
}
return controls;
}
Expand Down Expand Up @@ -250,13 +251,16 @@ export function Tidy5eActorSheetBaseMixin(BaseApplication: any) {
);
}

return this._onDropItemCreate(item);
return this._onDropItemCreate(item, event);
}

/**
* Handle the final creation of dropped Item data on the Actor.
*/
async _onDropItemCreate(itemData: Item5e[] | Item5e): Promise<Item5e[]> {
async _onDropItemCreate(
itemData: Item5e[] | Item5e,
event: DragEvent
): Promise<Item5e[]> {
let items = itemData instanceof Array ? itemData : [itemData];
const itemsWithoutAdvancement = items.filter(
(i) => !i.system.advancement?.length
Expand All @@ -283,7 +287,7 @@ export function Tidy5eActorSheetBaseMixin(BaseApplication: any) {
// Create the owned items & contents as normal
const toCreate = await dnd5e.documents.Item5e.createWithContents(items, {
transformFirst: (item: Item5e) =>
this._onDropSingleItem(item.toObject()),
this._onDropSingleItem(item.toObject(), event),
});

return dnd5e.documents.Item5e.createDocuments(toCreate, {
Expand All @@ -296,7 +300,10 @@ export function Tidy5eActorSheetBaseMixin(BaseApplication: any) {
/**
* Handles dropping of a single item onto this character sheet.
*/
async _onDropSingleItem(itemData: any): Promise<object | boolean> {
async _onDropSingleItem(
itemData: any,
event: DragEvent
): Promise<object | boolean> {
const isSupportedItemType =
this._supportedItemTypes.size === 0 ||
this._supportedItemTypes.has(itemData.type);
Expand Down Expand Up @@ -349,7 +356,7 @@ export function Tidy5eActorSheetBaseMixin(BaseApplication: any) {
this._onDropResetData(itemData);

// Stack identical consumables
const stacked = this._onDropStackConsumables(itemData);
const stacked = this._onDropStackConsumables(itemData, {}, event);
if (stacked) return false;

// Bypass normal creation flow for any items with advancement
Expand Down Expand Up @@ -404,25 +411,15 @@ export function Tidy5eActorSheetBaseMixin(BaseApplication: any) {
*/
_onDropStackConsumables(
itemData: any,
{ container = null } = {}
{ container = null } = {},
event: DragEvent
): Promise<Item5e> | null {
const droppedSourceId =
itemData._stats?.compendiumSource ?? itemData.flags.core?.sourceId;
if (itemData.type !== 'consumable' || !droppedSourceId) return null;
const similarItem = this.actor.items.find((i: Item5e) => {
const sourceId = i._stats?.compendiumSource;
return (
sourceId &&
sourceId === droppedSourceId &&
i.type === 'consumable' &&
i.name === itemData.name
);
});
if (!similarItem) return null;
return similarItem.update({
'system.quantity':
similarItem.system.quantity + Math.max(itemData.system.quantity, 1),
});
return FoundryAdapter.onDropStackConsumablesForActor(
this.actor,
itemData,
{ container },
event
);
}

/**
Expand Down Expand Up @@ -516,7 +513,7 @@ export function Tidy5eActorSheetBaseMixin(BaseApplication: any) {
}

async _onDropFolder(
_event: DragEvent & { currentTarget: HTMLElement },
event: DragEvent & { currentTarget: HTMLElement },
data: Record<string, any>
): Promise<object | boolean | undefined> {
if (!this.actor.isOwner) {
Expand All @@ -536,7 +533,7 @@ export function Tidy5eActorSheetBaseMixin(BaseApplication: any) {
})
);

return this._onDropItemCreate(droppedItemData);
return this._onDropItemCreate(droppedItemData, event);
}

/* -------------------------------------------- */
Expand Down
8 changes: 8 additions & 0 deletions src/scss/partials/_reset-v2.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
--button-hover-background-color: var(--t5e-faint-color);
}

input[type='checkbox'] {
margin: 0;
}

label.checkbox > input[type='checkbox'],
input[type='checkbox'] {
--input-height: 1.625rem;
Expand Down Expand Up @@ -48,6 +52,10 @@
--input-background-color: var(--t5e-faintest-color);
}

select {
width: unset;
}

input[type='text']:focus,
input[type='number']:focus,
input[type='password']:focus,
Expand Down
4 changes: 2 additions & 2 deletions src/sheets/Tidy5eCharacterSheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,7 @@ export class Tidy5eCharacterSheet
return this._onLongRest(event);
}

async _onDropSingleItem(itemData: any) {
async _onDropSingleItem(itemData: any, event: DragEvent) {
// Create a Consumable spell scroll on the Inventory tab
if (
itemData.type === CONSTANTS.ITEM_TYPE_SPELL &&
Expand All @@ -1318,7 +1318,7 @@ export class Tidy5eCharacterSheet
return scroll.toObject();
}

return super._onDropSingleItem(itemData);
return super._onDropSingleItem(itemData, event);
}

close(options: unknown = {}) {
Expand Down
15 changes: 11 additions & 4 deletions src/sheets/Tidy5eContainerSheetClassic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { DocumentTabSectionConfigApplication } from 'src/applications/section-co
import { TabManager } from 'src/runtime/tab/TabManager';
import { TidyHooks } from 'src/foundry/TidyHooks';
import { SettingsProvider } from 'src/settings/settings';
import { Inventory } from 'src/features/sections/Inventory';

export class Tidy5eContainerSheetClassic extends DragAndDropMixin(
SvelteApplicationMixin<ContainerSheetClassicContext>(
Expand Down Expand Up @@ -485,7 +486,11 @@ export class Tidy5eContainerSheetClassic extends DragAndDropMixin(
}

// If item already exists in same DocumentCollection, just adjust its container property
if (item.actor === this.item.actor && item.pack === this.item.pack) {
if (
item.actor === this.item.actor &&
item.pack === this.item.pack &&
Inventory.getDefaultInventoryTypes().includes(item.type)
) {
return item.update({
folder: this.item.folder,
'system.container': this.item.id,
Expand Down Expand Up @@ -535,7 +540,7 @@ export class Tidy5eContainerSheetClassic extends DragAndDropMixin(
createOptions.flags = itemData.flags;
}

const scroll = dnd5e.documents.Item5e.createScrollFromSpell(
const scroll = await dnd5e.documents.Item5e.createScrollFromSpell(
itemData,
createOptions
);
Expand All @@ -544,9 +549,11 @@ export class Tidy5eContainerSheetClassic extends DragAndDropMixin(
}

if (this.item.actor && container === this.item.id) {
const result = await this.item.actor.sheet._onDropStackConsumables(
const result = await FoundryAdapter.onDropStackConsumablesForActor(
this.actor,
itemData,
{ container }
{ container },
event
);
if (result) return false;
}
Expand Down
6 changes: 3 additions & 3 deletions src/sheets/Tidy5eKgarVehicleSheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ export class Tidy5eVehicleSheet
}
}

async _onDropSingleItem(itemData: any) {
async _onDropSingleItem(itemData: any, event: DragEvent) {
const cargoTypes = [
'weapon',
'equipment',
Expand Down Expand Up @@ -779,13 +779,13 @@ export class Tidy5eVehicleSheet
}

if (itemData.type === 'consumable') {
return super._onDropSingleItem(itemData);
return super._onDropSingleItem(itemData, event);
}

// Skip the default vehicle sheet handler, as we are handling all use cases.
const baseActor5eClass = getBaseActorSheet5e(this);
if (baseActor5eClass) {
return baseActor5eClass._onDropSingleItem.call(this, itemData);
return baseActor5eClass._onDropSingleItem.call(this, itemData, event);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/sheets/Tidy5eNpcSheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ export class Tidy5eNpcSheet
return this._onToggleAbilityProficiency(event);
}

async _onDropSingleItem(itemData: any) {
async _onDropSingleItem(itemData: any, event: DragEvent) {
// Create a Consumable spell scroll on the Inventory tab
if (
itemData.type === CONSTANTS.ITEM_TYPE_SPELL &&
Expand All @@ -1079,7 +1079,7 @@ export class Tidy5eNpcSheet
return scroll.toObject();
}

return super._onDropSingleItem(itemData);
return super._onDropSingleItem(itemData, event);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/sheets/item/tabs/ItemWeaponDetailsTab.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@

<!-- Weapon Mastery -->
<div class="form-group">
<label for="">{localize('DND5E.WEAPON.FIELDS.mastery.label')}</label>
<label for="{appId}-weapon-mastery">
{localize('DND5E.WEAPON.FIELDS.mastery.label')}
</label>
<Select
id="{appId}-weapon-mastery"
document={$context.item}
field="system.mastery"
value={$context.source.mastery}
Expand Down

0 comments on commit f12e0d4

Please sign in to comment.