mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-10-25 13:30:43 +00:00
improvements to many aspects of plugins
This commit is contained in:
1 parent
37d7a289eb
commit
0f5d61c89b
4 files changed
+33
-16
No files matched your search
@@ -5,7 +5,7 @@ function onPluginLoaded()
|
|||||||
print("HI!")
|
print("HI!")
|
||||||
end
|
end
|
||||||
|
|
||||||
function onPlayerAuthenticated(joined_name)
|
function onPlayerAuthenticated(joined_name, role, is_guest, identifiers)
|
||||||
print("hi welcome mista " .. joined_name)
|
print("hi welcome mista " .. joined_name)
|
||||||
|
|
||||||
print("current players:")
|
print("current players:")
|
||||||
@@ -14,8 +14,15 @@ function onPlayerAuthenticated(joined_name)
|
|||||||
end
|
end
|
||||||
print("yipee")
|
print("yipee")
|
||||||
|
|
||||||
|
print("Player count: " .. MP.GetPlayerCount())
|
||||||
|
|
||||||
return 0 -- 0 = do not block
|
return 0 -- 0 = do not block
|
||||||
end
|
end
|
||||||
|
|
||||||
MP.RegisterEventHandler("onPluginLoaded", "onPluginLoaded")
|
function onPlayerDisconnect(pid)
|
||||||
MP.RegisterEventHandler("onPlayerAuthenticated", "onPlayerAuthenticated")
|
print("Player with PID " .. pid .. " has left!")
|
||||||
|
end
|
||||||
|
|
||||||
|
MP.RegisterEventHandler("onInit", "onPluginLoaded")
|
||||||
|
MP.RegisterEventHandler("onPlayerAuth", "onPlayerAuthenticated")
|
||||||
|
MP.RegisterEventHandler("onPlayerDisconnect", "onPlayerDisconnect")
|
||||||
+10
-4
@@ -382,11 +382,10 @@ impl Server {
|
|||||||
.clone();
|
.clone();
|
||||||
info!("Welcome {name}!");
|
info!("Welcome {name}!");
|
||||||
joined_names.push(name.clone());
|
joined_names.push(name.clone());
|
||||||
let arg = Argument::String(name);
|
|
||||||
let mut vrx = Vec::new();
|
let mut vrx = Vec::new();
|
||||||
for plugin in &self.plugins {
|
for plugin in &self.plugins {
|
||||||
let (tx, rx) = tokio::sync::oneshot::channel();
|
let (tx, rx) = tokio::sync::oneshot::channel();
|
||||||
plugin.send_event(PluginBoundPluginEvent::CallEventHandler((ScriptEvent::OnPlayerAuthenticated, vec![arg.clone()], Some(tx)))).await;
|
plugin.send_event(PluginBoundPluginEvent::CallEventHandler((ScriptEvent::OnPlayerAuthenticated { name: name.clone() }, Some(tx)))).await;
|
||||||
// TODO: This never returns, because it blocks the entire process function
|
// TODO: This never returns, because it blocks the entire process function
|
||||||
// from running, so it never manages to run the function correctly.
|
// from running, so it never manages to run the function correctly.
|
||||||
// let res = rx.await.unwrap_or(Argument::Number(-1f32));
|
// let res = rx.await.unwrap_or(Argument::Number(-1f32));
|
||||||
@@ -447,8 +446,8 @@ impl Server {
|
|||||||
debug!("event: {:?}", event);
|
debug!("event: {:?}", event);
|
||||||
// TODO: Error handling (?)
|
// TODO: Error handling (?)
|
||||||
match event {
|
match event {
|
||||||
ServerBoundPluginEvent::PluginLoaded => plugin.send_event(PluginBoundPluginEvent::CallEventHandler((ScriptEvent::OnPluginLoaded, Vec::new(), None))).await,
|
ServerBoundPluginEvent::PluginLoaded => plugin.send_event(PluginBoundPluginEvent::CallEventHandler((ScriptEvent::OnPluginLoaded, None))).await,
|
||||||
ServerBoundPluginEvent::RequestPlayerCount(responder) => { let _ = responder.send(PluginBoundPluginEvent::PlayerCount(self.clients.len())); }
|
ServerBoundPluginEvent::RequestPlayerCount(responder) => { let _ = responder.send(PluginBoundPluginEvent::PlayerCount(self.clients.len() + self.clients_queue.len())); }
|
||||||
ServerBoundPluginEvent::RequestPlayers(responder) => {
|
ServerBoundPluginEvent::RequestPlayers(responder) => {
|
||||||
trace!("request players received");
|
trace!("request players received");
|
||||||
let mut players = HashMap::new();
|
let mut players = HashMap::new();
|
||||||
@@ -487,6 +486,7 @@ impl Server {
|
|||||||
self.process_lua_events().await?;
|
self.process_lua_events().await?;
|
||||||
|
|
||||||
// I'm sorry for this code :(
|
// I'm sorry for this code :(
|
||||||
|
// TODO: Clean this up. We should just grab the client once with `if let Some() = expr {}`
|
||||||
for i in 0..self.clients.len() {
|
for i in 0..self.clients.len() {
|
||||||
if self.clients.get(i).ok_or(ServerError::ClientDoesntExist)?.state == ClientState::Disconnect {
|
if self.clients.get(i).ok_or(ServerError::ClientDoesntExist)?.state == ClientState::Disconnect {
|
||||||
let id = self.clients.get(i).ok_or(ServerError::ClientDoesntExist)?.id;
|
let id = self.clients.get(i).ok_or(ServerError::ClientDoesntExist)?.id;
|
||||||
@@ -496,6 +496,12 @@ impl Server {
|
|||||||
self.broadcast(Packet::Raw(RawPacket::from_str(&delete_packet)), None)
|
self.broadcast(Packet::Raw(RawPacket::from_str(&delete_packet)), None)
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let id = self.clients[i].id;
|
||||||
|
for plugin in &mut self.plugins {
|
||||||
|
plugin.send_event(PluginBoundPluginEvent::CallEventHandler((ScriptEvent::OnPlayerDisconnect { pid: id }, None))).await;
|
||||||
|
}
|
||||||
|
|
||||||
info!("Disconnecting client {}...", id);
|
info!("Disconnecting client {}...", id);
|
||||||
self.clients.remove(i);
|
self.clients.remove(i);
|
||||||
info!("Client {} disconnected!", id);
|
info!("Client {} disconnected!", id);
|
||||||
|
|||||||
@@ -134,10 +134,11 @@ impl Backend for BackendLua {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call_event_handler(&mut self, event: ScriptEvent, args: Vec<Argument>, resp: Option<oneshot::Sender<Argument>>) {
|
fn call_event_handler(&mut self, event: ScriptEvent, resp: Option<oneshot::Sender<Argument>>) {
|
||||||
let event_name = match event {
|
let (event_name, args) = match event {
|
||||||
ScriptEvent::OnPluginLoaded => "onPluginLoaded",
|
ScriptEvent::OnPluginLoaded => ("onInit", vec![]),
|
||||||
ScriptEvent::OnPlayerAuthenticated => "onPlayerAuthenticated",
|
ScriptEvent::OnPlayerAuthenticated { name } => ("onPlayerAuth", vec![Argument::String(name)]),
|
||||||
|
ScriptEvent::OnPlayerDisconnect { pid } => ("onPlayerDisconnect", vec![Argument::Number(pid as f32)]),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut ret = -1f32;
|
let mut ret = -1f32;
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ pub trait Backend: Send {
|
|||||||
fn load(&mut self, code: String) -> anyhow::Result<()>;
|
fn load(&mut self, code: String) -> anyhow::Result<()>;
|
||||||
fn load_api(&mut self, tx: Arc<Sender<ServerBoundPluginEvent>>) -> anyhow::Result<()>;
|
fn load_api(&mut self, tx: Arc<Sender<ServerBoundPluginEvent>>) -> anyhow::Result<()>;
|
||||||
|
|
||||||
fn call_event_handler(&mut self, event: ScriptEvent, args: Vec<Argument>, resp: Option<oneshot::Sender<Argument>>);
|
fn call_event_handler(&mut self, event: ScriptEvent, resp: Option<oneshot::Sender<Argument>>);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: This is quite focused on Lua right now, perhaps in the future we want to modify this list
|
// TODO: This is quite focused on Lua right now, perhaps in the future we want to modify this list
|
||||||
@@ -28,12 +28,15 @@ pub enum Argument {
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum ScriptEvent {
|
pub enum ScriptEvent {
|
||||||
OnPluginLoaded,
|
OnPluginLoaded,
|
||||||
OnPlayerAuthenticated,
|
|
||||||
|
OnPlayerAuthenticated { name: String },
|
||||||
|
|
||||||
|
OnPlayerDisconnect { pid: u8 },
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum PluginBoundPluginEvent {
|
pub enum PluginBoundPluginEvent {
|
||||||
CallEventHandler((ScriptEvent, Vec<Argument>, Option<oneshot::Sender<Argument>>)),
|
CallEventHandler((ScriptEvent, Option<oneshot::Sender<Argument>>)),
|
||||||
|
|
||||||
PlayerCount(usize),
|
PlayerCount(usize),
|
||||||
Players(HashMap<u8, String>),
|
Players(HashMap<u8, String>),
|
||||||
@@ -72,8 +75,8 @@ impl Plugin {
|
|||||||
loop {
|
loop {
|
||||||
if let Some(message) = pb_rx.blocking_recv() {
|
if let Some(message) = pb_rx.blocking_recv() {
|
||||||
match message {
|
match message {
|
||||||
PluginBoundPluginEvent::CallEventHandler((event, args, resp)) => {
|
PluginBoundPluginEvent::CallEventHandler((event, resp)) => {
|
||||||
backend.call_event_handler(event, args, resp);
|
backend.call_event_handler(event, resp);
|
||||||
},
|
},
|
||||||
_ => {},
|
_ => {},
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user