Compare commits
No commits in common. "5e48377969c6c7fc698eb0a5c5805665ffd10288" and "b55207a55936105549d2724c0723a78e9a12c45c" have entirely different histories.
5e48377969
...
b55207a559
@ -14,8 +14,6 @@ use tokio::net::TcpListener;
|
|||||||
|
|
||||||
#[allow(clippy::too_many_lines)]
|
#[allow(clippy::too_many_lines)]
|
||||||
pub async fn handle_event(client: Client, event: Event, state: State) -> anyhow::Result<()> {
|
pub async fn handle_event(client: Client, event: Event, state: State) -> anyhow::Result<()> {
|
||||||
state.lua.gc_stop();
|
|
||||||
|
|
||||||
match event {
|
match event {
|
||||||
Event::AddPlayer(player_info) => {
|
Event::AddPlayer(player_info) => {
|
||||||
call_listeners(&state, "add_player", Player::from(player_info)).await;
|
call_listeners(&state, "add_player", Player::from(player_info)).await;
|
||||||
@ -24,13 +22,10 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> anyhow:
|
|||||||
let formatted_message = message.message();
|
let formatted_message = message.message();
|
||||||
info!("{}", formatted_message.to_ansi());
|
info!("{}", formatted_message.to_ansi());
|
||||||
|
|
||||||
|
let owners = state.lua.globals().get::<Vec<String>>("Owners")?;
|
||||||
if message.is_whisper()
|
if message.is_whisper()
|
||||||
&& let (Some(sender), content) = message.split_sender_and_content()
|
&& let (Some(sender), content) = message.split_sender_and_content()
|
||||||
&& state
|
&& owners.contains(&sender)
|
||||||
.lua
|
|
||||||
.globals()
|
|
||||||
.get::<Vec<String>>("Owners")?
|
|
||||||
.contains(&sender)
|
|
||||||
{
|
{
|
||||||
if let Err(error) = state.commands.execute(
|
if let Err(error) = state.commands.execute(
|
||||||
content,
|
content,
|
||||||
@ -60,7 +55,7 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> anyhow:
|
|||||||
}
|
}
|
||||||
Event::Disconnect(message) => {
|
Event::Disconnect(message) => {
|
||||||
call_listeners(&state, "disconnect", message.map(|m| m.to_string())).await;
|
call_listeners(&state, "disconnect", message.map(|m| m.to_string())).await;
|
||||||
exit(0)
|
exit(1)
|
||||||
}
|
}
|
||||||
Event::Login => call_listeners(&state, "login", ()).await,
|
Event::Login => call_listeners(&state, "login", ()).await,
|
||||||
Event::RemovePlayer(player_info) => {
|
Event::RemovePlayer(player_info) => {
|
||||||
@ -89,7 +84,8 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> anyhow:
|
|||||||
Event::Init => {
|
Event::Init => {
|
||||||
debug!("received initialize event");
|
debug!("received initialize event");
|
||||||
|
|
||||||
state.lua.globals().set(
|
let globals = state.lua.globals();
|
||||||
|
globals.set(
|
||||||
"client",
|
"client",
|
||||||
lua::client::Client {
|
lua::client::Client {
|
||||||
inner: Some(client),
|
inner: Some(client),
|
||||||
|
@ -9,10 +9,7 @@ pub fn register_functions(lua: &Lua, globals: &Table) -> Result<()> {
|
|||||||
"get_block_from_state",
|
"get_block_from_state",
|
||||||
lua.create_function(get_block_from_state)?,
|
lua.create_function(get_block_from_state)?,
|
||||||
)?;
|
)?;
|
||||||
globals.set(
|
globals.set("get_block_states", lua.create_function(get_block_states)?)?;
|
||||||
"get_block_states",
|
|
||||||
lua.create_async_function(get_block_states)?,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -21,24 +18,24 @@ pub fn get_block_from_state(lua: &Lua, state: u32) -> Result<Option<Table>> {
|
|||||||
let Ok(state) = BlockState::try_from(state) else {
|
let Ok(state) = BlockState::try_from(state) else {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
};
|
};
|
||||||
let block: Box<dyn AzaleaBlock> = state.into();
|
let b: Box<dyn AzaleaBlock> = state.into();
|
||||||
let behavior = block.behavior();
|
let bh = b.behavior();
|
||||||
|
|
||||||
let table = lua.create_table()?;
|
let block = lua.create_table()?;
|
||||||
table.set("id", block.id())?;
|
block.set("id", b.id())?;
|
||||||
table.set("friction", behavior.friction)?;
|
block.set("friction", bh.friction)?;
|
||||||
table.set("jump_factor", behavior.jump_factor)?;
|
block.set("jump_factor", bh.jump_factor)?;
|
||||||
table.set("destroy_time", behavior.destroy_time)?;
|
block.set("destroy_time", bh.destroy_time)?;
|
||||||
table.set("explosion_resistance", behavior.explosion_resistance)?;
|
block.set("explosion_resistance", bh.explosion_resistance)?;
|
||||||
table.set(
|
block.set(
|
||||||
"requires_correct_tool_for_drops",
|
"requires_correct_tool_for_drops",
|
||||||
behavior.requires_correct_tool_for_drops,
|
bh.requires_correct_tool_for_drops,
|
||||||
)?;
|
)?;
|
||||||
Ok(Some(table))
|
Ok(Some(block))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_block_states(
|
pub fn get_block_states(
|
||||||
lua: Lua,
|
lua: &Lua,
|
||||||
(block_names, filter_fn): (Vec<String>, Option<Function>),
|
(block_names, filter_fn): (Vec<String>, Option<Function>),
|
||||||
) -> Result<Vec<u16>> {
|
) -> Result<Vec<u16>> {
|
||||||
let mut matched = Vec::new();
|
let mut matched = Vec::new();
|
||||||
@ -52,7 +49,7 @@ pub async fn get_block_states(
|
|||||||
p.set("chest_type", b.property::<ChestType>().map(|v| v as u8))?;
|
p.set("chest_type", b.property::<ChestType>().map(|v| v as u8))?;
|
||||||
p.set("facing", b.property::<Facing>().map(|v| v as u8))?;
|
p.set("facing", b.property::<Facing>().map(|v| v as u8))?;
|
||||||
p.set("light_level", b.property::<LightLevel>().map(|v| v as u8))?;
|
p.set("light_level", b.property::<LightLevel>().map(|v| v as u8))?;
|
||||||
filter_fn.call_async::<bool>(p.clone()).await?
|
filter_fn.call::<bool>(p.clone())?
|
||||||
} else {
|
} else {
|
||||||
true
|
true
|
||||||
})
|
})
|
||||||
|
@ -59,7 +59,6 @@ impl UserData for Client {
|
|||||||
|
|
||||||
fn add_methods<M: UserDataMethods<Self>>(m: &mut M) {
|
fn add_methods<M: UserDataMethods<Self>>(m: &mut M) {
|
||||||
m.add_async_method("attack", interaction::attack);
|
m.add_async_method("attack", interaction::attack);
|
||||||
m.add_async_method("find_entities", world::find_entities);
|
|
||||||
m.add_async_method("go_to", movement::go_to);
|
m.add_async_method("go_to", movement::go_to);
|
||||||
m.add_async_method("look_at", movement::look_at);
|
m.add_async_method("look_at", movement::look_at);
|
||||||
m.add_async_method("mine", interaction::mine);
|
m.add_async_method("mine", interaction::mine);
|
||||||
@ -69,6 +68,7 @@ impl UserData for Client {
|
|||||||
m.add_method("chat", chat);
|
m.add_method("chat", chat);
|
||||||
m.add_method("disconnect", disconnect);
|
m.add_method("disconnect", disconnect);
|
||||||
m.add_method("find_blocks", world::find_blocks);
|
m.add_method("find_blocks", world::find_blocks);
|
||||||
|
m.add_method("find_entities", world::find_entities);
|
||||||
m.add_method("get_block_state", world::get_block_state);
|
m.add_method("get_block_state", world::get_block_state);
|
||||||
m.add_method("get_fluid_state", world::get_fluid_state);
|
m.add_method("get_fluid_state", world::get_fluid_state);
|
||||||
m.add_method("set_held_slot", container::set_held_slot);
|
m.add_method("set_held_slot", container::set_held_slot);
|
||||||
|
@ -117,15 +117,15 @@ pub fn jump(_lua: &Lua, client: &mut Client, _: ()) -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn looking_at(lua: &Lua, client: &Client) -> Result<Option<Table>> {
|
pub fn looking_at(lua: &Lua, client: &Client) -> Result<Option<Table>> {
|
||||||
let result = client.component::<HitResultComponent>();
|
let r = client.component::<HitResultComponent>();
|
||||||
Ok(if result.miss {
|
Ok(if r.miss {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
let table = lua.create_table()?;
|
let result = lua.create_table()?;
|
||||||
table.set("position", Vec3::from(result.block_pos))?;
|
result.set("position", Vec3::from(r.block_pos))?;
|
||||||
table.set("inside", result.inside)?;
|
result.set("inside", r.inside)?;
|
||||||
table.set("world_border", result.world_border)?;
|
result.set("world_border", r.world_border)?;
|
||||||
Some(table)
|
Some(result)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,29 +149,26 @@ pub async fn look_at(_lua: Lua, client: UserDataRef<Client>, position: Vec3) ->
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn pathfinder(lua: &Lua, client: &Client) -> Result<Table> {
|
pub fn pathfinder(lua: &Lua, client: &Client) -> Result<Table> {
|
||||||
let table = lua.create_table()?;
|
let pathfinder = lua.create_table()?;
|
||||||
table.set(
|
pathfinder.set(
|
||||||
"is_calculating",
|
"is_calculating",
|
||||||
client.component::<Pathfinder>().is_calculating,
|
client.component::<Pathfinder>().is_calculating,
|
||||||
)?;
|
)?;
|
||||||
table.set(
|
pathfinder.set(
|
||||||
"is_executing",
|
"is_executing",
|
||||||
if let Some(pathfinder) = client.get_component::<ExecutingPath>() {
|
if let Some(p) = client.get_component::<ExecutingPath>() {
|
||||||
table.set(
|
pathfinder.set("last_reached_node", Vec3::from(p.last_reached_node))?;
|
||||||
"last_reached_node",
|
pathfinder.set(
|
||||||
Vec3::from(pathfinder.last_reached_node),
|
|
||||||
)?;
|
|
||||||
table.set(
|
|
||||||
"last_node_reach_elapsed",
|
"last_node_reach_elapsed",
|
||||||
pathfinder.last_node_reached_at.elapsed().as_millis(),
|
p.last_node_reached_at.elapsed().as_millis(),
|
||||||
)?;
|
)?;
|
||||||
table.set("is_path_partial", pathfinder.is_path_partial)?;
|
pathfinder.set("is_path_partial", p.is_path_partial)?;
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
Ok(table)
|
Ok(pathfinder)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn position(_lua: &Lua, client: &Client) -> Result<Vec3> {
|
pub fn position(_lua: &Lua, client: &Client) -> Result<Vec3> {
|
||||||
|
@ -16,12 +16,12 @@ pub fn health(_lua: &Lua, client: &Client) -> Result<f32> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn hunger(lua: &Lua, client: &Client) -> Result<Table> {
|
pub fn hunger(lua: &Lua, client: &Client) -> Result<Table> {
|
||||||
let hunger = client.hunger();
|
let h = client.hunger();
|
||||||
|
|
||||||
let table = lua.create_table()?;
|
let hunger = lua.create_table()?;
|
||||||
table.set("food", hunger.food)?;
|
hunger.set("food", h.food)?;
|
||||||
table.set("saturation", hunger.saturation)?;
|
hunger.set("saturation", h.saturation)?;
|
||||||
Ok(table)
|
Ok(hunger)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn score(_lua: &Lua, client: &Client) -> Result<i32> {
|
pub fn score(_lua: &Lua, client: &Client) -> Result<i32> {
|
||||||
|
@ -10,15 +10,15 @@ use azalea::{
|
|||||||
},
|
},
|
||||||
world::{InstanceName, MinecraftEntityId},
|
world::{InstanceName, MinecraftEntityId},
|
||||||
};
|
};
|
||||||
use mlua::{Function, Lua, Result, Table, UserDataRef};
|
use mlua::{Function, Lua, Result, Table};
|
||||||
|
|
||||||
pub fn best_tool_for_block(lua: &Lua, client: &Client, block_state: u16) -> Result<Table> {
|
pub fn best_tool_for_block(lua: &Lua, client: &Client, block_state: u16) -> Result<Table> {
|
||||||
let result = client.best_tool_in_hotbar_for_block(BlockState { id: block_state });
|
let tr = client.best_tool_in_hotbar_for_block(BlockState { id: block_state });
|
||||||
|
|
||||||
let table = lua.create_table()?;
|
let tool_result = lua.create_table()?;
|
||||||
table.set("index", result.index)?;
|
tool_result.set("index", tr.index)?;
|
||||||
table.set("percentage_per_tick", result.percentage_per_tick)?;
|
tool_result.set("percentage_per_tick", tr.percentage_per_tick)?;
|
||||||
Ok(table)
|
Ok(tool_result)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dimension(_lua: &Lua, client: &Client) -> Result<String> {
|
pub fn dimension(_lua: &Lua, client: &Client) -> Result<String> {
|
||||||
@ -48,14 +48,9 @@ pub fn find_blocks(
|
|||||||
.collect())
|
.collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn find_entities(
|
pub fn find_entities(lua: &Lua, client: &Client, filter_fn: Function) -> Result<Vec<Table>> {
|
||||||
lua: Lua,
|
let mut matched = Vec::new();
|
||||||
client: UserDataRef<Client>,
|
|
||||||
filter_fn: Function,
|
|
||||||
) -> Result<Vec<Table>> {
|
|
||||||
let mut entities = Vec::new();
|
|
||||||
|
|
||||||
{
|
|
||||||
let mut ecs = client.ecs.lock();
|
let mut ecs = client.ecs.lock();
|
||||||
let mut query = ecs.query_filtered::<(
|
let mut query = ecs.query_filtered::<(
|
||||||
&MinecraftEntityId,
|
&MinecraftEntityId,
|
||||||
@ -67,7 +62,7 @@ pub async fn find_entities(
|
|||||||
&Pose,
|
&Pose,
|
||||||
), Without<Dead>>();
|
), Without<Dead>>();
|
||||||
|
|
||||||
for (id, uuid, kind, custom_name, position, direction, pose) in query.iter(&ecs) {
|
for (&id, uuid, kind, custom_name, position, direction, pose) in query.iter(&ecs) {
|
||||||
let entity = lua.create_table()?;
|
let entity = lua.create_table()?;
|
||||||
entity.set("id", id.0)?;
|
entity.set("id", id.0)?;
|
||||||
entity.set("uuid", uuid.to_string())?;
|
entity.set("uuid", uuid.to_string())?;
|
||||||
@ -76,16 +71,12 @@ pub async fn find_entities(
|
|||||||
entity.set("position", Vec3::from(position))?;
|
entity.set("position", Vec3::from(position))?;
|
||||||
entity.set("direction", Direction::from(direction))?;
|
entity.set("direction", Direction::from(direction))?;
|
||||||
entity.set("pose", *pose as u8)?;
|
entity.set("pose", *pose as u8)?;
|
||||||
entities.push(entity);
|
|
||||||
|
if filter_fn.call::<bool>(&entity)? {
|
||||||
|
matched.push(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut matched = Vec::new();
|
|
||||||
for entity in entities {
|
|
||||||
if filter_fn.call_async::<bool>(&entity).await? {
|
|
||||||
matched.push(entity)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(matched)
|
Ok(matched)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,16 +96,16 @@ pub fn get_block_state(_lua: &Lua, client: &Client, position: Vec3) -> Result<Op
|
|||||||
pub fn get_fluid_state(lua: &Lua, client: &Client, position: Vec3) -> Result<Option<Table>> {
|
pub fn get_fluid_state(lua: &Lua, client: &Client, position: Vec3) -> Result<Option<Table>> {
|
||||||
#[allow(clippy::cast_possible_truncation)]
|
#[allow(clippy::cast_possible_truncation)]
|
||||||
Ok(
|
Ok(
|
||||||
if let Some(state) = client.world().read().get_fluid_state(&BlockPos::new(
|
if let Some(fs) = client.world().read().get_fluid_state(&BlockPos::new(
|
||||||
position.x as i32,
|
position.x as i32,
|
||||||
position.y as i32,
|
position.y as i32,
|
||||||
position.z as i32,
|
position.z as i32,
|
||||||
)) {
|
)) {
|
||||||
let table = lua.create_table()?;
|
let fluid_state = lua.create_table()?;
|
||||||
table.set("kind", state.kind as u8)?;
|
fluid_state.set("kind", fs.kind as u8)?;
|
||||||
table.set("amount", state.amount)?;
|
fluid_state.set("amount", fs.amount)?;
|
||||||
table.set("falling", state.falling)?;
|
fluid_state.set("falling", fs.falling)?;
|
||||||
Some(table)
|
Some(fluid_state)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
},
|
},
|
||||||
|
@ -46,11 +46,12 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
console_subscriber::init();
|
console_subscriber::init();
|
||||||
|
|
||||||
let args = arguments::Arguments::parse();
|
let args = arguments::Arguments::parse();
|
||||||
|
|
||||||
let script_path = args.script.unwrap_or(PathBuf::from(DEFAULT_SCRIPT_PATH));
|
let script_path = args.script.unwrap_or(PathBuf::from(DEFAULT_SCRIPT_PATH));
|
||||||
let event_listeners = Arc::new(RwLock::new(HashMap::new()));
|
let event_listeners = Arc::new(RwLock::new(HashMap::new()));
|
||||||
|
|
||||||
let lua = Lua::new();
|
let lua = Lua::new();
|
||||||
let globals = lua.globals();
|
let globals = lua.globals();
|
||||||
|
|
||||||
globals.set("script_path", &*script_path)?;
|
globals.set("script_path", &*script_path)?;
|
||||||
lua::register_functions(&lua, &globals, event_listeners.clone())?;
|
lua::register_functions(&lua, &globals, event_listeners.clone())?;
|
||||||
lua.load(
|
lua.load(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user