跳转至

levilamina::client · 客户端

只在客户端可用的能力。

在服务器宿主上,这一整组都是空槽位

在服务器宿主上调用,得到的是运行时的 Err,编译不会报错。契约 §2.1:所有目标上的布局都一样,缺少的能力就是 NULL 槽位,所以同一份模组源码能为两个目标编译;加载到错误的目标上时,宿主会在握手时根据 mod_flags 明确拒绝。

用 is_available 判断,不要用 cfg。

回调在客户端线程上运行

不在服务器线程上。热键回调不能碰服务器的状态。

函数

client::is_available

Rust
pub fn is_available() -> bool

这个宿主是不是为客户端目标构建的,也就是 client_* 槽位有没有填上。

client::is_in_level

Rust
pub fn is_in_level() -> Result<bool>

客户端当前是否在一个世界里。

client::local_player_name

Rust
pub fn local_player_name() -> Result<String>

本地玩家的名字。不在世界里时返回 Err。

client::screen_name

Rust
pub fn screen_name() -> Result<String>

当前界面的名字,比如 "hud_screen" 或 "pause_screen"。

client::register_key

Rust
1
2
3
4
5
6
pub fn register_key(
    name: &str,
    key_codes: &[i32],
    allow_remap: bool,
    handler: impl FnMut(KeyAction, i32) + Send + 'static,
) -> Result<KeyBinding>

注册一个热键。

key_codes 是默认的绑定,allow_remap 决定玩家能不能在设置里改它。回调收到(动作,焦点影响),两者都在客户端线程上运行。

  • 参数:
    • name : &str
    • key_codes : &[i32]
    • allow_remap : bool
    • handler : impl FnMut(KeyAction, i32) + Send + 'static
  • 返回值类型:Result<KeyBinding>
  • 对应槽位:client_register_key

client::property

Rust
pub fn property(prop: i32) -> Result<f64>

按 PIER_CPROP_* 键读取一个数值型的客户端属性。

宿主没有这个槽位、宿主不认识这个键、或者客户端不在世界里时读取关于本地玩家的键,都会得到 Err。

  • 参数:
    • prop : i32
  • 返回值类型:Result<f64>
  • 对应槽位:client_get_num

client::local_position

Rust
pub fn local_position() -> Result<[f64; 3]>

本地玩家脚下的位置 [x, y, z],与 get_player_position 报告的是同一个点。

client::local_rotation

Rust
pub fn local_rotation() -> Result<(f32, f32)>

本地玩家的 (pitch, yaw),单位为度;pitch 为负表示向上看。

client::local_dimension

Rust
pub fn local_dimension() -> Result<i32>

本地玩家所在维度的 id。

client::gui_scale

Rust
pub fn gui_scale() -> Result<f32>

这个客户端绘制界面所用的 GUI 缩放。

client::server_ping_ms

Rust
pub fn server_ping_ms() -> Result<u64>

到服务器的往返延迟,单位毫秒,由这个客户端测得。

client::is_showing_menu

Rust
pub fn is_showing_menu() -> Result<bool>

是否正在显示菜单界面。

client::is_showing_pause_screen

Rust
pub fn is_showing_pause_screen() -> Result<bool>

是否正在显示暂停界面。

client::is_playing

Rust
pub fn is_playing() -> Result<bool>

客户端是否正在游玩一个世界。

client::is_in_realm

Rust
pub fn is_in_realm() -> Result<bool>

当前世界是否是 Realm。

client::is_game_input_enabled

Rust
pub fn is_game_input_enabled() -> Result<bool>

游戏输入是否能到达世界;上层有界面时就不能。

client::top_screen_name

Rust
pub fn top_screen_name() -> Result<String>

客户端界面栈最顶层的界面。

client::show_message

Rust
pub fn show_message(text: &str) -> Result<()>

在这个客户端的聊天栏里显示 text,不会发送给服务器。

  • 参数:
    • text : &str
  • 返回值类型:Result<()>
  • 对应槽位:client_action

client::set_title

Rust
pub fn set_title(text: &str) -> Result<()>

在这个客户端上把 text 显示为标题。

  • 参数:
    • text : &str
  • 返回值类型:Result<()>
  • 对应槽位:client_action

client::set_subtitle

Rust
pub fn set_subtitle(text: &str) -> Result<()>

在这个客户端上把 text 显示为副标题。

  • 参数:
    • text : &str
  • 返回值类型:Result<()>
  • 对应槽位:client_action

client::set_action_bar

Rust
pub fn set_action_bar(text: &str) -> Result<()>

在这个客户端的快捷栏上方显示 text。

  • 参数:
    • text : &str
  • 返回值类型:Result<()>
  • 对应槽位:client_action

client::show_tip

Rust
pub fn show_tip(text: &str) -> Result<()>

