@nativewrappers/redm 0.0.128 → 0.0.130

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.
@@ -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;
package/entities/Ped.d.ts CHANGED
@@ -212,7 +212,7 @@ export declare class Ped extends BaseEntity {
212
212
  get HasPistol(): boolean;
213
213
  get HasRepeater(): boolean;
214
214
  get CurrentWeapon(): WeaponModel;
215
- giveWeapon(weapon: WeaponModel, ammoCount: number, forceInHand?: boolean, forceInHolster?: boolean, attachPoint?: WeaponAttachPoints | undefined, allowMultipleCopies?: boolean, p7?: number, p8?: number, addReason?: ItemAddReason, ignoreUnlocks?: boolean, permanentDegradation?: number, p12?: boolean): Promise<void>;
215
+ giveWeapon(weapon: WeaponModel, ammoCount: number, forceInHand?: boolean, forceInHolster?: boolean, attachPoint?: WeaponAttachPoints | undefined, allowMultipleCopies?: boolean, p7?: number, p8?: number, addReason?: ItemAddReason, ignoreUnlocks?: boolean, permanentDegradation?: number, p12?: boolean): Promise<boolean>;
216
216
  setCurrentWeapon(weapon: WeaponModel, equipNow?: boolean, attachPoint?: WeaponAttachPoints, p4?: boolean, p5?: boolean): void;
217
217
  holsterWeapon(): void;
218
218
  setWeaponOnBack(disableAnim?: boolean): void;
package/entities/Ped.js CHANGED
@@ -454,7 +454,9 @@ class Ped extends BaseEntity {
454
454
  return new WeaponModel(weapon);
455
455
  }
456
456
  async giveWeapon(weapon, ammoCount, forceInHand = true, forceInHolster = false, attachPoint = void 0, allowMultipleCopies = false, p7 = 0.5, p8 = 1, addReason = ItemAddReason.Default, ignoreUnlocks = true, permanentDegradation = 0.5, p12 = false) {
457
- await weapon.request();
457
+ if (!await weapon.request()) {
458
+ return false;
459
+ }
458
460
  attachPoint = attachPoint ?? weapon.DefaultAttachPoint;
459
461
  Citizen.invokeNative(
460
462
  "0x5E3BDDBCB83F3D84",
@@ -472,6 +474,7 @@ class Ped extends BaseEntity {
472
474
  permanentDegradation,
473
475
  p12
474
476
  );
477
+ return true;
475
478
  }
476
479
  setCurrentWeapon(weapon, equipNow = true, attachPoint = WeaponAttachPoints.HandPrimary, p4 = false, p5 = false) {
477
480
  Citizen.invokeNative("0xADF692B254977C0C", this.handle, weapon.Hash, equipNow, attachPoint, p4, p5);
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  ],
9
9
  "license": "MIT",
10
10
  "type": "module",
11
- "version": "0.0.128",
11
+ "version": "0.0.130",
12
12
  "repository": {
13
13
  "type": "git",
14
14
  "url": "https://github.com/nativewrappers/nativewrappers.git"