diff --git a/tools/generate-native-tools.mjs b/tools/generate-native-tools.mjs index f9bd599c..9600b4c5 100644 --- a/tools/generate-native-tools.mjs +++ b/tools/generate-native-tools.mjs @@ -5,17 +5,22 @@ import {TOOL_TEMPLATES} from '../website/control-panel/public/tool-template-poli import {REFIT_UPGRADE_ITEMS} from '../website/control-panel/public/organisation-refit-policy.mjs'; import {EQUIPMENT_QUALITY_STYLES} from '../website/control-panel/public/equipment-template-policy.mjs'; import {itemRoman} from '../website/control-panel/public/item-lore-model.mjs'; +import {defaultItemDesign,validateItemDesign} from '../website/control-panel/public/item-design-model.mjs'; + +const hex=value=>'&x'+value.slice(1).split('').map(character=>'&'+character).join(''); export function nativeToolDefinition(input,{minecraftVersion='1.21.8'}={}){ - const template=[...TOOL_TEMPLATES,...REFIT_UPGRADE_ITEMS].find(row=>row.id===input?.id); + const template=[...TOOL_TEMPLATES,...REFIT_UPGRADE_ITEMS].find(row=>row.id===(input?.templateId||input?.id)); if(!template)throw TypeError('Choose a registered native tool or upgrade template.'); if(!/^1\.21\.(8|9|10|11)$/.test(minecraftVersion))throw TypeError('Review the native material registry before targeting another Minecraft version.'); if(template.material.startsWith('copper_')&&minecraftVersion==='1.21.8')throw TypeError('Copper tools require Minecraft 1.21.9 or newer.'); const style=EQUIPMENT_QUALITY_STYLES[template.quality]; if(!style||!Number.isInteger(template.equipmentMark)||template.equipmentMark<1||template.equipmentMark>14||!/^dr_[a-z0-9_]+$/.test(template.id))throw TypeError('Registered quality, MK and definition ID required.'); - const enchantments=Object.fromEntries(Object.entries(template.enchantments||{}).map(([enchantment,level],index)=>['enchantment'+(index+1),{enchantment,level}])); - const config={name:style.code+'&l'+template.name,material:template.material.toUpperCase(), - lore:[style.code+style.label.toUpperCase()+' &8| '+style.code+'&oMK '+itemRoman(template.equipmentMark),'&8S1 | '+(template.purpose?'UPGRADE COMPONENT':template.itemClass||'EQUIPMENT').toUpperCase(),'','&7&o'+(template.purpose?'Return this template with your original item to an organisation workshop.':'Forged for the long desert.')], + const design=input?.version===1?validateItemDesign(input):defaultItemDesign(template.id); + const enchantments=Object.fromEntries(Object.entries(design.enchantments).map(([enchantment,level],index)=>['enchantment'+(index+1),{enchantment,level}])); + const lore=design.lore.length?design.lore.map(line=>hex(line.color)+(line.bold?'&l':'')+(line.italic?'&o':'')+line.text):['&7&o'+(template.purpose?'Return this template with your original item to an organisation workshop.':'Forged for the long desert.')]; + const config={name:style.code+'&l'+design.name,material:template.material.toUpperCase(), + lore:[style.code+style.label.toUpperCase()+' &8| '+style.code+'&oMK '+itemRoman(template.equipmentMark),'&8S1 | '+(template.purpose?'UPGRADE COMPONENT':template.itemClass||'EQUIPMENT').toUpperCase(),'',...lore], glow:template.equipmentMark>=9,disableStack:!template.purpose,keepItemOnDeath:false,storeItemInfo:false,canBeUsedOnlyByTheOwner:false,cancelEventIfNoPermission:false,keepDefaultAttributes:true,ignoreKeepDefaultAttributesFeature:false, config_5:true,config_update:false,usageFeatures:{usage:-1,isRefreshableClean:false,usageLimit:-1},hiders:{hideAttributes:false,hideEnchantments:false,hideUsage:true},giveFirstJoinFeatures:{giveFirstJoin:false},itemRarity:{enableRarity:false,rarity:'UNCOMMON'},enchantments,activators:{},variables:{}}; return {id:template.id,quality:template.quality,equipmentMark:template.equipmentMark,config}; diff --git a/website/control-panel/public/item-design-model.mjs b/website/control-panel/public/item-design-model.mjs new file mode 100644 index 00000000..ddbe68c7 --- /dev/null +++ b/website/control-panel/public/item-design-model.mjs @@ -0,0 +1,36 @@ +import {itemTemplateCatalogue} from './item-metadata-policy.mjs'; +import {TOOL_TEMPLATES,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,...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,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)||levelmax)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={...input.trim};} + const lore=validateItemLore(input.lore);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 {...input,name:input.name.trim(),lore,enchantments:{...input.enchantments},trim}; +} diff --git a/website/tests/item-design-model.test.mjs b/website/tests/item-design-model.test.mjs new file mode 100644 index 00000000..2f29aef5 --- /dev/null +++ b/website/tests/item-design-model.test.mjs @@ -0,0 +1,18 @@ +import test from 'node:test';import assert from 'node:assert/strict'; +import {defaultItemDesign,itemDesignTemplate,validateItemDesign} from '../control-panel/public/item-design-model.mjs'; + +test('a standalone item and a kit piece use the same required quality, MK and bounded enchantments',()=>{ + for(const id of ['dr_s1_spice_shovel_mk1','dr_s1_sword_mk14','dr_s1_legendary_mk14_helmet','refined_spice','dr_imperial_codex']){ + const item=defaultItemDesign(id);assert.deepEqual(validateItemDesign(item),item);for(const field of ['quality','equipmentMark','itemClass']){const incomplete={...item};delete incomplete[field];assert.throws(()=>validateItemDesign(incomplete));} + } +}); +test('changing a submitted bound, class, material or command cannot create a stronger item',()=>{ + const item=defaultItemDesign('dr_s1_legendary_mk14_helmet'); + for(const patch of [{enchantments:{...item.enchantments,protection:255}},{equipmentMark:15},{quality:'mythic'},{itemClass:'relic'},{material:'bedrock'},{commands:['op player']},{enchantments:{...item.enchantments,fortune:7}}])assert.throws(()=>validateItemDesign({...item,...patch})); + assert.equal(itemDesignTemplate(item.templateId).enchantments.protection.max,6); +}); +test('designed lore and armour trim survive validation without changing tools or embedded native behaviours',()=>{ + const item=defaultItemDesign('dr_s1_legendary_mk14_chestplate');item.trim={pattern:'dune',material:'gold'};item.lore=[{text:'Forged for the long desert.',color:'#ffaa00',bold:false,italic:true}];const saved=validateItemDesign(item);assert.equal(saved.lore[0].color,'#FFAA00');assert.deepEqual(saved.trim,item.trim); + assert.throws(()=>validateItemDesign({...defaultItemDesign('dr_s1_spice_shovel_mk1'),trim:item.trim})); + assert.throws(()=>validateItemDesign({...item,lore:[{text:'