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/lib/Player.ts
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import type { ProtoGen } from "pw-js-api";
|
|
2
|
+
import type { Point } from "./types";
|
|
3
|
+
|
|
4
|
+
export interface IPlayer {
|
|
5
|
+
/**
|
|
6
|
+
* ID of the player.
|
|
7
|
+
*/
|
|
8
|
+
playerId: number;
|
|
9
|
+
/**
|
|
10
|
+
* ID of the player's account.
|
|
11
|
+
*/
|
|
12
|
+
accountId: string;
|
|
13
|
+
/**
|
|
14
|
+
* Name of the player.
|
|
15
|
+
*/
|
|
16
|
+
username: string;
|
|
17
|
+
/**
|
|
18
|
+
* ID of the player's equipped smiley.
|
|
19
|
+
*/
|
|
20
|
+
face: number;
|
|
21
|
+
/**
|
|
22
|
+
* String, could be an admin or developer.
|
|
23
|
+
*/
|
|
24
|
+
role: string;
|
|
25
|
+
/**
|
|
26
|
+
* If player is bot user's friend.
|
|
27
|
+
*/
|
|
28
|
+
isFriend: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Position of the user.
|
|
31
|
+
*
|
|
32
|
+
* Note: This helper does not simulate physics so positions will always be inaccurate.
|
|
33
|
+
*/
|
|
34
|
+
position?: Point;
|
|
35
|
+
/**
|
|
36
|
+
* If player is the world owner.
|
|
37
|
+
*/
|
|
38
|
+
isWorldOwner: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Rights
|
|
41
|
+
*/
|
|
42
|
+
rights: IPlayerRights;
|
|
43
|
+
/**
|
|
44
|
+
* current world state.
|
|
45
|
+
*/
|
|
46
|
+
states: IPlayerWorldState;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* List of active effects the player has.
|
|
50
|
+
*/
|
|
51
|
+
effects: PlayerEffect[];
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* If this player is the bot.
|
|
55
|
+
*/
|
|
56
|
+
isMe: boolean;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface IPlayerRights {
|
|
60
|
+
/**
|
|
61
|
+
* If the player has edit rights.
|
|
62
|
+
*/
|
|
63
|
+
canEdit: boolean;
|
|
64
|
+
/**
|
|
65
|
+
* If the player has god rights.
|
|
66
|
+
*/
|
|
67
|
+
canGod: boolean;
|
|
68
|
+
/**
|
|
69
|
+
* If the player has the ability to toggle minimap.
|
|
70
|
+
*/
|
|
71
|
+
canToggleMinimap: boolean;
|
|
72
|
+
/**
|
|
73
|
+
* If the player has the ability to change the world settings.
|
|
74
|
+
*/
|
|
75
|
+
canChangeWorldSettings: boolean;
|
|
76
|
+
/**
|
|
77
|
+
* List of commands (string) the player can use.
|
|
78
|
+
*/
|
|
79
|
+
availableCommands: string[];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface IPlayerWorldState {
|
|
83
|
+
/**
|
|
84
|
+
* Number of gold coins the player has.
|
|
85
|
+
*/
|
|
86
|
+
coinsGold: number;
|
|
87
|
+
/**
|
|
88
|
+
* Number of blue coins the player has.
|
|
89
|
+
*/
|
|
90
|
+
coinsBlue: number;
|
|
91
|
+
/**
|
|
92
|
+
* Number of times the player died.
|
|
93
|
+
*/
|
|
94
|
+
deaths: number;
|
|
95
|
+
/**
|
|
96
|
+
* Coordinates of collected coins?
|
|
97
|
+
*/
|
|
98
|
+
collectedItems: Point[];
|
|
99
|
+
/**
|
|
100
|
+
* If player has gold crown on.
|
|
101
|
+
*/
|
|
102
|
+
hasGoldCrown: boolean;
|
|
103
|
+
/**
|
|
104
|
+
* If player has won the world.
|
|
105
|
+
*/
|
|
106
|
+
hasSilverCrown: boolean;
|
|
107
|
+
/**
|
|
108
|
+
* Zero indexed, map of the player's switch state.
|
|
109
|
+
*/
|
|
110
|
+
switches: boolean[];
|
|
111
|
+
/**
|
|
112
|
+
* If player is in god mode right now.
|
|
113
|
+
*/
|
|
114
|
+
godmode: boolean;
|
|
115
|
+
/**
|
|
116
|
+
* If player is in mod mode right now.
|
|
117
|
+
*/
|
|
118
|
+
modmode: boolean;
|
|
119
|
+
/**
|
|
120
|
+
* ID of the team the player is associated with right now.
|
|
121
|
+
*/
|
|
122
|
+
teamId: number;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export interface IPlayerEffect {
|
|
126
|
+
/**
|
|
127
|
+
* The ID of the effect.
|
|
128
|
+
*/
|
|
129
|
+
effectId: number;
|
|
130
|
+
/**
|
|
131
|
+
* If applicable, the duration of the effect.
|
|
132
|
+
*/
|
|
133
|
+
duration?: number;
|
|
134
|
+
/**
|
|
135
|
+
* If applicable, the strength of the effect. (For example speed or multi jump effect)
|
|
136
|
+
*/
|
|
137
|
+
strength?: number;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export default class Player {
|
|
141
|
+
/**
|
|
142
|
+
* ID of the player.
|
|
143
|
+
*/
|
|
144
|
+
playerId: number;
|
|
145
|
+
/**
|
|
146
|
+
* ID of the player's account.
|
|
147
|
+
*/
|
|
148
|
+
accountId: string;
|
|
149
|
+
/**
|
|
150
|
+
* Name of the player.
|
|
151
|
+
*/
|
|
152
|
+
username: string;
|
|
153
|
+
/**
|
|
154
|
+
* ID of the player's equipped smiley.
|
|
155
|
+
*/
|
|
156
|
+
face: number;
|
|
157
|
+
/**
|
|
158
|
+
* String, could be an admin or developer.
|
|
159
|
+
*/
|
|
160
|
+
role: string;
|
|
161
|
+
/**
|
|
162
|
+
* If player is bot user's friend.
|
|
163
|
+
*/
|
|
164
|
+
isFriend: boolean;
|
|
165
|
+
/**
|
|
166
|
+
* Position of the user.
|
|
167
|
+
*
|
|
168
|
+
* Note: This helper does not simulate physics so positions will always be inaccurate.
|
|
169
|
+
*/
|
|
170
|
+
position?: Point;
|
|
171
|
+
/**
|
|
172
|
+
* If player is the world owner.
|
|
173
|
+
*/
|
|
174
|
+
isWorldOwner: boolean;
|
|
175
|
+
/**
|
|
176
|
+
* Rights
|
|
177
|
+
*/
|
|
178
|
+
rights!: IPlayerRights;
|
|
179
|
+
/**
|
|
180
|
+
* current world state.
|
|
181
|
+
*/
|
|
182
|
+
states!: IPlayerWorldState;
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* List of active effects the player has.
|
|
186
|
+
*/
|
|
187
|
+
effects:PlayerEffect[] = [];
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* If this player is the bot.
|
|
191
|
+
*/
|
|
192
|
+
isMe: boolean = false;
|
|
193
|
+
|
|
194
|
+
constructor(props: ProtoGen.PlayerProperties, states?: IPlayerWorldState | boolean) {
|
|
195
|
+
this.accountId = props.accountId;
|
|
196
|
+
this.face = props.face;
|
|
197
|
+
this.isFriend = props.isFriend;
|
|
198
|
+
this.isWorldOwner = props.isWorldOwner;
|
|
199
|
+
this.playerId = props.playerId;
|
|
200
|
+
this.position = props.position ? {
|
|
201
|
+
x: props.position.x,
|
|
202
|
+
y: props.position.y
|
|
203
|
+
} : undefined;
|
|
204
|
+
|
|
205
|
+
if (!props.rights) this.resetRights();
|
|
206
|
+
else this.rights = {
|
|
207
|
+
availableCommands: props.rights.availableCommands,
|
|
208
|
+
canChangeWorldSettings: props.rights.canChangeWorldSettings,
|
|
209
|
+
canEdit: props.rights.canEdit,
|
|
210
|
+
canGod: props.rights.canGod,
|
|
211
|
+
canToggleMinimap: props.rights.canToggleMinimap,
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
this.role = props.role;
|
|
215
|
+
this.username = props.username;
|
|
216
|
+
|
|
217
|
+
if (typeof states === "boolean") {
|
|
218
|
+
this.isMe = states;
|
|
219
|
+
states = undefined;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (!states) {
|
|
223
|
+
// Could be bot via init that don't get states.
|
|
224
|
+
this.resetState()
|
|
225
|
+
} else this.states = {
|
|
226
|
+
coinsBlue: states.coinsBlue,
|
|
227
|
+
coinsGold: states.coinsGold,
|
|
228
|
+
collectedItems: states.collectedItems.map(v => ({ x: v.x, y: v.y })),
|
|
229
|
+
deaths: states.deaths,
|
|
230
|
+
godmode: states.godmode,
|
|
231
|
+
hasGoldCrown: states.hasGoldCrown,
|
|
232
|
+
hasSilverCrown: states.hasSilverCrown,
|
|
233
|
+
modmode: states.modmode,
|
|
234
|
+
switches: states.switches,
|
|
235
|
+
teamId: states.teamId,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* This is destructive, this is only for on reset packet.
|
|
241
|
+
*/
|
|
242
|
+
resetState() {
|
|
243
|
+
this.states = {
|
|
244
|
+
coinsBlue: 0,
|
|
245
|
+
coinsGold: 0,
|
|
246
|
+
collectedItems: [],
|
|
247
|
+
deaths: 0,
|
|
248
|
+
godmode: false,
|
|
249
|
+
hasGoldCrown: false,
|
|
250
|
+
hasSilverCrown: false,
|
|
251
|
+
modmode: false,
|
|
252
|
+
switches: new Array(1000).fill(false),
|
|
253
|
+
teamId: 0,
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Destructive.
|
|
259
|
+
*/
|
|
260
|
+
resetRights() {
|
|
261
|
+
this.rights = {
|
|
262
|
+
availableCommands: [],
|
|
263
|
+
canChangeWorldSettings: false,
|
|
264
|
+
canEdit: false,
|
|
265
|
+
canGod: false,
|
|
266
|
+
canToggleMinimap: false
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export class PlayerEffect {
|
|
272
|
+
/**
|
|
273
|
+
* The ID of the effect.
|
|
274
|
+
*/
|
|
275
|
+
effectId: number;
|
|
276
|
+
/**
|
|
277
|
+
* If applicable, the duration of the effect.
|
|
278
|
+
*/
|
|
279
|
+
duration?: number;
|
|
280
|
+
/**
|
|
281
|
+
* If applicable, the strength of the effect. (For example speed or multi jump effect)
|
|
282
|
+
*/
|
|
283
|
+
strength?: number;
|
|
284
|
+
/**
|
|
285
|
+
* The time the effect occurred.
|
|
286
|
+
*/
|
|
287
|
+
triggeredAt: number;
|
|
288
|
+
|
|
289
|
+
constructor(effect: IPlayerEffect, triggeredAt?: number) {
|
|
290
|
+
this.effectId = effect.effectId;
|
|
291
|
+
this.duration = effect.duration;
|
|
292
|
+
this.strength = effect.strength;
|
|
293
|
+
|
|
294
|
+
this.triggeredAt = triggeredAt ?? Date.now();
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Note: If this effect is non timed, this will always return false.
|
|
300
|
+
*/
|
|
301
|
+
get hasExpired() {
|
|
302
|
+
if (this.duration === undefined) return false;
|
|
303
|
+
|
|
304
|
+
return Date.now() > (this.triggeredAt + this.duration);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Milliseconds showing how long before this expires.
|
|
309
|
+
*
|
|
310
|
+
* Note: If this effect is non timed, this will return infinity.
|
|
311
|
+
*/
|
|
312
|
+
get remaining() {
|
|
313
|
+
if (this.duration === undefined) return Infinity;
|
|
314
|
+
|
|
315
|
+
return Math.max(0, Date.now() - (this.triggeredAt + this.duration));
|
|
316
|
+
}
|
|
317
|
+
}
|
package/lib/index.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type * from "./types/index.d.ts";
|
|
2
|
+
export { default as PWGameWorldHelper } from "./Helper.js";
|
|
3
|
+
|
|
4
|
+
export { ComponentTypeHeader, default as BufferReader } from "./BufferReader.js";
|
|
5
|
+
|
|
6
|
+
export { default as Block, BlockArgsHeadings } from "./Block.js";
|
|
7
|
+
|
|
8
|
+
// import * from "./Helper";
|
|
@@ -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/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pw-js-world",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "An optional package for PW-JS-Api, aims to serve world purposes.",
|
|
5
|
+
"main": "lib/index.ts",
|
|
6
|
+
"exports": {
|
|
7
|
+
"bun": "./dist/index.js",
|
|
8
|
+
"import": "./esm.mjs",
|
|
9
|
+
"require": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"PixelWalker",
|
|
14
|
+
"Util",
|
|
15
|
+
"Utilities",
|
|
16
|
+
"Misc"
|
|
17
|
+
],
|
|
18
|
+
"author": "Doomester",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/doomestee/pw-js-world.git"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/node": "^22.10.5",
|
|
26
|
+
"ncp": "^2.0.0",
|
|
27
|
+
"rimraf": "^6.0.1",
|
|
28
|
+
"typescript": "^5.7.3"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"tslib": "~2.8.1"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"pw-js-api": ">=0.2.1"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"test": "bun test/index.ts",
|
|
38
|
+
"build": "rimraf dist && tsc -p tsconfig.json && ncp lib/types dist/types"
|
|
39
|
+
}
|
|
40
|
+
}
|
package/test/index.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { LayerType } from "../lib/Constants";
|
|
2
|
+
import { PWGameWorldHelper } from "../lib/index";
|
|
3
|
+
import { BlockNames, PWApiClient } from "pw-js-api";
|
|
4
|
+
|
|
5
|
+
const api = new PWApiClient("doomester@duck.com", "D!wr%Jy&z!uPSJ3m%5TG");
|
|
6
|
+
const helper = new PWGameWorldHelper();
|
|
7
|
+
|
|
8
|
+
await api.authenticate();
|
|
9
|
+
|
|
10
|
+
const con = await api.joinWorld("rfdeeb8d9d94f76", {
|
|
11
|
+
gameSettings: {
|
|
12
|
+
handlePackets: ["PING", "INIT"]
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
con
|
|
17
|
+
// .addHook<{ playerInitPacket: { def: string } }>(() => {})
|
|
18
|
+
.addHook(helper.receiveHook)
|
|
19
|
+
.addCallback("playerInitPacket", (data, states) => {
|
|
20
|
+
// console.log(states?.player?.isMe);
|
|
21
|
+
|
|
22
|
+
states?.player?.username
|
|
23
|
+
|
|
24
|
+
con.send("worldBlockPlacedPacket",
|
|
25
|
+
|
|
26
|
+
helper.createBlockPacket(BlockNames.BASIC_GREEN, LayerType.Foreground, [{
|
|
27
|
+
x: 18,
|
|
28
|
+
y: 93
|
|
29
|
+
}, {
|
|
30
|
+
x: 19,
|
|
31
|
+
y: 94
|
|
32
|
+
}, {
|
|
33
|
+
x: 20,
|
|
34
|
+
y: 95
|
|
35
|
+
}]));
|
|
36
|
+
|
|
37
|
+
const signPack = helper.createBlockPacket(BlockNames.SIGN_BLUE, LayerType.Foreground, {
|
|
38
|
+
x: 19,
|
|
39
|
+
y: 98
|
|
40
|
+
}, new Date().toString());
|
|
41
|
+
|
|
42
|
+
console.log(signPack);
|
|
43
|
+
con.send("worldBlockPlacedPacket", signPack);
|
|
44
|
+
|
|
45
|
+
// console.log(data.globalSwitchState);
|
|
46
|
+
})
|
|
47
|
+
.addCallback("debug", egg => {
|
|
48
|
+
if (!egg.includes("ping")) console.log(egg)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
.addCallback("worldBlockPlacedPacket", (data, states) => {
|
|
52
|
+
console.log("Prev Block Id: " + states?.oldBlocks[0].bId);
|
|
53
|
+
console.log("Prev Block Args: " + states?.oldBlocks[0].args);
|
|
54
|
+
console.log("New Block Id: " + states?.newBlocks[0].bId);
|
|
55
|
+
console.log("New Block Args: " + states?.newBlocks[0].args);
|
|
56
|
+
})
|
|
57
|
+
.addCallback("playerChatPacket", (data, states) => {
|
|
58
|
+
const msg = data.message;
|
|
59
|
+
const args = msg.split(" ");
|
|
60
|
+
|
|
61
|
+
switch (args[0]) {
|
|
62
|
+
case "!at":
|
|
63
|
+
if (args.length > 2) {
|
|
64
|
+
const nums = [parseInt(args[1]), parseInt(args[2])];
|
|
65
|
+
|
|
66
|
+
if (!nums.some(v => isNaN(v))) {
|
|
67
|
+
const block = helper.getBlockAt(nums[0], nums[1], 1);
|
|
68
|
+
|
|
69
|
+
return con.send("playerChatPacket", {
|
|
70
|
+
message: "Block at (X: " + nums[0] + ", Y: " + nums[1] + ") - " + block.name + " (ID: " + block.bId + ", Args: " + block.hasArgs() + ")"
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
break;
|
|
75
|
+
case "stat":
|
|
76
|
+
{
|
|
77
|
+
const player = states?.player;
|
|
78
|
+
|
|
79
|
+
console.log({
|
|
80
|
+
isMe: player?.isMe,
|
|
81
|
+
id: player?.playerId,
|
|
82
|
+
effects: player?.effects,
|
|
83
|
+
face: player?.face,
|
|
84
|
+
states: {
|
|
85
|
+
coins: [player?.states.coinsGold, player?.states.coinsBlue],
|
|
86
|
+
deaths: player?.states.deaths,
|
|
87
|
+
collected: player?.states.collectedItems.length,
|
|
88
|
+
modes: [player?.states.godmode, player?.states.modmode],
|
|
89
|
+
teamId: player?.states.teamId
|
|
90
|
+
},
|
|
91
|
+
name: player?.username
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
})
|