Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing a findAll feature for ItemContainer. #104

Merged
merged 1 commit into from
Feb 29, 2020
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
35 changes: 35 additions & 0 deletions src/world/items/item-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,41 @@ export class ItemContainer {
return this.findIndex(item) !== -1;
}

/**
* Finds all slots within the container that contain the specified items.
* @param search The item id or Item object to search for.
* @returns An array of slot numbers.
*/
public findAll(search: number | Item): number[] {
if(typeof search !== 'number') {
search = search.itemId;
}

const stackable = world.itemData.get(search).stackable;

if(stackable) {
const index = this.findIndex(search);

if(index === null) {
return [];
} else {
return [ index ];
}
} else {
const slots = [];

for(let i = 0; i < this.size; i++) {
const item = this.items[i];

if(item && item.itemId === search) {
slots.push(i);
}
}

return slots;
}
}

public findIndex(item: number | Item): number {
const itemId = (typeof item === 'number') ? item : item.itemId;
return this._items.findIndex(i => i !== null && i.itemId === itemId);
Expand Down