Skip to content

Commit

Permalink
feat: gamemode exit auto destroy if exist event, stream in/out callback
Browse files Browse the repository at this point in the history
  • Loading branch information
dockfries committed Oct 16, 2022
1 parent b247731 commit de3c8d7
Show file tree
Hide file tree
Showing 21 changed files with 553 additions and 130 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "omp-node-lib",
"version": "0.5.5",
"version": "0.5.6",
"description": "Better with omp-node-ts",
"main": "dist/bundle.js",
"types": "dist/bundle.d.ts",
Expand Down
8 changes: 8 additions & 0 deletions src/wrapper/streamer/3dtext/3dTextBus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { EventBus } from "@/utils/eventBus";

export enum _3dTextHooks {
created = "On3dTextCreate",
destroyed = "On3dTextDestroy",
}

export const _3dTextBus = new EventBus();
60 changes: 60 additions & 0 deletions src/wrapper/streamer/3dtext/3dTextEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import type { BasePlayer } from "@/controllers/player";
import { TCommonCallback } from "@/types";
import { StreamerItemTypes } from "omp-wrapper-streamer";
import { Streamer } from "../common";
import { Dynamic3DTextLabel } from "./base3DText";

import { _3dTextBus, _3dTextHooks } from "./3dTextBus";
import { OnGameModeExit } from "@/wrapper/native/callbacks";

export abstract class DynamicMapIconEvent<
P extends BasePlayer,
D extends Dynamic3DTextLabel
> {
private readonly _3dTexts = new Map<number, D>();
private readonly players;

constructor(playersMap: Map<number, P>) {
this.players = playersMap;
_3dTextBus.on(_3dTextHooks.created, (_3dText: D) => {
this._3dTexts.set(_3dText.id, _3dText);
});
_3dTextBus.on(_3dTextHooks.destroyed, (_3dText: D) => {
this._3dTexts.delete(_3dText.id);
});
Streamer.onItemStreamIn((type, item, player) => {
if (type === StreamerItemTypes.TEXT_3D_LABEL) {
const tl = this._3dTexts.get(item);
const p = this.players.get(player);
if (tl && p) this.onStreamIn(tl, p);
}
return 1;
});
Streamer.onItemStreamOut((type, item, player) => {
if (type === StreamerItemTypes.TEXT_3D_LABEL) {
const tl = this._3dTexts.get(item);
const p = this.players.get(player);
if (tl && p) this.onStreamOut(tl, p);
}
return 1;
});
OnGameModeExit(() => {
setTimeout(() => {
this.get3dTextLabelsArr().forEach((d) => {
d.isValid() && d.destroy();
});
});
});
}

protected abstract onStreamIn(label: D, player: P): TCommonCallback;
protected abstract onStreamOut(label: D, player: P): TCommonCallback;

public get3dTextLabelsArr(): Array<D> {
return [...this._3dTexts.values()];
}

public get3dTextLabelsMap(): Map<number, D> {
return this._3dTexts;
}
}
24 changes: 23 additions & 1 deletion src/wrapper/streamer/3dtext/base3DText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import {
DestroyDynamic3DTextLabel,
IsValidDynamic3DTextLabel,
StreamerDistances,
StreamerItemTypes,
} from "omp-wrapper-streamer";
import { Streamer } from "../common";
import { _3dTextBus, _3dTextHooks } from "./3dTextBus";

export class Dynamic3DTextLabel {
private sourceInfo: IDynamic3DTextLabel;
Expand Down Expand Up @@ -103,7 +106,7 @@ export class Dynamic3DTextLabel {
priority
);
}

_3dTextBus.emit(_3dTextHooks.created, this);
return this;
}
public destroy(): void | this {
Expand All @@ -112,6 +115,7 @@ export class Dynamic3DTextLabel {
"[Streamer3DTextLabel]: Unable to destroy before create"
);
DestroyDynamic3DTextLabel(this.id);
_3dTextBus.emit(_3dTextHooks.destroyed, this);
return this;
}
public isValid(): boolean {
Expand All @@ -136,4 +140,22 @@ export class Dynamic3DTextLabel {
this.sourceInfo.charset = charset;
return UpdateDynamic3DTextLabelText(this.id, rgba(color), text, charset);
}
public toggleCallbacks(toggle = true): void | number {
if (this.id === -1)
return logger.warn(
"[Streamer3DTextLabel]: Unable to toggle callbacks before create"
);
return Streamer.toggleItemCallbacks(
StreamerItemTypes.TEXT_3D_LABEL,
this.id,
toggle
);
}
public isToggleCallbacks(): boolean {
if (this.id === -1) false;
return Streamer.isToggleItemCallbacks(
StreamerItemTypes.TEXT_3D_LABEL,
this.id
);
}
}
8 changes: 8 additions & 0 deletions src/wrapper/streamer/actor/actorEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { BasePlayer } from "@/controllers/player";
import type { BodyPartsEnum } from "@/enums";
import { TCommonCallback } from "@/types";
import { promisifyCallback } from "@/utils/helperUtils";
import { OnGameModeExit } from "@/wrapper/native/callbacks";
import {
OnDynamicActorStreamIn,
OnDynamicActorStreamOut,
Expand Down Expand Up @@ -69,6 +70,13 @@ export abstract class DynamicActorEvent<
return pFn(p, act, amount, weaponid, bodypart);
}
);
OnGameModeExit(() => {
setTimeout(() => {
this.getActorsArr().forEach((a) => {
a.isValid() && a.destroy();
});
});
});
}

