Skip to content

Commit

Permalink
feat: Add initialData for all .create methods
Browse files Browse the repository at this point in the history
  • Loading branch information
xLuxy committed Aug 20, 2024
1 parent 9ecc03b commit 6b0ea96
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 19 deletions.
32 changes: 31 additions & 1 deletion shared/js/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,32 @@ function getEntityFactory(type) {
};
}

function setupInitialData(entity, type, ctx) {
if (!ctx || !entity instanceof alt.BaseObject || !ctx.initialData) return;

const { meta, syncedMeta, streamSyncedMeta } = ctx.initialData;

if (meta) {
for (const [key, value] of Object.entries(meta)) {
entity.meta[key] = value;
}
}

if (entity instanceof alt.Entity && alt.isServer) {
if (syncedMeta) {
for (const [key, value] of Object.entries(syncedMeta)) {
entity.syncedMeta[key] = value;
}
}

if (streamSyncedMeta) {
for (const [key, value] of Object.entries(streamSyncedMeta)) {
entity.streamSyncedMeta[key] = value;
}
}
}
}

export function registerFactory(altClass, type) {
altClass.setFactory = setEntityFactory(altClass, type);
altClass.getFactory = getEntityFactory(type);
Expand All @@ -58,6 +84,10 @@ export function getFactoryCreateFunction(type, ctxCb) {
if (ctxCb) ctx = ctxCb(ctx);
assertIsObject(ctx, "Invalid args");

return cppBindings.createEntity(type, ctx);
const entity = cppBindings.createEntity(type, ctx);

if (entity) setupInitialData(entity, type, ctx);

return entity;
};
}
100 changes: 88 additions & 12 deletions types/client/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,21 @@ declare module "@altv/client" {
volume: number;
radio?: boolean; // default: false
clearCache?: boolean; // default: true

initialData?: Partial<{
meta: Record<string, unknown>;
}>;
}

type AudioEventParametersMap = {
"inited": [],
"streamStarted": [],
"streamEnded": [],
"streamPaused": [],
"streamReset": [],
"streamSeek": [time: number]
"volumeChange": [vol: number]
"error": [code: number, message: string]
inited: [];
streamStarted: [];
streamEnded: [];
streamPaused: [];
streamReset: [];
streamSeek: [time: number];
volumeChange: [vol: number];
error: [code: number, message: string];
};

