web/website/scripts/generate-native-tools.mjs

43 lines
4.1 KiB
JavaScript

import {mkdir,writeFile} from 'node:fs/promises';
import {resolve} from 'node:path';
import {pathToFileURL} from 'node:url';
import {TOOL_TEMPLATES} from '../control-panel/public/tool-template-policy.mjs';
import {REFIT_UPGRADE_ITEMS} from '../control-panel/public/organisation-refit-policy.mjs';
import {EQUIPMENT_QUALITY_STYLES} from '../control-panel/public/equipment-template-policy.mjs';
import {itemRoman} from '../control-panel/public/item-lore-model.mjs';
import {defaultItemDesign,validateItemDesign} from '../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?.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 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};
}
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}));
}