refactor: remove redundant lua type wrappers
This commit is contained in:
parent
2cba4d797f
commit
0303244897
@ -2,54 +2,7 @@ use azalea::blocks::{
|
||||
Block as AzaleaBlock, BlockState,
|
||||
properties::{ChestType, Facing, LightLevel},
|
||||
};
|
||||
use mlua::{FromLua, Function, IntoLua, Lua, Result, Table, Value};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Block {
|
||||
pub id: String,
|
||||
pub friction: f32,
|
||||
pub jump_factor: f32,
|
||||
pub destroy_time: f32,
|
||||
pub explosion_resistance: f32,
|
||||
pub requires_correct_tool_for_drops: bool,
|
||||
}
|
||||
|
||||
impl IntoLua for Block {
|
||||
fn into_lua(self, lua: &Lua) -> Result<Value> {
|
||||
let table = lua.create_table()?;
|
||||
table.set("id", self.id)?;
|
||||
table.set("friction", self.friction)?;
|
||||
table.set("jump_factor", self.jump_factor)?;
|
||||
table.set("destroy_time", self.destroy_time)?;
|
||||
table.set("explosion_resistance", self.explosion_resistance)?;
|
||||
table.set(
|
||||
"requires_correct_tool_for_drops",
|
||||
self.requires_correct_tool_for_drops,
|
||||
)?;
|
||||
Ok(Value::Table(table))
|
||||
}
|
||||
}
|
||||
|
||||
impl FromLua for Block {
|
||||
fn from_lua(value: Value, _lua: &Lua) -> Result<Self> {
|
||||
if let Value::Table(table) = value {
|
||||
Ok(Self {
|
||||
id: table.get("id")?,
|
||||
friction: table.get("friction")?,
|
||||
jump_factor: table.get("jump_factor")?,
|
||||
destroy_time: table.get("destroy_time")?,
|
||||
explosion_resistance: table.get("explosion_resistance")?,
|
||||
requires_correct_tool_for_drops: table.get("requires_correct_tool_for_drops")?,
|
||||
})
|
||||
} else {
|
||||
Err(mlua::Error::FromLuaConversionError {
|
||||
from: value.type_name(),
|
||||
to: "Block".to_string(),
|
||||
message: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
use mlua::{Function, Lua, Result, Table};
|
||||
|
||||
pub fn register_functions(lua: &Lua, globals: &Table) -> Result<()> {
|
||||
globals.set(
|
||||
@ -61,21 +14,24 @@ pub fn register_functions(lua: &Lua, globals: &Table) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_block_from_state(_lua: &Lua, state: u32) -> Result<Option<Block>> {
|
||||
pub fn get_block_from_state(lua: &Lua, state: u32) -> Result<Option<Table>> {
|
||||
let Ok(state) = BlockState::try_from(state) else {
|
||||
return Ok(None);
|
||||
};
|
||||
let block: Box<dyn AzaleaBlock> = state.into();
|
||||
let behavior = block.behavior();
|
||||
let b: Box<dyn AzaleaBlock> = state.into();
|
||||
let bh = b.behavior();
|
||||
|
||||
Ok(Some(Block {
|
||||
id: block.id().to_string(),
|
||||
friction: behavior.friction,
|
||||
jump_factor: behavior.jump_factor,
|
||||
destroy_time: behavior.destroy_time,
|
||||
explosion_resistance: behavior.explosion_resistance,
|
||||
requires_correct_tool_for_drops: behavior.requires_correct_tool_for_drops,
|
||||
}))
|
||||
let block = lua.create_table()?;
|
||||
block.set("id", b.id())?;
|
||||
block.set("friction", bh.friction)?;
|
||||
block.set("jump_factor", bh.jump_factor)?;
|
||||
block.set("destroy_time", bh.destroy_time)?;
|
||||
block.set("explosion_resistance", bh.explosion_resistance)?;
|
||||
block.set(
|
||||
"requires_correct_tool_for_drops",
|
||||
bh.requires_correct_tool_for_drops,
|
||||
)?;
|
||||
Ok(Some(block))
|
||||
}
|
||||
|
||||
pub fn get_block_states(
|
||||
|
@ -8,9 +8,6 @@ use super::{
|
||||
container::item_stack::ItemStack,
|
||||
container::{Container, ContainerRef},
|
||||
direction::Direction,
|
||||
entity::Entity,
|
||||
fluid_state::FluidState,
|
||||
hunger::Hunger,
|
||||
vec3::Vec3,
|
||||
};
|
||||
use azalea::Client as AzaleaClient;
|
||||
|
@ -1,4 +1,4 @@
|
||||
use super::{Client, Hunger};
|
||||
use super::Client;
|
||||
use azalea::{
|
||||
ClientInformation,
|
||||
entity::metadata::{AirSupply, Score},
|
||||
@ -13,12 +13,13 @@ pub fn health(_lua: &Lua, client: &Client) -> Result<f32> {
|
||||
Ok(client.inner.as_ref().unwrap().health())
|
||||
}
|
||||
|
||||
pub fn hunger(_lua: &Lua, client: &Client) -> Result<Hunger> {
|
||||
pub fn hunger(lua: &Lua, client: &Client) -> Result<Table> {
|
||||
let h = client.inner.as_ref().unwrap().hunger();
|
||||
Ok(Hunger {
|
||||
food: h.food,
|
||||
saturation: h.saturation,
|
||||
})
|
||||
|
||||
let hunger = lua.create_table()?;
|
||||
hunger.set("food", h.food)?;
|
||||
hunger.set("saturation", h.saturation)?;
|
||||
Ok(hunger)
|
||||
}
|
||||
|
||||
pub fn score(_lua: &Lua, client: &Client) -> Result<i32> {
|
||||
|
@ -1,4 +1,4 @@
|
||||
use super::{Client, Entity, FluidState, Vec3};
|
||||
use super::{Client, Vec3};
|
||||
use azalea::{
|
||||
BlockPos,
|
||||
auto_tool::AutoToolClientExt,
|
||||
@ -52,7 +52,7 @@ pub fn find_blocks(
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub fn find_entities(_lua: &Lua, client: &Client, filter_fn: Function) -> Result<Vec<Entity>> {
|
||||
pub fn find_entities(lua: &Lua, client: &Client, filter_fn: Function) -> Result<Vec<Table>> {
|
||||
let mut matched = Vec::new();
|
||||
|
||||
let mut ecs = client.inner.as_ref().unwrap().ecs.lock();
|
||||
@ -65,19 +65,21 @@ pub fn find_entities(_lua: &Lua, client: &Client, filter_fn: Function) -> Result
|
||||
), Without<Dead>>();
|
||||
|
||||
for (&id, uuid, kind, position, custom_name) in query.iter(&ecs) {
|
||||
let entity = Entity {
|
||||
id: id.0,
|
||||
uuid: uuid.to_string(),
|
||||
kind: kind.to_string(),
|
||||
position: Vec3 {
|
||||
let entity = lua.create_table()?;
|
||||
entity.set("id", id.0)?;
|
||||
entity.set("uuid", uuid.to_string())?;
|
||||
entity.set("kind", kind.to_string())?;
|
||||
entity.set(
|
||||
"position",
|
||||
Vec3 {
|
||||
x: position.x,
|
||||
y: position.y,
|
||||
z: position.z,
|
||||
},
|
||||
custom_name: custom_name.as_ref().map(ToString::to_string),
|
||||
};
|
||||
)?;
|
||||
entity.set("custom_name", custom_name.as_ref().map(ToString::to_string))?;
|
||||
|
||||
if filter_fn.call::<bool>(entity.clone())? {
|
||||
if filter_fn.call::<bool>(&entity)? {
|
||||
matched.push(entity);
|
||||
}
|
||||
}
|
||||
@ -101,22 +103,28 @@ pub fn get_block_state(_lua: &Lua, client: &Client, position: Vec3) -> Result<Op
|
||||
.map(|b| b.id))
|
||||
}
|
||||
|
||||
pub fn get_fluid_state(_lua: &Lua, client: &Client, position: Vec3) -> Result<Option<FluidState>> {
|
||||
pub fn get_fluid_state(lua: &Lua, client: &Client, position: Vec3) -> Result<Option<Table>> {
|
||||
#[allow(clippy::cast_possible_truncation)]
|
||||
Ok(client
|
||||
.inner
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.world()
|
||||
.read()
|
||||
.get_fluid_state(&BlockPos::new(
|
||||
position.x as i32,
|
||||
position.y as i32,
|
||||
position.z as i32,
|
||||
))
|
||||
.map(|f| FluidState {
|
||||
kind: f.kind as u8,
|
||||
amount: f.amount,
|
||||
falling: f.falling,
|
||||
}))
|
||||
Ok(
|
||||
if let Some(fs) = client
|
||||
.inner
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.world()
|
||||
.read()
|
||||
.get_fluid_state(&BlockPos::new(
|
||||
position.x as i32,
|
||||
position.y as i32,
|
||||
position.z as i32,
|
||||
))
|
||||
{
|
||||
let fluid_state = lua.create_table()?;
|
||||
fluid_state.set("kind", fs.kind as u8)?;
|
||||
fluid_state.set("amount", fs.amount)?;
|
||||
fluid_state.set("falling", fs.falling)?;
|
||||
Some(fluid_state)
|
||||
} else {
|
||||
None
|
||||
},
|
||||
)
|
||||
}
|
||||
|
@ -1,43 +0,0 @@
|
||||
use super::vec3::Vec3;
|
||||
use mlua::{FromLua, IntoLua, Lua, Result, Value};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Entity {
|
||||
pub id: u32,
|
||||
pub uuid: String,
|
||||
pub kind: String,
|
||||
pub position: Vec3,
|
||||
pub custom_name: Option<String>,
|
||||
}
|
||||
|
||||
impl IntoLua for Entity {
|
||||
fn into_lua(self, lua: &Lua) -> Result<Value> {
|
||||
let entity = lua.create_table()?;
|
||||
entity.set("id", self.id)?;
|
||||
entity.set("uuid", self.uuid)?;
|
||||
entity.set("kind", self.kind)?;
|
||||
entity.set("position", self.position)?;
|
||||
entity.set("custom_name", self.custom_name)?;
|
||||
Ok(Value::Table(entity))
|
||||
}
|
||||
}
|
||||
|
||||
impl FromLua for Entity {
|
||||
fn from_lua(value: Value, _lua: &Lua) -> Result<Self> {
|
||||
if let Value::Table(table) = value {
|
||||
Ok(Self {
|
||||
id: table.get("id")?,
|
||||
uuid: table.get("uuid")?,
|
||||
kind: table.get("kind")?,
|
||||
position: table.get("position")?,
|
||||
custom_name: table.get("custom_name")?,
|
||||
})
|
||||
} else {
|
||||
Err(mlua::Error::FromLuaConversionError {
|
||||
from: value.type_name(),
|
||||
to: "Position".to_string(),
|
||||
message: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
use mlua::{FromLua, IntoLua, Lua, Result, Value};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct FluidState {
|
||||
pub kind: u8,
|
||||
pub amount: u8,
|
||||
pub falling: bool,
|
||||
}
|
||||
|
||||
impl IntoLua for FluidState {
|
||||
fn into_lua(self, lua: &Lua) -> Result<Value> {
|
||||
let table = lua.create_table()?;
|
||||
table.set("kind", self.kind)?;
|
||||
table.set("amount", self.amount)?;
|
||||
table.set("falling", self.falling)?;
|
||||
Ok(Value::Table(table))
|
||||
}
|
||||
}
|
||||
|
||||
impl FromLua for FluidState {
|
||||
fn from_lua(value: Value, _lua: &Lua) -> Result<Self> {
|
||||
if let Value::Table(table) = value {
|
||||
Ok(Self {
|
||||
kind: table.get("kind")?,
|
||||
amount: table.get("amount")?,
|
||||
falling: table.get("falling")?,
|
||||
})
|
||||
} else {
|
||||
Err(mlua::Error::FromLuaConversionError {
|
||||
from: value.type_name(),
|
||||
to: "FluidState".to_string(),
|
||||
message: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
use mlua::{FromLua, IntoLua, Lua, Result, Value};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Hunger {
|
||||
pub food: u32,
|
||||
pub saturation: f32,
|
||||
}
|
||||
|
||||
impl IntoLua for Hunger {
|
||||
fn into_lua(self, lua: &Lua) -> Result<Value> {
|
||||
let table = lua.create_table()?;
|
||||
table.set("food", self.food)?;
|
||||
table.set("saturation", self.saturation)?;
|
||||
Ok(Value::Table(table))
|
||||
}
|
||||
}
|
||||
|
||||
impl FromLua for Hunger {
|
||||
fn from_lua(value: Value, _lua: &Lua) -> Result<Self> {
|
||||
if let Value::Table(table) = value {
|
||||
Ok(Self {
|
||||
food: table.get("food")?,
|
||||
saturation: table.get("saturation")?,
|
||||
})
|
||||
} else {
|
||||
Err(mlua::Error::FromLuaConversionError {
|
||||
from: value.type_name(),
|
||||
to: "Hunger".to_string(),
|
||||
message: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
@ -2,9 +2,6 @@ pub mod block;
|
||||
pub mod client;
|
||||
pub mod container;
|
||||
pub mod direction;
|
||||
pub mod entity;
|
||||
pub mod fluid_state;
|
||||
pub mod hunger;
|
||||
pub mod logging;
|
||||
pub mod vec3;
|
||||
|
||||
|
@ -28,7 +28,7 @@ impl FromLua for Vec3 {
|
||||
} else {
|
||||
Err(mlua::Error::FromLuaConversionError {
|
||||
from: value.type_name(),
|
||||
to: "Position".to_string(),
|
||||
to: "Vec3".to_string(),
|
||||
message: None,
|
||||
})
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user