在这个客户端上把 text 显示为提示。

  • 参数:
    • text : &str
  • 返回值类型:Result<()>
  • 对应槽位:client_action

client::play_ui_sound

Rust
pub fn play_ui_sound(name: &str, volume: f32, pitch: f32) -> Result<()>

在这个客户端上按名字播放一个界面音效,例如 "random.click"。

  • 参数:
    • name : &str
    • volume : f32
    • pitch : f32
  • 返回值类型:Result<()>
  • 对应槽位:client_action

client::grab_mouse

Rust
pub fn grab_mouse() -> Result<()>

让游戏捕获鼠标,效果和点击进入世界一样。

client::release_mouse

Rust
pub fn release_mouse() -> Result<()>

把鼠标释放给界面。

client::set_input_disabled

Rust
pub fn set_input_disabled(disabled: bool) -> Result<()>

禁用或启用游戏输入。

  • 参数:
    • disabled : bool
  • 返回值类型:Result<()>
  • 对应槽位:client_action

client::leave_level

Rust
pub fn leave_level() -> Result<()>

离开当前世界回到菜单。引擎以异步方式完成。

client::registered_keys

Rust
pub fn registered_keys() -> Result<Vec<String>>

按键注册表里所有按键绑定的名字,不论是哪个模组注册的。

client::has_key

Rust
pub fn has_key(name: &str) -> Result<bool>

是否注册了这个名字的按键绑定。

  • 参数:
    • name : &str
  • 返回值类型:Result<bool>
  • 对应槽位:client_list_keys

client::show_popup

Rust
pub fn show_popup(message: &str, subtitle: &str) -> Result<()>

在这个客户端上显示一个弹出提示,副标题可以为空。

  • 参数:
    • message : &str
    • subtitle : &str
  • 返回值类型:Result<()>
  • 对应槽位:client_action

client::show_dev_console_message

Rust
pub fn show_dev_console_message(text: &str) -> Result<()>

把 text 写入客户端的开发者控制台。

  • 参数:
    • text : &str
  • 返回值类型:Result<()>
  • 对应槽位:client_action

client::set_suspend_input

Rust
pub fn set_suspend_input(suspended: bool) -> Result<()>

暂停或恢复全部输入。

  • 参数:
    • suspended : bool
  • 返回值类型:Result<()>
  • 对应槽位:client_action

client::set_suspend_directional_input

Rust
pub fn set_suspend_directional_input(suspended: bool) -> Result<()>

暂停或恢复移动输入。

  • 参数:
    • suspended : bool
  • 返回值类型:Result<()>
  • 对应槽位:client_action

client::set_game_input_enabled

Rust
pub fn set_game_input_enabled(enabled: bool) -> Result<()>

启用或禁用游戏输入。

  • 参数:
    • enabled : bool
  • 返回值类型:Result<()>
  • 对应槽位:client_action

client::refocus_mouse

Rust
pub fn refocus_mouse(lost: bool) -> Result<()>

把鼠标交还给游戏;lost 告诉引擎焦点曾经丢失。

  • 参数:
    • lost : bool
  • 返回值类型:Result<()>
  • 对应槽位:client_action

client::mute_audio

Rust
pub fn mute_audio() -> Result<()>

把客户端静音。

client::unmute_audio

Rust
pub fn unmute_audio() -> Result<()>

取消客户端静音。

client::fade_out_music

Rust
pub fn fade_out_music() -> Result<()>

让正在播放的音乐淡出。

client::set_gui_scale_offset

Rust
pub fn set_gui_scale_offset(offset: i32) -> Result<()>

设置 GUI 缩放偏移。

  • 参数:
    • offset : i32
  • 返回值类型:Result<()>
  • 对应槽位:client_action

client::reset_player_movement

Rust
pub fn reset_player_movement() -> Result<()>

重置本地玩家的移动输入。

client::push_leave_game_screen

Rust
pub fn push_leave_game_screen() -> Result<()>

打开询问是否离开世界的界面。

client::open_content_log_history

Rust
pub fn open_content_log_history() -> Result<()>

打开内容日志历史。

client::set_clipboard

Rust
pub fn set_clipboard(text: &str) -> Result<()>

把 text 写入系统剪贴板。

  • 参数:
    • text : &str
  • 返回值类型:Result<()>
  • 对应槽位:client_action

client::navigate_how_to_play

Rust
pub fn navigate_how_to_play(topic: &str) -> Result<()>

打开“游戏指南”界面并定位到 topic,主题为空时打开首页。

  • 参数:
    • topic : &str
  • 返回值类型:Result<()>
  • 对应槽位:client_action

client::options

Rust
pub fn options() -> Result<Vec<(String, String)>>

全部客户端选项,以 (key, value) 对的形式给出,值是 options.txt 保存时用的文本形式。

