From 37aea6a5e7a549a6dccd9d250d5e061c08d575a4 Mon Sep 17 00:00:00 2001 From: unimatrixgit <95320811+unimatrixgit@users.noreply.github.com> Date: Sun, 22 Nov 2026 09:37:28 +0100 Subject: [PATCH 1/5] Define shared MK quality tool and organisation refit policies --- tools/generate-item-catalogue.mjs | 6 +++ tools/generate-native-tools.mjs | 38 +++++++++++++ .../public/item-metadata-policy.mjs | 31 +++++++++++ .../public/mining-tool-policy.mjs | 22 ++++++++ .../public/organisation-refit-policy.mjs | 54 +++++++++++++++++++ .../public/tool-template-policy.mjs | 35 ++++++++++++ website/tests/native-tools.test.mjs | 16 ++++++ .../tests/organisation-refit-policy.test.mjs | 35 ++++++++++++ 8 files changed, 237 insertions(+) create mode 100644 tools/generate-item-catalogue.mjs create mode 100644 tools/generate-native-tools.mjs create mode 100644 website/control-panel/public/item-metadata-policy.mjs create mode 100644 website/control-panel/public/mining-tool-policy.mjs create mode 100644 website/control-panel/public/organisation-refit-policy.mjs create mode 100644 website/control-panel/public/tool-template-policy.mjs create mode 100644 website/tests/native-tools.test.mjs create mode 100644 website/tests/organisation-refit-policy.test.mjs diff --git a/tools/generate-item-catalogue.mjs b/tools/generate-item-catalogue.mjs new file mode 100644 index 00000000..c7340911 --- /dev/null +++ b/tools/generate-item-catalogue.mjs @@ -0,0 +1,6 @@ +import {writeFile,mkdir} from 'node:fs/promises'; +import {resolve,dirname} from 'node:path'; +import {pathToFileURL} from 'node:url'; +import {itemTemplateCatalogue} from '../website/control-panel/public/item-metadata-policy.mjs'; +export const itemCatalogueJSON=()=>JSON.stringify(itemTemplateCatalogue(),null,2)+'\n'; +if(process.argv[1]&&import.meta.url===pathToFileURL(resolve(process.argv[1])).href){const target=process.argv[2];if(!target)throw Error('Catalogue output required.');await mkdir(dirname(resolve(target)),{recursive:true});await writeFile(target,itemCatalogueJSON());console.log('Generated shared item metadata catalogue.');} diff --git a/tools/generate-native-tools.mjs b/tools/generate-native-tools.mjs new file mode 100644 index 00000000..f9bd599c --- /dev/null +++ b/tools/generate-native-tools.mjs @@ -0,0 +1,38 @@ +import {mkdir,writeFile} from 'node:fs/promises'; +import {resolve} from 'node:path'; +import {pathToFileURL} from 'node:url'; +import {TOOL_TEMPLATES} from '../website/control-panel/public/tool-template-policy.mjs'; +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'; + +export function nativeToolDefinition(input,{minecraftVersion='1.21.8'}={}){ + const template=[...TOOL_TEMPLATES,...REFIT_UPGRADE_ITEMS].find(row=>row.id===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.')], + 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}; +} +export function nativeToolCatalogue(options){ + if(!options?.minecraftVersion)throw TypeError('The actual Minecraft server version is required.'); + const definitions=[],unavailable=[]; + for(const template of [...TOOL_TEMPLATES,...REFIT_UPGRADE_ITEMS]){ + try{definitions.push(nativeToolDefinition(template,options));} + catch(error){if(template.material.startsWith('copper_')&&options?.minecraftVersion==='1.21.8')unavailable.push({id:template.id,reason:'Requires Minecraft 1.21.9 or newer.'});else throw error;} + } + return {schema:1,minecraftVersion:options.minecraftVersion,definitions,unavailable}; +} +if(process.argv[1]&&import.meta.url===pathToFileURL(resolve(process.argv[1])).href){ + const [output,minecraftVersion]=process.argv.slice(2);if(!output||!minecraftVersion)throw Error('Output directory and actual Minecraft server version required.'); + const catalogue=nativeToolCatalogue({minecraftVersion});await mkdir(output,{recursive:true}); + for(const item of catalogue.definitions)await writeFile(resolve(output,item.id+'.yml'),JSON.stringify(item.config,null,2)+'\n'); + await writeFile(resolve(output,'catalogue.json'),JSON.stringify(catalogue,null,2)+'\n'); + console.log(JSON.stringify({generated:catalogue.definitions.length,unavailable:catalogue.unavailable.length,minecraftVersion})); +} diff --git a/website/control-panel/public/item-metadata-policy.mjs b/website/control-panel/public/item-metadata-policy.mjs new file mode 100644 index 00000000..c4201f1c --- /dev/null +++ b/website/control-panel/public/item-metadata-policy.mjs @@ -0,0 +1,31 @@ +import {equipmentTemplate} from './equipment-template-policy.mjs'; +import {TOOL_TEMPLATES,elytraTemplate,ROCKET_TEMPLATES} from './tool-template-policy.mjs'; +import {EVERYDAY_ITEMS} from './mining-tool-policy.mjs'; +import {REFIT_UPGRADE_ITEMS} from './organisation-refit-policy.mjs'; +export const ITEM_METADATA_POLICY='s1-item-metadata-v1'; +// Native item rarity is a baseline, not an enchantment multiplier or an issuance grant. +export const NATIVE_ITEM_GRADES=Object.freeze({ + COMMON:Object.freeze({quality:'common',equipmentMark:1}), + UNCOMMON:Object.freeze({quality:'uncommon',equipmentMark:4}), + RARE:Object.freeze({quality:'rare',equipmentMark:7}), + EPIC:Object.freeze({quality:'very_rare',equipmentMark:10}) +}); +export const ITEM_CLASSES=Object.freeze(['resource','equipment','mining-tool','weapon','menu','consumable','flight-equipment','flight-consumable','relic']); +export function itemMetadata({quality,equipmentMark,itemClass='resource'}={}){ + if(!['common','uncommon','rare','very_rare','legendary','mythic','relic'].includes(quality)||!ITEM_CLASSES.includes(itemClass))throw TypeError('Choose a registered item class and quality.'); + if(quality==='relic'||itemClass==='relic'){ + if(quality!=='relic'||itemClass!=='relic'||equipmentMark!=='infinite')throw TypeError('Relics use the separate infinite mark.'); + }else if(!Number.isInteger(equipmentMark)||equipmentMark<0||equipmentMark>14)throw TypeError('Choose MK0–MK14.'); + return {policy:ITEM_METADATA_POLICY,quality,equipmentMark,itemClass}; +} +export function itemTemplateCatalogue(){ + const rows=[...TOOL_TEMPLATES,...ROCKET_TEMPLATES,...REFIT_UPGRADE_ITEMS.map(t=>({...t,itemClass:'resource'})),...['basic','merged'].map(elytraTemplate), + ...Object.values(EVERYDAY_ITEMS).map(t=>({...t,material:t.itemClass==='menu'?'compass':'orange_dye'}))]; + const qualities=['common','uncommon','rare','very_rare','legendary']; + for(let mark=0;mark<=14;mark++){ + const quality=qualities[Math.floor(mark/3)]; + for(const slot of Object.keys(equipmentTemplate({quality,mark}).enchantments))rows.push({id:`dr_s1_${quality}_mk${mark}_${slot}`,material:'netherite_'+slot,quality,equipmentMark:mark,itemClass:'equipment'}); + } + for(const slot of ['helmet','chestplate','leggings','boots'])rows.push({id:`dr_s1_mythic_mk14_${slot}`,material:'netherite_'+slot,quality:'mythic',equipmentMark:14,itemClass:'equipment'}); + return {policy:ITEM_METADATA_POLICY,nativeRarity:NATIVE_ITEM_GRADES,templates:Object.fromEntries(rows.map(t=>[t.id,{...itemMetadata(t),material:t.material}]))}; +} diff --git a/website/control-panel/public/mining-tool-policy.mjs b/website/control-panel/public/mining-tool-policy.mjs new file mode 100644 index 00000000..81b905b5 --- /dev/null +++ b/website/control-panel/public/mining-tool-policy.mjs @@ -0,0 +1,22 @@ +// Server-owned mining progression. Material, rarity and enchantments are one template. +export const MINING_TOOL_POLICY='s1-spice-shovels-v1'; +const profiles=[ + ['wooden','uncommon',0,0,0],['wooden','uncommon',1,0,0], + ['stone','uncommon',1,1,0],['stone','rare',2,1,0], + ['copper','rare',2,2,0],['copper','rare',3,2,1],['iron','rare',3,2,2], + ['iron','very_rare',4,3,2],['golden','very_rare',4,3,3], + ['golden','very_rare',5,3,3],['diamond','very_rare',5,4,3], + ['diamond','legendary',6,4,3],['netherite','legendary',6,4,4],['netherite','legendary',7,4,4] +]; +export function miningToolTemplate(mark){ + if(!Number.isInteger(mark)||mark<1||mark>14)throw TypeError('Choose a mining shovel from MK I to MK XIV.'); + const [material,quality,efficiency,unbreaking,fortune]=profiles[mark-1]; + return {policy:MINING_TOOL_POLICY,season:'S1',id:`dr_s1_spice_shovel_mk${mark}`,name:'Spice Harvest Shovel',material:material+'_shovel',minimumMinecraftVersion:material==='copper'?'1.21.9':'1.21.8',quality,equipmentMark:mark,itemClass:'mining-tool',enchantments:{...(efficiency?{efficiency}:{}),...(unbreaking?{unbreaking}:{}),...(fortune?{fortune}:{}),...(mark>=9?{mending:1}:{})}}; +} +export const MINING_SHOVELS=Object.freeze(profiles.map((_,i)=>Object.freeze(miningToolTemplate(i+1)))); +export const EVERYDAY_ITEMS=Object.freeze({ + imperialCodex:Object.freeze({id:'dr_imperial_codex',quality:'uncommon',equipmentMark:1,itemClass:'menu'}), + spice:Object.freeze({id:'spice',quality:'uncommon',equipmentMark:1,itemClass:'resource'}), + refinedSpice:Object.freeze({id:'refined_spice',quality:'rare',equipmentMark:2,itemClass:'consumable'}), + pollutedRefinedSpice:Object.freeze({id:'polluted_refined_spice',quality:'rare',equipmentMark:3,itemClass:'resource'}) +}); diff --git a/website/control-panel/public/organisation-refit-policy.mjs b/website/control-panel/public/organisation-refit-policy.mjs new file mode 100644 index 00000000..443bafbd --- /dev/null +++ b/website/control-panel/public/organisation-refit-policy.mjs @@ -0,0 +1,54 @@ +import {toolTemplate,TOOL_KINDS} from './tool-template-policy.mjs'; +import {equipmentTemplate,qualityForMark,maximumEquipmentEnchantments} from './equipment-template-policy.mjs'; + +// A preview/validation policy, never inventory evidence or permission to issue gear. +export const ORGANISATION_REFIT_POLICY='organisation-refit-v1'; +export const REFIT_COOLDOWN_MS=60_000; +export const REFIT_QUALITIES=Object.freeze(['common','uncommon','rare','very_rare']); +export const REFIT_ORGANISATIONS=Object.freeze(['clan','company','politics']); +export const ORGANISATION_GEAR_STYLE=Object.freeze({name:'#bb77ff',mark:'#bb77ff',minecraft:'&d'}); +export const REFIT_UPGRADE_ITEMS=Object.freeze([ + Object.freeze({id:'dr_mark_upgrade',name:'Equipment Refit Template',material:'netherite_upgrade_smithing_template',quality:'uncommon',equipmentMark:1,purpose:'mark'}), + Object.freeze({id:'dr_quality_upgrade',name:'Quality Refit Template',material:'netherite_upgrade_smithing_template',quality:'rare',equipmentMark:2,purpose:'quality'}) +]); +export function refitReputationLevel(mark){ + if(!Number.isInteger(mark)||mark<1||mark>14)throw TypeError('Invalid refit mark.'); + return mark===1?1:mark*2; +} +function profile(kind,mark){ + if(TOOL_KINDS.includes(kind))return toolTemplate(kind,mark); + if(!['helmet','chestplate','leggings','boots'].includes(kind))throw TypeError('This item has no registered refit ladder.'); + const quality=qualityForMark(mark),template=equipmentTemplate({quality,mark}); + return {quality,equipmentMark:mark,material:'netherite_'+kind,enchantments:maximumEquipmentEnchantments(template,kind)}; +} +function organisation(value){ + if(!value||!REFIT_ORGANISATIONS.includes(value.kind)||typeof value.id!=='string'||!/^[a-f0-9-]{36}$/i.test(value.id)||typeof value.tag!=='string'||!/^[A-Za-z0-9_-]{1,16}$/.test(value.tag))throw TypeError('A registered organisation and tag are required.'); + return {kind:value.kind,id:value.id,tag:value.tag}; +} +export function organisationRefitPlan(item,{organisation:org,reputationLevel,season,maxMark=14}={}){ + const maker=organisation(org); + if(!item||typeof item.instanceId!=='string'||!/^[a-f0-9-]{36}:[0-9]{1,4}$/i.test(item.instanceId)||item.amount!==1||!Number.isSafeInteger(item.revision)||item.revision<1)throw TypeError('Surrender one registered original item.'); + if(!/^S[0-9]{1,4}$/.test(season||'')||!Number.isInteger(maxMark)||maxMark<1||maxMark>14)throw TypeError('Current registered season limits required.'); + if(!REFIT_QUALITIES.includes(item.quality))throw TypeError('Legendary, Mythic and Relic items are excluded from ordinary refits.'); + const from=profile(item.kind,item.equipmentMark),to=profile(item.kind,item.equipmentMark+1); + if(from.quality!==item.quality||from.material!==item.material)throw TypeError('Original item does not match its registered ladder.'); + if(to.equipmentMark>maxMark||!REFIT_QUALITIES.includes(to.quality))throw TypeError('The next tier requires special acquisition, not an ordinary refit.'); + const requiredReputationLevel=refitReputationLevel(to.equipmentMark); + if(!Number.isSafeInteger(reputationLevel)||reputationLevelbefore)throw TypeError('Enchantment requires a registered refit profile: '+id); + enchantments[id]=Math.min(after,Math.max(value,Math.ceil(value*after/before))); + } + const qualityChanged=from.quality!==to.quality; + return {policy:ORGANISATION_REFIT_POLICY,requiredReputationLevel,cooldownMs:REFIT_COOLDOWN_MS, + consume:{original:{instanceId:item.instanceId,revision:item.revision,amount:1},templates:[{id:'dr_mark_upgrade',amount:1},...(qualityChanged?[{id:'dr_quality_upgrade',amount:1}]:[])]}, + output:{...item,amount:1,revision:item.revision+1,material:to.material,quality:to.quality,equipmentMark:to.equipmentMark,enchantments,organisationGear:maker,lastRefitSeason:season}, + history:{instanceId:item.instanceId,fromMark:from.equipmentMark,toMark:to.equipmentMark,fromQuality:from.quality,toQuality:to.quality,organisation:maker,season}, + delivery:'replace-surrendered-instance',qualityChanged}; +} diff --git a/website/control-panel/public/tool-template-policy.mjs b/website/control-panel/public/tool-template-policy.mjs new file mode 100644 index 00000000..25a956b5 --- /dev/null +++ b/website/control-panel/public/tool-template-policy.mjs @@ -0,0 +1,35 @@ +import {miningToolTemplate} from './mining-tool-policy.mjs'; +import {equipmentTemplate,maximumEquipmentEnchantments} from './equipment-template-policy.mjs'; +export const TOOL_POLICY='s1-tools-v1'; +export const TOOL_KINDS=Object.freeze(['shovel','pickaxe','axe','hoe','sword','bow','crossbow','trident','hammer']); +const fixed={bow:'bow',crossbow:'crossbow',trident:'trident',hammer:'mace'}; +const label=value=>value.replaceAll('_',' ').replace(/\b[a-z]/g,c=>c.toUpperCase()); +const level=(cap,mark)=>Math.floor(cap*(mark-1)/13); +export function toolTemplate(kind,mark){ + if(!TOOL_KINDS.includes(kind))throw TypeError('Choose a supported tool.'); + const mining=miningToolTemplate(mark);if(['trident','hammer'].includes(kind)&&mark<7)throw TypeError('Tridents and hammers start at MK VII.'); + if(kind==='shovel')return mining; + let enchantments={...mining.enchantments}; + const durability={...(level(4,mark)?{unbreaking:level(4,mark)}:{}),...(mark>=9?{mending:1}:{})}; + if(kind==='sword')enchantments={...durability,...(level(7,mark)?{sharpness:level(7,mark)}:{}),...(level(4,mark)?{looting:level(4,mark),sweeping_edge:level(4,mark)}:{})}; + if(kind==='bow')enchantments={...(durability.unbreaking?{unbreaking:durability.unbreaking}:{}),...(level(7,mark)?{power:level(7,mark)}:{}),...(level(3,mark)?{punch:level(3,mark)}:{}),...(mark>=8?{flame:1}:{}),...(mark>=12?{infinity:1}:{})}; + if(kind==='crossbow')enchantments={...durability,...(level(4,mark)?{quick_charge:level(4,mark)}:{}),...(level(6,mark)?{piercing:level(6,mark)}:{})}; + if(kind==='trident')enchantments={...durability,impaling:level(7,mark),loyalty:Math.max(1,level(4,mark)),...(mark>=10?{channeling:1}:{})}; + if(kind==='hammer')enchantments={...durability,density:level(7,mark),...(mark>=12?{wind_burst:level(4,mark)}:{})}; + return {policy:TOOL_POLICY,season:'S1',id:`dr_s1_${kind}_mk${mark}`,name:kind==='hammer'?'Imperial Hammer':label(kind),material:fixed[kind]||mining.material.replace(/shovel$/,kind),quality:mining.quality,equipmentMark:mark,itemClass:['pickaxe','axe','hoe'].includes(kind)?'mining-tool':'weapon',enchantments}; +} +export function elytraTemplate(variant){ + if(!['basic','merged'].includes(variant))throw TypeError('Choose basic or merged Elytra.'); + const merged=variant==='merged',quality=merged?'mythic':'legendary',mark=merged?14:13; + return {policy:TOOL_POLICY,season:'S1',id:`dr_s1_elytra_${variant}`,name:merged?'Merged Elytra':'Basic Elytra',material:merged?'netherite_chestplate':'elytra',quality,equipmentMark:mark,itemClass:'flight-equipment',glider:merged,enchantments:merged?maximumEquipmentEnchantments(equipmentTemplate({quality,mark}),'chestplate'):{unbreaking:4,mending:1}}; +} +// Distinct instruments: ordinary spice and refined spice are never interchangeable. +export const ROCKET_TEMPLATES=Object.freeze([ + Object.freeze({policy:TOOL_POLICY,season:'S1',id:'dr_s1_spice_rocket_mk1',name:'Spice Rocket',material:'firework_rocket',quality:'uncommon',equipmentMark:1,itemClass:'flight-consumable',effectMultiplier:1,spiceItemId:'spice'}), + Object.freeze({policy:TOOL_POLICY,season:'S1',id:'dr_s1_refined_rocket_mk2',name:'Refined Spice Rocket',material:'firework_rocket',quality:'uncommon',equipmentMark:2,itemClass:'flight-consumable',effectMultiplier:2,spiceItemId:'refined_spice'}), + Object.freeze({policy:TOOL_POLICY,season:'S1',id:'dr_s1_polluted_rocket_mk3',name:'Polluted Spice Rocket',material:'firework_rocket',quality:'rare',equipmentMark:3,itemClass:'flight-consumable',effectMultiplier:4,spiceItemId:'polluted_refined_spice'}) +]); +export const TOOL_TEMPLATES=Object.freeze(TOOL_KINDS.flatMap(kind=>{ + const first=['trident','hammer'].includes(kind)?7:1; + return Array.from({length:15-first},(_,i)=>Object.freeze(toolTemplate(kind,i+first))); +})); diff --git a/website/tests/native-tools.test.mjs b/website/tests/native-tools.test.mjs new file mode 100644 index 00000000..dda291fb --- /dev/null +++ b/website/tests/native-tools.test.mjs @@ -0,0 +1,16 @@ +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'; +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); + assert(old.definitions.every(d=>!d.config.material.startsWith('COPPER_'))); + 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('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); + const forged=nativeToolDefinition({...toolTemplate('shovel',14),enchantments:{efficiency:255}});assert.equal(forged.config.enchantments.enchantment1.level,7); + const tokens=nativeToolCatalogue({minecraftVersion:'1.21.8'}).definitions.filter(d=>d.id.endsWith('_upgrade'));assert.equal(tokens.length,2);assert(tokens.every(t=>t.config.material==='NETHERITE_UPGRADE_SMITHING_TEMPLATE')); +}); diff --git a/website/tests/organisation-refit-policy.test.mjs b/website/tests/organisation-refit-policy.test.mjs new file mode 100644 index 00000000..acd64d95 --- /dev/null +++ b/website/tests/organisation-refit-policy.test.mjs @@ -0,0 +1,35 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import {organisationRefitPlan,refitReputationLevel} from '../control-panel/public/organisation-refit-policy.mjs'; +import {toolTemplate} from '../control-panel/public/tool-template-policy.mjs'; +const org={kind:'clan',id:'11111111-1111-4111-8111-111111111111',tag:'UART'}; +const item=mark=>({...toolTemplate('shovel',mark),kind:'shovel',instanceId:'22222222-2222-4222-8222-222222222222:0',revision:1,amount:1,originSeason:'S1',customName:'My old shovel'}); +const plan=(i,extra={})=>organisationRefitPlan(i,{organisation:org,reputationLevel:100,season:'S2',...extra}); +test('refit replaces one item, changes material, and retains original identity and origin season',()=>{ + const before=item(2),after=plan(before); + assert.equal(after.consume.original.instanceId,after.output.instanceId); + assert.equal(after.output.material,'stone_shovel');assert.equal(after.output.amount,1); + assert.equal(after.output.originSeason,'S1');assert.equal(after.output.lastRefitSeason,'S2'); + assert.equal(after.output.customName,before.customName);assert.equal(before.material,'wooden_shovel'); + assert.equal(after.output.revision,2);assert.deepEqual(after.output.organisationGear,org); +}); +test('existing enchantments scale proportionately without adding missing enchantments',()=>{ + const before={...item(3),enchantments:{efficiency:1,vanishing_curse:1}},after=plan(before); + assert.deepEqual(after.output.enchantments,{efficiency:2,vanishing_curse:1}); + assert.equal(after.qualityChanged,true);assert.equal(after.consume.templates.length,2); + const later=plan({...item(9),enchantments:{mending:1}});assert.deepEqual(later.output.enchantments,{mending:1}); +}); +test('reputation thresholds cannot be skipped; all three organisation kinds share the same policy',()=>{ + assert.equal(refitReputationLevel(1),1);assert.equal(refitReputationLevel(4),8); + assert.throws(()=>plan(item(3),{reputationLevel:7}),/Reputation level 8/); + for(const kind of ['clan','company','politics'])assert.equal(plan(item(3),{organisation:{...org,kind}}).output.organisationGear.kind,kind); + assert.throws(()=>plan(item(3),{organisation:{...org,kind:'personal'}}),/organisation/); +}); +test('ordinary refits cannot create Legendary, Mythic or Relics, skip tiers or accept forged profiles',()=>{ + assert.throws(()=>plan(item(11)),/special acquisition/); + assert.throws(()=>plan(item(12)),/excluded/); + assert.throws(()=>plan({...item(3),quality:'relic'}),/excluded/); + assert.throws(()=>plan({...item(3),amount:2}),/one registered/); + assert.throws(()=>plan({...item(3),material:'netherite_shovel'}),/registered ladder/); + assert.throws(()=>plan({...item(3),enchantments:{efficiency:99}}),/registered refit profile/); +}); From e0c6e9992a85b4e89fbf8bc9d1d4d3ff3a61a917 Mon Sep 17 00:00:00 2001 From: unimatrixgit <95320811+unimatrixgit@users.noreply.github.com> Date: Sun, 22 Nov 2026 09:54:46 +0100 Subject: [PATCH 2/5] Share item design validation and native export across item and kit editors --- tools/generate-native-tools.mjs | 13 ++++--- .../public/item-design-model.mjs | 36 +++++++++++++++++++ website/tests/item-design-model.test.mjs | 18 ++++++++++ website/tests/native-tools.test.mjs | 8 +++++ 4 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 website/control-panel/public/item-design-model.mjs create mode 100644 website/tests/item-design-model.test.mjs 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:'