Skip to content

Commit 90258c6

Browse files
committed
ADVPlayer.IsValid() was never finished apparently so, I expanded it a bit and increased it's usage.
1 parent 4d79f16 commit 90258c6

File tree

8 files changed

+88
-68
lines changed

8 files changed

+88
-68
lines changed

PyPlugins/tests/TestPlayerDeath.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -130,26 +130,29 @@ def _testDeath_new(self, event):
130130
alog(str(k) + " | " + str(v))
131131

132132
victim = Source2Py.ADVPlayer(ev['userid'])
133-
attacker = Source2Py.ADVPlayer(ev['attacker'])
134-
assister = Source2Py.ADVPlayer(ev['assister'])
135133

136-
killstring = victim.GetName() + " was killed by "
137-
138-
if (attacker.IsValid()):
139-
killstring = killstring + attacker.GetName()
140-
heal = 15
141-
current_hp = attacker.GetHealth()
142-
alog(str(attacker.GetName() + " healed by " + str(heal) + " from " + str(current_hp) + " to " + str(current_hp+heal)))
143-
attacker.AddHealth(15)
144-
145-
if (assister.IsValid()):
146-
killstring = killstring + ", and " + assister.GetName()
147-
heal = 5
148-
current_hp = assister.GetHealth()
149-
alog(str(assister.GetName() + " healed by " + str(heal) + " from " + str(current_hp) + " to " + str(current_hp+heal)))
150-
assister.AddHealth(5)
151-
152-
alog(killstring)
134+
if(victim.IsValid()):
135+
killstring = victim.GetName() + " was killed by "
136+
137+
attacker = Source2Py.ADVPlayer(ev['attacker'])
138+
if (attacker.IsValid()):
139+
killstring = killstring + attacker.GetName()
140+
heal = 15
141+
current_hp = attacker.GetHealth()
142+
alog(str(attacker.GetName() + " healed by " + str(heal) + " from " + str(current_hp) + " to " + str(current_hp+heal)))
143+
attacker.AddHealth(15)
144+
145+
assister = Source2Py.ADVPlayer(ev['assister'])
146+
if (assister.IsValid()):
147+
name = assister.GetName()
148+
if (name):
149+
killstring = killstring + ", and " + assister.GetName()
150+
heal = 5
151+
current_hp = assister.GetHealth()
152+
alog(str(assister.GetName() + " healed by " + str(heal) + " from " + str(current_hp) + " to " + str(current_hp+heal)))
153+
assister.AddHealth(5)
154+
155+
alog(killstring)
153156
pass
154157
def OnPlayerDeath(self, event):
155158
alog("START")

