Skip to content

Commit

Permalink
feat: local console commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Stuyk committed May 4, 2024
1 parent 6ceff5c commit ca28241
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 4 deletions.
24 changes: 24 additions & 0 deletions docs/utilities/console-commands.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Console Commands

Console commands are commands that can be ran by opening your client-side `F8` console menu and typing a command.

```sh
pos # Get local player position
floorpos # Get floor position at local player position
vehpos # Get vehicle position
rot # Get local player rotation
vehrot # Get vehicle rotation
dimension # Get local player dimension
weapons # Get local player equipped weapon hashes
weapon # Get local player weapon
id # Get local player id
remoteid # Get local player remote id
resources # Get loaded resources
ping # Get local player ping
fps # Get local player fps
debug #Check if debug is on
players # Get closest players
objects # Get closest objects
entities # Get closeset entities (Like virtual entities)
vehicles # Get closest vehicles
```
3 changes: 3 additions & 0 deletions docs/utilities/index.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
expanded: true
label: 'Utilities'
order: -1001
3 changes: 3 additions & 0 deletions docs/webview/composables/index.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
expanded: false
label: 'Composables'
order: -1000
124 changes: 124 additions & 0 deletions src/main/client/system/consoleCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import * as alt from 'alt-client';
import { distance2d } from '../../shared/utility/vector.js';

const Commands = {
pos: () => {
return alt.Player.local.pos;
},
floorpos: () => {
return alt.Player.local.pos.sub(0, 0, 1);
},
vehpos: () => {
return alt.Player.local.vehicle ? alt.Player.local.vehicle.pos : 'Not in a vehicle.';
},
rot: () => {
return alt.Player.local.rot;
},
vehrot: () => {
return alt.Player.local.vehicle ? alt.Player.local.vehicle.rot : 'Not in a vehicle.';
},
dimension: () => {
return alt.Player.local.dimension;
},
weapons: () => {
return alt.Player.local.weapons;
},
weapon: () => {
return alt.Player.local.currentWeapon;
},
id: () => {
return alt.Player.local.id;
},
remoteid: () => {
return alt.Player.local.remoteID;
},
resources: () => {
if (!alt.debug) {
return 'Unavailable';
}

return alt.getAllResources();
},
ping: () => {
return alt.getPing();
},
fps: () => {
return alt.getFps();
},
debug: () => {
return alt.debug;
},
players: () => {
if (!alt.debug) {
return 'Unavailable';
}

return alt.Player.all.map((x) => {
const dist = distance2d(alt.Player.local.pos, x.pos);
return { id: x.id, dimension: x.dimension, dist, pos: x.pos };
});
},
objects: () => {
if (!alt.debug) {
return 'Unavailable';
}

return alt.Object.all.map((x) => {
const dist = distance2d(alt.Player.local.pos, x.pos);
return { id: x.id, dimension: x.dimension, dist, pos: x.pos, model: x.model };
});
},
entities: () => {
if (!alt.debug) {
return 'Unavailable';
}

return JSON.stringify(
alt.VirtualEntity.all.map((x) => {
const dist = distance2d(alt.Player.local.pos, x.pos);
const keys = x.getStreamSyncedMetaKeys();

const data = {};
for (let key of keys) {
data[key] = x.getStreamSyncedMeta(key);
}

return { id: x.id, dimension: x.dimension, dist, pos: x.pos, data };
}),
null,
'\t'
);
},
vehicles: () => {
if (!alt.debug) {
return 'Unavailable';
}

return alt.Vehicle.all.map((x) => {
const dist = distance2d(alt.Player.local.pos, x.pos);
return { id: x.id, dimension: x.dimension, dist, pos: x.pos, model: x.model };
});
},
};

function handleConsoleCommand(name: string, ...args: string[]) {
name = name.toLowerCase();
if (!Commands[name]) {
alt.log(`That command does not exist.`);
alt.log(`Here are the supported commands:`);
for (let key of Object.keys(Commands)) {
alt.log(key);
}
return;
}

const result = Commands[name](...args);
if (!result) {
return;
}

alt.log(`${name} - Command Result`);
alt.log(result);
}

alt.on('consoleCommand', handleConsoleCommand);
1 change: 1 addition & 0 deletions src/main/client/system/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as alt from 'alt-client';

import './consoleCommand.js';
import './native.js';
import './notification.js';
import './stats.js';
Expand Down
7 changes: 3 additions & 4 deletions src/main/client/webview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ let cursorCount: number = 0;
let isPageOpen = false;
let openPages: PageNames[] = [];


function handleServerEvent(event: string, ...args: any[]) {
alt.emitServer(event, ...args);
}
Expand Down Expand Up @@ -139,7 +138,7 @@ export function useWebview(path = 'http://assets/webview/index.html') {
isPageOpen = false;
webview.emit(Events.view.hide, vueName);
unfocus();
const index = openPages.findIndex(page => page === vueName);
const index = openPages.findIndex((page) => page === vueName);
if (index > -1) openPages.splice(index, 1);
}

Expand All @@ -163,12 +162,12 @@ export function useWebview(path = 'http://assets/webview/index.html') {

/**
* Check if specific page is open.
*
*
* @param {PageNames} vueName
* @returns {boolean}
*/
function isSpecificPageOpen(vueName: PageNames): boolean {
return openPages.findIndex(page => page === vueName) > -1;
return openPages.findIndex((page) => page === vueName) > -1;
}

if (!isInitialized) {
Expand Down

0 comments on commit ca28241

Please sign in to comment.