Skip to content

Commit

Permalink
Merge pull request #12 from rune-js/commands
Browse files Browse the repository at this point in the history
Adding a player input command handling system.
  • Loading branch information
TheBlackParade authored Dec 29, 2019
2 parents 18efd53 + b1ac8f7 commit 96a54b1
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function unequipItem(player: Player, inventory: ItemContainer, equipment: ItemCo
const inventorySlot = inventory.getFirstOpenSlot();

if(inventorySlot === -1) {
player.packetSender.sendChatboxMessage(`You don't have enough free space to do that.`);
player.packetSender.chatboxMessage(`You don't have enough free space to do that.`);
return false;
}

Expand Down
83 changes: 83 additions & 0 deletions src/server/world/entity/mob/player/action/input-command-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Player } from '../player';
import { logger } from '@runejs/logger/dist/logger';
import { world } from '../../../../../game-server';
import { throws } from 'assert';
import { interfaceIds } from '../game-interface';

type commandHandler = (player: Player, args?: string[]) => void;

const commands: { [key: string]: commandHandler } = {

pos: (player: Player) => {
player.packetSender.chatboxMessage(`@[ ${player.position.x}, ${player.position.y}, ${player.position.level} ]`);
},

move: (player: Player, args: string[]) => {
if(args.length < 2 || args.length > 3) {
throw `move x y [level]`;
}

const x: number = parseInt(args[0], 10);
const y: number = parseInt(args[1], 10);
let level: number = 0;

if(args.length === 3) {
level = parseInt(args[2]);
}

if(isNaN(x) || isNaN(y) || isNaN(level)) {
throw `move x y [level]`;
}

const oldChunk = world.chunkManager.getChunkForWorldPosition(player.position);

player.position.move(x, y, level);

const newChunk = world.chunkManager.getChunkForWorldPosition(player.position);

if(!oldChunk.equals(newChunk)) {
oldChunk.removePlayer(player);
newChunk.addPlayer(player);
player.packetSender.sendCurrentMapRegion();
}

player.updateFlags.mapRegionUpdateRequired = true;
},

give: (player: Player, args: string[]) => {
if(args.length !== 1) {
throw `give itemId`;
}

const inventorySlot = player.inventory.getFirstOpenSlot();

if(inventorySlot === -1) {
player.packetSender.chatboxMessage(`You don't have enough free space to do that.`);
return;
}

const itemId: number = parseInt(args[0]);

if(isNaN(itemId)) {
throw `give itemId`;
}

const item = { itemId, amount: 1 };
player.inventory.add(item);
player.packetSender.sendUpdateSingleInterfaceItem(interfaceIds.inventory, inventorySlot, item);
player.packetSender.chatboxMessage(`Adding 1x ${world.itemData.get(itemId).name} to inventory.`);
}

};

export const inputCommandAction = (player: Player, command: string, args: string[]): void => {
if(commands.hasOwnProperty(command)) {
try {
commands[command](player, args);
} catch(invalidSyntaxError) {
player.packetSender.chatboxMessage(`Invalid command syntax, try ::${invalidSyntaxError}`);
}
} else {
logger.info(`Unhandled command ${command} with arguments ${JSON.stringify(args)}.`);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const unequipItemAction = (player: Player, itemId: number, equipmentSlot:
const inventorySlot = inventory.getFirstOpenSlot();

if(inventorySlot === -1) {
player.packetSender.sendChatboxMessage(`You don't have enough free space to do that.`);
player.packetSender.chatboxMessage(`You don't have enough free space to do that.`);
return;
}

Expand Down
19 changes: 19 additions & 0 deletions src/server/world/entity/mob/player/packet/impl/command-packet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { incomingPacket } from '../incoming-packet';
import { Player } from '../../player';
import { RsBuffer } from '../../../../../../net/rs-buffer';
import { inputCommandAction } from '../../action/input-command-action';

export const commandPacket: incomingPacket = (player: Player, packetId: number, packetSize: number, packet: RsBuffer): void => {
const input = packet.readString();

if(!input || input.trim().length === 0) {
return;
}

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

args.splice(0, 1);

inputCommandAction(player, command, args);
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { cameraTurnPacket } from './impl/camera-turn-packet';
import { buttonClickPacket } from './impl/button-click-packet';
import { walkPacket } from './impl/walk-packet';
import { itemOption1Packet } from './impl/item-option-1-packet';
import { commandPacket } from './impl/command-packet';

const packets: { [key: number]: incomingPacket } = {
19: interfaceClickPacket,
Expand All @@ -24,7 +25,9 @@ const packets: { [key: number]: incomingPacket } = {
163: characterDesignPacket,

24: itemEquipPacket,
3: itemOption1Packet
3: itemOption1Packet,

56: commandPacket
};

export function handlePacket(player: Player, packetId: number, packetSize: number, buffer: Buffer): void {
Expand Down
2 changes: 1 addition & 1 deletion src/server/world/entity/mob/player/packet/packet-sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export class PacketSender {
this.send(new Packet(5));
}

public sendChatboxMessage(message: string): void {
public chatboxMessage(message: string): void {
const packet = new Packet(63, PacketType.DYNAMIC_SMALL);
packet.writeString(message);

Expand Down
2 changes: 1 addition & 1 deletion src/server/world/entity/mob/player/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class Player extends Mob {

this.packetSender.sendMembershipStatusAndWorldIndex();
this.packetSender.sendCurrentMapRegion();
this.packetSender.sendChatboxMessage('Welcome to RuneScape.');
this.packetSender.chatboxMessage('Welcome to RuneScape.');

DEFAULT_TAB_INTERFACES.forEach((interfaceId: number, tabIndex: number) => {
if(interfaceId !== -1) {
Expand Down

0 comments on commit 96a54b1

Please sign in to comment.