Skip to content

Commit

Permalink
Merge pull request #143 from Promises/console-commands
Browse files Browse the repository at this point in the history
send command response to console if the command was issued from the console
  • Loading branch information
TheBlackParade authored Apr 1, 2020
2 parents 06b3a07 + 6056058 commit f061ea8
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 33 deletions.
1 change: 1 addition & 0 deletions src/net/incoming-packet-directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const ignore = [ 234, 160, 216, 13, 58 /* camera move */ ];
const packets: { [key: number]: incomingPacket } = {
75: chatPacket,
248: commandPacket,
246: commandPacket,

73: walkPacket,
236: walkPacket,
Expand Down
2 changes: 1 addition & 1 deletion src/net/incoming-packet-sizes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ export const incomingPacketSizes: number[] = [
-3, -3, -3, -3, -3, -3, 0, -3, -3, -3, //210
-3, -3, -3, -3, -3, -3, -3, -3, 8, -3, //220
-3, 13, -3, -3, 4, -3, -1, -3, 4, -3, //230
-3, -3, -3, -3, -3, -3, -3, -3, -1, -3, //240
-3, -3, -3, -3, -3, -1, -3, -3, -1, -3, //240
-3, -3, -3, -3, -3, -3, -3 //250
];
5 changes: 3 additions & 2 deletions src/net/incoming-packets/command-packet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import { ByteBuffer } from '@runejs/byte-buffer';
export const commandPacket: incomingPacket = (player: Player, packetId: number, packetSize: number, packet: ByteBuffer): void => {
const input = packet.getString();

if(!input || input.trim().length === 0) {
if (!input || input.trim().length === 0) {
return;
}
const isConsole = packetId == 246;

const args = input.trim().split(' ');
const command = args[0];

args.splice(0, 1);

inputCommandAction(player, command, args);
inputCommandAction(player, command, isConsole, args);
};
7 changes: 7 additions & 0 deletions src/net/outgoing-packets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,13 @@ export class OutgoingPackets {
this.queue(packet);
}

public consoleMessage(message: string): void {
const packet = new Packet(83, PacketType.DYNAMIC_SMALL);
packet.putString(message);

this.queue(packet);
}

public updateSkill(skillId: number, level: number, exp: number): void {
const packet = new Packet(34);
packet.put(level);
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/commands/current-position-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { commandAction } from '@server/world/actor/player/action/input-command-a

const action: commandAction = (details) => {
const { player } = details;
player.sendMessage(`@[ ${player.position.x}, ${player.position.y}, ${player.position.level} ]`);
player.sendLogMessage(`@[ ${player.position.x}, ${player.position.y}, ${player.position.level} ]`, details.isConsole);
};

export default new RunePlugin({
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/commands/give-item-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const action: commandAction = (details) => {
const inventorySlot = player.inventory.getFirstOpenSlot();

if(inventorySlot === -1) {
player.sendMessage(`You don't have enough free space to do that.`);
player.sendLogMessage(`You don't have enough free space to do that.`, details.isConsole);
return;
}

Expand Down Expand Up @@ -43,7 +43,8 @@ const action: commandAction = (details) => {
}
}

player.sendMessage(`Added ${actualAmount}x ${itemDefinition.name} to inventory.`);
player.sendLogMessage(`Added ${actualAmount}x ${itemDefinition.name} to inventory.`, details.isConsole);

};

export default new RunePlugin({
Expand Down
7 changes: 4 additions & 3 deletions src/plugins/commands/reload-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import { injectPlugins } from '@server/game-server';
const action: commandAction = (details) => {
const { player } = details;

player.sendMessage('Reloading plugins...');
player.sendLogMessage('Reloading plugins...', details.isConsole);


injectPlugins()
.then(() => player.sendMessage('Plugins reloaded.'))
.catch(() => player.sendMessage('Error reloading plugins.'));
.then(() => player.sendLogMessage('Plugins reloaded.', details.isConsole))
.catch(() => player.sendLogMessage('Error reloading plugins.', details.isConsole));
};

export default new RunePlugin({ type: ActionType.COMMAND, commands: 'plugins', action });
6 changes: 4 additions & 2 deletions src/plugins/commands/tracking-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ const quadtreeAction: commandAction = (details) => {

const trackedPlayersAction: commandAction = (details) => {
const { player } = details;
player.sendMessage(`Tracked players: ${player.trackedPlayers.length}`);
player.sendLogMessage(`Tracked players: ${player.trackedPlayers.length}`, details.isConsole);

};

const trackedNpcsAction: commandAction = (details) => {
const { player } = details;
player.sendMessage(`Tracked npcs: ${player.trackedNpcs.length}`);
player.sendLogMessage(`Tracked npcs: ${player.trackedNpcs.length}`, details.isConsole);

};

export default new RunePlugin([{
Expand Down
45 changes: 23 additions & 22 deletions src/world/actor/player/action/input-command-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface CommandActionDetails {
player: Player;
// The command that the player entered.
command: string;
// If the player used the console
isConsole: boolean;
// The arguments that the player entered for their command.
args: { [key: string]: number | string };
}
Expand All @@ -37,8 +39,7 @@ export interface CommandActionPlugin extends ActionPlugin {
/**
* A directory of all command interaction plugins.
*/
let commandInteractions: CommandActionPlugin[] = [
];
let commandInteractions: CommandActionPlugin[] = [];

/**
* Sets the list of command interaction plugins.
Expand All @@ -48,23 +49,23 @@ export const setCommandPlugins = (plugins: ActionPlugin[]): void => {
commandInteractions = plugins as CommandActionPlugin[];
};

export const inputCommandAction = (player: Player, command: string, inputArgs: string[]): void => {
export const inputCommandAction = (player: Player, command: string, isConsole: boolean, inputArgs: string[]): void => {
const plugins = commandInteractions.filter(plugin => {
if(Array.isArray(plugin.commands)) {
if (Array.isArray(plugin.commands)) {
return plugin.commands.indexOf(command) !== -1;
} else {
return plugin.commands === command;
}
});

if(plugins.length === 0) {
player.outgoingPackets.chatboxMessage(`Unhandled command: ${command}`);
if (plugins.length === 0) {
player.sendLogMessage(`Unhandled command: ${command}`, isConsole);
return;
}

plugins.forEach(plugin => {
try {
if(plugin.args) {
if (plugin.args) {
const pluginArgs = plugin.args;
let syntaxError = `Syntax error. Try ::${command}`;

Expand All @@ -73,34 +74,34 @@ export const inputCommandAction = (player: Player, command: string, inputArgs: s
});

const requiredArgLength = plugin.args.filter(arg => arg.defaultValue !== undefined).length;
if(requiredArgLength > inputArgs.length) {
player.outgoingPackets.chatboxMessage(syntaxError);
if (requiredArgLength > inputArgs.length) {
player.sendLogMessage(syntaxError, isConsole);
return;
}

const actionArgs = {};

for(let i = 0; i < plugin.args.length; i++) {
for (let i = 0; i < plugin.args.length; i++) {
let argValue: string | number = inputArgs[i] || null;
const pluginArg = plugin.args[i];

if(argValue === null) {
if(pluginArg.defaultValue === undefined) {
player.outgoingPackets.chatboxMessage(syntaxError);
if (argValue === null) {
if (pluginArg.defaultValue === undefined) {
player.sendLogMessage(syntaxError, isConsole);
return;
} else {
argValue = pluginArg.defaultValue;
}
} else {
if(pluginArg.type === 'number') {
if (pluginArg.type === 'number') {
argValue = parseInt(argValue);
if(isNaN(argValue)) {
player.outgoingPackets.chatboxMessage(syntaxError);
if (isNaN(argValue)) {
player.sendLogMessage(syntaxError, isConsole);
return;
}
} else {
if(!argValue || argValue.trim() === '') {
player.outgoingPackets.chatboxMessage(syntaxError);
if (!argValue || argValue.trim() === '') {
player.sendLogMessage(syntaxError, isConsole);
return;
}
}
Expand All @@ -109,12 +110,12 @@ export const inputCommandAction = (player: Player, command: string, inputArgs: s
actionArgs[pluginArg.name] = argValue;
}

plugin.action({player, command, args: actionArgs});
plugin.action({player, command, isConsole, args: actionArgs});
} else {
plugin.action({player, command, args: {}});
plugin.action({player, command, isConsole, args: {}});
}
} catch(commandError) {
player.outgoingPackets.chatboxMessage(`Command error: ${commandError}`);
} catch (commandError) {
player.sendLogMessage(`Command error: ${commandError}`, isConsole);
}
});
};
8 changes: 8 additions & 0 deletions src/world/actor/player/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,14 @@ export class Player extends Actor {
}
}

public sendLogMessage(message: string, isConsole: boolean): void {
if(isConsole) {
this.outgoingPackets.consoleMessage(message);
} else {
this.outgoingPackets.chatboxMessage(message);
}
}

/**
* Closes the currently active widget or widget pair.
* @param notifyClient [optional] Whether or not to notify the game client that widgets should be cleared. Defaults to true.
Expand Down

0 comments on commit f061ea8

Please sign in to comment.