PyPlugins/tests/TestPlayerHurt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ def _testADVAPI(self,
111111
attacker = Source2Py.ADVPlayer(event.GetPlayerSlot(geks))
112112
if(attacker.IsValid()):
113113
alog("attacker is valid")
114+
alog("test self.GetHealth() on attacker: " + str(attacker.test()))
114115
else:
115116
alog("attacker is invalid")
116117

117-
alog("test self.GetHealth() on attacker: " + str(attacker.test()))
118118
alog("_testADVAPI END")
119119
pass
120120
def OnPlayerHurt(self,

PyPlugins/tests/TestPlayerInputs.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def OnGameFrame(self,
2525
bFirstTick: bool,
2626
bLastTick: bool
2727
):
28-
28+
#we will be only reporting button presses on frames where the state changed.
2929
pass
3030
def OnPlayer______(self,
3131
_slot: int
@@ -34,7 +34,12 @@ def OnPlayer______(self,
3434
try:
3535
alog("_slot: " + str(_slot))
3636
player = ADVPlayer(_slot)
37-
alog(player.GetName() + " is airborn!")
37+
if (player.IsValid()):
38+
name = player.GetName()
39+
if (name):
40+
alog(name + " is airborn!")
41+
else:
42+
alog("name was invalid")
3843
except Exception as e:
3944
alog(e)
4045
alog(traceback.format_exc())

PyPlugins/tests/TestPlayerJump.py

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,33 @@ def OnGameFrame(self,
3333
#note that just being in this list does not mean your actually in the air
3434
#just that airborn/ladder/grounded state is being tracked.
3535
player = ADVPlayer(_slot)
36-
grounded = player.IsOnGround()
37-
if not grounded:
38-
if player.IsOnLadder():
39-
grounded = 1 #ladders count as ground too.
40-
'''
41-
if player.inWater():
42-
figure out how to figure out.
43-
grounded = 1
44-
'''
36+
if (player.IsValid()):
37+
grounded = player.IsOnGround()
38+
if not grounded:
39+
if player.IsOnLadder():
40+
grounded = 1 #ladders count as ground too.
41+
'''
42+
if player.inWater():
43+
figure out how to figure out.
44+
grounded = 1
45+
'''
4546

46-
if self.last_grounded.get(_slot, 1) != grounded:
47-
#if the last grounded state does not match what you are now, like
48-
#if you fall off a box, ladder, or roof... or jump :)
49-
self.last_grounded[_slot] = grounded
50-
if grounded:
51-
ev = Source2Py.CreateFakeEvent("player_land", True)
52-
if (ev):
53-
geks = Source2Py.GameEventKeySymbol_t("userid")
54-
ev.SetInt(geks, _slot)
55-
Source2Py.FireFakeEvent(ev, True)
56-
else:
57-
ev = Source2Py.CreateFakeEvent("player_airborn", True)
58-
if (ev):
59-
geks = Source2Py.GameEventKeySymbol_t("userid")
60-
ev.SetInt(geks, _slot)
61-
Source2Py.FireFakeEvent(ev, True)
47+
if self.last_grounded.get(_slot, 1) != grounded:
48+
#if the last grounded state does not match what you are now, like
49+
#if you fall off a box, ladder, or roof... or jump :)
50+
self.last_grounded[_slot] = grounded
51+
if grounded:
52+
ev = Source2Py.CreateFakeEvent("player_land", True)
53+
if (ev):
54+
geks = Source2Py.GameEventKeySymbol_t("userid")
55+
ev.SetInt(geks, _slot)
56+
Source2Py.FireFakeEvent(ev, True)
57+
else:
58+
ev = Source2Py.CreateFakeEvent("player_airborn", True)
59+
if (ev):
60+
geks = Source2Py.GameEventKeySymbol_t("userid")
61+
ev.SetInt(geks, _slot)
62+
Source2Py.FireFakeEvent(ev, True)
6263

6364
pass
6465
def OnPlayerJump(self,
@@ -81,11 +82,12 @@ def OnPlayerJump(self,
8182
try:
8283
self.players_in_air[_slot] = True
8384
player = ADVPlayer(_slot)
84-
name = player.GetName()
85-
if (name):
86-
alog(name + " jumped!")
87-
else:
88-
alog("player.GetName() returned nullptr (jumped!)")
85+
if (player.IsValid()):
86+
name = player.GetName()
87+
if (name):
88+
alog(name + " jumped!")
89+
else:
90+
alog("player.GetName() returned nullptr (jumped!)")
8991
except Exception as e:
9092
alog(e)
9193
alog(traceback.format_exc())
@@ -121,12 +123,12 @@ def OnPlayerLand(self,
121123
try:
122124
alog("_slot: " + str(_slot))
123125
player = ADVPlayer(_slot)
124-
name = player.GetName()
125-
if (name):
126-
alog(name + " landed!")
127-
else:
128-
alog("player.GetName() returned nullptr (landed!)")
129-
126+
if (player.IsValid()):
127+
name = player.GetName()
128+
if (name):
129+
alog(name + " landed!")
130+
else:
131+
alog("player.GetName() returned nullptr (landed!)")
130132
except Exception as e:
131133
alog(e)
132134
alog(traceback.format_exc())
@@ -142,11 +144,12 @@ def OnPlayerAirborn(self,
142144
try:
143145
alog("_slot: " + str(_slot))
144146
player = ADVPlayer(_slot)
145-
name = player.GetName()
146-
if (name):
147-
alog(name + " is airborn!")
148-
else:
149-
alog("player.GetName() returned nullptr (is airborn!)")
147+
if (player.IsValid()):
148+
name = player.GetName()
149+
if (name):
150+
alog(name + " is airborn!")
151+
else:
152+
alog("player.GetName() returned nullptr (is airborn!)")
150153
except Exception as e:
151154
alog(e)
152155
alog(traceback.format_exc())

PyPlugins/tests/TestPlayerSpawn.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ def OnPlayerSpawn_post(self,
3636
_slot: int
3737
):
3838
alog("START")
39-
bonus_hp = randint(10, 50)
4039
player = ADVPlayer(_slot)
41-
player.AddHealth(bonus_hp)
40+
if (player.IsValid()):
41+
bonus_hp = randint(10, 50)
42+
player.AddHealth(bonus_hp)
4243
alog("END")
4344
pass

src/PyModule.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ PYBIND11_EMBEDDED_MODULE(Source2Py, m) {
134134
.def("GetIndex", &ADVAPI::GetIndex)
135135
.def("IsOnGround", &ADVAPI::IsOnGround)
136136
.def("IsOnLadder", &ADVAPI::IsOnLadder)
137-
.def("GetButtons", &ADVAPI::GetButtons)
137+
.def("GetButtons", &ADVAPI::GetButtonStates)
138138
.def("test",
139139
[](ADVAPI& self) -> int
140140
//Tristen, don't delete this, you'll eventually forget again.

src/adventuremod.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ bool ADVAPI::IsValid()
3232
if (this->GetIndex() < 0)
3333
return false;
3434

35+
CCSPlayerController* pc = this->GetPC();
36+
if (!pc)
37+
return false;
38+
39+
CBaseEntity* pawn = pc->GetPawn();
40+
if (!pawn)
41+
return false;
42+
3543
return true;
3644
}
3745

@@ -51,7 +59,7 @@ bool ADVAPI::IsOnLadder()
5159
return false;
5260
}
5361

54-
uint64* ADVAPI::GetButtons()
62+
uint64* ADVAPI::GetButtonStates()
5563
{
5664
CBaseEntity* pawn = (CBaseEntity*)GetPawn();
5765
CCSPlayerPawnBase* base = (CCSPlayerPawnBase*)pawn;

src/adventuremod.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ADVAPI
3535
bool IsValid();
3636
bool IsOnGround();
3737
bool IsOnLadder();
38-
uint64* GetButtons();
38+
uint64* GetButtonStates();
3939

4040

4141

0 commit comments

Comments
 (0)