pw-js-world 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -20
- package/dist/Helper.d.ts +2 -2
- package/dist/types/index.d.ts +18 -18
- package/esm.mjs +18 -18
- package/lib/Block.ts +216 -216
- package/lib/BufferReader.ts +769 -769
- package/lib/Constants.ts +3 -3
- package/lib/Helper.ts +602 -602
- package/lib/Player.ts +316 -316
- package/lib/index.ts +7 -7
- package/lib/types/index.d.ts +18 -18
- package/package.json +1 -1
- package/bun.lockb +0 -0
- package/test/index.ts +0 -96
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025 Doomester
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Doomester
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
SOFTWARE.
|
package/dist/Helper.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BlockNames, Hook } from "pw-js-api";
|
|
2
|
-
import type {
|
|
2
|
+
import type { ProtoGen } from "pw-js-api";
|
|
3
3
|
import Block from "./Block";
|
|
4
4
|
import Player, { IPlayerEffect, IPlayerRights } from "./Player";
|
|
5
5
|
import { LayerType } from "./Constants";
|
|
@@ -38,7 +38,7 @@ export default class PWGameWorldHelper {
|
|
|
38
38
|
*
|
|
39
39
|
* If you didn't put the hook before init, this may throw error.
|
|
40
40
|
*/
|
|
41
|
-
get meta(): WorldMeta | null;
|
|
41
|
+
get meta(): ProtoGen.WorldMeta | null;
|
|
42
42
|
/**
|
|
43
43
|
* If this helper is ready. When it's false, the helper will not return anything for any of the packets.
|
|
44
44
|
*/
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
// I cba so all of the typings will go here
|
|
2
|
-
|
|
3
|
-
type Point = { x: number, y: number };
|
|
4
|
-
|
|
5
|
-
export interface SendableBlockPacket {
|
|
6
|
-
/**
|
|
7
|
-
* If true, just leave one position
|
|
8
|
-
*/
|
|
9
|
-
isFillOperation: boolean;
|
|
10
|
-
blockId: number;
|
|
11
|
-
layer: number;
|
|
12
|
-
/**
|
|
13
|
-
* Note: (I THINK) 250 positions limit.
|
|
14
|
-
*/
|
|
15
|
-
positions: Point[];
|
|
16
|
-
extraFields?: Uint8Array;
|
|
17
|
-
}
|
|
18
|
-
|
|
1
|
+
// I cba so all of the typings will go here
|
|
2
|
+
|
|
3
|
+
type Point = { x: number, y: number };
|
|
4
|
+
|
|
5
|
+
export interface SendableBlockPacket {
|
|
6
|
+
/**
|
|
7
|
+
* If true, just leave one position
|
|
8
|
+
*/
|
|
9
|
+
isFillOperation: boolean;
|
|
10
|
+
blockId: number;
|
|
11
|
+
layer: number;
|
|
12
|
+
/**
|
|
13
|
+
* Note: (I THINK) 250 positions limit.
|
|
14
|
+
*/
|
|
15
|
+
positions: Point[];
|
|
16
|
+
extraFields?: Uint8Array;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
19
|
export type BlockArg = (string | number | bigint | boolean | Buffer);
|
package/esm.mjs
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
let WorldPlayerHelper = (await import("./dist/Helper.js")).default;
|
|
2
|
-
let Block = (await import("./dist/Block.js")).default;
|
|
3
|
-
let BufferReader = (await import("./dist/BufferReader.js")).default;
|
|
4
|
-
let Player = (await import("./dist/Player.js")).default;
|
|
5
|
-
|
|
6
|
-
if ("default" in WorldPlayerHelper) WorldPlayerHelper = WorldPlayerHelper.default;
|
|
7
|
-
if ("default" in Block) Block = Block.default;
|
|
8
|
-
if ("default" in BufferReader) BufferReader = BufferReader.default;
|
|
9
|
-
if ("default" in Player) Player = Player.default;
|
|
10
|
-
|
|
11
|
-
const Constants = (await import("./dist/Constants.js")).default;
|
|
12
|
-
|
|
13
|
-
export default {
|
|
14
|
-
WorldPlayerHelper, Block, BufferReader, Player, Constants
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export {
|
|
18
|
-
WorldPlayerHelper, Block, BufferReader, Player, Constants
|
|
1
|
+
let WorldPlayerHelper = (await import("./dist/Helper.js")).default;
|
|
2
|
+
let Block = (await import("./dist/Block.js")).default;
|
|
3
|
+
let BufferReader = (await import("./dist/BufferReader.js")).default;
|
|
4
|
+
let Player = (await import("./dist/Player.js")).default;
|
|
5
|
+
|
|
6
|
+
if ("default" in WorldPlayerHelper) WorldPlayerHelper = WorldPlayerHelper.default;
|
|
7
|
+
if ("default" in Block) Block = Block.default;
|
|
8
|
+
if ("default" in BufferReader) BufferReader = BufferReader.default;
|
|
9
|
+
if ("default" in Player) Player = Player.default;
|
|
10
|
+
|
|
11
|
+
const Constants = (await import("./dist/Constants.js")).default;
|
|
12
|
+
|
|
13
|
+
export default {
|
|
14
|
+
WorldPlayerHelper, Block, BufferReader, Player, Constants
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export {
|
|
18
|
+
WorldPlayerHelper, Block, BufferReader, Player, Constants
|
|
19
19
|
};
|
package/lib/Block.ts
CHANGED
|
@@ -1,217 +1,217 @@
|
|
|
1
|
-
import { BlockNames } from "pw-js-api";
|
|
2
|
-
import type { BlockArg, Point, SendableBlockPacket } from "./types";
|
|
3
|
-
import BufferReader, { ComponentTypeHeader } from "./BufferReader";
|
|
4
|
-
import { LayerType } from "./Constants";
|
|
5
|
-
|
|
6
|
-
export default class Block {
|
|
7
|
-
bId: number;
|
|
8
|
-
args: BlockArg[] = [];
|
|
9
|
-
|
|
10
|
-
constructor(bId: number | keyof typeof BlockNames, args?: BlockArg[]) {
|
|
11
|
-
if (typeof bId === "number") this.bId = bId;
|
|
12
|
-
else {
|
|
13
|
-
this.bId = BlockNames[bId];
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
if (args) this.args = args;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* I mean... Just use .args.length !== 0 to see if it has args.
|
|
21
|
-
*
|
|
22
|
-
* But anyway, this will return true if there is at least one args, otherwise false.
|
|
23
|
-
*/
|
|
24
|
-
hasArgs() : boolean {
|
|
25
|
-
return this.args.length !== 0;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* For helper.
|
|
30
|
-
*
|
|
31
|
-
* This is in Block class for organisation.
|
|
32
|
-
*
|
|
33
|
-
* This will deserialise by using the reader to get the block ID then retrieve the args, if applicable.
|
|
34
|
-
*/
|
|
35
|
-
static deserialize(reader: BufferReader) : Block {
|
|
36
|
-
return new Block(reader.readUInt32LE()).deserializeArgs(reader);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
protected deserializeArgs(reader: BufferReader, flag = false) : this {
|
|
40
|
-
const format: ComponentTypeHeader[] = (BlockArgsHeadings as any)[this.name];
|
|
41
|
-
|
|
42
|
-
for (let i = 0; i < (format?.length ?? 0); i++) {
|
|
43
|
-
if (flag) {
|
|
44
|
-
reader.expectUInt8(format[i]);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
this.args[i] = reader.read(format[i], !flag);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return this;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* For helper.
|
|
55
|
-
*
|
|
56
|
-
* This is in Block class for organisation.
|
|
57
|
-
*/
|
|
58
|
-
static deserializeArgs(blockId: number, reader: BufferReader, flag = false) : BlockArg[] {
|
|
59
|
-
const format: ComponentTypeHeader[] = (BlockArgsHeadings as any)[BlockNames[blockId]];
|
|
60
|
-
|
|
61
|
-
const args = [];
|
|
62
|
-
|
|
63
|
-
for (let i = 0; i < (format?.length ?? 0); i++) {
|
|
64
|
-
if (flag) {
|
|
65
|
-
reader.expectUInt8(format[i]);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
args[i] = reader.read(format[i], !flag);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return args;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Serializes the block into a buffer. This is used to convert
|
|
76
|
-
* the block into a binary format that can be sent over the game
|
|
77
|
-
* server. As this is static, block id and args are required.
|
|
78
|
-
*
|
|
79
|
-
* - Little Endian
|
|
80
|
-
* - With Id
|
|
81
|
-
* - Type Byte omitted
|
|
82
|
-
*/
|
|
83
|
-
public static serializeArgs(bId: number, args: BlockArg[]): Buffer;
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Serializes the block into a buffer. This is used to convert
|
|
87
|
-
* the block into a binary format that can be sent over the game
|
|
88
|
-
* server. As this is static, block id and args are required.
|
|
89
|
-
*
|
|
90
|
-
* - Big Endian
|
|
91
|
-
* - No Id
|
|
92
|
-
* - Type Byte included
|
|
93
|
-
*/
|
|
94
|
-
public static serializeArgs(bId: number, args: BlockArg[], options: { endian: "big"; writeId: false; readTypeByte: true }): Buffer;
|
|
95
|
-
|
|
96
|
-
public static serializeArgs(bId: number, args: BlockArg[], options?: { endian: "little" | "big"; writeId: boolean; readTypeByte: boolean }): Buffer {
|
|
97
|
-
options ||= {
|
|
98
|
-
endian: "little",
|
|
99
|
-
writeId: true,
|
|
100
|
-
readTypeByte: false,
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
const buffer: Buffer[] = [];
|
|
104
|
-
|
|
105
|
-
if (options.writeId) {
|
|
106
|
-
const idBuffer = Buffer.alloc(4);
|
|
107
|
-
idBuffer.writeUInt32LE(bId);
|
|
108
|
-
buffer.push(idBuffer);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const blockData:ComponentTypeHeader[] = (BlockArgsHeadings as any)[BlockNames[bId]] ?? [];
|
|
112
|
-
|
|
113
|
-
for (let i = 0, len = blockData.length; i < len; i++) {
|
|
114
|
-
const entry = BufferReader.Dynamic(i, args[i]);
|
|
115
|
-
buffer.push(entry);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return Buffer.concat(buffer);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
*
|
|
123
|
-
* @param pos List of points (X and Y)
|
|
124
|
-
*/
|
|
125
|
-
toPacket(pos: Point[], layer: LayerType) : SendableBlockPacket;
|
|
126
|
-
toPacket(x: number, y: number, layer: LayerType) : SendableBlockPacket;
|
|
127
|
-
toPacket(pos: Point[] | number, y: number, layer?: LayerType) : SendableBlockPacket {
|
|
128
|
-
if (typeof pos === "number") {
|
|
129
|
-
pos = [{
|
|
130
|
-
x: pos, y
|
|
131
|
-
}];
|
|
132
|
-
|
|
133
|
-
layer = layer ?? 0;
|
|
134
|
-
} else layer = y ?? 0;
|
|
135
|
-
|
|
136
|
-
return {
|
|
137
|
-
isFillOperation: false,
|
|
138
|
-
blockId: this.bId,
|
|
139
|
-
layer,
|
|
140
|
-
positions: pos,
|
|
141
|
-
extraFields: Block.serializeArgs(this.bId, this.args, { endian: "big", writeId: false, readTypeByte: true })
|
|
142
|
-
} satisfies SendableBlockPacket;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* This will return the block name in UPPER_CASE form.
|
|
147
|
-
*
|
|
148
|
-
* For eg EFFECTS_INVULNERABILITY.
|
|
149
|
-
*/
|
|
150
|
-
get name() : keyof typeof BlockNames {
|
|
151
|
-
return BlockNames[this.bId] as keyof typeof BlockNames;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Returns a copy of the block.
|
|
156
|
-
*/
|
|
157
|
-
clone(obj?: false) : Block;
|
|
158
|
-
clone(obj: true) : { bId: number, args: BlockArg[], name: string }
|
|
159
|
-
clone(obj = false) {
|
|
160
|
-
if (obj === true) return { bId: this.bId, args: this.args, name: this.name };
|
|
161
|
-
|
|
162
|
-
return new Block(this.bId, this.args);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* This mapping contains definitions of block data which require additional
|
|
168
|
-
* arguments to be sent or received with.
|
|
169
|
-
*/
|
|
170
|
-
export const BlockArgsHeadings = {
|
|
171
|
-
COIN_GOLD_DOOR: [ComponentTypeHeader.Int32],
|
|
172
|
-
COIN_BLUE_DOOR: [ComponentTypeHeader.Int32],
|
|
173
|
-
COIN_GOLD_GATE: [ComponentTypeHeader.Int32],
|
|
174
|
-
COIN_BLUE_GATE: [ComponentTypeHeader.Int32],
|
|
175
|
-
|
|
176
|
-
EFFECTS_JUMP_HEIGHT: [ComponentTypeHeader.Int32],
|
|
177
|
-
EFFECTS_FLY: [ComponentTypeHeader.Boolean],
|
|
178
|
-
EFFECTS_SPEED: [ComponentTypeHeader.Int32],
|
|
179
|
-
EFFECTS_INVULNERABILITY: [ComponentTypeHeader.Boolean],
|
|
180
|
-
EFFECTS_CURSE: [ComponentTypeHeader.Int32],
|
|
181
|
-
EFFECTS_ZOMBIE: [ComponentTypeHeader.Int32],
|
|
182
|
-
EFFECTS_GRAVITYFORCE: [ComponentTypeHeader.Int32],
|
|
183
|
-
EFFECTS_MULTI_JUMP: [ComponentTypeHeader.Int32],
|
|
184
|
-
// gravity effects no data
|
|
185
|
-
// effects off
|
|
186
|
-
// effects zombie
|
|
187
|
-
|
|
188
|
-
TOOL_PORTAL_WORLD_SPAWN: [ComponentTypeHeader.Int32],
|
|
189
|
-
|
|
190
|
-
SIGN_NORMAL: [ComponentTypeHeader.String],
|
|
191
|
-
SIGN_RED: [ComponentTypeHeader.String],
|
|
192
|
-
SIGN_GREEN: [ComponentTypeHeader.String],
|
|
193
|
-
SIGN_BLUE: [ComponentTypeHeader.String],
|
|
194
|
-
SIGN_GOLD: [ComponentTypeHeader.String],
|
|
195
|
-
|
|
196
|
-
PORTAL: [ComponentTypeHeader.Int32, ComponentTypeHeader.Int32, ComponentTypeHeader.Int32],
|
|
197
|
-
PORTAL_INVISIBLE: [ComponentTypeHeader.Int32, ComponentTypeHeader.Int32, ComponentTypeHeader.Int32],
|
|
198
|
-
PORTAL_WORLD: [ComponentTypeHeader.String, ComponentTypeHeader.Int32],
|
|
199
|
-
|
|
200
|
-
SWITCH_LOCAL_TOGGLE: [ComponentTypeHeader.Int32],
|
|
201
|
-
SWITCH_LOCAL_ACTIVATOR: [ComponentTypeHeader.Int32, ComponentTypeHeader.Boolean],
|
|
202
|
-
SWITCH_LOCAL_RESETTER: [ComponentTypeHeader.Boolean],
|
|
203
|
-
SWITCH_LOCAL_DOOR: [ComponentTypeHeader.Int32],
|
|
204
|
-
SWITCH_LOCAL_GATE: [ComponentTypeHeader.Int32],
|
|
205
|
-
SWITCH_GLOBAL_TOGGLE: [ComponentTypeHeader.Int32],
|
|
206
|
-
SWITCH_GLOBAL_ACTIVATOR: [ComponentTypeHeader.Int32, ComponentTypeHeader.Boolean],
|
|
207
|
-
SWITCH_GLOBAL_RESETTER: [ComponentTypeHeader.Boolean],
|
|
208
|
-
SWITCH_GLOBAL_DOOR: [ComponentTypeHeader.Int32],
|
|
209
|
-
SWITCH_GLOBAL_GATE: [ComponentTypeHeader.Int32],
|
|
210
|
-
|
|
211
|
-
HAZARD_DEATH_DOOR: [ComponentTypeHeader.Int32],
|
|
212
|
-
HAZARD_DEATH_GATE: [ComponentTypeHeader.Int32],
|
|
213
|
-
|
|
214
|
-
NOTE_DRUM: [ComponentTypeHeader.ByteArray],
|
|
215
|
-
NOTE_PIANO: [ComponentTypeHeader.ByteArray],
|
|
216
|
-
NOTE_GUITAR: [ComponentTypeHeader.ByteArray],
|
|
1
|
+
import { BlockNames } from "pw-js-api";
|
|
2
|
+
import type { BlockArg, Point, SendableBlockPacket } from "./types";
|
|
3
|
+
import BufferReader, { ComponentTypeHeader } from "./BufferReader";
|
|
4
|
+
import { LayerType } from "./Constants";
|
|
5
|
+
|
|
6
|
+
export default class Block {
|
|
7
|
+
bId: number;
|
|
8
|
+
args: BlockArg[] = [];
|
|
9
|
+
|
|
10
|
+
constructor(bId: number | keyof typeof BlockNames, args?: BlockArg[]) {
|
|
11
|
+
if (typeof bId === "number") this.bId = bId;
|
|
12
|
+
else {
|
|
13
|
+
this.bId = BlockNames[bId];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (args) this.args = args;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* I mean... Just use .args.length !== 0 to see if it has args.
|
|
21
|
+
*
|
|
22
|
+
* But anyway, this will return true if there is at least one args, otherwise false.
|
|
23
|
+
*/
|
|
24
|
+
hasArgs() : boolean {
|
|
25
|
+
return this.args.length !== 0;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* For helper.
|
|
30
|
+
*
|
|
31
|
+
* This is in Block class for organisation.
|
|
32
|
+
*
|
|
33
|
+
* This will deserialise by using the reader to get the block ID then retrieve the args, if applicable.
|
|
34
|
+
*/
|
|
35
|
+
static deserialize(reader: BufferReader) : Block {
|
|
36
|
+
return new Block(reader.readUInt32LE()).deserializeArgs(reader);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
protected deserializeArgs(reader: BufferReader, flag = false) : this {
|
|
40
|
+
const format: ComponentTypeHeader[] = (BlockArgsHeadings as any)[this.name];
|
|
41
|
+
|
|
42
|
+
for (let i = 0; i < (format?.length ?? 0); i++) {
|
|
43
|
+
if (flag) {
|
|
44
|
+
reader.expectUInt8(format[i]);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
this.args[i] = reader.read(format[i], !flag);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* For helper.
|
|
55
|
+
*
|
|
56
|
+
* This is in Block class for organisation.
|
|
57
|
+
*/
|
|
58
|
+
static deserializeArgs(blockId: number, reader: BufferReader, flag = false) : BlockArg[] {
|
|
59
|
+
const format: ComponentTypeHeader[] = (BlockArgsHeadings as any)[BlockNames[blockId]];
|
|
60
|
+
|
|
61
|
+
const args = [];
|
|
62
|
+
|
|
63
|
+
for (let i = 0; i < (format?.length ?? 0); i++) {
|
|
64
|
+
if (flag) {
|
|
65
|
+
reader.expectUInt8(format[i]);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
args[i] = reader.read(format[i], !flag);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return args;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Serializes the block into a buffer. This is used to convert
|
|
76
|
+
* the block into a binary format that can be sent over the game
|
|
77
|
+
* server. As this is static, block id and args are required.
|
|
78
|
+
*
|
|
79
|
+
* - Little Endian
|
|
80
|
+
* - With Id
|
|
81
|
+
* - Type Byte omitted
|
|
82
|
+
*/
|
|
83
|
+
public static serializeArgs(bId: number, args: BlockArg[]): Buffer;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Serializes the block into a buffer. This is used to convert
|
|
87
|
+
* the block into a binary format that can be sent over the game
|
|
88
|
+
* server. As this is static, block id and args are required.
|
|
89
|
+
*
|
|
90
|
+
* - Big Endian
|
|
91
|
+
* - No Id
|
|
92
|
+
* - Type Byte included
|
|
93
|
+
*/
|
|
94
|
+
public static serializeArgs(bId: number, args: BlockArg[], options: { endian: "big"; writeId: false; readTypeByte: true }): Buffer;
|
|
95
|
+
|
|
96
|
+
public static serializeArgs(bId: number, args: BlockArg[], options?: { endian: "little" | "big"; writeId: boolean; readTypeByte: boolean }): Buffer {
|
|
97
|
+
options ||= {
|
|
98
|
+
endian: "little",
|
|
99
|
+
writeId: true,
|
|
100
|
+
readTypeByte: false,
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const buffer: Buffer[] = [];
|
|
104
|
+
|
|
105
|
+
if (options.writeId) {
|
|
106
|
+
const idBuffer = Buffer.alloc(4);
|
|
107
|
+
idBuffer.writeUInt32LE(bId);
|
|
108
|
+
buffer.push(idBuffer);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const blockData:ComponentTypeHeader[] = (BlockArgsHeadings as any)[BlockNames[bId]] ?? [];
|
|
112
|
+
|
|
113
|
+
for (let i = 0, len = blockData.length; i < len; i++) {
|
|
114
|
+
const entry = BufferReader.Dynamic(i, args[i]);
|
|
115
|
+
buffer.push(entry);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return Buffer.concat(buffer);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
*
|
|
123
|
+
* @param pos List of points (X and Y)
|
|
124
|
+
*/
|
|
125
|
+
toPacket(pos: Point[], layer: LayerType) : SendableBlockPacket;
|
|
126
|
+
toPacket(x: number, y: number, layer: LayerType) : SendableBlockPacket;
|
|
127
|
+
toPacket(pos: Point[] | number, y: number, layer?: LayerType) : SendableBlockPacket {
|
|
128
|
+
if (typeof pos === "number") {
|
|
129
|
+
pos = [{
|
|
130
|
+
x: pos, y
|
|
131
|
+
}];
|
|
132
|
+
|
|
133
|
+
layer = layer ?? 0;
|
|
134
|
+
} else layer = y ?? 0;
|
|
135
|
+
|
|
136
|
+
return {
|
|
137
|
+
isFillOperation: false,
|
|
138
|
+
blockId: this.bId,
|
|
139
|
+
layer,
|
|
140
|
+
positions: pos,
|
|
141
|
+
extraFields: Block.serializeArgs(this.bId, this.args, { endian: "big", writeId: false, readTypeByte: true })
|
|
142
|
+
} satisfies SendableBlockPacket;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* This will return the block name in UPPER_CASE form.
|
|
147
|
+
*
|
|
148
|
+
* For eg EFFECTS_INVULNERABILITY.
|
|
149
|
+
*/
|
|
150
|
+
get name() : keyof typeof BlockNames {
|
|
151
|
+
return BlockNames[this.bId] as keyof typeof BlockNames;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Returns a copy of the block.
|
|
156
|
+
*/
|
|
157
|
+
clone(obj?: false) : Block;
|
|
158
|
+
clone(obj: true) : { bId: number, args: BlockArg[], name: string }
|
|
159
|
+
clone(obj = false) {
|
|
160
|
+
if (obj === true) return { bId: this.bId, args: this.args, name: this.name };
|
|
161
|
+
|
|
162
|
+
return new Block(this.bId, this.args);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* This mapping contains definitions of block data which require additional
|
|
168
|
+
* arguments to be sent or received with.
|
|
169
|
+
*/
|
|
170
|
+
export const BlockArgsHeadings = {
|
|
171
|
+
COIN_GOLD_DOOR: [ComponentTypeHeader.Int32],
|
|
172
|
+
COIN_BLUE_DOOR: [ComponentTypeHeader.Int32],
|
|
173
|
+
COIN_GOLD_GATE: [ComponentTypeHeader.Int32],
|
|
174
|
+
COIN_BLUE_GATE: [ComponentTypeHeader.Int32],
|
|
175
|
+
|
|
176
|
+
EFFECTS_JUMP_HEIGHT: [ComponentTypeHeader.Int32],
|
|
177
|
+
EFFECTS_FLY: [ComponentTypeHeader.Boolean],
|
|
178
|
+
EFFECTS_SPEED: [ComponentTypeHeader.Int32],
|
|
179
|
+
EFFECTS_INVULNERABILITY: [ComponentTypeHeader.Boolean],
|
|
180
|
+
EFFECTS_CURSE: [ComponentTypeHeader.Int32],
|
|
181
|
+
EFFECTS_ZOMBIE: [ComponentTypeHeader.Int32],
|
|
182
|
+
EFFECTS_GRAVITYFORCE: [ComponentTypeHeader.Int32],
|
|
183
|
+
EFFECTS_MULTI_JUMP: [ComponentTypeHeader.Int32],
|
|
184
|
+
// gravity effects no data
|
|
185
|
+
// effects off
|
|
186
|
+
// effects zombie
|
|
187
|
+
|
|
188
|
+
TOOL_PORTAL_WORLD_SPAWN: [ComponentTypeHeader.Int32],
|
|
189
|
+
|
|
190
|
+
SIGN_NORMAL: [ComponentTypeHeader.String],
|
|
191
|
+
SIGN_RED: [ComponentTypeHeader.String],
|
|
192
|
+
SIGN_GREEN: [ComponentTypeHeader.String],
|
|
193
|
+
SIGN_BLUE: [ComponentTypeHeader.String],
|
|
194
|
+
SIGN_GOLD: [ComponentTypeHeader.String],
|
|
195
|
+
|
|
196
|
+
PORTAL: [ComponentTypeHeader.Int32, ComponentTypeHeader.Int32, ComponentTypeHeader.Int32],
|
|
197
|
+
PORTAL_INVISIBLE: [ComponentTypeHeader.Int32, ComponentTypeHeader.Int32, ComponentTypeHeader.Int32],
|
|
198
|
+
PORTAL_WORLD: [ComponentTypeHeader.String, ComponentTypeHeader.Int32],
|
|
199
|
+
|
|
200
|
+
SWITCH_LOCAL_TOGGLE: [ComponentTypeHeader.Int32],
|
|
201
|
+
SWITCH_LOCAL_ACTIVATOR: [ComponentTypeHeader.Int32, ComponentTypeHeader.Boolean],
|
|
202
|
+
SWITCH_LOCAL_RESETTER: [ComponentTypeHeader.Boolean],
|
|
203
|
+
SWITCH_LOCAL_DOOR: [ComponentTypeHeader.Int32],
|
|
204
|
+
SWITCH_LOCAL_GATE: [ComponentTypeHeader.Int32],
|
|
205
|
+
SWITCH_GLOBAL_TOGGLE: [ComponentTypeHeader.Int32],
|
|
206
|
+
SWITCH_GLOBAL_ACTIVATOR: [ComponentTypeHeader.Int32, ComponentTypeHeader.Boolean],
|
|
207
|
+
SWITCH_GLOBAL_RESETTER: [ComponentTypeHeader.Boolean],
|
|
208
|
+
SWITCH_GLOBAL_DOOR: [ComponentTypeHeader.Int32],
|
|
209
|
+
SWITCH_GLOBAL_GATE: [ComponentTypeHeader.Int32],
|
|
210
|
+
|
|
211
|
+
HAZARD_DEATH_DOOR: [ComponentTypeHeader.Int32],
|
|
212
|
+
HAZARD_DEATH_GATE: [ComponentTypeHeader.Int32],
|
|
213
|
+
|
|
214
|
+
NOTE_DRUM: [ComponentTypeHeader.ByteArray],
|
|
215
|
+
NOTE_PIANO: [ComponentTypeHeader.ByteArray],
|
|
216
|
+
NOTE_GUITAR: [ComponentTypeHeader.ByteArray],
|
|
217
217
|
} as const;
|