protected abstract onStreamIn(actor: A, player: P): TCommonCallback;
Expand Down
17 changes: 17 additions & 0 deletions src/wrapper/streamer/actor/baseActor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ import {
SetDynamicActorPos,
SetDynamicActorVirtualWorld,
StreamerDistances,
StreamerItemTypes,
} from "omp-wrapper-streamer";
import { Streamer } from "../common";
import { actorBus, actorHooks } from "./actorBus";

export class DynamicActor {
Expand Down Expand Up @@ -258,4 +260,19 @@ export class DynamicActor {
);
return GetActorAnimation(this.id);
}
public toggleCallbacks(toggle = true): void | number {
if (this.id === -1)
return logger.warn(
"[StreamerActor]: Unable to toggle callbacks before create"
);
return Streamer.toggleItemCallbacks(
StreamerItemTypes.ACTOR,
this.id,
toggle
);
}
public isToggleCallbacks(): boolean {
if (this.id === -1) false;
return Streamer.isToggleItemCallbacks(StreamerItemTypes.ACTOR, this.id);
}
}
37 changes: 36 additions & 1 deletion src/wrapper/streamer/area/areaEvent.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { BasePlayer, DynamicArea, TCommonCallback } from "@/main";
import { BasePlayer, DynamicArea, Streamer, TCommonCallback } from "@/main";
import { promisifyCallback } from "@/utils/helperUtils";
import { OnGameModeExit } from "@/wrapper/native/callbacks";
import {
OnPlayerEnterDynamicArea,
OnPlayerLeaveDynamicArea,
StreamerItemTypes,
} from "omp-wrapper-streamer";
import { areaBus, areaHooks } from "./areaBus";

Expand Down Expand Up @@ -47,8 +49,41 @@ export abstract class DynamicAreaEvent<
);
return pFn(p, a);
});
Streamer.onItemStreamIn((type, item, player) => {
if (type === StreamerItemTypes.AREA) {
const a = this.areas.get(item);
const p = this.players.get(player);
if (a && p) this.onStreamIn(a, p);
}
return 1;
});
Streamer.onItemStreamOut((type, item, player) => {
if (type === StreamerItemTypes.AREA) {
const a = this.areas.get(item);
const p = this.players.get(player);
if (a && p) this.onStreamOut(a, p);
}
return 1;
});
OnGameModeExit(() => {
setTimeout(() => {
this.getAreasArr().forEach((a) => {
a.isValid() && a.destroy();
});
});
});
}

protected abstract onPlayerEnter(player: P, area: A): TCommonCallback;
protected abstract onPlayerLeave(player: P, area: A): TCommonCallback;
protected abstract onStreamIn(area: A, player: P): TCommonCallback;
protected abstract onStreamOut(area: A, player: P): TCommonCallback;

public getAreasArr(): Array<A> {
return [...this.areas.values()];
}

public getAreasMap(): Map<number, A> {
return this.areas;
}
}
18 changes: 17 additions & 1 deletion src/wrapper/streamer/area/baseArea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { logger } from "@/logger";
import { InvalidEnum } from "@/enums";
import { BasePlayer } from "@/controllers/player";
import { BaseVehicle } from "@/controllers/vehicle";
import { DynamicObject } from "@/wrapper/streamer";
import { DynamicObject, Streamer } from "@/wrapper/streamer";
import { TDynamicArea, TDynamicAreaTypes } from "@/types";
import { areaBus, areaHooks } from "./areaBus";
import {
Expand Down Expand Up @@ -42,6 +42,7 @@ import {
IsToggleDynAreaSpectateMode,
IsValidDynamicArea,
StreamerAreaTypes,
StreamerItemTypes,
StreamerObjectTypes,
ToggleDynAreaSpectateMode,
} from "omp-wrapper-streamer";
Expand Down Expand Up @@ -433,4 +434,19 @@ export class DynamicArea {
if (this.id === -1) return false;
return IsToggleDynAreaSpectateMode(this.id);
}
public toggleCallbacks(toggle = true): void | number {
if (this.id === -1)
return logger.warn(
"[StreamerArea]: Unable to toggle callbacks before create"
);
return Streamer.toggleItemCallbacks(
StreamerItemTypes.AREA,
this.id,
toggle
);
}
public isToggleCallbacks(): boolean {
if (this.id === -1) false;
return Streamer.isToggleItemCallbacks(StreamerItemTypes.AREA, this.id);
}
}
13 changes: 13 additions & 0 deletions src/wrapper/streamer/checkpoint/baseCheckpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import {
IsPlayerInDynamicCP,
IsValidDynamicCP,
StreamerDistances,
StreamerItemTypes,
TogglePlayerAllDynamicCPs,
TogglePlayerDynamicCP,
} from "omp-wrapper-streamer";
import { checkPointBus, checkPointHooks } from "./checkPointBus";
import { Streamer } from "../common";

