mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Added plugin command support
This commit is contained in:
parent
2743825181
commit
cae81e625e
Binary file not shown.
@ -17,6 +17,8 @@
|
|||||||
option_zip
|
option_zip
|
||||||
)]
|
)]
|
||||||
|
|
||||||
|
pub extern crate plugin_api;
|
||||||
|
|
||||||
pub mod assets;
|
pub mod assets;
|
||||||
pub mod astar;
|
pub mod astar;
|
||||||
pub mod character;
|
pub mod character;
|
||||||
|
@ -13,6 +13,13 @@ impl Into<u64> for Uid {
|
|||||||
fn into(self) -> u64 { self.0 }
|
fn into(self) -> u64 { self.0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl Into<plugin_api::Uid> for Uid {
|
||||||
|
fn into(self) -> plugin_api::Uid {
|
||||||
|
plugin_api::Uid(self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<u64> for Uid {
|
impl From<u64> for Uid {
|
||||||
fn from(uid: u64) -> Self { Self(uid) }
|
fn from(uid: u64) -> Self { Self(uid) }
|
||||||
}
|
}
|
||||||
|
@ -12,11 +12,34 @@ pub trait Event: Serialize + DeserializeOwned{
|
|||||||
type Response: Serialize + DeserializeOwned;
|
type Response: Serialize + DeserializeOwned;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Unify this with common/src/comp/uid.rs:Uid
|
||||||
|
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
|
||||||
|
pub struct Uid(pub u64);
|
||||||
|
|
||||||
pub mod events {
|
pub mod events {
|
||||||
|
|
||||||
|
use crate::Uid;
|
||||||
|
|
||||||
use super::Event;
|
use super::Event;
|
||||||
use serde::{Serialize,Deserialize};
|
use serde::{Serialize,Deserialize};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
|
||||||
|
pub struct ChatCommandEvent {
|
||||||
|
pub command: String,
|
||||||
|
pub command_args: Vec<String>,
|
||||||
|
pub player: Player,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Event for ChatCommandEvent {
|
||||||
|
type Response = Result<Vec<String>, String>;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
|
||||||
|
pub struct Player {
|
||||||
|
pub id: Uid,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
|
||||||
pub struct PlayerJoinEvent {
|
pub struct PlayerJoinEvent {
|
||||||
pub player_name: String,
|
pub player_name: String,
|
||||||
pub player_id: usize
|
pub player_id: usize
|
||||||
@ -26,7 +49,7 @@ pub mod events {
|
|||||||
type Response = PlayerJoinResult;
|
type Response = PlayerJoinResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
|
||||||
pub enum PlayerJoinResult {
|
pub enum PlayerJoinResult {
|
||||||
CloseConnection,
|
CloseConnection,
|
||||||
None
|
None
|
||||||
@ -38,7 +61,7 @@ pub mod events {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
|
||||||
pub struct PluginLoadEvent;
|
pub struct PluginLoadEvent;
|
||||||
|
|
||||||
impl Event for PluginLoadEvent {
|
impl Event for PluginLoadEvent {
|
||||||
|
1
plugin/hello/Cargo.lock
generated
1
plugin/hello/Cargo.lock
generated
@ -20,7 +20,6 @@ checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de"
|
|||||||
name = "hello"
|
name = "hello"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"veloren-plugin-api",
|
|
||||||
"veloren-plugin-rt",
|
"veloren-plugin-rt",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -9,6 +9,5 @@ crate-type = ["cdylib"]
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
plugin-rt = { package = "veloren-plugin-rt", path = "../rt" }
|
plugin-rt = { package = "veloren-plugin-rt", path = "../rt" }
|
||||||
plugin-api = { package = "veloren-plugin-api", path = "../api" }
|
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
pub extern crate plugin_rt;
|
extern crate plugin_rt;
|
||||||
|
|
||||||
use plugin_rt::*;
|
use plugin_rt::*;
|
||||||
use plugin_api::{Action, events::*};
|
use plugin_rt::api::{Action, events::*};
|
||||||
|
|
||||||
#[event_handler]
|
#[event_handler]
|
||||||
pub fn on_load(load: PluginLoadEvent) -> () {
|
pub fn on_load(load: PluginLoadEvent) -> () {
|
||||||
@ -9,6 +9,11 @@ pub fn on_load(load: PluginLoadEvent) -> () {
|
|||||||
println!("Hello world");
|
println!("Hello world");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[event_handler]
|
||||||
|
pub fn on_command_testplugin(command: ChatCommandEvent) -> Result<Vec<String>, String> {
|
||||||
|
Ok(vec![format!("Player of id {:?} sended command with args {:?}",command.player,command.command_args)])
|
||||||
|
}
|
||||||
|
|
||||||
#[event_handler]
|
#[event_handler]
|
||||||
pub fn on_player_join(input: PlayerJoinEvent) -> PlayerJoinResult {
|
pub fn on_player_join(input: PlayerJoinEvent) -> PlayerJoinResult {
|
||||||
send_actions(vec![Action::PlayerSendMessage(input.player_id,format!("Welcome {} on our server",input.player_name))]);
|
send_actions(vec![Action::PlayerSendMessage(input.player_id,format!("Welcome {} on our server",input.player_name))]);
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
#![feature(const_fn)]
|
#![feature(const_fn)]
|
||||||
|
|
||||||
|
pub extern crate plugin_derive;
|
||||||
|
|
||||||
pub use plugin_api as api;
|
pub use plugin_api as api;
|
||||||
pub use plugin_derive::*;
|
pub use plugin_derive::*;
|
||||||
|
|
||||||
|
@ -46,22 +46,9 @@ use crate::{
|
|||||||
state_ext::StateExt,
|
state_ext::StateExt,
|
||||||
sys::sentinel::{DeletedEntities, TrackedComps},
|
sys::sentinel::{DeletedEntities, TrackedComps},
|
||||||
};
|
};
|
||||||
use common::{
|
use common::{assets::Asset, cmd::ChatCommand, comp::{self, ChatType}, event::{EventBus, ServerEvent}, msg::{
|
||||||
assets::Asset,
|
|
||||||
cmd::ChatCommand,
|
|
||||||
comp::{self, ChatType},
|
|
||||||
event::{EventBus, ServerEvent},
|
|
||||||
msg::{
|
|
||||||
ClientType, DisconnectReason, ServerGeneral, ServerInfo, ServerInit, ServerMsg, WorldMapMsg,
|
ClientType, DisconnectReason, ServerGeneral, ServerInfo, ServerInit, ServerMsg, WorldMapMsg,
|
||||||
},
|
}, outcome::Outcome, plugin::PluginMgr, recipe::default_recipe_book, resources::TimeOfDay, rtsim::RtSimEntity, sync::{Uid, WorldSyncExt}, terrain::TerrainChunkSize, vol::{ReadVol, RectVolSize}};
|
||||||
outcome::Outcome,
|
|
||||||
recipe::default_recipe_book,
|
|
||||||
resources::TimeOfDay,
|
|
||||||
rtsim::RtSimEntity,
|
|
||||||
sync::WorldSyncExt,
|
|
||||||
terrain::TerrainChunkSize,
|
|
||||||
vol::{ReadVol, RectVolSize},
|
|
||||||
};
|
|
||||||
use common_sys::state::State;
|
use common_sys::state::State;
|
||||||
use futures_executor::block_on;
|
use futures_executor::block_on;
|
||||||
use metrics::{PhysicsMetrics, ServerMetrics, StateTickMetrics, TickMetrics};
|
use metrics::{PhysicsMetrics, ServerMetrics, StateTickMetrics, TickMetrics};
|
||||||
@ -1008,13 +995,61 @@ impl Server {
|
|||||||
if let Ok(command) = kwd.parse::<ChatCommand>() {
|
if let Ok(command) = kwd.parse::<ChatCommand>() {
|
||||||
command.execute(self, entity, args);
|
command.execute(self, entity, args);
|
||||||
} else {
|
} else {
|
||||||
self.notify_client(
|
let plugin_manager = self.state.ecs().read_resource::<PluginMgr>();
|
||||||
entity,
|
let rs = plugin_manager.execute_event(&format!("on_command_{}",&kwd), &common::plugin_api::events::ChatCommandEvent {
|
||||||
ChatType::CommandError.server_msg(format!(
|
command: kwd.clone(),
|
||||||
"Unknown command '/{}'.\nType '/help' for available commands",
|
command_args: args.split(" ").map(|x| x.to_owned()).collect(),
|
||||||
kwd
|
player: common::plugin_api::events::Player {
|
||||||
)),
|
id: (*(self.state.ecs().read_storage::<Uid>().get(entity).expect("Can't get player UUID [This should never appen]"))).into()
|
||||||
);
|
},
|
||||||
|
});
|
||||||
|
match rs {
|
||||||
|
Ok(e) => {
|
||||||
|
if e.is_empty() {
|
||||||
|
self.notify_client(
|
||||||
|
entity,
|
||||||
|
ChatType::CommandError.server_msg(format!(
|
||||||
|
"Unknown command '/{}'.\nType '/help' for available commands",
|
||||||
|
kwd
|
||||||
|
))
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
e.into_iter().for_each(|e| {
|
||||||
|
match e {
|
||||||
|
Ok(e) => {
|
||||||
|
if !e.is_empty() {
|
||||||
|
self.notify_client(
|
||||||
|
entity,
|
||||||
|
ChatType::CommandInfo.server_msg(e.join("\n")),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
self.notify_client(
|
||||||
|
entity,
|
||||||
|
ChatType::CommandError.server_msg(format!(
|
||||||
|
"Error occurred while executing command '/{}'.\n{}",
|
||||||
|
kwd,
|
||||||
|
e
|
||||||
|
)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
error!(?e, "Can't execute command {} {}",kwd,args);
|
||||||
|
self.notify_client(
|
||||||
|
entity,
|
||||||
|
ChatType::CommandError.server_msg(format!(
|
||||||
|
"Internal error while executing '/{}'.\nContact the server administrator",
|
||||||
|
kwd
|
||||||
|
))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user