Skip to content

Commit

Permalink
Merge pull request #22 from rune-js/item-swapping
Browse files Browse the repository at this point in the history
Adding the ability to swap inventory items.
  • Loading branch information
TheBlackParade authored Jan 5, 2020
2 parents 3a74085 + b185dd5 commit 2b21c92
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/net/rs-buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,14 @@ export class RsBuffer {
return this.buffer.readInt8(this.readerIndex++);
}

public readNegativeOffsetByte(): number {
public readPreNegativeOffsetByte(): number {
return 128 - (this.readByte() & 0xff);
}

public readPostNegativeOffsetByte(): number {
return (this.readByte() & 0xff) - 128;
}

public readShortBE(): number {
const value = this.buffer.readInt16BE(this.readerIndex);
this.readerIndex += 2;
Expand Down
8 changes: 8 additions & 0 deletions src/world/entity/mob/items/item-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ export class ItemContainer {
return slots;
}

public swap(fromSlot: number, toSlot: number): void {
const fromItem = this._items[fromSlot];
const toItem = this._items[toSlot];

this._items[toSlot] = fromItem;
this._items[fromSlot] = toItem;
}

public weight(): number {
let weight = 0;

Expand Down
14 changes: 14 additions & 0 deletions src/world/entity/mob/player/action/swap-item-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Player } from '../player';
import { interfaceIds } from '../game-interface';

export const swapItemAction = (player: Player, fromSlot: number, toSlot: number, interfaceId: number) => {
if(interfaceId === interfaceIds.inventory) {
const inventory = player.inventory;

if(toSlot > inventory.size - 1 || fromSlot > inventory.size - 1) {
return;
}

inventory.swap(fromSlot, toSlot);
}
};
22 changes: 22 additions & 0 deletions src/world/entity/mob/player/packet/impl/item-swap-packet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { incomingPacket } from '../incoming-packet';
import { Player } from '../../player';
import { RsBuffer } from '../../../../../../net/rs-buffer';
import { swapItemAction } from '../../action/swap-item-action';

export const itemSwapPacket: incomingPacket = (player: Player, packetId: number, packetSize: number, packet: RsBuffer): void => {
const toSlot = packet.readNegativeOffsetShortLE();
const swapType = packet.readPostNegativeOffsetByte();
const interfaceId = packet.readNegativeOffsetShortBE();
const fromSlot = packet.readShortLE();

if(toSlot < 0 || fromSlot < 0) {
return;
}

if(swapType === 0) {
// Swap
swapItemAction(player, fromSlot, toSlot, interfaceId);
} else if(swapType === 1) {
// @TODO insert
}
};
2 changes: 1 addition & 1 deletion src/world/entity/mob/player/packet/impl/walk-packet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const walkPacket: incomingPacket = (player: Player, packetId: number, pac

for(let i = 0; i < totalSteps; i++) {
const x = packet.readByte();
const y = packet.readNegativeOffsetByte();
const y = packet.readPreNegativeOffsetByte();
walkingQueue.add(x + firstX, y + firstY);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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';
import { itemSwapPacket } from './impl/item-swap-packet';

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

163: characterDesignPacket,

24: itemEquipPacket,
3: itemOption1Packet,
24: itemEquipPacket,
3: itemOption1Packet,
123: itemSwapPacket,

56: commandPacket
};
Expand Down

0 comments on commit 2b21c92

Please sign in to comment.