client::option

Rust
pub fn option(key: &str) -> Result<String>

按 options.txt 里的键读取一个客户端选项,例如 "gfx_field_of_view"。

  • 参数:
    • key : &str
  • 返回值类型:Result<String>
  • 对应槽位:client_option_get

client::set_option

Rust
pub fn set_option(key: &str, value: &str) -> Result<()>

用 options.txt 的文本形式写入一个客户端选项。引擎保留它能解析的部分,不报告任何结果,所以需要确认的调用方要把这个键读回来。

  • 参数:
    • key : &str
    • value : &str
  • 返回值类型:Result<()>
  • 对应槽位:client_option_set

Client

Rust
pub struct Client;

把客户端当作一个值,供下面的属性表使用:Client.health() 读取本地玩家的生命值,Client.is_mouse_grabbed() 向客户端实例询问。

  • 实现的 trait:Debug、Clone、Copy、Default、PartialEq、Eq

Client::pos_x

Rust
pub fn pos_x(&self) -> Result<f64>

Client::pos_y

Rust
pub fn pos_y(&self) -> Result<f64>

Client::pos_z

Rust
pub fn pos_z(&self) -> Result<f64>

Client::pitch

Rust
pub fn pitch(&self) -> Result<f64>

Client::yaw

Rust
pub fn yaw(&self) -> Result<f64>

Client::dimension

Rust
pub fn dimension(&self) -> Result<i32>

Client::gui_scale

Rust
pub fn gui_scale(&self) -> Result<f64>

Client::server_ping_ms

Rust
pub fn server_ping_ms(&self) -> Result<f64>

Client::is_showing_menu

Rust
pub fn is_showing_menu(&self) -> Result<bool>

Client::is_showing_pause_screen

Rust
pub fn is_showing_pause_screen(&self) -> Result<bool>

Client::is_playing

Rust
pub fn is_playing(&self) -> Result<bool>

Client::is_in_realm

Rust
pub fn is_in_realm(&self) -> Result<bool>

Client::is_game_input_enabled

Rust
pub fn is_game_input_enabled(&self) -> Result<bool>

Client::health

Rust
pub fn health(&self) -> Result<f64>

Client::max_health

Rust
pub fn max_health(&self) -> Result<f64>

Client::is_on_ground

Rust
pub fn is_on_ground(&self) -> Result<bool>

Client::is_in_water

Rust
pub fn is_in_water(&self) -> Result<bool>

Client::is_sneaking

Rust
pub fn is_sneaking(&self) -> Result<bool>

Client::is_sprinting

Rust
pub fn is_sprinting(&self) -> Result<bool>

Client::is_swimming

Rust
pub fn is_swimming(&self) -> Result<bool>

Client::is_on_fire

Rust
pub fn is_on_fire(&self) -> Result<bool>

Client::is_flying

Rust
pub fn is_flying(&self) -> Result<bool>

Client::game_type

Rust
pub fn game_type(&self) -> Result<i32>

Client::selected_slot

Rust
pub fn selected_slot(&self) -> Result<i32>

Client::vel_x

Rust
pub fn vel_x(&self) -> Result<f64>

Client::vel_y

Rust
pub fn vel_y(&self) -> Result<f64>

Client::vel_z

Rust
pub fn vel_z(&self) -> Result<f64>

Client::xp_needed_next_level

Rust
pub fn xp_needed_next_level(&self) -> Result<f64>

Client::is_leaving_game

Rust
pub fn is_leaving_game(&self) -> Result<bool>

Client::is_exiting_level

Rust
pub fn is_exiting_level(&self) -> Result<bool>

Client::is_in_bed_screen

Rust
pub fn is_in_bed_screen(&self) -> Result<bool>

Client::is_in_death_screen

Rust
pub fn is_in_death_screen(&self) -> Result<bool>

Client::is_showing_loading_screen

Rust
pub fn is_showing_loading_screen(&self) -> Result<bool>

Client::is_showing_progress_screen

Rust
pub fn is_showing_progress_screen(&self) -> Result<bool>

Client::is_showing_world_progress_screen

Rust
pub fn is_showing_world_progress_screen(&self) -> Result<bool>

Client::is_showing_death_screen

Rust
pub fn is_showing_death_screen(&self) -> Result<bool>

Client::is_showing_server_form

Rust
pub fn is_showing_server_form(&self) -> Result<bool>

Client::is_in_world_without_menu

Rust
pub fn is_in_world_without_menu(&self) -> Result<bool>

Client::is_world_active

Rust
pub fn is_world_active(&self) -> Result<bool>

Client::has_level

Rust
pub fn has_level(&self) -> Result<bool>

Client::is_pre_game

Rust
pub fn is_pre_game(&self) -> Result<bool>

