Carpet

Carpet

2M Downloads

Player connect and disconnect event

altrisi opened this issue ยท 6 comments

commented

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.

commented

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?

commented

__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.

commented

actually, this totally already works. I have almost got the app working

commented

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));
commented

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

commented

Tried and tested, even works with multiple players leaving and joining in the same tick. You can quite clearly see why. Gnembon, do you think over time a library could be made with functions such as these, or is is not too useful?