Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions package/client/resource/getClosestPlayer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Vector3 } from "@nativewrappers/fivem";
import { cache } from '../../../shared/resource/cache/index';

export function getClosestPlayer(
coords: Vector3,
maxDistance: number = 2.0,
includePlayer?: boolean
): [number, number, Vector3] | undefined {
const players = GetActivePlayers();

let closestId: number | undefined;
let closestPed: number | undefined;
let closestCoords: Vector3 | undefined;

for (let i = 0; i < players.length; i++) {
const playerId = players[i];

if (includePlayer || playerId !== cache.playerId) {
const playerPed = GetPlayerPed(playerId);
const [x, y, z] = GetEntityCoords(playerPed, false);

const distance = coords.distance(new Vector3(x, y, z));
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a new Vector3 object in each iteration of the loop is inefficient. Consider reusing a single Vector3 object or using Vector3.fromArray() method which appears to be available based on similar code in the codebase (see getNearbyVehicles function). This would reduce memory allocations and improve performance in loops with many players.

Copilot uses AI. Check for mistakes.

if (distance < maxDistance) {
maxDistance = distance;
closestId = playerId;
closestPed = playerPed;
closestCoords = new Vector3(x, y, z);
}
}
}

return closestCoords ? [closestId as number, closestPed as number, closestCoords] : undefined;
}
1 change: 1 addition & 0 deletions package/client/resource/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export * from './callback';
export * from './points';
export * from './dui';
export * from './addKeybind';
export * from './getClosestPlayer'
32 changes: 32 additions & 0 deletions package/server/resource/getClosestPlayer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Vector3 } from "@nativewrappers/fivem";

export function getClosestPlayer(
coords: Vector3,
maxDistance: number = 2.0,
ignorePlayerId?: number | false
): [number, number, Vector3] | undefined {
const players = GetActivePlayers();

let closestId: number | undefined;
let closestPed: number | undefined;
let closestCoords: Vector3 | undefined;
for (let i = 0; i < players.length; i++) {
const playerId = players[i];

if (!ignorePlayerId || playerId !== ignorePlayerId) {
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for ignorePlayerId is incorrect. When ignorePlayerId is false (a valid value according to the type), the condition "!ignorePlayerId" evaluates to true, causing the player to be included when they should be ignored. Additionally, when ignorePlayerId is undefined, the condition "playerId !== ignorePlayerId" will always be true, but the outer "!ignorePlayerId" will be false, excluding all players. The logic should be: "if (ignorePlayerId === undefined || ignorePlayerId === false || playerId !== ignorePlayerId)"

Copilot uses AI. Check for mistakes.
const playerPed = GetPlayerPed(playerId);
const [x, y, z] = GetEntityCoords(playerPed, false);

const distance = coords.distance(new Vector3(x, y, z));
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a new Vector3 object in each iteration of the loop is inefficient. Consider reusing a single Vector3 object or using Vector3.fromArray() method which appears to be available based on similar code in the codebase (see getNearbyVehicles function). This would reduce memory allocations and improve performance in loops with many players.

Copilot uses AI. Check for mistakes.

if (distance < maxDistance) {
maxDistance = distance;
closestId = playerId;
closestPed = playerPed;
closestCoords = new Vector3(x, y, z);
}
}
}

return closestCoords ? [closestId as number, closestPed as number, closestCoords] : undefined;
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type uses type assertions "as number" which indicates a code smell. Since closestId and closestPed are only set when closestCoords is also set, they should be properly typed or the function logic should ensure they are never undefined when closestCoords exists. Consider initializing maxDistance to a separate variable to track the closest distance, or restructure the logic to avoid type assertions.

Copilot uses AI. Check for mistakes.
}
1 change: 1 addition & 0 deletions package/server/resource/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './locale';
export * from './callback';
export * from './version';
export * from './addCommand';
export * from './getClosestPlayer';

export function setVehicleProperties(vehicle: number, props: VehicleProperties) {
Entity(vehicle).state.set('ox_lib:setVehicleProperties', props, true)
Expand Down