rock-mod
Version:
Rock-Mod is a powerful framework designed for creating and managing mods for Grand Theft Auto (GTA) games.
67 lines (66 loc) • 2.62 kB
JavaScript
import { RageBaseObjectsIterator } from "./RageBaseObjectsIterator";
import { RockMod } from "../../../RockMod";
import { ClientInternalEventName } from "../../../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.findByID(mpEntity.id);
if (!baseObject) {
return;
}
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;
}
getByRemoteID(remoteId) {
const baseObject = this.findByRemoteID(remoteId);
if (!baseObject) {
throw new Error(`BaseObject [${this._baseObjectsType}] with id ${remoteId} not found`);
}
return baseObject;
}
findByRemoteID(remoteId) {
const baseObject = Array.from(this._baseObjects.values()).find((obj) => obj.remoteId === remoteId);
return baseObject !== null && baseObject !== void 0 ? baseObject : null;
}
deleteById(id) {
const object = this.getByID(id);
object.destroy();
this.unregisterBaseObject(object);
return object;
}
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);
}
}