OnClientConnect getting Player problem

All other Source.Python topics and issues.
nullable
Senior Member
Posts: 137
Joined: Sat Nov 08, 2014 7:22 pm

OnClientConnect getting Player problem

Postby nullable » Thu Dec 01, 2016 10:32 pm

Hi,

After update new version of sourcepython, we have a problem with getting player.

Syntax: Select all

@OnClientConnect
def on_client_connect(allow_connect_ptr, player_edict, player_name, ip_port, reject_msg_ptr, max_reject_length):
player = Player(index_from_edict(player_edict))



Traceback:

Syntax: Select all

Traceback (most recent call last):
File "../addons/source-python/plugins/automation/automation.py", line 246, in on_client_connect
player = Player(index_from_edict(edict))
File "../addons/source-python/packages/source-python/players/_base.py", line 86, in __init__
super().__init__(index)
File "../addons/source-python/packages/source-python/entities/entity.py", line 108, in __init__
super().__init__(index)

ValueError: Conversion from "Index" (2) to "BaseEntity" failed.


Please help us to fix this problem.
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: OnClientConnect getting Player problem

Postby L'In20Cible » Thu Dec 01, 2016 10:49 pm

OnClientConnect is called before the instances are created and linked so getting a Player instance from there is not possible. Use OnClientActive instead.
sl0
Junior Member
Posts: 4
Joined: Thu Dec 01, 2016 11:30 pm

Re: OnClientConnect getting Player problem

Postby sl0 » Thu Dec 01, 2016 11:39 pm

tnx, but it worked when second arg was `index`, before https://github.com/Source-Python-Dev-Te ... 63bf9987fd
OnClientActive is not what we need, as i understand we can't drop connection from OnClientActive
We using `@OnClientConnect` listener for checking is player.steamid is allowed for connect to the server, if not - dropping
something like:

Syntax: Select all

@OnClientConnect
def on_client_connect(allow_connect_ptr, edict, player_name, ip_port, reject_msg_ptr, max_reject_length):
player = Player(index_from_edict(edict)) # before update it was Player(index_from_userid(index))
if player.steamid not in users:
allow_connect_ptr.set_bool(False)
reject_msg_ptr.set_string_array('Connect was blocked')
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: OnClientConnect getting Player problem

Postby L'In20Cible » Fri Dec 02, 2016 2:50 am

sl0 wrote:tnx, but it worked when second arg was `index`, before https://github.com/Source-Python-Dev-Te ... 63bf9987fd

Well, it was simply because the listeners were not called if it was unable to get an index from the edict. As you can see there: https://github.com/Source-Python-Dev-Te ... n.cpp#L416

In short, it was not "working" but ignored the raised exceptions. You can do it yourself:

Syntax: Select all

@OnClientConnect
def on_client_connect(allow_connect_ptr, edict, player_name, ip_port, reject_msg_ptr, max_reject_length):
try:
index = index_from_edict(edict)
except ValueError:
return
player = Player(index) # before update it was Player(index_from_userid(index))
if player.steamid not in users:
allow_connect_ptr.set_bool(False)
reject_msg_ptr.set_string_array('Connect was blocked')


But it won't kick anyone as it will always exit the function. As you can see in the traceback you posted above, the exception is raised by the BaseEntity class initialization that cannot find the instance matching the given index (being not created at this point). Even tho you was able to get the Player instance, the steamid attribute would returns STEAM_ID_PENDING.

sl0 wrote:OnClientActive is not what we need, as i understand we can't drop connection from OnClientActive
We using `@OnClientConnect` listener for checking is player.steamid is allowed for connect to the server, if not - dropping
something like:

Syntax: Select all

@OnClientConnect
def on_client_connect(allow_connect_ptr, edict, player_name, ip_port, reject_msg_ptr, max_reject_length):
player = Player(index_from_edict(edict)) # before update it was Player(index_from_userid(index))
if player.steamid not in users:
allow_connect_ptr.set_bool(False)
reject_msg_ptr.set_string_array('Connect was blocked')
Ayuto posted a nice snippet couple months ago: viewtopic.php?p=7955#p7955
sl0
Junior Member
Posts: 4
Joined: Thu Dec 01, 2016 11:30 pm

Re: OnClientConnect getting Player problem

Postby sl0 » Fri Dec 02, 2016 9:41 am

tnx, your right
i forget that we have also @Event('player_connect') in our code for do same thing - kick user if it is not allowed

Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 32 guests