Player connect and disconnect event
altrisi opened this issue ยท 6 comments
I think it could be useful to have events to handle player connection and disconnection (e.g. to save/load data related to that player, or to edit the player entity before it gets unavailable, if possible?).
It could be called something like __on_player_connects(player)
and __on_player_disconnects(player)
.
It could work based on similar (same) functionality already available for Carpet extensions.
I tihnk it's already possible by checking for a particular player's pos or smth, and if it's null, it means that they are not in the server, right?
__on_player_connects(player)
could also be used to display a welcome or welcome back message when the player joins the server, give players a starting kit when they join, and/or to run offline progress on the player's finances or other stats, in addition to just handling save/load for the player.
Actually, this is so cool, imma add it to the scarpet repo
//gnembon/fabric-carpet #294
//By: Ghoulboy
//Global variables
global_players=l();
global_new_players=l();
global_left_players=l();
//Events
__on_tick()->(
players=entity_selector('@e[type=player]');
for(players,//Cos if not it will only check in that dimension
if(global_players~_==null,//Checks for new players. When it's loaded, will print all currently logged on players.
__on_player_joins(_);
global_players:length(global_players)=_;
put(global_new_players,null,_),
delete(global_new_players:(global_new_players~_))
)
);
for(global_players,//Cos if not it will only check in that dimension
if(players~_==null,//Checks for players left.
__on_player_leaves(_);
delete(global_players:(global_players~_));
put(global_left_players,null,_),
delete(global_left_players:(global_left_players~_))
)
);
);
__on_player_joins(player)->(print('Welcome, '+player));
__on_player_leaves(player)->(print('Good bye, '+player));
Ofc, you can also do other stuff besides printing the player's name with that function, you can even in another app do import('this_app','__on_player_joins')
and then do something, like I mentioned in #244