MC/website/web/item-design-model.mjs

36 lines
4.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {itemTemplateCatalogue} from './item-metadata-policy.mjs';
import {TOOL_TEMPLATES,CRAFTED_ELYTRA_TEMPLATE,elytraTemplate} from './tool-template-policy.mjs';
import {equipmentTemplate} from './equipment-template-policy.mjs';
import {REFIT_UPGRADE_ITEMS} from './organisation-refit-policy.mjs';
import {validateItemLore,TRIM_PATTERNS,TRIM_MATERIALS} from './kit-design-model.mjs';
// One design contract for standalone items and the pieces embedded in a kit.
// A design describes an item; it cannot grant ownership, mint it or run commands.
export const ITEM_DESIGN_VERSION=1;
const catalogue=itemTemplateCatalogue().templates;
const tools=new Map([...TOOL_TEMPLATES,CRAFTED_ELYTRA_TEMPLATE,...REFIT_UPGRADE_ITEMS,...['basic','merged'].map(elytraTemplate)].map(row=>[row.id,row]));
const exact=(row,keys)=>row&&typeof row==='object'&&!Array.isArray(row)&&Object.keys(row).length===keys.length&&keys.every(key=>Object.hasOwn(row,key));
const title=value=>value.replaceAll('_',' ').replace(/\b[a-z]/g,c=>c.toUpperCase());
export function itemDesignTemplate(id){
const metadata=Object.hasOwn(catalogue,id)?catalogue[id]:null;if(!metadata)throw TypeError('Choose a registered item template.');
const armor=/^dr_s1_(common|uncommon|rare|very_rare|legendary|mythic)_mk(\d+)_(helmet|chestplate|leggings|boots)$/.exec(id);
const native=tools.get(id);
const enchantments=armor?equipmentTemplate({quality:metadata.quality,mark:metadata.equipmentMark}).enchantments[armor[3]]:Object.fromEntries(Object.entries(native?.enchantments||{}).map(([key,max])=>[key,{min:1,max}]));
return {id,...metadata,image:'/assets/minecraft-items/'+(id==='refined_spice'?'refined_spice':metadata.material)+'.png',name:native?.name||title(armor?armor[3]:id.replace(/^dr_/,'')),armor:!!armor,enchantments:structuredClone(enchantments)};
}
export function defaultItemDesign(templateId){
const template=itemDesignTemplate(templateId);
return {version:ITEM_DESIGN_VERSION,templateId,quality:template.quality,equipmentMark:template.equipmentMark,itemClass:template.itemClass,name:template.name,lore:[],enchantments:Object.fromEntries(Object.entries(template.enchantments).map(([key,bounds])=>[key,bounds.max])),trim:null};
}
export function validateItemDesign(input){
if(!exact(input,['version','templateId','quality','equipmentMark','itemClass','name','lore','enchantments','trim'])||input.version!==ITEM_DESIGN_VERSION)throw TypeError('Every item needs its template, quality, MK and complete design fields.');
const template=itemDesignTemplate(input.templateId);
if(input.quality!==template.quality||input.equipmentMark!==template.equipmentMark||input.itemClass!==template.itemClass)throw TypeError('Quality, MK and class must match the selected progression template.');
if(typeof input.name!=='string'||input.name.trim().length<1||input.name.length>64||/[<>§\x00-\x1f\x7f]|&[0-9a-fk-orx]/i.test(input.name))throw TypeError('Use an item name of 1–64 plain characters.');
const names=Object.keys(template.enchantments);
if(!exact(input.enchantments,names))throw TypeError('Use only the selected item enchantment profile.');
for(const key of names){const level=input.enchantments[key],{min,max}=template.enchantments[key];if(!Number.isSafeInteger(level)||level<min||level>max)throw TypeError('Enchantment outside the current MK and quality limits: '+key);}
let trim=null;if(input.trim!==null){if(!template.armor||!exact(input.trim,['pattern','material'])||!TRIM_PATTERNS.includes(input.trim.pattern)||!TRIM_MATERIALS.includes(input.trim.material))throw TypeError('Only armour can use a registered trim.');trim={pattern:input.trim.pattern,material:input.trim.material};}
const lore=validateItemLore(input.lore).map(({text,color,bold,italic})=>({text,color,bold,italic}));if(lore.some(line=>/&[0-9a-fk-orx]/i.test(line.text)))throw TypeError('Use the lore colour and formatting fields instead of embedded codes.');
return {version:ITEM_DESIGN_VERSION,templateId:template.id,quality:template.quality,equipmentMark:template.equipmentMark,itemClass:template.itemClass,name:input.name.trim(),lore,enchantments:Object.fromEntries(names.map(key=>[key,input.enchantments[key]])),trim};
}