145 lines
No EOL
3.1 KiB
JavaScript
145 lines
No EOL
3.1 KiB
JavaScript
import { ItemHandler } from "../Item/Handler.mjs";
|
|
|
|
/** @this {Actor} */
|
|
async function genericEmbeddedDelete($event) {
|
|
let data = $event.currentTarget.dataset;
|
|
let item = await fromUuid(data.embeddedId);
|
|
|
|
if (!item) {
|
|
ui.notifications.error(
|
|
`dotdungeon.notification.error.item-not-found`,
|
|
{ console: false }
|
|
);
|
|
return;
|
|
};
|
|
|
|
Dialog.confirm({
|
|
title: game.i18n.format(
|
|
`dotdungeon.dialogs.${item.type}.delete.title`,
|
|
item
|
|
),
|
|
content: game.i18n.format(
|
|
`dotdungeon.dialogs.${item.type}.delete.content`,
|
|
item
|
|
),
|
|
yes: () => {
|
|
item.delete();
|
|
},
|
|
defaultYes: false,
|
|
});
|
|
};
|
|
|
|
/** @this {Actor} */
|
|
async function createCustomItem(defaults, opts = {}) {
|
|
let items = await this.createEmbeddedDocuments(`Item`, defaults);
|
|
if (items.length == 0) {
|
|
throw new Error();
|
|
};
|
|
this.sheet.render();
|
|
if (
|
|
game.settings.get(`dotdungeon`, `openEmbeddedOnCreate`)
|
|
&& !opts.overrideSheetOpen
|
|
) {
|
|
for (const item of items) {
|
|
item.sheet.render(true);
|
|
};
|
|
};
|
|
};
|
|
|
|
/** @this {Actor} */
|
|
async function createCustomUntyped() {
|
|
await createCustomItem.bind(this)([{
|
|
type: `untyped`,
|
|
name: game.i18n.format(`dotdungeon.defaults.untyped.name`),
|
|
}]);
|
|
};
|
|
|
|
/** @this {Actor} */
|
|
async function createCustomAspect() {
|
|
await createCustomItem.bind(this)([{
|
|
type: `aspect`,
|
|
name: game.i18n.format(`dotdungeon.defaults.aspect.name`),
|
|
}]);
|
|
};
|
|
|
|
/** @this {Actor} */
|
|
async function createCustomSpell() {
|
|
await createCustomItem.bind(this)([{
|
|
type: `spell`,
|
|
name: game.i18n.format(`dotdungeon.defaults.spell.name`),
|
|
}]);
|
|
};
|
|
|
|
/** @this {Actor} */
|
|
async function createCustomPet() {
|
|
const body = new URLSearchParams({
|
|
number: 1,
|
|
animal: `Cat`,
|
|
"X-Requested-With": "fetch"
|
|
})
|
|
const r = await fetch(
|
|
`https://randommer.io/pet-names`,
|
|
{
|
|
method: "POST",
|
|
body
|
|
}
|
|
);
|
|
await createCustomItem.bind(this)([{
|
|
type: `pet`,
|
|
name: (await r.json())[0] ?? game.i18n.localize(`dotdungeon.defaults.pet.name`),
|
|
}]);
|
|
};
|
|
|
|
/** @this {Actor} */
|
|
async function atAspectLimit() {
|
|
let limit = game.settings.get(`dotdungeon`, `aspectLimit`);
|
|
return this.itemTypes.aspect.length >= limit;
|
|
};
|
|
|
|
/**
|
|
* @param {ItemHandler} item
|
|
* @this {Actor}
|
|
*/
|
|
async function preAspectEmbed(item) {
|
|
if (await atAspectLimit.bind(this)()) {
|
|
ui.notifications.error(
|
|
game.i18n.format(
|
|
`dotdungeon.notification.error.aspect-limit-reached`,
|
|
{ limit: game.settings.get(`dotdungeon`, `aspectLimit`) }
|
|
),
|
|
{ console: false }
|
|
);
|
|
return false;
|
|
};
|
|
};
|
|
|
|
/**
|
|
* @param {ItemHandler} item
|
|
* @this {Actor}
|
|
*/
|
|
async function preUntypedEmbed(item) {
|
|
let inventoryItem = this.itemTypes.untyped.find(i => i.name === item.name);
|
|
if (inventoryItem) {
|
|
inventoryItem.update({"system.quantity": inventoryItem.system.quantity + 1});
|
|
ui.notifications.info(
|
|
game.i18n.format(
|
|
`dotdungeon.notification.info.increased-item-quantity`,
|
|
{ name: inventoryItem.name }
|
|
),
|
|
{ console: false }
|
|
);
|
|
return false;
|
|
};
|
|
};
|
|
|
|
export default {
|
|
atAspectLimit,
|
|
createCustomItem,
|
|
createCustomUntyped,
|
|
createCustomAspect,
|
|
createCustomSpell,
|
|
createCustomPet,
|
|
genericEmbeddedDelete,
|
|
preAspectEmbed,
|
|
preUntypedEmbed,
|
|
}; |