UNPKG

pw-js-world

Version:

An optional package for PW-JS-Api, aims to serve world purposes.

131 lines (130 loc) 4.31 kB
import Block from "./Block.js"; import type PWGameWorldHelper from "./Helper.js"; import type { BlockArg, SendableBlockPacket } from "./types"; /** * This is external to the main Helper, it will allow developers to use the structure without needing to use helper if they so wish. * * All of the functions are static! */ export default class StructureHelper { /** * NOTE: If you're reading a file, you must get it then pass it to read. * * This is for reading the structure itself, if you have just the blocks (and width/height), you must use deserialiseStructBlocks; * * @param data Buffer representing the JSON structure itself. */ static read(data: Buffer | Uint8Array | IStructure): DeserialisedStructure; /** * If width or height are not provided, the structure may be trimmed (empty blocks). * * This is ideal if you want the trimmed structure in that case. */ static deserialiseStructBlocks(struct: IStructureBlocks, width?: number, height?: number): { blocks: [Block[][], Block[][], Block[][]]; width: number; height: number; }; } /** * Represents the structure in its deserialised form, allows for modification */ export declare class DeserialisedStructure { blocks: [Block[][], Block[][], Block[][]]; width: number; height: number; constructor(blocks: [Block[][], Block[][], Block[][]], struct: Omit<IStructure, "version" | "blocks">); /** * This will return a new object that meets IStructureBlocks interface. * * NOTE: This requires you to have called API getlistblocks (unless you have joined the world) */ getSerialisedBlocks(): IStructureBlocks; /** * This will return the structure form, giving you the freedom to choose your own way of saving. */ toStruct(): IStructure; /** * Buffer form of the structure. * * Ideal for server runtimes (and browser if polyfilled) */ toBuffer(): Buffer<ArrayBuffer>; /** * The JSON stringified of the structure. */ toJSONString(space?: number): string; /** * (This is for browser client or Bun) * * Blob form of the structure. */ toBlob(): Blob; /** * Uint8Array form of the structure. */ toBytes(): Uint8Array<ArrayBufferLike>; /** * This will return a list of packets containing all of the blocks. */ toPackets(x: number, y: number): SendableBlockPacket[]; /** * This will return a list of packets containing all of the blocks. * * If you pass in the blocks (from PWGameWorldHelper) for the 3rd parameter, this will be used to check for any already placed blocks. */ toPackets(x: number, y: number, helper: PWGameWorldHelper): SendableBlockPacket[]; } export interface IStructure { /** * Version of the structure object, not the world. */ version: number; /** * The maximum width of the structure. */ width: number; /** * The maximum height of the structure. */ height: number; /** * Object containing the mappings and the blocks. */ blocks: IStructureBlocks; } export interface IStructureBlocks { /** * Index starts at 0, this is the mapping of blocks (in block name ids in UPPER_CASE) */ mapping: string[]; /** * Index starts at 0, this is the mapping of args (2nd elements and beyond in each block pos in blocks) */ args: BlockArg[]; /** * If array, it's index corresponds to the mapping, the element will be array of locations and args indexed by layers * (while impossible, it's for possible compatibility with mirrored blocks, foreground block in background layer etc) * * If argMapping exists in a block, it'll be an index that corresponds to the block's arguments in args array. * * If string, it's the encoded version of the object, use atob then JSON parse. */ blocks: [ [ x: number, y: number, ...argMapping: number[] ][], [ x: number, y: number, ...argMapping: number[] ][], [ x: number, y: number, ...argMapping: number[] ][] ][]; }