Change whole gui system
This commit is contained in:
+1
-1
@@ -1,4 +1,4 @@
|
||||
// commands the way minecraft's brigadier (and so fabric's command api) does them: a tree of literal words and typed
|
||||
// commands the way minecraft's brigadier (and so neoforge's commands) does them: a tree of literal words and typed
|
||||
// arguments, each argument type parsing its text and suggesting what could go there. the server runs them, and players
|
||||
// get its suggestions as they type. see docs/modding/misc/commands.md
|
||||
import { EverythingRegistry } from "./everything_registry.ts";
|
||||
|
||||
@@ -150,7 +150,7 @@ export interface BlockRegistry {
|
||||
compiled_states?: CompiledStateDefinition[];
|
||||
}
|
||||
|
||||
// what a tool is made of, like fabric's ToolMaterial
|
||||
// what a tool is made of, like minecraft's ToolMaterial
|
||||
export interface ToolProperties {
|
||||
// like "pickaxe", matched against blocks' tool_to_break
|
||||
type: string;
|
||||
|
||||
+17
-21
@@ -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;
|
||||
}
|
||||
|
||||
+54
-16
@@ -35,36 +35,69 @@ export interface ClientContext {
|
||||
log(...args: unknown[]): void;
|
||||
}
|
||||
|
||||
export interface ModScreen<Props = unknown> {
|
||||
on_open?(): void;
|
||||
on_tick?(dt: number): void;
|
||||
// a screen with no menu, only on this client, like neoforge's Screen. see docs/modding/gui/index.md
|
||||
export interface Screen {
|
||||
// when it opens, and whenever the window changes size. in gui pixels
|
||||
on_init?(width: number, height: number): void;
|
||||
// 20 times a second
|
||||
on_tick?(): void;
|
||||
on_render(g: Graphics): void;
|
||||
// the server sent new props for this screen
|
||||
on_props?(props: Props): void;
|
||||
// true when not set
|
||||
should_close_on_escape?(): boolean;
|
||||
// the player closed it, with escape
|
||||
on_close?(): void;
|
||||
// return true to keep the screen open when escape is pressed
|
||||
on_escape?(): boolean;
|
||||
// it's gone for any reason, including another screen replacing it
|
||||
on_removed?(): void;
|
||||
}
|
||||
|
||||
// the client side of a menu the server opened, see docs/modding/inventories/menus.md
|
||||
export interface ClientMenu {
|
||||
readonly type: Id;
|
||||
readonly title: string;
|
||||
// what the server's build function set, sent once
|
||||
readonly open_data: unknown;
|
||||
// the server's latest values of its add_data numbers
|
||||
readonly data: Readonly<Record<string, number>>;
|
||||
// like clickMenuButton, the server's on_button decides what it does
|
||||
click_button(id: number): void;
|
||||
close(): void;
|
||||
}
|
||||
|
||||
// draws a menu, like neoforge's AbstractContainerScreen. the engine puts it in the middle of the window, and draws
|
||||
// the menu's slots, the item on the cursor and item tooltips. g's 0, 0 is the screen's top left
|
||||
export interface MenuScreen {
|
||||
// in gui pixels, like imageWidth and imageHeight
|
||||
width: number;
|
||||
height: number;
|
||||
// under the slots
|
||||
on_render_background?(g: Graphics): void;
|
||||
// over the slots
|
||||
on_render_labels?(g: Graphics): void;
|
||||
// 20 times a second, like containerTick
|
||||
on_tick?(): void;
|
||||
}
|
||||
|
||||
export type Color = [number, number, number, number];
|
||||
|
||||
// immediate mode drawing, like the debug ui, styled with the gui sprites (assets/sprites/gui.png)
|
||||
// immediate mode drawing, like the debug ui, styled with the gui sprites (assets/sprites/gui.png). everything is in
|
||||
// gui pixels, like neoforge's: 18 of them are a slot
|
||||
export interface Graphics {
|
||||
readonly width: number;
|
||||
readonly height: number;
|
||||
readonly mouse: { x: number; y: number; down: boolean; pressed: boolean; right_pressed: boolean };
|
||||
rect(x: number, y: number, w: number, h: number, color?: Color): void;
|
||||
panel(x: number, y: number, w: number, h: number): void;
|
||||
// how tall a line of text is at scale 1, the game's text size
|
||||
readonly text_height: number;
|
||||
text(text: string, x: number, y: number, options?: { scale?: number; color?: Color }): void;
|
||||
measure_text(text: string, scale?: number): number;
|
||||
texture(id: Id, x: number, y: number, w: number, h: number): void;
|
||||
// part is the part of the texture to draw, [u, v, width, height] from 0 to 1, like a progress bar's filled part
|
||||
texture(id: Id, x: number, y: number, w: number, h: number, part?: [number, number, number, number]): void;
|
||||
item(id: Id, x: number, y: number, count?: number): void;
|
||||
clip(x: number, y: number, w: number, h: number, draw: () => void): void;
|
||||
button(label: string, x: number, y: number, w: number, h: number): boolean;
|
||||
text_input(id: string, x: number, y: number, w: number): string;
|
||||
slider(id: string, x: number, y: number, w: number, min: number, max: number): number;
|
||||
// server synced slots, same rules as container screens
|
||||
slots(container: string, layout: { slot: number; x: number; y: number }[], x: number, y: number): void;
|
||||
// false while a text input has the keyboard
|
||||
key_pressed(key: KeyCode): boolean;
|
||||
// shown next to the mouse over everything else, the last one this frame wins
|
||||
@@ -77,10 +110,12 @@ export interface Graphics {
|
||||
block_mouse(x: number, y: number, w: number, h: number): void;
|
||||
}
|
||||
|
||||
// drawn on every inventory screen (the player's inventory, chests, furnaces...), over it, like just enough items'
|
||||
// item list. it gets the screen's box to lay itself out around
|
||||
// drawn on every menu's screen (the player's inventory, chests, furnaces...), over it, like just enough items' item
|
||||
// list. it gets the screen's box to lay itself out around
|
||||
export interface InventoryOverlay {
|
||||
on_render(g: Graphics, screen: InventoryScreenView): void;
|
||||
// the screen closed for any reason, like the player closing their inventory
|
||||
on_close?(): void;
|
||||
}
|
||||
|
||||
export interface InventoryScreenView {
|
||||
@@ -93,9 +128,12 @@ export interface InventoryScreenView {
|
||||
}
|
||||
|
||||
export interface ClientUi {
|
||||
register_screen<Props>(id: Id, create: (props: Props) => ModScreen<Props>): void;
|
||||
// open a screen that doesn't involve the server, like a settings page
|
||||
open<Props>(id: Id, props: Props): void;
|
||||
// the screen for a menu type of this mod, like RegisterMenuScreensEvent. called each time one opens
|
||||
register_menu_screen(type: Id, create: (menu: ClientMenu) => MenuScreen): void;
|
||||
// a screen that doesn't involve the server, like a settings page, over whatever's open
|
||||
open(screen: Screen): void;
|
||||
// closes the screen on top
|
||||
close(): void;
|
||||
add_inventory_overlay(overlay: InventoryOverlay): void;
|
||||
}
|
||||
|
||||
|
||||
+58
-43
@@ -18,6 +18,7 @@ export interface ServerContext {
|
||||
items: ItemApi;
|
||||
recipes: RecipeApi;
|
||||
tags: TagApi;
|
||||
menus: MenuRegistry;
|
||||
ui: ServerUi;
|
||||
net: ServerNet;
|
||||
storage: ModStorage;
|
||||
@@ -62,7 +63,7 @@ export interface ComponentRegistry {
|
||||
register_item<P>(id: Id, component: ItemComponent<P>): void;
|
||||
}
|
||||
|
||||
// commands, fabric's (brigadier's) way: see common/commands.ts and docs/modding/misc/commands.md
|
||||
// commands, brigadier's way like neoforge's: see common/commands.ts and docs/modding/misc/commands.md
|
||||
|
||||
// who ran a command
|
||||
export interface CommandSource {
|
||||
@@ -79,7 +80,7 @@ export interface Command {
|
||||
}
|
||||
|
||||
export interface CommandRegistry {
|
||||
// a tree of literal() and argument() nodes, like fabric's dispatcher.register(literal("heal").then(...))
|
||||
// a tree of literal() and argument() nodes, like neoforge's dispatcher.register(literal("heal").then(...))
|
||||
register(node: CommandNode<CommandSource>): void;
|
||||
register(name: string, command: Command): void;
|
||||
literal(name: string): CommandNode<CommandSource>;
|
||||
@@ -304,7 +305,7 @@ export interface RecipeApi {
|
||||
list(type: Id): any[];
|
||||
}
|
||||
|
||||
// guis
|
||||
// forms, not built yet: menus whose screens the engine provides, see docs/modding_plan.md
|
||||
|
||||
export interface ActionForm {
|
||||
title: string;
|
||||
@@ -331,60 +332,74 @@ export interface ModalForm {
|
||||
|
||||
export type FormResult<T> = ({ canceled: true } & Partial<T>) | ({ canceled: false } & T);
|
||||
|
||||
// menus, like neoforge's: the server side of a gui, see docs/modding/inventories/menus.md
|
||||
|
||||
export type SlotFilter = "smeltable" | "fuel" | ((item: ItemStack) => boolean);
|
||||
|
||||
export interface ContainerScreenOptions {
|
||||
container: Container;
|
||||
// x and y in slot units, below the player's inventory. can be fractional
|
||||
layout: {
|
||||
slot: number;
|
||||
x: number;
|
||||
y: number;
|
||||
// what can be put in, checked on the server
|
||||
filter?: SlotFilter;
|
||||
// can only be taken from, all at once, like the furnace result
|
||||
output_only?: boolean;
|
||||
// runs when a player takes from an output_only slot, before they get it. false stops them taking it
|
||||
on_take?: (item: ItemStack) => boolean;
|
||||
// a texture drawn in the slot while it's empty, like minecraft's armor slots
|
||||
icon?: Id;
|
||||
}[];
|
||||
// height of the screen's area in slots, fits the layout and bars when not set
|
||||
rows?: number;
|
||||
// progress bars, filled with the property value divided by the property max
|
||||
bars?: {
|
||||
x: number;
|
||||
y: number;
|
||||
value: string;
|
||||
max: string;
|
||||
direction: "up" | "right";
|
||||
empty_texture: Id;
|
||||
full_texture: Id;
|
||||
}[];
|
||||
export interface SlotOptions {
|
||||
// gui pixels from the screen's top left to the slot's 18 pixel frame
|
||||
x: number;
|
||||
y: number;
|
||||
// what can be put in, checked on the server
|
||||
filter?: SlotFilter;
|
||||
// can only be taken from, all at once, like the furnace result
|
||||
output_only?: boolean;
|
||||
// runs when a player takes from an output_only slot, before they get it. false stops them taking it
|
||||
on_take?: (item: ItemStack) => boolean;
|
||||
// a texture drawn in the slot while it's empty, like minecraft's armor slots
|
||||
icon?: Id;
|
||||
}
|
||||
|
||||
export interface ScreenHandle<Props = unknown> {
|
||||
// what a menu type's build function gets, like neoforge's server menu constructor
|
||||
export interface MenuBuilder {
|
||||
title: string;
|
||||
// sent to the client once when it opens, any json up to 16 KB, like IContainerFactory's extra data
|
||||
open_data: unknown;
|
||||
// a slot of a container from ctx.containers, or of player.inventory. returns its menu slot index
|
||||
add_slot(container: Container, slot: number, options: SlotOptions): number;
|
||||
// the player's 36 slots, like addStandardInventorySlots: the rest of the inventory in 3 rows from x and y, the
|
||||
// hotbar 58 pixels under them. added in inventory order, so inventory slot i is the returned index plus i
|
||||
add_player_inventory(x: number, y: number): number;
|
||||
// a number the screen shows, like DataSlot: read every tick and sent when it changes
|
||||
add_data(name: string, get: () => number): void;
|
||||
// checked every tick, false closes it
|
||||
still_valid(check: (player: Player) => boolean): void;
|
||||
// closes it when the player is over 8 blocks from the block, or it's been broken or replaced
|
||||
still_valid_near(block: BlockRef): void;
|
||||
// a button the client pressed, like clickMenuButton. any number can come, check it
|
||||
on_button(fn: (player: Player, id: number) => void): void;
|
||||
// shift clicks: the menu slot indexes the stack in this one tries, in order. the default rule when not set
|
||||
quick_move(fn: (player: Player, index: number) => number[]): void;
|
||||
on_tick(fn: () => void): void;
|
||||
// when it closes for any reason: the player closed it, left, opened another, or it was closed
|
||||
on_close(fn: () => void): void;
|
||||
}
|
||||
|
||||
export interface MenuHandle {
|
||||
readonly player: Player;
|
||||
readonly open: boolean;
|
||||
// numbers the bars show, synced to the player when they change
|
||||
set_property(id: string, value: number): void;
|
||||
update(props: Props): void; // custom screens only
|
||||
close(): void;
|
||||
// when it closes for any reason: the player closed it, left, opened another screen, or it was closed
|
||||
on_close(fn: () => void): void;
|
||||
}
|
||||
|
||||
export interface MenuType<Args> {
|
||||
readonly id: Id;
|
||||
// like openMenu: builds a menu for the player with these args and opens it, closing whatever they had open
|
||||
open(player: Player, args: Args): MenuHandle;
|
||||
}
|
||||
|
||||
export interface MenuRegistry {
|
||||
// like registering a MenuType. the client script gives it a screen with ctx.ui.register_menu_screen
|
||||
register<Args = undefined>(
|
||||
id: Id,
|
||||
build: (menu: MenuBuilder, args: Args, player: Player) => void,
|
||||
): MenuType<Args>;
|
||||
}
|
||||
|
||||
export interface ServerUi {
|
||||
message_form(player: Player, form: MessageForm): Promise<FormResult<{ selection: 0 | 1 }>>;
|
||||
action_form(player: Player, form: ActionForm): Promise<FormResult<{ selection: number }>>;
|
||||
modal_form(player: Player, form: ModalForm): Promise<FormResult<{ values: (boolean | number | string)[] }>>;
|
||||
open_container(player: Player, options: ContainerScreenOptions): ScreenHandle;
|
||||
open_screen<Props>(
|
||||
player: Player,
|
||||
id: Id,
|
||||
props: Props,
|
||||
options?: { containers?: Record<string, Container> },
|
||||
): ScreenHandle<Props>;
|
||||
}
|
||||
|
||||
// mod channels
|
||||
|
||||
+1
-1
@@ -19,7 +19,7 @@ export const NAMESPACE_PATTERN = /^[a-z0-9_]{1,32}$/;
|
||||
export const RESERVED_NAMESPACES = ["bworld", "engine"];
|
||||
// a tag's id may have folders, like c:ingots/iron. see docs/modding/resources/tags.md
|
||||
export const TAG_ID_PATTERN = /^[a-z0-9_]+:[a-z0-9_]+(\/[a-z0-9_]+)*$/;
|
||||
// the namespace for tags every mod shares, like neoforge's and fabric's. no mod owns it
|
||||
// the namespace for tags every mod shares, like neoforge's. no mod owns it
|
||||
export const SHARED_TAG_NAMESPACE = "c";
|
||||
export const TAG_REGISTRIES = ["blocks", "items", "entities"] as const;
|
||||
export type TagRegistry = typeof TAG_REGISTRIES[number];
|
||||
|
||||
+42
-29
@@ -7,7 +7,7 @@ export const AIR_ID = "bworld:air";
|
||||
|
||||
// bump when a client and server of different versions can't play together.
|
||||
// the server rejects a different version before the client downloads anything
|
||||
export const PROTOCOL_VERSION = 13;
|
||||
export const PROTOCOL_VERSION = 14;
|
||||
|
||||
export interface PlayerInfo {
|
||||
id: string;
|
||||
@@ -52,28 +52,24 @@ export type EntityEvent = "hurt" | "death" | "swing";
|
||||
// x, y, z, block id, and its state bits when they aren't 0
|
||||
export type BlockChange = [number, number, number, string, number?];
|
||||
|
||||
// the containers a client can see and click. "screen" is whatever the open server screen shows
|
||||
export type ContainerKey = "inventory" | "crafting" | "screen";
|
||||
// the player's own containers, synced to them whether or not a menu is open
|
||||
export type ContainerKey = "inventory" | "crafting";
|
||||
|
||||
export const CRAFTING_RESULT_SLOT = 9;
|
||||
|
||||
// a screen the server opens, drawn below the player's inventory and hotbar
|
||||
export interface ScreenLayout {
|
||||
// height of the screen's own area, in slots
|
||||
rows: number;
|
||||
// x and y in slots, can be fractional
|
||||
// output slots can only be taken from, like the furnace result. icon is a texture drawn in the slot while it's empty
|
||||
slots: { index: number; x: number; y: number; output?: boolean; icon?: string }[];
|
||||
// progress bars filled from properties[value] / properties[max]
|
||||
bars: {
|
||||
x: number;
|
||||
y: number;
|
||||
value: string;
|
||||
max: string;
|
||||
direction: "up" | "right";
|
||||
empty_texture: string;
|
||||
full_texture: string;
|
||||
}[];
|
||||
// menus, like neoforge's: the server's menu decides what every click does, clients draw it and send clicks by menu
|
||||
// slot index. menu 0 is the player's own inventory and crafting grid, always there and never opened or sent, like
|
||||
// minecraft's InventoryMenu: slots 0-35 are the inventory (the hotbar is 0-8), 36-45 the crafting grid
|
||||
export const PLAYER_MENU = 0;
|
||||
export const PLAYER_MENU_CRAFTING = 36;
|
||||
|
||||
// one slot of an opened menu as the client draws it. x and y in gui pixels from the screen's top left, output slots
|
||||
// can only be taken from, icon is a texture drawn while it's empty
|
||||
export interface MenuSlotInfo {
|
||||
x: number;
|
||||
y: number;
|
||||
output?: boolean;
|
||||
icon?: string;
|
||||
}
|
||||
|
||||
// clients send what the player is trying to do, the server decides what happens
|
||||
@@ -100,12 +96,15 @@ 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 }
|
||||
// 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
|
||||
| { type: "close_screen" }
|
||||
// a slot of the open menu, or of menu 0 when none is. shift moves the stack to the other part of the menu instead,
|
||||
// like minecraft's quick move. clicks for a menu that isn't open any more are ignored
|
||||
| { type: "click"; menu: number; slot: number; button: number; shift?: boolean }
|
||||
// q on a slot (the held one when no screen is open, in menu 0) throws one item, or the whole stack with ctrl
|
||||
| { type: "drop_item"; menu: number; slot: number; all: boolean }
|
||||
// a button of the open menu, like minecraft's clickMenuButton. the menu decides what the number means
|
||||
| { type: "menu_button"; menu: number; button: number }
|
||||
// closes the open menu, or the player's own inventory screen with 0
|
||||
| { type: "close_menu"; menu: number }
|
||||
| { type: "chat"; text: string }
|
||||
// what could come next in a command being typed, answered with suggestions. id matches them up
|
||||
| { type: "suggest"; id: number; text: string }
|
||||
@@ -186,9 +185,23 @@ export type ServerMessage =
|
||||
| { type: "teleport"; id: number; x: number; y: number; z: number; motion?: [number, number, number] }
|
||||
| { type: "container"; container: ContainerKey; items: (ItemData | null)[] }
|
||||
| { type: "cursor"; item: ItemData | null }
|
||||
| { type: "open_screen"; layout: ScreenLayout; properties: Record<string, number> }
|
||||
| { type: "screen_properties"; properties: Record<string, number> }
|
||||
| { type: "close_screen" }
|
||||
// a menu opened, replacing any other. id counts 1 to 100 and around, type is the menu type whose screen draws it.
|
||||
// its slots' items come in menu_content, its data's changes in menu_data
|
||||
| {
|
||||
type: "open_menu";
|
||||
id: number;
|
||||
menu_type: string;
|
||||
title: string;
|
||||
open_data: unknown;
|
||||
slots: MenuSlotInfo[];
|
||||
data: Record<string, number>;
|
||||
}
|
||||
// what's in every slot of the open menu, by menu slot index, when any of it changed
|
||||
| { type: "menu_content"; id: number; items: (ItemData | null)[] }
|
||||
// all the menu's data, when any of it changed
|
||||
| { type: "menu_data"; id: number; data: Record<string, number> }
|
||||
// the server closed it (the block broke, the player walked away), it already put everything back
|
||||
| { type: "close_menu"; id: number }
|
||||
// the world time, every second and when it's changed. clients count ticks in between
|
||||
| { type: "time"; time: number }
|
||||
// answers suggest: start is where in the text the suggestions replace from, up to the caret
|
||||
|
||||
Reference in New Issue
Block a user