diff --git a/.npmignore b/.npmignore index a2205e5..63a8809 100644 --- a/.npmignore +++ b/.npmignore @@ -3,4 +3,5 @@ test/ lib/build/** build/** docs/ -pnpm-lock.yaml \ No newline at end of file +pnpm-lock.yaml +src/* \ No newline at end of file diff --git a/package.json b/package.json index f4c679c..a7427a7 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,10 @@ { "name": "growtopia.js", - "version": "1.4.0", + "version": "1.4.1", "description": "A package to create a growtopia private servers.", "main": "dist/index.js", "types": "dist/index.d.ts", + "type": "module", "scripts": { "test": "npm run build && npm run build:ts", "dev": "rimraf dist && npm run build && npm run build:ts && node test/basic-example/index.js", diff --git a/src/index.ts b/src/index.ts index 8f4071f..7681bbb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,12 +1,12 @@ -export type * from "../types/index"; +export type * from "../types/index.d"; -export * from "./structures/Client"; -export * from "./structures/Peer"; -export * from "./structures/ItemsDat"; -export * from "./structures/GrowApi"; -export * from "./util/Constants"; -export * from "./util/Utils"; +export * from "./structures/Client.js"; +export * from "./structures/Peer.js"; +export * from "./structures/ItemsDat.js"; +export * from "./structures/GrowApi.js"; +export * from "./util/Constants.js"; +export * from "./util/Utils.js"; -export * from "./packets/TankPacket"; -export * from "./packets/TextPacket"; -export * from "./packets/Variant"; +export * from "./packets/TankPacket.js"; +export * from "./packets/TextPacket.js"; +export * from "./packets/Variant.js"; diff --git a/src/packets/TankPacket.ts b/src/packets/TankPacket.ts index b088406..19bc709 100644 --- a/src/packets/TankPacket.ts +++ b/src/packets/TankPacket.ts @@ -1,4 +1,4 @@ -import { Tank } from "../../types/packets"; +import type { Tank } from "../../types/packets"; const TANK_HEADER_SIZE = 60; diff --git a/src/packets/Variant.ts b/src/packets/Variant.ts index 5825369..e67c7f3 100644 --- a/src/packets/Variant.ts +++ b/src/packets/Variant.ts @@ -1,8 +1,8 @@ -import { TankPacket } from "./TankPacket"; +import { TankPacket } from "./TankPacket.js"; // Types -import { VariantArg, VariantArray, VariantOptions } from "../../types/packets"; -import { VariantTypes } from "../util/Constants"; +import type { VariantArg, VariantArray, VariantOptions } from "../../types/packets"; +import Constants from "../util/Constants.js"; /** * Represents the Variant class. @@ -33,6 +33,7 @@ class Variant { public static toArray(data: Buffer): VariantArray[] { let arr: VariantArray[] = []; + const VariantTypes = Constants.VariantTypes; let pos = 60; const count = data.readUint8(60); @@ -109,6 +110,8 @@ class Variant { * Parses the data of the Variant and returns a TankPacket from it. */ public parse() { + const VariantTypes = Constants.VariantTypes; + let buf = [this.args.length]; this.args.forEach((arg) => { diff --git a/src/structures/Client.ts b/src/structures/Client.ts index fcd33e5..f83a05a 100644 --- a/src/structures/Client.ts +++ b/src/structures/Client.ts @@ -1,12 +1,13 @@ import EventEmitter from "eventemitter3"; -import { Caching, ClientOptions, ClientType } from "../../types/client"; -import { PacketTypes } from "../util/Constants"; -import { parseText } from "../util/Utils"; -import { Peer } from "./Peer"; -import { ActionEvent, LoginInfo, PeerData } from "../../types"; -import { TankPacket } from "../packets/TankPacket"; -import { WebServer } from "./WebServer"; -const Native = require("../../build/Release/gtjs.node").Client; +import type { Caching, ClientOptions, ClientType } from "../../types/client"; +import Constants from "../util/Constants.js"; +import Utils from "../util/Utils.js"; +import { Peer } from "./Peer.js"; +import type { ActionEvent, LoginInfo, PeerData } from "../../types"; +import { TankPacket } from "../packets/TankPacket.js"; +import { WebServer } from "./WebServer.js"; +import { createRequire } from "node:module"; +const Native = createRequire(import.meta.url)("../../build/Release/gtjs.node").Client; class Client extends EventEmitter { public _client: ClientType; @@ -127,16 +128,18 @@ class Client extends EventEmitter { const type = data.readInt32LE(); const peer = new Peer(this, netID); + const PacketTypes = Constants.PacketTypes; + switch (type) { case PacketTypes.STR: { - const parsed = parseText(data); + const parsed = Utils.parseText(data); this.emit("text", peer, parsed); break; } case PacketTypes.ACTION: { - const parsed = parseText(data); + const parsed = Utils.parseText(data); this.emit("action", peer, parsed); break; } diff --git a/src/structures/GrowApi.ts b/src/structures/GrowApi.ts index 454824e..d610c75 100644 --- a/src/structures/GrowApi.ts +++ b/src/structures/GrowApi.ts @@ -1,5 +1,5 @@ -import { Caching } from "../../types"; -import { Client } from "./Client"; +import type { Caching } from "../../types"; +import { Client } from "./Client.js"; class GrowApi { public client?: Client; diff --git a/src/structures/ItemsDat.ts b/src/structures/ItemsDat.ts index e561371..0d5565b 100644 --- a/src/structures/ItemsDat.ts +++ b/src/structures/ItemsDat.ts @@ -1,4 +1,4 @@ -import { ExtendString, ItemDefinition, ItemsDatMeta, StringOptions } from "../../types"; +import type { ExtendString, ItemDefinition, ItemsDatMeta, StringOptions } from "../../types"; class ItemsDat { private mempos = 0; diff --git a/src/structures/Peer.ts b/src/structures/Peer.ts index eedd029..863340b 100644 --- a/src/structures/Peer.ts +++ b/src/structures/Peer.ts @@ -1,7 +1,7 @@ -import { ClientType, PeerData } from "../../types"; -import { Sendable } from "../../types/packets"; -import { Variant } from "../packets/Variant"; -import { Client } from "./Client"; +import type { ClientType, PeerData } from "../../types"; +import type { Sendable } from "../../types/packets"; +import { Variant } from "../packets/Variant.js"; +import { Client } from "./Client.js"; class Peer { public data: T; diff --git a/src/structures/WebServer.ts b/src/structures/WebServer.ts index 1eab92d..c18f38d 100644 --- a/src/structures/WebServer.ts +++ b/src/structures/WebServer.ts @@ -3,6 +3,9 @@ import { readFileSync } from "fs"; import http from "http"; import https from "https"; import path from "path"; +import { fileURLToPath } from "url"; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); const options = { key: readFileSync(path.join(__dirname, "..", "..", "misc", "ssl", "server.key")), diff --git a/src/util/Constants.ts b/src/util/Constants.ts index c68fb6d..b6b6586 100644 --- a/src/util/Constants.ts +++ b/src/util/Constants.ts @@ -1,7 +1,7 @@ /** * Types for each Argument. */ -export enum VariantTypes { +enum VariantTypes { NONE, FLOAT_1, STRING, @@ -14,7 +14,7 @@ export enum VariantTypes { /** * Growtopia Packet Types */ -export enum PacketTypes { +enum PacketTypes { UNK, HELLO, STR, @@ -29,7 +29,7 @@ export enum PacketTypes { /** * Growtopia Tank Packet Types */ -export enum TankTypes { +enum TankTypes { STATE = 0, CALL_FUNCTION, UPDATE_STATUS, @@ -72,7 +72,7 @@ export enum TankTypes { SEND_PLAYER_TRIBUTE_DATA } -export enum TileFlags { +enum TileFlags { FLAGS_TILEEXTRA = 0x0001, FLAGS_LOCKED = 0x0002, FLAGS_SEED = 0x0010, @@ -90,14 +90,14 @@ export enum TileFlags { FLAGS_GLUE = 0x08000000 } -export enum TileOptionsFlags { +enum TileOptionsFlags { MUSIC_BLOCKS_DISABLED = 0x0010, MUSIC_BLOCKS_INVIS = 0x0020, LOCK_ALLOW_BUILD_ONLY = 0x0040, - LOCK_ENABLE_RAINBOW = 0x0080, + LOCK_ENABLE_RAINBOW = 0x0080 } -export enum TilePropertiesFlags { +enum TilePropertiesFlags { MULTI_FACING = 1 << 0, WRENCHABLE = 1 << 1, PERMANENT = 1 << 2, @@ -108,7 +108,7 @@ export enum TilePropertiesFlags { WORLD_LOCK = 1 << 7 } -export enum TileCategoryFlags { +enum TileCategoryFlags { BETA = 1 << 0, AUTO_PICKUP = 1 << 1, MOD = 1 << 2, @@ -119,7 +119,7 @@ export enum TileCategoryFlags { UNTRADEABLE = 1 << 7 } -export enum TileActionTypes { +enum TileActionTypes { FIST = 0, WRENCH = 1, DOOR = 2, @@ -261,7 +261,7 @@ export enum TileActionTypes { FRIENDS_ENTRANCE = 142 } -export enum TileExtraTypes { +enum TileExtraTypes { NONE, DOOR, SIGN, @@ -321,3 +321,15 @@ export enum TileExtraTypes { KRANKEN_GALACTIC = 0x50, WEATHER_INFINITY = 0x4d } + +export default { + VariantTypes, + PacketTypes, + TankTypes, + TileActionTypes, + TileCategoryFlags, + TileExtraTypes, + TileFlags, + TileOptionsFlags, + TilePropertiesFlags +}; diff --git a/src/util/Utils.ts b/src/util/Utils.ts index 9d5cd33..4379246 100644 --- a/src/util/Utils.ts +++ b/src/util/Utils.ts @@ -1,7 +1,5 @@ -import { DataObject } from "../../types"; - -export function parseText(chunk: Buffer) { - let data: DataObject = {}; +function parseText(chunk: Buffer) { + let data: Record = {}; chunk[chunk.length - 1] = 0; let str = chunk.toString("utf-8", 4); @@ -22,3 +20,5 @@ export function parseText(chunk: Buffer) { return data; } + +export default { parseText }; diff --git a/tsconfig.json b/tsconfig.json index aabafbf..d259709 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -25,9 +25,9 @@ // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ /* Modules */ - "module": "CommonJS" /* Specify what module code is generated. */, + "module": "ES2022" /* Specify what module code is generated. */, // "rootDir": "./", /* Specify the root folder within your source files. */ - // "moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */, + "moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */, // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ diff --git a/types/index.d.ts b/types/index.d.ts index 8382f2c..5d38778 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,7 +1,3 @@ -export interface DataObject { - [key: string]: string | number; -} - export interface PeerData { netID: number; }