Share item design validation and native export across item and kit editors
This commit is contained in:
parent
37aea6a5e7
commit
e0c6e9992a
4 changed files with 71 additions and 4 deletions
|
|
@ -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};
|
||||
|
|
|
|||
36
website/control-panel/public/item-design-model.mjs
Normal file
36
website/control-panel/public/item-design-model.mjs
Normal file
|
|
@ -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)||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={...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};
|
||||
}
|
||||
18
website/tests/item-design-model.test.mjs
Normal file
18
website/tests/item-design-model.test.mjs
Normal file
|
|
@ -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:'<script>',color:'#FFFFFF',bold:false,italic:false}]}));
|
||||
});
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
import test from 'node:test';import assert from 'node:assert/strict';
|
||||
import {nativeToolCatalogue,nativeToolDefinition} from '../../tools/generate-native-tools.mjs';
|
||||
import {toolTemplate} from '../control-panel/public/tool-template-policy.mjs';
|
||||
import {defaultItemDesign} from '../control-panel/public/item-design-model.mjs';
|
||||
test('native tool export uses the actual server version and never substitutes copper materials',()=>{
|
||||
const old=nativeToolCatalogue({minecraftVersion:'1.21.8'}),modern=nativeToolCatalogue({minecraftVersion:'1.21.9'});
|
||||
assert.equal(old.unavailable.length,10);assert.equal(modern.unavailable.length,0);assert.equal(old.definitions.length+old.unavailable.length,modern.definitions.length);
|
||||
|
|
@ -8,6 +9,13 @@ test('native tool export uses the actual server version and never substitutes co
|
|||
assert.throws(()=>nativeToolDefinition(toolTemplate('shovel',5),{minecraftVersion:'1.21.8'}),/Copper tools/);
|
||||
assert.equal(modern.definitions.find(d=>d.id==='dr_s1_spice_shovel_mk5').config.material,'COPPER_SHOVEL');
|
||||
});
|
||||
|
||||
test('the native item engine receives the same validated name, lore and enchantment choices as the web design',()=>{
|
||||
const design=defaultItemDesign('dr_s1_spice_shovel_mk14');design.name='Desert Artisan';design.enchantments.efficiency=6;design.lore=[{text:'Made for the deep dunes.',color:'#FFAA00',bold:false,italic:true}];
|
||||
const native=nativeToolDefinition(design);assert.equal(native.config.name,'&6&lDesert Artisan');assert.equal(native.config.enchantments.enchantment1.level,6);assert(native.config.lore.some(line=>line.endsWith('&oMade for the deep dunes.')));
|
||||
assert.throws(()=>nativeToolDefinition({...design,enchantments:{...design.enchantments,efficiency:255}}));
|
||||
assert.throws(()=>nativeToolDefinition({...design,name:'&kHidden name'}));
|
||||
});
|
||||
test('every native definition has quality and MK, bounded enchantments and no automatic issuance',()=>{
|
||||
for(const d of nativeToolCatalogue({minecraftVersion:'1.21.8'}).definitions){assert(d.quality);assert(d.equipmentMark>=1&&d.equipmentMark<=14);assert.equal(d.config.giveFirstJoinFeatures.giveFirstJoin,false);assert.deepEqual(d.config.activators,{});assert.equal(d.config.keepItemOnDeath,false);}
|
||||
const max=nativeToolDefinition(toolTemplate('shovel',14));assert.equal(max.config.material,'NETHERITE_SHOVEL');assert.equal(max.config.enchantments.enchantment1.level,7);
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff