refactor: unify scripting commands
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
use crate::State;
|
||||
use crate::{
|
||||
State,
|
||||
scripting::{eval, exec, reload},
|
||||
};
|
||||
use azalea::{
|
||||
GameProfileComponent, brigadier::prelude::*, chat::ChatPacket, entity::metadata::Player,
|
||||
prelude::*,
|
||||
@@ -37,42 +40,16 @@ impl CommandSource {
|
||||
pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
|
||||
commands.register(literal("reload").executes(|ctx: &Ctx| {
|
||||
let source = ctx.source.lock();
|
||||
let lua = source.state.lua.lock();
|
||||
let config_path = match lua.globals().get::<String>("config_path") {
|
||||
Ok(path) => path,
|
||||
Err(error) => {
|
||||
source.reply(&format!(
|
||||
"failed to get config_path from lua globals: {error:?}"
|
||||
));
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
if let Err(error) = match &std::fs::read_to_string(&config_path) {
|
||||
Ok(string) => lua.load(string).exec(),
|
||||
Err(error) => {
|
||||
source.reply(&format!("failed to read {config_path:?}: {error:?}"));
|
||||
return 0;
|
||||
}
|
||||
} {
|
||||
source.reply(&format!(
|
||||
"failed to execute configuration as lua code: {error:?}"
|
||||
));
|
||||
return 0;
|
||||
}
|
||||
source.reply(&format!("{:?}", reload(&source.state.lua.lock())));
|
||||
1
|
||||
}));
|
||||
|
||||
commands.register(
|
||||
literal("eval").then(argument("expr", string()).executes(|ctx: &Ctx| {
|
||||
literal("eval").then(argument("code", string()).executes(|ctx: &Ctx| {
|
||||
let source = ctx.source.lock();
|
||||
source.reply(&format!(
|
||||
"{:?}",
|
||||
source
|
||||
.state
|
||||
.lua
|
||||
.lock()
|
||||
.load(get_string(ctx, "expr").unwrap())
|
||||
.eval::<String>()
|
||||
eval(&source.state.lua.lock(), &get_string(ctx, "code").unwrap())
|
||||
));
|
||||
1
|
||||
})),
|
||||
@@ -83,12 +60,7 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
|
||||
let source = ctx.source.lock();
|
||||
source.reply(&format!(
|
||||
"{:?}",
|
||||
source
|
||||
.state
|
||||
.lua
|
||||
.lock()
|
||||
.load(get_string(ctx, "code").unwrap())
|
||||
.exec()
|
||||
exec(&source.state.lua.lock(), &get_string(ctx, "code").unwrap())
|
||||
));
|
||||
1
|
||||
})),
|
||||
|
Reference in New Issue
Block a user