Skip to content

Commit 8ed307e

Browse files
committed
send command response to console if the command was issued from the console
1 parent 06b3a07 commit 8ed307e

File tree

9 files changed

+54
-32
lines changed

9 files changed

+54
-32
lines changed

src/net/incoming-packet-directory.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const ignore = [ 234, 160, 216, 13, 58 /* camera move */ ];
2727
const packets: { [key: number]: incomingPacket } = {
2828
75: chatPacket,
2929
248: commandPacket,
30+
246: commandPacket,
3031

3132
73: walkPacket,
3233
236: walkPacket,

src/net/incoming-packets/command-packet.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import { ByteBuffer } from '@runejs/byte-buffer';
66
export const commandPacket: incomingPacket = (player: Player, packetId: number, packetSize: number, packet: ByteBuffer): void => {
77
const input = packet.getString();
88

9-
if(!input || input.trim().length === 0) {
9+
if (!input || input.trim().length === 0) {
1010
return;
1111
}
12+
const isConsole = packetId == 246;
1213

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

1617
args.splice(0, 1);
1718

18-
inputCommandAction(player, command, args);
19+
inputCommandAction(player, command, isConsole, args);
1920
};

src/net/outgoing-packets.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,13 @@ export class OutgoingPackets {
444444
this.queue(packet);
445445
}
446446

447+
public consoleMessage(message: string): void {
448+
const packet = new Packet(83, PacketType.DYNAMIC_SMALL);
449+
packet.putString(message);
450+
451+
this.queue(packet);
452+
}
453+
447454
public updateSkill(skillId: number, level: number, exp: number): void {
448455
const packet = new Packet(34);
449456
packet.put(level);

src/plugins/commands/current-position-command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { commandAction } from '@server/world/actor/player/action/input-command-a
33

44
const action: commandAction = (details) => {
55
const { player } = details;
6-
player.sendMessage(`@[ ${player.position.x}, ${player.position.y}, ${player.position.level} ]`);
6+
player.sendLogMessage(`@[ ${player.position.x}, ${player.position.y}, ${player.position.level} ]`, details.isConsole);
77
};
88

99
export default new RunePlugin({

src/plugins/commands/give-item-command.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const action: commandAction = (details) => {
88
const inventorySlot = player.inventory.getFirstOpenSlot();
99

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

@@ -43,7 +43,8 @@ const action: commandAction = (details) => {
4343
}
4444
}
4545

46-
player.sendMessage(`Added ${actualAmount}x ${itemDefinition.name} to inventory.`);
46+
player.sendLogMessage(`Added ${actualAmount}x ${itemDefinition.name} to inventory.`, details.isConsole);
47+
4748
};
4849

4950
export default new RunePlugin({

src/plugins/commands/reload-plugins.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import { injectPlugins } from '@server/game-server';
55
const action: commandAction = (details) => {
66
const { player } = details;
77

8-
player.sendMessage('Reloading plugins...');
8+
player.sendLogMessage('Reloading plugins...', details.isConsole);
9+
910

1011
injectPlugins()
11-
.then(() => player.sendMessage('Plugins reloaded.'))
12-
.catch(() => player.sendMessage('Error reloading plugins.'));
12+
.then(() => player.sendLogMessage('Plugins reloaded.', details.isConsole))
13+
.catch(() => player.sendLogMessage('Error reloading plugins.', details.isConsole));
1314
};
1415

1516
export default new RunePlugin({ type: ActionType.COMMAND, commands: 'plugins', action });

src/plugins/commands/tracking-commands.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ const quadtreeAction: commandAction = (details) => {
1717

1818
const trackedPlayersAction: commandAction = (details) => {
1919
const { player } = details;
20-
player.sendMessage(`Tracked players: ${player.trackedPlayers.length}`);
20+
player.sendLogMessage(`Tracked players: ${player.trackedPlayers.length}`, details.isConsole);
21+
2122
};
2223

2324
const trackedNpcsAction: commandAction = (details) => {
2425
const { player } = details;
25-
player.sendMessage(`Tracked npcs: ${player.trackedNpcs.length}`);
26+
player.sendLogMessage(`Tracked npcs: ${player.trackedNpcs.length}`, details.isConsole);
27+
2628
};
2729

2830
export default new RunePlugin([{

src/world/actor/player/action/input-command-action.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export interface CommandActionDetails {
1414
player: Player;
1515
// The command that the player entered.
1616
command: string;
17+
// If the player used the console
18+
isConsole: boolean;
1719
// The arguments that the player entered for their command.
1820
args: { [key: string]: number | string };
1921
}
@@ -37,8 +39,7 @@ export interface CommandActionPlugin extends ActionPlugin {
3739
/**
3840
* A directory of all command interaction plugins.
3941
*/
40-
let commandInteractions: CommandActionPlugin[] = [
41-
];
42+
let commandInteractions: CommandActionPlugin[] = [];
4243

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

51-
export const inputCommandAction = (player: Player, command: string, inputArgs: string[]): void => {
52+
export const inputCommandAction = (player: Player, command: string, isConsole: boolean, inputArgs: string[]): void => {
5253
const plugins = commandInteractions.filter(plugin => {
53-
if(Array.isArray(plugin.commands)) {
54+
if (Array.isArray(plugin.commands)) {
5455
return plugin.commands.indexOf(command) !== -1;
5556
} else {
5657
return plugin.commands === command;
5758
}
5859
});
5960

60-
if(plugins.length === 0) {
61-
player.outgoingPackets.chatboxMessage(`Unhandled command: ${command}`);
61+
if (plugins.length === 0) {
62+
player.sendLogMessage(`Unhandled command: ${command}`, isConsole);
6263
return;
6364
}
6465

6566
plugins.forEach(plugin => {
6667
try {
67-
if(plugin.args) {
68+
if (plugin.args) {
6869
const pluginArgs = plugin.args;
6970
let syntaxError = `Syntax error. Try ::${command}`;
7071

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

7576
const requiredArgLength = plugin.args.filter(arg => arg.defaultValue !== undefined).length;
76-
if(requiredArgLength > inputArgs.length) {
77-
player.outgoingPackets.chatboxMessage(syntaxError);
77+
if (requiredArgLength > inputArgs.length) {
78+
player.sendLogMessage(syntaxError, isConsole);
7879
return;
7980
}
8081

8182
const actionArgs = {};
8283

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

87-
if(argValue === null) {
88-
if(pluginArg.defaultValue === undefined) {
89-
player.outgoingPackets.chatboxMessage(syntaxError);
88+
if (argValue === null) {
89+
if (pluginArg.defaultValue === undefined) {
90+
player.sendLogMessage(syntaxError, isConsole);
9091
return;
9192
} else {
9293
argValue = pluginArg.defaultValue;
9394
}
9495
} else {
95-
if(pluginArg.type === 'number') {
96+
if (pluginArg.type === 'number') {
9697
argValue = parseInt(argValue);
97-
if(isNaN(argValue)) {
98-
player.outgoingPackets.chatboxMessage(syntaxError);
98+
if (isNaN(argValue)) {
99+
player.sendLogMessage(syntaxError, isConsole);
99100
return;
100101
}
101102
} else {
102-
if(!argValue || argValue.trim() === '') {
103-
player.outgoingPackets.chatboxMessage(syntaxError);
103+
if (!argValue || argValue.trim() === '') {
104+
player.sendLogMessage(syntaxError, isConsole);
104105
return;
105106
}
106107
}
@@ -109,12 +110,12 @@ export const inputCommandAction = (player: Player, command: string, inputArgs: s
109110
actionArgs[pluginArg.name] = argValue;
110111
}
111112

112-
plugin.action({player, command, args: actionArgs});
113+
plugin.action({player, command, isConsole, args: actionArgs});
113114
} else {
114-
plugin.action({player, command, args: {}});
115+
plugin.action({player, command, isConsole, args: {}});
115116
}
116-
} catch(commandError) {
117-
player.outgoingPackets.chatboxMessage(`Command error: ${commandError}`);
117+
} catch (commandError) {
118+
player.sendLogMessage(`Command error: ${commandError}`, isConsole);
118119
}
119120
});
120121
};

src/world/actor/player/player.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,14 @@ export class Player extends Actor {
736736
}
737737
}
738738

739+
public sendLogMessage(message: string, isConsole: boolean): void {
740+
if(isConsole) {
741+
this.outgoingPackets.consoleMessage(message);
742+
} else {
743+
this.outgoingPackets.chatboxMessage(message);
744+
}
745+
}
746+
739747
/**
740748
* Closes the currently active widget or widget pair.
741749
* @param notifyClient [optional] Whether or not to notify the game client that widgets should be cleared. Defaults to true.

0 commit comments

Comments
 (0)