export class DynamicCheckpoint {
private sourceInfo: IDynamicCheckPoint;
Expand Down Expand Up @@ -126,4 +128,15 @@ export class DynamicCheckpoint {
>(player: P, checkpoints: Map<number, C>): C | undefined {
return checkpoints.get(GetPlayerVisibleDynamicCP(player.id));
}
public toggleCallbacks(toggle = true): void | number {
if (this.id === -1)
return logger.warn(
"[StreamerCheckpoint]: Unable to toggle callbacks before create"
);
return Streamer.toggleItemCallbacks(StreamerItemTypes.CP, this.id, toggle);
}
public isToggleCallbacks(): boolean {
if (this.id === -1) false;
return Streamer.isToggleItemCallbacks(StreamerItemTypes.CP, this.id);
}
}
28 changes: 28 additions & 0 deletions src/wrapper/streamer/checkpoint/checkPointEvent.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import type { BasePlayer } from "@/controllers/player";
import { TCommonCallback } from "@/types";
import { promisifyCallback } from "@/utils/helperUtils";
import { OnGameModeExit } from "@/wrapper/native/callbacks";
import {
OnPlayerEnterDynamicCP,
OnPlayerLeaveDynamicCP,
StreamerItemTypes,
} from "omp-wrapper-streamer";
import { Streamer } from "../common";
import { DynamicCheckpoint } from "./baseCheckpoint";
import { checkPointBus, checkPointHooks } from "./checkPointBus";

Expand Down Expand Up @@ -47,10 +50,35 @@ export abstract class DynamicCheckPointEvent<
);
return pFn(p, cp);
});
Streamer.onItemStreamIn((type, item, player) => {
if (type === StreamerItemTypes.CP) {
const cp = this.checkpoints.get(item);
const p = this.players.get(player);
if (cp && p) this.onStreamIn(cp, p);
}
return 1;
});
Streamer.onItemStreamOut((type, item, player) => {
if (type === StreamerItemTypes.CP) {
const cp = this.checkpoints.get(item);
const p = this.players.get(player);
if (cp && p) this.onStreamOut(cp, p);
}
return 1;
});
OnGameModeExit(() => {
setTimeout(() => {
this.getCheckPointsArr().forEach((cp) => {
cp.isValid() && cp.destroy();
});
});
});
}

protected abstract onPlayerEnter(player: P, checkpoint: C): TCommonCallback;
protected abstract onPlayerLeave(player: P, checkpoint: C): TCommonCallback;
protected abstract onStreamIn(checkpoint: C, player: P): TCommonCallback;
protected abstract onStreamOut(checkpoint: C, player: P): TCommonCallback;

public getCheckPointsArr(): Array<C> {
return [...this.checkpoints.values()];
Expand Down
22 changes: 5 additions & 17 deletions src/wrapper/streamer/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,8 @@ import {
} from "omp-wrapper-streamer";

export abstract class Streamer {
constructor() {
Streamer_OnItemStreamIn(this.onItemStreamIn);
Streamer_OnItemStreamOut(this.onItemStreamOut);
Streamer_OnPluginError(this.onPluginError);
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
private constructor() {}
public static getTickRate = Streamer_GetTickRate;
public static setTickRate = Streamer_SetTickRate;
public static getPlayerTickRate<P extends BasePlayer>(player: P): number {
Expand Down Expand Up @@ -318,16 +315,7 @@ export abstract class Streamer {
public static removeArrayData = Streamer_RemoveArrayData;
public static getArrayDataLength = Streamer_GetArrayDataLength;
public static getUpperBound = Streamer_GetUpperBound;

protected abstract onItemStreamIn(
type: StreamerItemTypes,
id: number,
forplayerid: number
): number;
protected abstract onItemStreamOut(
type: StreamerItemTypes,
id: number,
forplayerid: number
): number;
protected abstract onPluginError(error: string): number;
public static onItemStreamIn = Streamer_OnItemStreamIn;
public static onItemStreamOut = Streamer_OnItemStreamOut;
public static onPluginError = Streamer_OnPluginError;
}
Loading

0 comments on commit de3c8d7

Please sign in to comment.