rock-mod
Version:
Rock-Mod is a powerful framework designed for creating and managing mods for Grand Theft Auto (GTA) games.
72 lines (71 loc) • 2.56 kB
JavaScript
import { RockMod } from "../../../RockMod";
import { BaseObjectType } from "../../../../shared/entities/IBaseObject";
export class RageEntityPoolRouter {
registerByDto(entity) {
return this._withEntityPool(entity.type, (pool) => {
const existingEntity = pool.findByRemoteID(entity.remoteId);
if (existingEntity) {
return existingEntity;
}
const mpEntity = this._getMpEntityByRemoteId(entity.type, entity.remoteId);
if (!mpEntity) {
return null;
}
return pool.registerById(mpEntity.id);
});
}
unregisterByDto(entity) {
return this._withEntityPool(entity.type, (pool) => {
const existingEntity = pool.findByRemoteID(entity.remoteId);
if (!existingEntity) {
return null;
}
return pool.unregisterById(existingEntity.id);
});
}
registerFromMp(mpEntity) {
if (!mpEntity) {
return null;
}
const entityType = mpEntity.type;
return this._withEntityPool(entityType, (pool) => pool.registerById(mpEntity.id));
}
resolveFromMp(mpEntity) {
if (!mpEntity) {
return null;
}
const entityType = mpEntity.type;
return this._withEntityPool(entityType, (pool) => pool.findByID(mpEntity.id));
}
_withEntityPool(entityType, operation) {
const rockMod = RockMod.instance;
if (!rockMod) {
return null;
}
const resolvePool = RageEntityPoolRouter._entityPoolsByType[entityType];
if (!resolvePool) {
return null;
}
return operation(resolvePool(rockMod));
}
_getMpEntityByRemoteId(entityType, remoteId) {
switch (entityType) {
case BaseObjectType.Player:
return mp.players.atRemoteId(remoteId);
case BaseObjectType.Vehicle:
return mp.vehicles.atRemoteId(remoteId);
case BaseObjectType.Object:
return mp.objects.atRemoteId(remoteId);
case BaseObjectType.Ped:
return mp.peds.atRemoteId(remoteId);
default:
return null;
}
}
}
RageEntityPoolRouter._entityPoolsByType = {
[BaseObjectType.Player]: (rockMod) => rockMod.players,
[BaseObjectType.Vehicle]: (rockMod) => rockMod.vehicles,
[BaseObjectType.Object]: (rockMod) => rockMod.objects,
[BaseObjectType.Ped]: (rockMod) => rockMod.peds,
};