Skip to content

Commit 4e30f13

Browse files
committed
Fixed issues with BaseEntity and PlayerEntity along with fixing inheritance of PlayerEntity
1 parent 390884e commit 4e30f13

File tree

2 files changed

+40
-31
lines changed

2 files changed

+40
-31
lines changed

addons/source-python/packages/source-python/entities/entity.py

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@
2323
class BaseEntity(object):
2424
'''Class used to interact directly with entities'''
2525

26-
index = 0
27-
edict = 0
28-
_entities = None
29-
3026
def __new__(cls, index, *entities):
3127
'''Override the __new__ class method to verify the given index
3228
is of the correct entity type and add the index attribute'''
@@ -45,8 +41,8 @@ def __new__(cls, index, *entities):
4541
self = object.__new__(cls)
4642

4743
# Set the entity's base attributes
48-
self.index = index
49-
self.edict = edict
44+
self._index = index
45+
self._edict = edict
5046
self._entities = frozenset(list(entities) + ['entity'])
5147

5248
# Return the instance
@@ -64,13 +60,6 @@ def __getattr__(self, attr):
6460
# Return the instance's value for the given attribute
6561
return getattr(instance, attr)
6662

67-
# Is this an inherited class and does
68-
# the inheriting class have the property?
69-
if self.__class__ != BaseEntity and hasattr(self.__class__, attr):
70-
71-
# Get the attribute
72-
return getattr(self.__class__, attr).fget(self)
73-
7463
# Is the attribute a property of this entity?
7564
if attr in self.properties:
7665

@@ -162,23 +151,25 @@ def _get_function(self, item):
162151
def __setattr__(self, attr, value):
163152
'''Finds if the attribute is value and sets its value'''
164153

165-
# Does the class have the given attribute?
166-
if hasattr(self.__class__, attr):
154+
# Is the given attribute private?
155+
if attr.startswith('_'):
167156

168-
# Is this an inherited class?
169-
if self.__class__ != BaseEntity:
157+
# Get the name of the private attribute
158+
name = attr[1:]
170159

171-
# Get the attribute and set its value
172-
getattr(self.__class__, attr).fset(self, value)
160+
# Is the attribute a property?
161+
if (name in dir(self) and isinstance(
162+
getattr(self.__class__, name), property)):
163+
164+
# Set the private attribute's value
165+
super(BaseEntity, self).__setattr__(attr, value)
173166

174167
# No need to go further
175168
return
176169

177-
# Set the attribute
178-
object.__setattr__(self, attr, value)
179-
180-
# No need to go further
181-
return
170+
# If not a property, do not allow the private attribute
171+
raise ValueError(
172+
'Invalid private attribute "{0}" given.'.format(attr))
182173

183174
# Loop through all instances
184175
# (used to set using edict/IPlayerInfo attributes)
@@ -327,6 +318,21 @@ def set_color(self, args):
327318
# Set the "color" property for BaseEntity
328319
color = property(get_color, set_color)
329320

321+
@property
322+
def index(self):
323+
'''Returns the entity's index'''
324+
return self._index
325+
326+
@property
327+
def edict(self):
328+
'''Returns the entity's edict instance'''
329+
return self._edict
330+
331+
@property
332+
def entities(self):
333+
'''Returns the set of entity names to use for the instance'''
334+
return self._entities
335+
330336
@property
331337
def instances(self):
332338
'''Yields the entity's edict instance'''
@@ -356,19 +362,19 @@ def pointer(self):
356362
@property
357363
def properties(self):
358364
'''Returns all properties for all entities'''
359-
return Properties.get_entity_properties(self._entities)
365+
return Properties.get_entity_properties(self.entities)
360366

361367
@property
362368
def keyvalues(self):
363369
'''Returns all keyvalues for all entities'''
364-
return KeyValues.get_entity_keyvalues(self._entities)
370+
return KeyValues.get_entity_keyvalues(self.entities)
365371

366372
@property
367373
def offsets(self):
368374
'''Returns all offsets for all entities'''
369-
return Offsets.get_entity_offsets(self._entities)
375+
return Offsets.get_entity_offsets(self.entities)
370376

371377
@property
372378
def functions(self):
373379
'''Returns all dynamic calling functions for all entities'''
374-
return Functions.get_entity_functions(self._entities)
380+
return Functions.get_entity_functions(self.entities)

addons/source-python/packages/source-python/players/entity.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@
2222
class PlayerEntity(BaseEntity, _PlayerWeapons):
2323
'''Class used to interact directly with players'''
2424

25-
info = None
26-
2725
def __init__(self, index):
2826
'''Override the __init__ method to set the
2927
"entities" attribute and set the PlayerInfo'''
3028

3129
# Set the player's info attribute
32-
self.info = CPlayerInfo(self.edict)
30+
self._info = CPlayerInfo(self.edict)
3331

3432
# Is the IPlayerInfo instance valid?
3533
if self.info is None:
@@ -40,6 +38,11 @@ def __init__(self, index):
4038
# Set the entities attribute
4139
self._entities = frozenset(['entity', 'player'])
4240

41+
@property
42+
def info(self):
43+
'''Returns the player's IPlayerInfo instance'''
44+
return self._info
45+
4346
@property
4447
def instances(self):
4548
'''Yields the player's IPlayerInfo and Edict instances'''

0 commit comments

Comments
 (0)