Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/world/items/item-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { world } from '@server/game-server';
export interface ContainerUpdateEvent {
slot?: number;
item?: Item;
type: 'ADD' | 'REMOVE' | 'SWAP' | 'SET' | 'SET_ALL';
type: 'ADD' | 'REMOVE' | 'SWAP' | 'SET' | 'SET_ALL' | 'UPDATE_ALL';
}

export class ItemContainer {
Expand Down Expand Up @@ -41,7 +41,33 @@ export class ItemContainer {
}
}

public find(item: Item): number {
for(let i = 0; i < this._size; i++) {
if (this._items[i] !== null &&
this._items[i].itemId === item.itemId &&
this._items[i].amount >= item.amount) {
return i;
}
}
return null;
}

public add(item: Item, fireEvent: boolean = true): void {
const findItem = this.find({itemId: item.itemId, amount: 1});
if (findItem !== null) {
const cacheItem = world.itemData.get(item.itemId);
if (cacheItem.stackable) {
this.set(findItem, {
itemId: item.itemId,
amount: this._items[findItem].amount += item.amount
}, true);
if (fireEvent) {
this._containerUpdated.next({type: 'UPDATE_ALL', slot: findItem, item});
}
return;
}
}

for(let i = 0; i < this._size; i++) {
if(this._items[i] === null) {
this._items[i] = item;
Expand Down