Shift clicking

This commit is contained in:
2026-09-30 00:30:33 -03:00
parent 7dc78aac76
commit 88659bb7af
6 changed files with 227 additions and 10 deletions
+62 -6
View File
@@ -11,7 +11,16 @@ import {
TICKS_PER_SECOND,
} from "$/common/constants.ts";
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
import { can_take_output, click_slot, Container, ItemData, ItemStack, take_output } from "$/common/inventory.ts";
import {
can_take_output,
click_slot,
Container,
ItemData,
ItemStack,
move_into,
room_for,
take_output,
} from "$/common/inventory.ts";
import {
AIR_ID,
ClientMessage,
@@ -50,7 +59,7 @@ import {
} from "$/common/commands.ts";
import type { GameLoop } from "./game_loop.ts";
import { consume_recipe_items, update_crafting_result } from "./crafting.ts";
import { OpenScreen, SavedPlayer, ServerPlayer } from "./player.ts";
import { INVENTORY_SIZE, OpenScreen, SavedPlayer, ServerPlayer } from "./player.ts";
import { encode_skin, MAX_SKIN_BYTES, skin_problem } from "./skins.ts";
import { ServerWorld, Tile } from "./world.ts";
import type { WorldSave } from "./saves.ts";
@@ -738,7 +747,7 @@ export class GameServer {
}
break;
case "click":
this.#click(player, message.container, message.index, message.button);
this.#click(player, message.container, message.index, message.button, message.shift === true);
break;
case "drop_item":
this.#drop_item(player, message.container, message.index, message.all === true);
@@ -943,7 +952,7 @@ export class GameServer {
this.throw_item(player, thrown);
}
#click(player: ServerPlayer, key: unknown, index: unknown, button: unknown) {
#click(player: ServerPlayer, key: unknown, index: unknown, button: unknown, shift: boolean) {
if (!is_int(index) || !is_int(button)) {
return;
}
@@ -952,7 +961,9 @@ export class GameServer {
return;
}
if (key === "crafting" && index === CRAFTING_RESULT_SLOT) {
if (shift) {
this.#quick_move(player, key as ContainerKey, container, index);
} else if (key === "crafting" && index === CRAFTING_RESULT_SLOT) {
const result = container.get_item(CRAFTING_RESULT_SLOT);
if (result && take_output(result, player.cursor)) {
consume_recipe_items(container);
@@ -975,11 +986,56 @@ export class GameServer {
if (key === "crafting") {
update_crafting_result(container, this.recipes.shaped);
}
if (key === "screen") {
if (key === "screen" || (shift && player.screen)) {
this.world.dirty = true;
}
}
// shift click, like minecraft's quick move: the stack goes to the other part of the screen, as much as fits. with a
// container open that's between it and the inventory, otherwise between the hotbar and the rest of the inventory
#quick_move(player: ServerPlayer, key: ContainerKey, container: Container, index: number) {
const inventory = player.inventory;
const hotbar = Array.from({ length: 9 }, (_, i) => i);
const main = Array.from({ length: INVENTORY_SIZE - 9 }, (_, i) => i + 9);
// like minecraft, what comes out of a container fills the hotbar from the right first
const from_container = [...main, ...hotbar].reverse();
const item = container.get_item(index);
if (!item) {
return;
}
if (key === "crafting" && index === CRAFTING_RESULT_SLOT) {
// crafts as many as fit, while the grid keeps making the same thing
const id = item.type_id;
let result: ItemStack | undefined = item;
while (result?.type_id === id && room_for(result, inventory, from_container) >= result.amount) {
move_into(result.clone(), inventory, from_container);
consume_recipe_items(container);
update_crafting_result(container, this.recipes.shaped);
result = container.get_item(CRAFTING_RESULT_SLOT);
}
} else if (key === "crafting") {
move_into(item, inventory, [...main, ...hotbar]);
} else if (key === "screen") {
const screen = player.screen!;
const output = screen.layout.slots.find((s) => s.index === index)?.output;
if (!output) {
move_into(item, inventory, from_container);
} else if (
room_for(item, inventory, from_container) >= item.amount && (screen.on_take.get(index)?.(item) ?? true)
) {
move_into(item.clone(), inventory, from_container);
container.set_item(index, undefined);
}
} else if (player.screen) {
const screen = player.screen;
const inputs = screen.layout.slots.filter((s) => !s.output).map((s) => s.index);
move_into(item, screen.container, inputs, (i) => screen.filters.get(i)?.(item) ?? true);
} else {
move_into(item, inventory, index < 9 ? main : hotbar);
}
}
#chat(player: ServerPlayer, raw: unknown) {
if (typeof raw !== "string") {
return;