Client::is_in_multiplayer_game

Rust
pub fn is_in_multiplayer_game(&self) -> Result<bool>

Client::is_multiplayer_client

Rust
pub fn is_multiplayer_client(&self) -> Result<bool>

Client::is_primary_level_multiplayer

Rust
pub fn is_primary_level_multiplayer(&self) -> Result<bool>

Client::is_primary_level_cross_platform

Rust
pub fn is_primary_level_cross_platform(&self) -> Result<bool>

Client::is_keyboard_enabled

Rust
pub fn is_keyboard_enabled(&self) -> Result<bool>

Client::is_using_controller

Rust
pub fn is_using_controller(&self) -> Result<bool>

Client::is_using_touchscreen

Rust
pub fn is_using_touchscreen(&self) -> Result<bool>

Client::is_mouse_grabbed

Rust
pub fn is_mouse_grabbed(&self) -> Result<bool>

Client::is_mouse_and_keyboard

Rust
pub fn is_mouse_and_keyboard(&self) -> Result<bool>

Client::has_connected_controller

Rust
pub fn has_connected_controller(&self) -> Result<bool>

Client::controller_id

Rust
pub fn controller_id(&self) -> Result<i32>

Client::is_touch_gameplay_allowed

Rust
pub fn is_touch_gameplay_allowed(&self) -> Result<bool>

Client::is_split_screen_active

Rust
pub fn is_split_screen_active(&self) -> Result<bool>

Client::split_screen_count

Rust
pub fn split_screen_count(&self) -> Result<i32>

Client::gui_scale_offset

Rust
pub fn gui_scale_offset(&self) -> Result<i32>

Client::is_edu_mode

Rust
pub fn is_edu_mode(&self) -> Result<bool>

Client::is_in_editor

Rust
pub fn is_in_editor(&self) -> Result<bool>

Client::is_ready_to_render

Rust
pub fn is_ready_to_render(&self) -> Result<bool>

Client::is_full_vanilla_pack_on_stack

Rust
pub fn is_full_vanilla_pack_on_stack(&self) -> Result<bool>

Client::server_connection_time

Rust
pub fn server_connection_time(&self) -> Result<f64>

Client::remote_server_time_ms

Rust
pub fn remote_server_time_ms(&self) -> Result<f64>

Client::remote_server_network_time_ms

Rust
pub fn remote_server_network_time_ms(&self) -> Result<f64>

Client::frame_alpha

Rust
pub fn frame_alpha(&self) -> Result<f64>

Client::game_update_seconds

Rust
pub fn game_update_seconds(&self) -> Result<f64>

Client::is_primary_client

Rust
pub fn is_primary_client(&self) -> Result<bool>

Client::user_id

Rust
pub fn user_id(&self) -> Result<i32>

Client::is_gameplay_tips_enabled

Rust
pub fn is_gameplay_tips_enabled(&self) -> Result<bool>

Client::has_commands

Rust
pub fn has_commands(&self) -> Result<bool>

Client::screen_name

Rust
pub fn screen_name(&self) -> Result<String>

Client::top_screen_name

Rust
pub fn top_screen_name(&self) -> Result<String>

Client::platform_id

Rust
pub fn platform_id(&self) -> Result<String>

Client::platform_online_id

Rust
pub fn platform_online_id(&self) -> Result<String>

Client::local_xuid

Rust
pub fn local_xuid(&self) -> Result<String>

Client::local_uuid

Rust
pub fn local_uuid(&self) -> Result<String>

Client::local_name_tag

Rust
pub fn local_name_tag(&self) -> Result<String>

Client::clipboard

Rust
pub fn clipboard(&self) -> Result<String>

KeyBinding

Rust
1
2
3
pub struct KeyBinding {
    // private fields
}

一个已注册的热键。丢弃它就会注销。

  • 实现的 trait:Drop、Debug

KeyBinding::name

Rust
pub fn name(&self) -> &str
  • 返回值类型:&str

KeyBinding::key_codes

Rust
pub fn key_codes(&self) -> Result<Vec<i32>>

当前绑定的按键码。玩家重新绑定以后,就和默认值不同了。

KeyBinding::forget

Rust
pub fn forget(mut self)

让它一直存活到模组卸载,那时由宿主清掉。

KeyAction

Rust
1
2
3
4
5
6
pub enum KeyAction {
        Up,
        Down,
        /// The host reported an action code this side does not recognize.
        Unknown(i32),
}

一个按键动作。

  • 实现的 trait:Debug、Clone、Copy、PartialEq、Eq

KeyAction::from_i32

Rust
pub fn from_i32(v: i32) -> KeyAction
  • 参数:
    • v : i32
  • 返回值类型:KeyAction

KeyAction::is_down

Rust
pub fn is_down(self) -> bool
  • 返回值类型:bool