import {worldGuardChunkCount} from './claim-worldguard.mjs'; import {gameOrganisationError as fail} from './game-organisation-policy.mjs'; export const CLAIM_DIRECTIONS=Object.freeze(['north','east','south','west','all']); function regions(snapshot){return(snapshot?.estates||[]).flatMap(e=>Object.values(e.worldguard?.regions||{}));} export function registeredClaimChunks(snapshot){ if(snapshot?.source!=='minecraft'||!snapshot.homeBlock)return []; const unique=new Map(); for(const region of regions(snapshot)){ const a=region.min,b=region.max; if(region.type!=='cuboid'||![a?.x,a?.z,b?.x,b?.z].every(Number.isSafeInteger)||a.x>b.x||a.z>b.z)throw fail('The registered estate geometry requires review.'); for(let x=Math.floor(a.x/16);x<=Math.floor(b.x/16);x++)for(let z=Math.floor(a.z/16);z<=Math.floor(b.z/16);z++){ unique.set(x+':'+z,{x,z});if(unique.size>10000)throw fail('The registered estate exceeds its inspection bound.'); } }if(unique.size===0){const p=snapshot.homeBlock.position;if(!Number.isSafeInteger(p?.x)||!Number.isSafeInteger(p?.z))throw fail('The registered Home Block requires review.');unique.set('home',{x:Math.floor(p.x/16),z:Math.floor(p.z/16)});}return [...unique.values()]; } export function expandRegisteredClaim(snapshot,direction,capacity,others=[]){ if(!CLAIM_DIRECTIONS.includes(direction)||!Number.isSafeInteger(capacity)||capacity<1||capacity>10000)throw fail('Review the claim direction and current entitlement.'); if(snapshot?.source!=='minecraft'||!snapshot.homeBlock||snapshot.estates?.length!==1)throw fail('Register a game Home Block before expanding this estate.'); const next=structuredClone(snapshot),estate=next.estates[0],document=estate.worldguard,entries=Object.entries(document?.regions||{}); if(entries.length!==1||worldGuardChunkCount(document)!==snapshot.usedChunks)throw fail('This estate needs a scoped geometry review before expansion.'); const region=entries[0][1],min=region.min,max=region.max; if(min.x%16||min.z%16||(max.x+1)%16||(max.z+1)%16)throw fail('The estate must follow complete chunk boundaries.'); if(direction==='north'||direction==='all')min.z-=16; if(direction==='east'||direction==='all')max.x+=16; if(direction==='south'||direction==='all')max.z+=16; if(direction==='west'||direction==='all')min.x-=16; if([min.x,min.z,max.x,max.z].some(n=>Math.abs(n)>29999980))throw fail('Expansion would exceed the world boundary.'); const chunks=registeredClaimChunks(next);if(chunks.length>capacity)throw fail('This expansion needs '+chunks.length+' chunks; your unlocked limit is '+capacity+'.'); const occupied=new Set(chunks.map(c=>c.x+':'+c.z)); for(const other of others){if(other.source!=='minecraft'||other.homeBlock?.worldUuid!==next.homeBlock.worldUuid)continue; if(registeredClaimChunks(other).some(c=>occupied.has(c.x+':'+c.z)))throw fail('This expansion overlaps another organisation’s estate.'); } next.usedChunks=chunks.length;estate.chunks=chunks.length;return next; }