Change whole gui system

This commit is contained in:
2026-10-01 16:22:02 -03:00
parent 013a4fbb97
commit deba36260e
48 changed files with 1925 additions and 1302 deletions
+17 -21
View File
@@ -299,43 +299,39 @@ function right_click(container: Container, index: number, cursor: Cursor) {
slot.amount = half;
}
// a slot of some container, menus' slots can come from several
export interface SlotRef {
container: Container;
index: number;
}
// 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);
export function move_into(item: ItemStack, slots: SlotRef[], accepts: (slot: SlotRef) => boolean = () => true) {
for (const slot of slots) {
const there = slot.container.get_item(slot.index);
if (item.amount <= 0) return;
if (there && there !== item && there.type_id === item.type_id && accepts(index)) {
if (there && there !== item && there.type_id === item.type_id && accepts(slot)) {
const moving = Math.min(there.max_amount - there.amount, item.amount);
there.amount += moving;
item.amount -= moving;
}
}
for (const index of slots) {
for (const slot of slots) {
if (item.amount <= 0) return;
if (!container.get_item(index) && accepts(index)) {
container.set_item(index, item.clone());
if (!slot.container.get_item(slot.index) && accepts(slot)) {
slot.container.set_item(slot.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 {
export function room_for(item: ItemStack, slots: SlotRef[], accepts: (slot: SlotRef) => boolean = () => true): number {
let room = 0;
for (const index of slots) {
const there = container.get_item(index);
if (!accepts(index)) continue;
for (const slot of slots) {
const there = slot.container.get_item(slot.index);
if (!accepts(slot)) continue;
if (!there) room += item.max_amount;
else if (there.type_id === item.type_id) room += there.max_amount - there.amount;
}