Shift clicking
This commit is contained in:
@@ -190,11 +190,13 @@ export class GuiInventoryScreen extends GuiScreen {
|
||||
}
|
||||
|
||||
click(slot: Slot, button: number) {
|
||||
this.send({ type: "click", container: slot.container, index: slot.index, button });
|
||||
const shift = InputManager.is_key_down("ShiftLeft") || InputManager.is_key_down("ShiftRight");
|
||||
this.send({ type: "click", container: slot.container, index: slot.index, button, shift });
|
||||
|
||||
// output slots depend on server side things like recipes, just wait for the server
|
||||
// output slots depend on server side things like recipes, and where a shift click goes on slot filters only the
|
||||
// server has, just wait for the server
|
||||
const container = this.get_container(slot.container);
|
||||
if (container && !slot.output) {
|
||||
if (container && !slot.output && !shift) {
|
||||
click_slot(container, slot.index, this.inventories.cursor, button);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
+2
-1
@@ -92,7 +92,8 @@ export type ClientMessage =
|
||||
// right click on a block: interact with it, or place the held block against `face`
|
||||
| { type: "use_block"; x: number; y: number; z: number; face: Faces }
|
||||
| { type: "select_slot"; slot: number }
|
||||
| { type: "click"; container: ContainerKey; index: number; button: number }
|
||||
// shift moves the stack to the other part of the screen instead, like minecraft's quick move
|
||||
| { type: "click"; container: ContainerKey; index: number; button: number; shift?: boolean }
|
||||
// q on a slot (the held one when no screen is open) throws one item, or the whole stack with ctrl
|
||||
| { type: "drop_item"; container: ContainerKey; index: number; all: boolean }
|
||||
// closes the open screen, including the player's own inventory screen
|
||||
|
||||
@@ -61,6 +61,11 @@ Give the block `"interactive": true` so clients don't guess that the click place
|
||||
client. `on_take` is the place to use up a recipe's inputs, so taking the result and spending the inputs happen in the
|
||||
same step, like the smithing table does.
|
||||
|
||||
Shift clicking follows the same rules. A stack shift clicked in the player's inventory goes into the screen's slots in
|
||||
`layout` order, only those whose `filter` takes it and never `output_only` ones: first onto stacks of the same item,
|
||||
then into empty slots. A stack shift clicked in the screen goes to the player's inventory, and an `output_only` slot
|
||||
only gives its item that way when it all fits, after `on_take` agrees.
|
||||
|
||||
## Progress Bars
|
||||
|
||||
`bars` draws progress bars, filled with one property divided by another:
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
// shift clicking a slot moves its stack to the other part of the screen, like minecraft's quick move
|
||||
import { assertEquals } from "@std/assert";
|
||||
import { CRAFTING_RESULT_SLOT } from "$/common/protocol.ts";
|
||||
import { test_game } from "./helpers.ts";
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
type Msg = any;
|
||||
|
||||
const last_container = (messages: Msg[], container: string) =>
|
||||
messages.filter((m) => m.type === "container" && m.container === container).at(-1)?.items;
|
||||
|
||||
// a player standing on a stone floor, with the items given in their inventory from the first hotbar slot on
|
||||
async function setup(items: [string, number][] = []) {
|
||||
const t = await test_game("mods");
|
||||
t.join(1, "alice");
|
||||
for (let x = -3; x <= 3; x++) {
|
||||
for (let z = -3; z <= 3; z++) t.game.set_block(x, 99, z, "bworld:stone");
|
||||
}
|
||||
t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
|
||||
for (const [id, count] of items) t.send(1, { type: "chat", text: `/give ${id} ${count}` });
|
||||
t.take(1);
|
||||
return t;
|
||||
}
|
||||
|
||||
Deno.test("without a container open, shift clicking moves between the hotbar and the rest of the inventory", async () => {
|
||||
const t = await setup([["bworld:coal", 10], ["bworld:stick", 5]]);
|
||||
t.send(1, { type: "click", container: "inventory", index: 0, button: 0, shift: true });
|
||||
let inventory = last_container(t.take(1), "inventory");
|
||||
assertEquals(inventory[0], null);
|
||||
assertEquals(inventory[9], { id: "bworld:coal", count: 10 });
|
||||
|
||||
// back down it goes to the first free hotbar slot, the sticks are in the second
|
||||
t.send(1, { type: "click", container: "inventory", index: 9, button: 0, shift: true });
|
||||
inventory = last_container(t.take(1), "inventory");
|
||||
assertEquals(inventory[0], { id: "bworld:coal", count: 10 });
|
||||
assertEquals(inventory[1], { id: "bworld:stick", count: 5 });
|
||||
assertEquals(inventory[9], null);
|
||||
});
|
||||
|
||||
Deno.test("shift clicking tops up stacks of the same item before using empty slots", async () => {
|
||||
const t = await setup([["bworld:coal", 20]]);
|
||||
// half of it to the main inventory by hand, then the other half shift clicked onto it
|
||||
t.send(1, { type: "click", container: "inventory", index: 0, button: 2 });
|
||||
t.send(1, { type: "click", container: "inventory", index: 9, button: 0 });
|
||||
t.send(1, { type: "click", container: "inventory", index: 0, button: 0, shift: true });
|
||||
const inventory = last_container(t.take(1), "inventory");
|
||||
assertEquals(inventory[0], null);
|
||||
assertEquals(inventory[9], { id: "bworld:coal", count: 20 });
|
||||
assertEquals(inventory[10], null);
|
||||
});
|
||||
|
||||
Deno.test("with a chest open, shift clicking moves between it and the inventory", async () => {
|
||||
const t = await setup([["bworld:coal", 10]]);
|
||||
t.game.set_block(1, 100, 0, "bworld:chest");
|
||||
t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
|
||||
|
||||
t.send(1, { type: "click", container: "inventory", index: 0, button: 0, shift: true });
|
||||
let messages = t.take(1);
|
||||
assertEquals(last_container(messages, "inventory")[0], null);
|
||||
assertEquals(last_container(messages, "screen")[0], { id: "bworld:coal", count: 10 });
|
||||
|
||||
// like minecraft, out of a container it fills the hotbar from the right
|
||||
t.send(1, { type: "click", container: "screen", index: 0, button: 0, shift: true });
|
||||
messages = t.take(1);
|
||||
assertEquals(last_container(messages, "screen")[0], null);
|
||||
assertEquals(last_container(messages, "inventory")[8], { id: "bworld:coal", count: 10 });
|
||||
});
|
||||
|
||||
Deno.test("shift clicking into a furnace goes by its slot filters, and takes the result out", async () => {
|
||||
const t = await setup([["bworld:iron_ore", 1], ["bworld:coal", 1], ["bworld:stick", 1]]);
|
||||
t.game.set_block(1, 100, 0, "bworld:furnace");
|
||||
t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
|
||||
|
||||
// the ore goes in the input, the coal in the fuel slot, and sticks fit neither so they stay
|
||||
for (const index of [0, 1, 2]) t.send(1, { type: "click", container: "inventory", index, button: 0, shift: true });
|
||||
let messages = t.take(1);
|
||||
assertEquals(last_container(messages, "screen"), [
|
||||
{ id: "bworld:iron_ore", count: 1 },
|
||||
{ id: "bworld:coal", count: 1 },
|
||||
null,
|
||||
]);
|
||||
assertEquals(last_container(messages, "inventory")[2], { id: "bworld:stick", count: 1 });
|
||||
|
||||
for (let i = 0; i < 210; i++) t.game.tick();
|
||||
t.send(1, { type: "click", container: "screen", index: 2, button: 0, shift: true });
|
||||
messages = t.take(1);
|
||||
assertEquals(last_container(messages, "screen")[2], null);
|
||||
assertEquals(last_container(messages, "inventory")[8], { id: "bworld:iron_ingot", count: 1 });
|
||||
});
|
||||
|
||||
Deno.test("shift clicking the crafting result crafts as many as fit", async () => {
|
||||
const t = await setup([["bworld:log", 3]]);
|
||||
// the logs into the grid, then the result shift clicked: 3 crafts of 2 planks
|
||||
t.send(1, { type: "click", container: "inventory", index: 0, button: 0 });
|
||||
t.send(1, { type: "click", container: "crafting", index: 0, button: 0 });
|
||||
t.send(1, { type: "click", container: "crafting", index: CRAFTING_RESULT_SLOT, button: 0, shift: true });
|
||||
const messages = t.take(1);
|
||||
assertEquals(last_container(messages, "crafting").every((item: Msg) => item === null), true);
|
||||
assertEquals(last_container(messages, "inventory")[8], { id: "bworld:planks", count: 6 });
|
||||
});
|
||||
|
||||
Deno.test("shift clicking the crafting grid puts its items back in the inventory", async () => {
|
||||
const t = await setup([["bworld:log", 3]]);
|
||||
t.send(1, { type: "click", container: "inventory", index: 0, button: 0 });
|
||||
t.send(1, { type: "click", container: "crafting", index: 4, button: 0 });
|
||||
t.send(1, { type: "click", container: "crafting", index: 4, button: 0, shift: true });
|
||||
const messages = t.take(1);
|
||||
assertEquals(last_container(messages, "crafting").every((item: Msg) => item === null), true);
|
||||
assertEquals(last_container(messages, "inventory")[9], { id: "bworld:log", count: 3 });
|
||||
});
|
||||
Reference in New Issue
Block a user