UNPKG

rock-mod

Version:

Rock-Mod is a powerful framework designed for creating and managing mods for Grand Theft Auto (GTA) games.

47 lines (46 loc) 1.9 kB
import { RageBaseObjectsIterator } from "./RageBaseObjectsIterator"; import { RockMod } from "../../../RockMod"; import { ClientInternalEventName } from "@RockMod/client/net/common/events/types"; export class RageBaseObjectsManager { get baseObjects() { return this._baseObjects; } get iterator() { return this._iterator; } constructor(options) { this._baseObjects = new Map(); this._baseObjectsType = options.baseObjectsType; this._iterator = new RageBaseObjectsIterator(this._baseObjects); mp.events.add("entityDestroyed", (mpEntity) => { if (mpEntity.type === this._baseObjectsType) { const baseObject = this.getByID(mpEntity.id); this.unregisterBaseObject(baseObject); } }); } getByID(id) { const baseObject = this.findByID(id); if (!baseObject) { throw new Error(`BaseObject [${this._baseObjectsType}] with id ${id} not found`); } return baseObject; } findByID(id) { const baseObject = this._baseObjects.get(id); return baseObject !== null && baseObject !== void 0 ? baseObject : null; } registerBaseObject(baseObject) { if (this._baseObjects.has(baseObject.id)) { throw new Error(`BaseObject [${this._baseObjectsType}] with id ${baseObject.id} already exists`); } this._baseObjects.set(baseObject.id, baseObject); RockMod.instance.net.events.emitInternal(ClientInternalEventName.EntityCreated, baseObject); } unregisterBaseObject(baseObject) { if (!this._baseObjects.delete(baseObject.id)) { throw new Error(`BaseObject [${this._baseObjectsType}] with id ${baseObject.id} not found`); } RockMod.instance.net.events.emitInternal(ClientInternalEventName.EntityDestroyed, baseObject); } }