Shift clicking
This commit is contained in:
@@ -299,6 +299,49 @@ function right_click(container: Container, index: number, cursor: Cursor) {
|
||||
slot.amount = half;
|
||||
}
|
||||
|
||||
// shift clicking, like minecraft's moveItemStackTo: tops up stacks of the same item in these slots, in order, then
|
||||
// fills empty ones. what doesn't fit stays in the item
|
||||
export function move_into(
|
||||
item: ItemStack,
|
||||
container: Container,
|
||||
slots: number[],
|
||||
accepts: (index: number) => boolean = () => true,
|
||||
) {
|
||||
for (const index of slots) {
|
||||
const there = container.get_item(index);
|
||||
if (item.amount <= 0) return;
|
||||
if (there && there !== item && there.type_id === item.type_id && accepts(index)) {
|
||||
const moving = Math.min(there.max_amount - there.amount, item.amount);
|
||||
there.amount += moving;
|
||||
item.amount -= moving;
|
||||
}
|
||||
}
|
||||
for (const index of slots) {
|
||||
if (item.amount <= 0) return;
|
||||
if (!container.get_item(index) && accepts(index)) {
|
||||
container.set_item(index, item.clone());
|
||||
item.amount = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// how many of the item move_into could put in these slots
|
||||
export function room_for(
|
||||
item: ItemStack,
|
||||
container: Container,
|
||||
slots: number[],
|
||||
accepts: (index: number) => boolean = () => true,
|
||||
): number {
|
||||
let room = 0;
|
||||
for (const index of slots) {
|
||||
const there = container.get_item(index);
|
||||
if (!accepts(index)) continue;
|
||||
if (!there) room += item.max_amount;
|
||||
else if (there.type_id === item.type_id) room += there.max_amount - there.amount;
|
||||
}
|
||||
return room;
|
||||
}
|
||||
|
||||
// whether take_output would take it
|
||||
export function can_take_output(item: ItemStack, cursor: Cursor): boolean {
|
||||
const holding = cursor.item;
|
||||
|
||||
Reference in New Issue
Block a user