Worm bin working with both worms and grubs

This commit is contained in:
2026-10-12 02:22:44 -03:00
parent ff5fb971da
commit df26248f4a
13 changed files with 222 additions and 60 deletions
+37 -16
View File
@@ -203,12 +203,13 @@ export interface Cursor {
export const LEFT_CLICK = 0;
export const RIGHT_CLICK = 2;
// what clicking a normal slot does, the server runs this for real and clients run it to predict
export function click_slot(container: Container, index: number, cursor: Cursor, button: number) {
// what clicking a normal slot does, the server runs this for real and clients run it to predict. limit is the most
// the slot holds of anything, like minecraft's Slot.getMaxStackSize
export function click_slot(container: Container, index: number, cursor: Cursor, button: number, limit = Infinity) {
if (button === LEFT_CLICK) {
left_click(container, index, cursor);
left_click(container, index, cursor, limit);
} else if (button === RIGHT_CLICK) {
right_click(container, index, cursor);
right_click(container, index, cursor, limit);
}
if (cursor.item && cursor.item.amount <= 0) {
cursor.item = undefined;
@@ -222,7 +223,7 @@ function swap_with_cursor(container: Container, index: number, cursor: Cursor) {
slot.set_item(original);
}
function left_click(container: Container, index: number, cursor: Cursor) {
function left_click(container: Container, index: number, cursor: Cursor, limit: number) {
const holding = cursor.item;
const slot = container.get_slot(index);
const slot_item = slot.get_item();
@@ -237,8 +238,8 @@ function left_click(container: Container, index: number, cursor: Cursor) {
// if you are holding something and slot type equals holding type
// try to add to stack
if (slot_item && slot.type_id === holding.type_id) {
const space_left = slot.max_amount! - slot_item.amount;
const amount_to_add = Math.min(space_left, holding.amount);
const space_left = Math.min(slot.max_amount!, limit) - slot_item.amount;
const amount_to_add = Math.max(0, Math.min(space_left, holding.amount));
slot_item.amount += amount_to_add;
holding.amount -= amount_to_add;
@@ -246,12 +247,23 @@ function left_click(container: Container, index: number, cursor: Cursor) {
return;
}
// more than the slot holds: an empty one takes as many as it can, a full one keeps what it has
if (holding.amount > limit) {
if (!slot_item) {
const placed = holding.clone();
placed.amount = limit;
slot.set_item(placed);
holding.amount -= limit;
}
return;
}
// if something on hand but not the same
// swap
swap_with_cursor(container, index, cursor);
}
function right_click(container: Container, index: number, cursor: Cursor) {
function right_click(container: Container, index: number, cursor: Cursor, limit: number) {
const holding = cursor.item;
const slot = container.get_slot(index);
const slot_item = slot.get_item();
@@ -261,7 +273,7 @@ function right_click(container: Container, index: number, cursor: Cursor) {
// and slot type equals holding type
// add 1 to matching stack
if (slot_item && slot.type_id === holding.type_id) {
if (slot_item.amount < slot_item.max_amount) {
if (slot_item.amount < Math.min(slot_item.max_amount, limit)) {
slot_item.amount += 1;
holding.amount -= 1;
}
@@ -278,8 +290,8 @@ function right_click(container: Container, index: number, cursor: Cursor) {
}
// if something on hand but not the same
// swap
swap_with_cursor(container, index, cursor);
// swap, when all of it fits
if (holding.amount <= limit) swap_with_cursor(container, index, cursor);
return;
}
@@ -303,6 +315,13 @@ function right_click(container: Container, index: number, cursor: Cursor) {
export interface SlotRef {
container: Container;
index: number;
// the most it holds of anything, when that's less than a stack
max_count?: number;
}
// how many of item the slot holds
function slot_cap(slot: SlotRef, item: ItemStack): number {
return Math.min(item.max_amount, slot.max_count ?? Infinity);
}
// shift clicking, like minecraft's moveItemStackTo: tops up stacks of the same item in these slots, in order, then
@@ -312,7 +331,7 @@ export function move_into(item: ItemStack, slots: SlotRef[], accepts: (slot: Slo
const there = slot.container.get_item(slot.index);
if (item.amount <= 0) return;
if (there && there !== item && there.type_id === item.type_id && accepts(slot)) {
const moving = Math.min(there.max_amount - there.amount, item.amount);
const moving = Math.max(0, Math.min(slot_cap(slot, there) - there.amount, item.amount));
there.amount += moving;
item.amount -= moving;
}
@@ -320,8 +339,10 @@ export function move_into(item: ItemStack, slots: SlotRef[], accepts: (slot: Slo
for (const slot of slots) {
if (item.amount <= 0) return;
if (!slot.container.get_item(slot.index) && accepts(slot)) {
slot.container.set_item(slot.index, item.clone());
item.amount = 0;
const moved = item.clone();
moved.amount = Math.min(item.amount, slot_cap(slot, item));
slot.container.set_item(slot.index, moved);
item.amount -= moved.amount;
}
}
}
@@ -332,8 +353,8 @@ export function room_for(item: ItemStack, slots: SlotRef[], accepts: (slot: Slot
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;
if (!there) room += slot_cap(slot, item);
else if (there.type_id === item.type_id) room += Math.max(0, slot_cap(slot, there) - there.amount);
}
return room;
}
+2
View File
@@ -494,6 +494,8 @@ export interface SlotOptions {
on_take?: (item: ItemStack) => boolean;
// a texture drawn in the slot while it's empty, like minecraft's armor slots
icon?: Id;
// the most it holds, when that's less than a stack, like minecraft's Slot.getMaxStackSize
max_count?: number;
}
// what a menu type's build function gets, like neoforge's server menu constructor
+2
View File
@@ -77,6 +77,8 @@ export interface MenuSlotInfo {
y: number;
output?: boolean;
icon?: string;
// the most it holds of anything, when that's less than a stack
max_count?: number;
}
// clients send what the player is trying to do, the server decides what happens