pw-js-world 0.0.1
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 +21 -0
- package/README.md +67 -0
- package/bun.lockb +0 -0
- package/dist/Block.d.ts +117 -0
- package/dist/Block.js +156 -0
- package/dist/BufferReader.d.ts +324 -0
- package/dist/BufferReader.js +674 -0
- package/dist/Constants.d.ts +4 -0
- package/dist/Constants.js +6 -0
- package/dist/Helper.d.ts +200 -0
- package/dist/Helper.js +482 -0
- package/dist/Player.d.ts +222 -0
- package/dist/Player.js +110 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +5 -0
- package/dist/types/index.d.ts +19 -0
- package/esm.mjs +19 -0
- package/lib/Block.ts +217 -0
- package/lib/BufferReader.ts +770 -0
- package/lib/Constants.ts +4 -0
- package/lib/Helper.ts +603 -0
- package/lib/Player.ts +317 -0
- package/lib/index.ts +8 -0
- package/lib/types/index.d.ts +19 -0
- package/package.json +40 -0
- package/test/index.ts +96 -0
package/dist/Player.d.ts
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import type { ProtoGen } from "pw-js-api";
|
|
2
|
+
import type { Point } from "./types";
|
|
3
|
+
export interface IPlayer {
|
|
4
|
+
/**
|
|
5
|
+
* ID of the player.
|
|
6
|
+
*/
|
|
7
|
+
playerId: number;
|
|
8
|
+
/**
|
|
9
|
+
* ID of the player's account.
|
|
10
|
+
*/
|
|
11
|
+
accountId: string;
|
|
12
|
+
/**
|
|
13
|
+
* Name of the player.
|
|
14
|
+
*/
|
|
15
|
+
username: string;
|
|
16
|
+
/**
|
|
17
|
+
* ID of the player's equipped smiley.
|
|
18
|
+
*/
|
|
19
|
+
face: number;
|
|
20
|
+
/**
|
|
21
|
+
* String, could be an admin or developer.
|
|
22
|
+
*/
|
|
23
|
+
role: string;
|
|
24
|
+
/**
|
|
25
|
+
* If player is bot user's friend.
|
|
26
|
+
*/
|
|
27
|
+
isFriend: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Position of the user.
|
|
30
|
+
*
|
|
31
|
+
* Note: This helper does not simulate physics so positions will always be inaccurate.
|
|
32
|
+
*/
|
|
33
|
+
position?: Point;
|
|
34
|
+
/**
|
|
35
|
+
* If player is the world owner.
|
|
36
|
+
*/
|
|
37
|
+
isWorldOwner: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Rights
|
|
40
|
+
*/
|
|
41
|
+
rights: IPlayerRights;
|
|
42
|
+
/**
|
|
43
|
+
* current world state.
|
|
44
|
+
*/
|
|
45
|
+
states: IPlayerWorldState;
|
|
46
|
+
/**
|
|
47
|
+
* List of active effects the player has.
|
|
48
|
+
*/
|
|
49
|
+
effects: PlayerEffect[];
|
|
50
|
+
/**
|
|
51
|
+
* If this player is the bot.
|
|
52
|
+
*/
|
|
53
|
+
isMe: boolean;
|
|
54
|
+
}
|
|
55
|
+
export interface IPlayerRights {
|
|
56
|
+
/**
|
|
57
|
+
* If the player has edit rights.
|
|
58
|
+
*/
|
|
59
|
+
canEdit: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* If the player has god rights.
|
|
62
|
+
*/
|
|
63
|
+
canGod: boolean;
|
|
64
|
+
/**
|
|
65
|
+
* If the player has the ability to toggle minimap.
|
|
66
|
+
*/
|
|
67
|
+
canToggleMinimap: boolean;
|
|
68
|
+
/**
|
|
69
|
+
* If the player has the ability to change the world settings.
|
|
70
|
+
*/
|
|
71
|
+
canChangeWorldSettings: boolean;
|
|
72
|
+
/**
|
|
73
|
+
* List of commands (string) the player can use.
|
|
74
|
+
*/
|
|
75
|
+
availableCommands: string[];
|
|
76
|
+
}
|
|
77
|
+
export interface IPlayerWorldState {
|
|
78
|
+
/**
|
|
79
|
+
* Number of gold coins the player has.
|
|
80
|
+
*/
|
|
81
|
+
coinsGold: number;
|
|
82
|
+
/**
|
|
83
|
+
* Number of blue coins the player has.
|
|
84
|
+
*/
|
|
85
|
+
coinsBlue: number;
|
|
86
|
+
/**
|
|
87
|
+
* Number of times the player died.
|
|
88
|
+
*/
|
|
89
|
+
deaths: number;
|
|
90
|
+
/**
|
|
91
|
+
* Coordinates of collected coins?
|
|
92
|
+
*/
|
|
93
|
+
collectedItems: Point[];
|
|
94
|
+
/**
|
|
95
|
+
* If player has gold crown on.
|
|
96
|
+
*/
|
|
97
|
+
hasGoldCrown: boolean;
|
|
98
|
+
/**
|
|
99
|
+
* If player has won the world.
|
|
100
|
+
*/
|
|
101
|
+
hasSilverCrown: boolean;
|
|
102
|
+
/**
|
|
103
|
+
* Zero indexed, map of the player's switch state.
|
|
104
|
+
*/
|
|
105
|
+
switches: boolean[];
|
|
106
|
+
/**
|
|
107
|
+
* If player is in god mode right now.
|
|
108
|
+
*/
|
|
109
|
+
godmode: boolean;
|
|
110
|
+
/**
|
|
111
|
+
* If player is in mod mode right now.
|
|
112
|
+
*/
|
|
113
|
+
modmode: boolean;
|
|
114
|
+
/**
|
|
115
|
+
* ID of the team the player is associated with right now.
|
|
116
|
+
*/
|
|
117
|
+
teamId: number;
|
|
118
|
+
}
|
|
119
|
+
export interface IPlayerEffect {
|
|
120
|
+
/**
|
|
121
|
+
* The ID of the effect.
|
|
122
|
+
*/
|
|
123
|
+
effectId: number;
|
|
124
|
+
/**
|
|
125
|
+
* If applicable, the duration of the effect.
|
|
126
|
+
*/
|
|
127
|
+
duration?: number;
|
|
128
|
+
/**
|
|
129
|
+
* If applicable, the strength of the effect. (For example speed or multi jump effect)
|
|
130
|
+
*/
|
|
131
|
+
strength?: number;
|
|
132
|
+
}
|
|
133
|
+
export default class Player {
|
|
134
|
+
/**
|
|
135
|
+
* ID of the player.
|
|
136
|
+
*/
|
|
137
|
+
playerId: number;
|
|
138
|
+
/**
|
|
139
|
+
* ID of the player's account.
|
|
140
|
+
*/
|
|
141
|
+
accountId: string;
|
|
142
|
+
/**
|
|
143
|
+
* Name of the player.
|
|
144
|
+
*/
|
|
145
|
+
username: string;
|
|
146
|
+
/**
|
|
147
|
+
* ID of the player's equipped smiley.
|
|
148
|
+
*/
|
|
149
|
+
face: number;
|
|
150
|
+
/**
|
|
151
|
+
* String, could be an admin or developer.
|
|
152
|
+
*/
|
|
153
|
+
role: string;
|
|
154
|
+
/**
|
|
155
|
+
* If player is bot user's friend.
|
|
156
|
+
*/
|
|
157
|
+
isFriend: boolean;
|
|
158
|
+
/**
|
|
159
|
+
* Position of the user.
|
|
160
|
+
*
|
|
161
|
+
* Note: This helper does not simulate physics so positions will always be inaccurate.
|
|
162
|
+
*/
|
|
163
|
+
position?: Point;
|
|
164
|
+
/**
|
|
165
|
+
* If player is the world owner.
|
|
166
|
+
*/
|
|
167
|
+
isWorldOwner: boolean;
|
|
168
|
+
/**
|
|
169
|
+
* Rights
|
|
170
|
+
*/
|
|
171
|
+
rights: IPlayerRights;
|
|
172
|
+
/**
|
|
173
|
+
* current world state.
|
|
174
|
+
*/
|
|
175
|
+
states: IPlayerWorldState;
|
|
176
|
+
/**
|
|
177
|
+
* List of active effects the player has.
|
|
178
|
+
*/
|
|
179
|
+
effects: PlayerEffect[];
|
|
180
|
+
/**
|
|
181
|
+
* If this player is the bot.
|
|
182
|
+
*/
|
|
183
|
+
isMe: boolean;
|
|
184
|
+
constructor(props: ProtoGen.PlayerProperties, states?: IPlayerWorldState | boolean);
|
|
185
|
+
/**
|
|
186
|
+
* This is destructive, this is only for on reset packet.
|
|
187
|
+
*/
|
|
188
|
+
resetState(): void;
|
|
189
|
+
/**
|
|
190
|
+
* Destructive.
|
|
191
|
+
*/
|
|
192
|
+
resetRights(): void;
|
|
193
|
+
}
|
|
194
|
+
export declare class PlayerEffect {
|
|
195
|
+
/**
|
|
196
|
+
* The ID of the effect.
|
|
197
|
+
*/
|
|
198
|
+
effectId: number;
|
|
199
|
+
/**
|
|
200
|
+
* If applicable, the duration of the effect.
|
|
201
|
+
*/
|
|
202
|
+
duration?: number;
|
|
203
|
+
/**
|
|
204
|
+
* If applicable, the strength of the effect. (For example speed or multi jump effect)
|
|
205
|
+
*/
|
|
206
|
+
strength?: number;
|
|
207
|
+
/**
|
|
208
|
+
* The time the effect occurred.
|
|
209
|
+
*/
|
|
210
|
+
triggeredAt: number;
|
|
211
|
+
constructor(effect: IPlayerEffect, triggeredAt?: number);
|
|
212
|
+
/**
|
|
213
|
+
* Note: If this effect is non timed, this will always return false.
|
|
214
|
+
*/
|
|
215
|
+
get hasExpired(): boolean;
|
|
216
|
+
/**
|
|
217
|
+
* Milliseconds showing how long before this expires.
|
|
218
|
+
*
|
|
219
|
+
* Note: If this effect is non timed, this will return infinity.
|
|
220
|
+
*/
|
|
221
|
+
get remaining(): number;
|
|
222
|
+
}
|
package/dist/Player.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
export default class Player {
|
|
2
|
+
constructor(props, states) {
|
|
3
|
+
/**
|
|
4
|
+
* List of active effects the player has.
|
|
5
|
+
*/
|
|
6
|
+
this.effects = [];
|
|
7
|
+
/**
|
|
8
|
+
* If this player is the bot.
|
|
9
|
+
*/
|
|
10
|
+
this.isMe = false;
|
|
11
|
+
this.accountId = props.accountId;
|
|
12
|
+
this.face = props.face;
|
|
13
|
+
this.isFriend = props.isFriend;
|
|
14
|
+
this.isWorldOwner = props.isWorldOwner;
|
|
15
|
+
this.playerId = props.playerId;
|
|
16
|
+
this.position = props.position ? {
|
|
17
|
+
x: props.position.x,
|
|
18
|
+
y: props.position.y
|
|
19
|
+
} : undefined;
|
|
20
|
+
if (!props.rights)
|
|
21
|
+
this.resetRights();
|
|
22
|
+
else
|
|
23
|
+
this.rights = {
|
|
24
|
+
availableCommands: props.rights.availableCommands,
|
|
25
|
+
canChangeWorldSettings: props.rights.canChangeWorldSettings,
|
|
26
|
+
canEdit: props.rights.canEdit,
|
|
27
|
+
canGod: props.rights.canGod,
|
|
28
|
+
canToggleMinimap: props.rights.canToggleMinimap,
|
|
29
|
+
};
|
|
30
|
+
this.role = props.role;
|
|
31
|
+
this.username = props.username;
|
|
32
|
+
if (typeof states === "boolean") {
|
|
33
|
+
this.isMe = states;
|
|
34
|
+
states = undefined;
|
|
35
|
+
}
|
|
36
|
+
if (!states) {
|
|
37
|
+
// Could be bot via init that don't get states.
|
|
38
|
+
this.resetState();
|
|
39
|
+
}
|
|
40
|
+
else
|
|
41
|
+
this.states = {
|
|
42
|
+
coinsBlue: states.coinsBlue,
|
|
43
|
+
coinsGold: states.coinsGold,
|
|
44
|
+
collectedItems: states.collectedItems.map(v => ({ x: v.x, y: v.y })),
|
|
45
|
+
deaths: states.deaths,
|
|
46
|
+
godmode: states.godmode,
|
|
47
|
+
hasGoldCrown: states.hasGoldCrown,
|
|
48
|
+
hasSilverCrown: states.hasSilverCrown,
|
|
49
|
+
modmode: states.modmode,
|
|
50
|
+
switches: states.switches,
|
|
51
|
+
teamId: states.teamId,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* This is destructive, this is only for on reset packet.
|
|
56
|
+
*/
|
|
57
|
+
resetState() {
|
|
58
|
+
this.states = {
|
|
59
|
+
coinsBlue: 0,
|
|
60
|
+
coinsGold: 0,
|
|
61
|
+
collectedItems: [],
|
|
62
|
+
deaths: 0,
|
|
63
|
+
godmode: false,
|
|
64
|
+
hasGoldCrown: false,
|
|
65
|
+
hasSilverCrown: false,
|
|
66
|
+
modmode: false,
|
|
67
|
+
switches: new Array(1000).fill(false),
|
|
68
|
+
teamId: 0,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Destructive.
|
|
73
|
+
*/
|
|
74
|
+
resetRights() {
|
|
75
|
+
this.rights = {
|
|
76
|
+
availableCommands: [],
|
|
77
|
+
canChangeWorldSettings: false,
|
|
78
|
+
canEdit: false,
|
|
79
|
+
canGod: false,
|
|
80
|
+
canToggleMinimap: false
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
export class PlayerEffect {
|
|
85
|
+
constructor(effect, triggeredAt) {
|
|
86
|
+
this.effectId = effect.effectId;
|
|
87
|
+
this.duration = effect.duration;
|
|
88
|
+
this.strength = effect.strength;
|
|
89
|
+
this.triggeredAt = triggeredAt !== null && triggeredAt !== void 0 ? triggeredAt : Date.now();
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Note: If this effect is non timed, this will always return false.
|
|
93
|
+
*/
|
|
94
|
+
get hasExpired() {
|
|
95
|
+
if (this.duration === undefined)
|
|
96
|
+
return false;
|
|
97
|
+
return Date.now() > (this.triggeredAt + this.duration);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Milliseconds showing how long before this expires.
|
|
101
|
+
*
|
|
102
|
+
* Note: If this effect is non timed, this will return infinity.
|
|
103
|
+
*/
|
|
104
|
+
get remaining() {
|
|
105
|
+
if (this.duration === undefined)
|
|
106
|
+
return Infinity;
|
|
107
|
+
return Math.max(0, Date.now() - (this.triggeredAt + this.duration));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiUGxheWVyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vbGliL1BsYXllci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUEySUEsTUFBTSxDQUFDLE9BQU8sT0FBTyxNQUFNO0lBc0R2QixZQUFZLEtBQWdDLEVBQUUsTUFBb0M7UUFWbEY7O1dBRUc7UUFDSCxZQUFPLEdBQWtCLEVBQUUsQ0FBQztRQUU1Qjs7V0FFRztRQUNILFNBQUksR0FBWSxLQUFLLENBQUM7UUFHbEIsSUFBSSxDQUFDLFNBQVMsR0FBRyxLQUFLLENBQUMsU0FBUyxDQUFDO1FBQ2pDLElBQUksQ0FBQyxJQUFJLEdBQUcsS0FBSyxDQUFDLElBQUksQ0FBQztRQUN2QixJQUFJLENBQUMsUUFBUSxHQUFHLEtBQUssQ0FBQyxRQUFRLENBQUM7UUFDL0IsSUFBSSxDQUFDLFlBQVksR0FBRyxLQUFLLENBQ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USxDQUFDO1FBRWpELE9BQU8sSUFBSSxDQUFDLEdBQUcsQ0FBQyxDQUFDLEVBQUUsSUFBSSxDQUFDLEdBQUcsRUFBRSxHQUFHLENBQUMsSUFBSSxDQUFDLFdBQVcsR0FBRyxJQUFJLENBQUMsUUFBUSxDQUFDLENBQUMsQ0FBQztJQUN4RSxDQUFDO0NBQ0oifQ==
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { default as PWGameWorldHelper } from "./Helper.js";
|
|
2
|
+
export { ComponentTypeHeader, default as BufferReader } from "./BufferReader.js";
|
|
3
|
+
export { default as Block, BlockArgsHeadings } from "./Block.js";
|
|
4
|
+
// import * from "./Helper";
|
|
5
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9saWIvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQ0EsT0FBTyxFQUFFLE9BQU8sSUFBSSxpQkFBaUIsRUFBRSxNQUFNLGFBQWEsQ0FBQztBQUUzRCxPQUFPLEVBQUUsbUJBQW1CLEVBQUUsT0FBTyxJQUFJLFlBQVksRUFBRSxNQUFNLG1CQUFtQixDQUFDO0FBRWpGLE9BQU8sRUFBRSxPQUFPLElBQUksS0FBSyxFQUFFLGlCQUFpQixFQUFFLE1BQU0sWUFBWSxDQUFDO0FBRWpFLDRCQUE0QiJ9
|
|
@@ -0,0 +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
|
+
|
|
19
|
+
export type BlockArg = (string | number | bigint | boolean | Buffer);
|
package/esm.mjs
ADDED
|
@@ -0,0 +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
|
|
19
|
+
};
|
package/lib/Block.ts
ADDED
|
@@ -0,0 +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],
|
|
217
|
+
} as const;
|