export abstract class Audio extends BaseObject {
Expand Down Expand Up @@ -139,6 +143,10 @@ declare module "@altv/client" {

export interface AudioFilterCreateOptions {
hash: number | string;

initialData?: Partial<{
meta: Record<string, unknown>;
}>;
}

export abstract class AudioFilter {
Expand Down Expand Up @@ -187,6 +195,10 @@ declare module "@altv/client" {
export interface AudioOutputAttachedCreateOptions {
entity: WorldObject;
categoryHash?: number; // default: 'radio' hashed

initialData?: Partial<{
meta: Record<string, unknown>;
}>;
}

export abstract class AudioOutputAttached extends AudioOutput {
Expand All @@ -204,8 +216,11 @@ declare module "@altv/client" {
}

export interface AudioOutputFrontendCreateOptions {
//
categoryHash?: number; // default: 'radio' hashed

initialData?: Partial<{
meta: Record<string, unknown>;
}>;
}

export abstract class AudioOutputFrontend extends AudioOutput {
Expand Down Expand Up @@ -250,16 +265,33 @@ declare module "@altv/client" {
readonly syncedMeta: Readonly<altShared.BaseObjectSyncedMeta & Record<string, unknown>>;
}

export type PointBlipCreateOptions = { pos: altShared.IVector3; entity?: never } | { entity: Entity; pos?: never };
export type PointBlipCreateOptions =
| { pos: altShared.IVector3; entity?: never }
| ({ entity: Entity; pos?: never } & {
initialData?: Partial<{
meta: BlipMeta & Record<string, unknown>;
}>;
});

type BlipCreateOptions = ({ blipType: altShared.Enums.BlipType.AREA } & altShared.AreaBlipCreateOptions) | ({ blipType: altShared.Enums.BlipType.RADIUS } & altShared.RadiusBlipCreateOptions) | ({ blipType: altShared.Enums.BlipType.DESTINATION } & PointBlipCreateOptions);
type BlipCreateOptions =
| ({ blipType: altShared.Enums.BlipType.AREA } & altShared.AreaBlipCreateOptions)
| ({ blipType: altShared.Enums.BlipType.RADIUS } & altShared.RadiusBlipCreateOptions)
| (({ blipType: altShared.Enums.BlipType.DESTINATION } & PointBlipCreateOptions) & {
initialData?: Partial<{
meta: BlipMeta & Record<string, unknown>;
}>;
});

export interface MarkerCreateOptions {
type: altShared.Enums.MarkerType;
pos: altShared.IVector3;
color?: altShared.IRGBA; // default: {r: 255, g: 255, b: 255, a: 100}
useStreaming?: boolean; // default: false
streamingDistance?: number; // default: 0

initialData?: Partial<{
meta: MarkerMeta & Record<string, unknown>;
}>;
}

export abstract class Blip extends WorldObject {
Expand Down Expand Up @@ -385,6 +417,10 @@ declare module "@altv/client" {
iconColor: altShared.RGBA;
nextPos: altShared.IVector3;
streamingDistance: number;

initialData?: Partial<{
meta: CheckpointMeta & Record<string, unknown>;
}>;
}

export abstract class Checkpoint extends ColShape {
Expand Down Expand Up @@ -569,6 +605,10 @@ declare module "@altv/client" {
dynamic?: boolean; // default: false
useStreaming?: boolean; // default: false
streamingDistance?: number; // default: 0

initialData?: Partial<{
meta: ObjectMeta & Record<string, unknown>;
}>;
}

// @ts-expect-error - Suppress "Class static side 'typeof LocalObject' incorrectly extends base class static side 'typeof Object'.""
Expand Down Expand Up @@ -627,6 +667,10 @@ declare module "@altv/client" {
scale?: number; // default: 1
useStreaming?: boolean; // default: false
streamingDistance?: number; // default: 0

inititalData?: Partial<{
meta: ObjectMeta & Record<string, unknown>;
}>;
}

export namespace WeaponObject {
Expand Down Expand Up @@ -661,6 +705,10 @@ declare module "@altv/client" {
heading?: number; // default: 0
useStreaming?: boolean; // default: true
streamingDistance?: number; // default: 0

initialData?: Partial<{
meta: PedMeta & Record<string, unknown>;
}>;
}

// @ts-expect-error Suppress "Class static side 'typeof LocalPed' incorrectly extends base class static side 'typeof Ped'."
Expand Down Expand Up @@ -710,6 +758,10 @@ declare module "@altv/client" {
rot: altShared.Vector3;
useStreaming?: boolean; // default: true
streamingDistance?: number; // default: 300

initialData?: Partial<{
meta: VehicleMeta & Record<string, unknown>;
}>;
}

// @ts-expect-error Supress "Class static side 'typeof LocalVehicle' incorrectly extends base class static side 'typeof Vehicle'.""
Expand Down Expand Up @@ -815,6 +867,10 @@ declare module "@altv/client" {

export interface RmlDocumentCreateOptions {
url: string;

initialData?: Partial<{
meta: Record<string, unknown>;
}>;
}

export abstract class RmlDocument extends RmlElement {
Expand Down Expand Up @@ -1117,13 +1173,21 @@ declare module "@altv/client" {
fontScale?: number; // default: 1
useStreaming?: boolean; // default: false
streamingDistance?: number; // default: 0

initialData?: Partial<{
meta: Record<string, unknown>;
}>;
}

export interface VirtualEntityCreateOptions {
group: VirtualEntityGroup;
pos: altShared.IVector3;
streamingDistance: number;
data?: Record<string, unknown>;

initialData?: Partial<{
meta: VirtualEntityMeta & Record<string, unknown>;
}>;
}

export abstract class VirtualEntityGroup extends BaseObject {
Expand Down Expand Up @@ -1191,6 +1255,10 @@ declare module "@altv/client" {

export interface WebSocketClientCreateOptions {
url: string;

initialData?: Partial<{
meta: Record<string, unknown>;
}>;
}

export abstract class WebSocketClient extends BaseObject {
Expand Down Expand Up @@ -1251,12 +1319,20 @@ declare module "@altv/client" {
export interface _WebViewTextureCreateOptions {
drawable: number | string; // default: 0
targetTexture: string;

initialData?: Partial<{
meta: Record<string, unknown>;
}>;
}

export interface _WebViewCreateOptionsDrawable {
url: string;
drawable: number | string;
targetTexture: string;

initialData?: Partial<{
meta: Record<string, unknown>;
}>;
}

export interface _WebViewCreateOptionsOverlay {
Expand Down
2 changes: 1 addition & 1 deletion types/client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@altv/client",
"version": "0.0.41",
"version": "0.0.42",
"description": "This package contains the type definitions for the alt:V JS module v2 client types",
"types": "index.d.ts",
"files": [
Expand Down
49 changes: 47 additions & 2 deletions types/server/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ declare module "@altv/server" {
targets?: Array<Entity>;

blipType: altShared.Enums.BlipType;

initialData?: Partial<{
meta: BlipMeta & Record<string, unknown>;
syncedMeta: altShared.BlipSyncedMeta & Record<string, unknown>;
}>;
}

export type PointBlipCreateOptions = { pos: altShared.IVector3; entity?: never } | { entity: Entity; pos?: never };
Expand Down Expand Up @@ -234,6 +239,11 @@ declare module "@altv/server" {

type: altShared.Enums.MarkerType;
color?: altShared.IRGBA; // default: { r: 255, g: 255, b: 255, a: 255 }

initialData?: Partial<{
meta: MarkerMeta & Record<string, unknown>;
syncedMeta: altShared.MarkerSyncedMeta & Record<string, unknown>;
}>;
}

export interface CheckpointCreateOptions {
Expand All @@ -243,6 +253,12 @@ declare module "@altv/server" {
height: number;
color: altShared.RGBA;
streamingDistance: number;

initialData?: Partial<{
meta: CheckpointMeta & Record<string, unknown>;
syncedMeta: altShared.CheckpointSyncedMeta & Record<string, unknown>;
streamSyncedMeta: altShared.CheckpointStreamSyncedMeta & Record<string, unknown>;
}>;
}

// @ts-expect-error - Suppresses "Class static side 'typeof Checkpoint' incorrectly extends base class static side 'typeof ColShape'.""
Expand Down Expand Up @@ -327,6 +343,12 @@ declare module "@altv/server" {
textureVariation?: number; // default: 0
lodDistance?: number; // default: 100
streamingDistance?: number; // default: 0

initialData?: Partial<{
meta: ObjectMeta & Record<string, unknown>;
syncedMeta: altShared.ObjectSyncedMeta & Record<string, unknown>;
streamSyncedMeta: altShared.ObjectStreamSyncedMeta & Record<string, unknown>;
}>;
}

export abstract class Object extends Entity {
Expand Down Expand Up @@ -358,6 +380,12 @@ declare module "@altv/server" {
pos: altShared.IVector3;
heading?: number; // default: 0
streamingDistance?: number; // default: 0

initialData?: Partial<{
meta: PedMeta & Record<string, unknown>;
syncedMeta: altShared.PedSyncedMeta & Record<string, unknown>;
streamSyncedMeta: altShared.PedStreamSyncedMeta & Record<string, unknown>;
}>;
}

export abstract class Ped extends Entity {
Expand Down Expand Up @@ -576,6 +604,12 @@ declare module "@altv/server" {
pos: altShared.IVector3;
rot?: altShared.IVector3; // default: { x: 0, y: 0, z: 0 }
streamingDistance?: number; // default: 0

initialData?: Partial<{
meta: VehicleMeta & Record<string, unknown>;
syncedMeta: altShared.VehicleSyncedMeta & Record<string, unknown>;
streamSyncedMeta: altShared.VehicleStreamSyncedMeta & Record<string, unknown>;
}>;
}

export abstract class Vehicle extends Entity {
Expand Down Expand Up @@ -765,7 +799,14 @@ declare module "@altv/server" {
group: VirtualEntityGroup;
pos: altShared.IVector3;
streamingDistance: number;

data?: altShared.VirtualEntityStreamSyncedMeta & Record<string, unknown>;

initialData?: Partial<{
meta: VirtualEntityMeta & Record<string, unknown>;
// syncedMeta: altShared.VirtualEntitySyncedMeta & Record<string, unknown>;
streamSyncedMeta: altShared.VirtualEntityStreamSyncedMeta & Record<string, unknown>;
}>;
}

export abstract class VirtualEntityGroup extends BaseObject {
Expand Down Expand Up @@ -814,6 +855,10 @@ declare module "@altv/server" {
export interface VoiceChannelCreateOptions {
spatial: boolean;
maxDistance?: number;

initialData?: Partial<{
meta: VoiceChannelMeta & Record<string, unknown>;
}>;
}

export abstract class VoiceChannel extends BaseObject {
Expand All @@ -823,14 +868,14 @@ declare module "@altv/server" {
readonly players: ReadonlyArray<Player>;
readonly playerCount: number;

meta: VoiceChannelMeta & Record<string, unknown>;
meta: VoiceChannelMeta & Record<string, unknown>;
/**
Priority of voice channel. If a player is in two channels and both can be heard by another player, only one should play the voice.
Value is of type integer, can be negative.
*/
priority: number;
/**
* Hash of the filter name (should also be created on clientside).
* Hash of the filter name (should also be created on clientside).
* See docs: [Audio filters](https://docs.altv.mp/articles/audio_filters.html), [Voice](https://docs.altv.mp/articles/voice.html).
*/
filter: number;
Expand Down
Loading

0 comments on commit 6b0ea96

Please sign in to comment.