@nativewrappers/server 0.0.129 → 0.0.131

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/Game.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- import { Player } from "./entities/Player";
2
1
  export declare abstract class Game {
3
2
  static hashCache: Map<string, number>;
4
3
  /**
@@ -13,13 +12,7 @@ export declare abstract class Game {
13
12
  static get GameTime(): number;
14
13
  static get GameBuild(): number;
15
14
  static get GameName(): string;
16
- static registerCommand(name: string, handler: (player: Player, args: any[]) => void, restricted?: boolean): void;
17
15
  static get RegisteredCommands(): [{
18
16
  name: string;
19
17
  }];
20
- /**
21
- * Get an iterable list of players currently on the server.
22
- * @returns Iterable list of Player objects.
23
- */
24
- static PlayerList(): IterableIterator<Player>;
25
18
  }
package/Game.js CHANGED
@@ -1,5 +1,6 @@
1
1
  var __defProp = Object.defineProperty;
2
2
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+ import { Delay } from "./common/utils/Delay";
3
4
  import { Player } from "./entities/Player";
4
5
  class Game {
5
6
  static {
@@ -34,28 +35,9 @@ class Game {
34
35
  static get GameName() {
35
36
  return GetGameName();
36
37
  }
37
- static registerCommand(name, handler, restricted = false) {
38
- RegisterCommand(
39
- name,
40
- (source, args) => {
41
- const player = new Player(Number.parseInt(source));
42
- handler(player, args);
43
- },
44
- restricted
45
- );
46
- }
47
38
  static get RegisteredCommands() {
48
39
  return GetRegisteredCommands();
49
40
  }
50
- /**
51
- * Get an iterable list of players currently on the server.
52
- * @returns Iterable list of Player objects.
53
- */
54
- static *PlayerList() {
55
- for (const id of getPlayers()) {
56
- yield new Player(id);
57
- }
58
- }
59
41
  }
60
42
  export {
61
43
  Game
@@ -70,10 +70,12 @@ class NetworkedMap extends Map {
70
70
  }
71
71
  /*
72
72
  * Resyncs the entire map to the client, useful for if there's a mismatch in the clients map (when multiple players change things, in cases like inventories)
73
- *
74
- * NOTE: This doesn't check that the player is already subscribed to the map, you should do your own due-diligence to only call this for players already subscribed
75
73
  */
76
74
  resync(source2) {
75
+ if (!this.#subscribers.has(source2)) {
76
+ console.error(`[NetworkedMap:resync] Tried to call resync on a source that wasn't already subscribed`);
77
+ return;
78
+ }
77
79
  const packed_data = msgpack_pack([this.#syncName, [[4 /* Init */, this.size === 0 ? [] : Array.from(this)]]]);
78
80
  TriggerClientEventInternal(
79
81
  `${GlobalData.CurrentResource}:syncChanges`,
@@ -83,7 +85,7 @@ class NetworkedMap extends Map {
83
85
  );
84
86
  }
85
87
  /*
86
- * Adds a new subscriber to the map
88
+ * Adds a new subscriber to the map, this will automatically call resync on the client.
87
89
  */
88
90
  addSubscriber(source2) {
89
91
  this.#subscribers.add(source2);
@@ -155,10 +157,45 @@ class NetworkedMap extends Map {
155
157
  ln(value);
156
158
  }
157
159
  }
160
+ /*
161
+ * Adds {@param value} to the map under {@param key}, {@param value} will automatically be turned into a proxy
162
+ * object and the NetworkedMap will automatically send events on SubValue changes.
163
+ * ```ts
164
+ *
165
+ * const map = new NetworkedMap<number, { name:string , quantity: number, obj: { nested_obj_description: string }}>("someUniqueName");
166
+ *
167
+ * const item = {
168
+ * name: "magic_item",
169
+ * quantity: 1,
170
+ * obj: {
171
+ * something_description: "Will never be updated after init"
172
+ * }
173
+ * }
174
+ *
175
+ * // automatically converts `item` into a proxy object
176
+ * map.set(1, item)
177
+ *
178
+ * // if you immediately change the value this will not be recognized, you have to get the object again from the map
179
+ * item.quantity = 2;
180
+ *
181
+ * // reactive object
182
+ * const proxy_object = map.get(1)!;
183
+ *
184
+ * // sub value change will be sent to the client on the next network tick.
185
+ * proxy_object.quantity = 2;
186
+ *
187
+ * // NOTE: this doesn't work on deeply nested objects, it will only work on anything assigned to the first object
188
+ * // This update will never be sent to the client, similar to how state bags work.
189
+ * proxy_object.obj.nested_obj_description = "Some new value"
190
+ * ```
191
+ * i.e. if you have { name: "magic_item", quantity: 1}
192
+ * if you change the item.quantity field this will automatically be sync'd to the client, is only one depth deep
193
+ * so if you have { obj: { some_quantity: 1 } } setting item.obj.some_quantity will not update the client.
194
+ */
158
195
  set(key, value) {
159
196
  let v = value;
160
197
  if (value instanceof Object) {
161
- const curMap = this;
198
+ const curThis = this;
162
199
  const objectChangeHandler = {
163
200
  get(target, prop, reciever) {
164
201
  return Reflect.get(target, prop, reciever);
@@ -166,9 +203,9 @@ class NetworkedMap extends Map {
166
203
  set(target, p, newValue, receiver) {
167
204
  const success = Reflect.set(target, p, newValue, receiver);
168
205
  if (success) {
169
- curMap.#pushChangeForListener(key, target);
206
+ curThis.#pushChangeForListener(key, target);
170
207
  $SERVER: {
171
- curMap.#queuedChanges.push([0 /* SubValueChanged */, key, p, newValue]);
208
+ curThis.#queuedChanges.push([0 /* SubValueChanged */, key, p, newValue]);
172
209
  }
173
210
  }
174
211
  return success;
@@ -22,6 +22,10 @@ export declare class BaseEntity {
22
22
  */
23
23
  get AttachedTo(): BaseEntity | null;
24
24
  get Position(): Vector3;
25
+ /**
26
+ * This is an RPC native, meaning that it can fail to work.
27
+ */
28
+ set Position(pos: Vector3);
25
29
  get Heading(): number;
26
30
  get PositionAndHeading(): Vector4;
27
31
  get Health(): number;
@@ -70,6 +70,12 @@ class BaseEntity {
70
70
  get Position() {
71
71
  return Vector3.fromArray(GetEntityCoords(this.handle));
72
72
  }
73
+ /**
74
+ * This is an RPC native, meaning that it can fail to work.
75
+ */
76
+ set Position(pos) {
77
+ SetEntityCoords(this.handle, pos.x, pos.y, pos.z, false, false, false, false);
78
+ }
73
79
  get Heading() {
74
80
  return GetEntityHeading(this.handle);
75
81
  }
package/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./Game";
2
+ export * from "./utils/waitForEntityToExist";
2
3
  export * from "./type/Anticheat";
3
4
  export * from "./type/Hash";
4
5
  export * from "./enum/OrphanMode";
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./Game";
2
+ export * from "./utils/waitForEntityToExist";
2
3
  export * from "./type/Anticheat";
3
4
  export * from "./type/Hash";
4
5
  export * from "./enum/OrphanMode";
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  ],
9
9
  "license": "MIT",
10
10
  "type": "module",
11
- "version": "0.0.129",
11
+ "version": "0.0.131",
12
12
  "repository": {
13
13
  "type": "git",
14
14
  "url": "https://github.com/nativewrappers/nativewrappers.git"
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Waits for the specified {@param netId} to exist, useful for when you're creating an entity on the client
3
+ * ane sending it to the server, as it can "race" the net event, resulting in it never getting created.
4
+ * @param netId the network id of the entity to wait to exist
5
+ * @param [timeoutMs=1000] the amount of time to wait for the entity to exist before giving up
6
+ */
7
+ export declare function waitForEntityToExist(netId: number, timeoutMs?: number): Promise<number | null>;
@@ -0,0 +1,19 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+ import { Delay } from "../common/utils/Delay";
4
+ async function waitForEntityToExist(netId, timeoutMs = 1e3) {
5
+ const startTime = GetGameTimer();
6
+ const timeout = startTime + timeoutMs;
7
+ while (GetGameTimer() < timeout) {
8
+ const entity = NetworkGetEntityFromNetworkId(netId);
9
+ if (entity !== 0) {
10
+ return entity;
11
+ }
12
+ await Delay(0);
13
+ }
14
+ return null;
15
+ }
16
+ __name(waitForEntityToExist, "waitForEntityToExist");
17
+ export {
18
+ waitForEntityToExist
19
+ };