web/website/node/claim-worldguard.mjs

19 lines
2.1 KiB
JavaScript

const uuid=value=>typeof value==='string'&&/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(value);
export function worldGuardHomeRegion(clanId,position,{ownerUuid=null,memberUuids=[],organisationKind='clan'}={}){
if(!['clan','company','protected'].includes(organisationKind)||!uuid(clanId)||!['x','y','z'].every(k=>Number.isSafeInteger(position[k])))throw Error('Invalid Home Block coordinates.');
const x=Math.floor(position.x/16)*16,z=Math.floor(position.z/16)*16,id='dr_'+organisationKind+'_'+clanId.replaceAll('-','');
return {regions:{[id]:{min:{x,y:-64,z},max:{x:x+15,y:319,z:z+15},members:{'unique-ids':[...new Set(memberUuids.filter(uuid))].filter(v=>v!==ownerUuid)},flags:{},owners:{'unique-ids':uuid(ownerUuid)?[ownerUuid]:[]},type:'cuboid',priority:0}}};
}
export function worldGuardChunkCount(document){
if(!document?.regions||typeof document.regions!=='object'||Array.isArray(document.regions))throw Error('Invalid WorldGuard region document.');
const chunks=new Set(),entries=Object.entries(document.regions);if(entries.length>1000)throw Error('Too many claim regions.');
for(const [id,region]of entries){
if(!/^[a-zA-Z0-9_,-]+$/.test(id)||id==='__global__'||region.type!=='cuboid'||!Number.isSafeInteger(region.priority)||!region.flags||typeof region.flags!=='object'||Array.isArray(region.flags))throw Error('Invalid WorldGuard claim region.');
for(const k of ['x','y','z'])if(!Number.isSafeInteger(region.min?.[k])||!Number.isSafeInteger(region.max?.[k])||region.min[k]>region.max[k])throw Error('Invalid WorldGuard bounds.');
for(const domain of [region.owners,region.members]){if(!domain||!Array.isArray(domain['unique-ids'])||!domain['unique-ids'].every(uuid))throw Error('Invalid WorldGuard UUID domain.');}
const minX=Math.floor(region.min.x/16),maxX=Math.floor(region.max.x/16),minZ=Math.floor(region.min.z/16),maxZ=Math.floor(region.max.z/16);
if((maxX-minX+1)*(maxZ-minZ+1)>10000)throw Error('Claim region exceeds supported capacity.');
for(let x=minX;x<=maxX;x++)for(let z=minZ;z<=maxZ;z++){chunks.add(x+':'+z);if(chunks.size>10000)throw Error('Claim capacity exceeds inspection limit.');}
}
return chunks.size;
}