pw-js-world
Version:
An optional package for PW-JS-Api, aims to serve world purposes.
120 lines (119 loc) • 3.75 kB
TypeScript
import { type Hook } from "pw-js-api";
import type { ProtoGen } from "pw-js-api";
import Block from "./Block.js";
import Player from "./Player.js";
import { LayerType } from "./Constants.js";
import type { Point, PWGameHook } from "./types/index.js";
import { DeserialisedStructure } from "./Structure.js";
/**
* To use this helper, you must first create an instance of this,
*
* then: <PWGameClient>.addCallback("raw", helper.onRawPacketRecv)
*/
export default class PWGameWorldHelper {
/**
* Arrays of blocks (by layer, x, y)
*/
blocks: [Block[][], Block[][], Block[][]];
players: Map<number, Player>;
globalSwitches: boolean[];
private _meta?;
private _width;
private _height;
private _init;
private _selfPlayerId;
/**
* The current world's width.
*
* If you didn't put the hook before init, this may throw error.
*/
get width(): number;
/**
* The current world's height.
*
* If you didn't put the hook before init, this may throw error.
*/
get height(): number;
/**
* The current world's metadata.
*
* If you didn't put the hook before init, this may throw error.
*/
get meta(): ProtoGen.WorldMeta | null;
/**
* If this helper is ready. When it's false, the helper will not return anything for any of the packets.
*/
get initialised(): boolean;
/**
* The bot's player object.
*
* If you didn't put the hook before init, this may throw error.
*/
get botPlayer(): Player;
/**
* The bot's player id in the world.
*
* If you didn't put the hook before init, this may throw error.
*/
get botPlayerId(): number;
/**
* This must go in .use() of the main PW-JS-API Game Client class.
*
* <PWGameClient>.use(<PWGameWorldHelper>.receiveHook)
*
* DO NOT PUT () AFTER RECEIVEHOOK
*/
receiveHook: Hook<PWGameHook>;
/**
* Internal function.
*/
private initialise;
/**
* Internal function.
*/
private deserialize;
private convertSwitchState;
/**
* Internal function, this triggers when the world gets cleared.
*
* Clears the blocks map and promptly fill it with empty except the border which becomes basci gray.
*/
private clear;
/**
* Gets the block at the position.
*
* Difference between this and using this.blocks directly is that this function will validate the positions and the layer.
*/
getBlockAt(pos: Point, l: LayerType): Block;
getBlockAt(x: number | Point, y: number, l: LayerType): Block;
/**
* Player ID.
*
* The main bot player is excluded from the criteria.
*/
getPlayer(id: number, isAccount?: false): Player | undefined;
/**
* Username is case insensitive.
*
* The main bot player is excluded from the criteria.
*/
getPlayer(username: string, isAccount?: false): Player | undefined;
/**
* The ID of the account (must have second parameter set to true)
*
* The main bot player is excluded from the criteria.
*/
getPlayer(accountId: string, isAccount: true): Player | undefined;
/**
* Returns the list of current players in the world.
*/
getPlayers(): Player[];
/**
* This will return a DeserialisedStructure which will allow you to easily save to a file if you wish.
*
* The blocks are cloned and thus you're free to modify the blocks in the structure without the risk of it affecting this helper's blocks.
*
* NOTE: endX and endY are also included!
*/
sectionBlocks(startX: number, startY: number, endX: number, endY: number): DeserialisedStructure;
}