Skip to content

Commit

Permalink
improvement: properly register and remove hooks and listeners when re…
Browse files Browse the repository at this point in the history
…quired to save server resources when unused
  • Loading branch information
Kalle committed Jan 7, 2025
1 parent 0f6b2fb commit 320e006
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
11 changes: 6 additions & 5 deletions src/RollTheDice+DiceBigTaserBattery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ public partial class RollTheDice : BasePlugin

private Dictionary<string, string> DiceBigTaserBattery(CCSPlayerController player, CCSPlayerPawn playerPawn)
{
// create listener if not exists
if (_playersWithBigTaserBattery.Count() == 0)
{
RegisterEventHandler<EventWeaponFire>(EventDiceBigTaserBatteryOnWeaponFire);
}
int battery = _random.Next(2, 10);
_playersWithBigTaserBattery.Add(player, battery);
player.GiveNamedItem("weapon_taser");
Expand All @@ -20,18 +25,14 @@ private Dictionary<string, string> DiceBigTaserBattery(CCSPlayerController playe
};
}

private void DiceBigTaserBatteryLoad()
{
RegisterEventHandler<EventWeaponFire>(EventDiceBigTaserBatteryOnWeaponFire);
}

private void DiceBigTaserBatteryUnload()
{
DiceBigTaserBatteryReset();
}

private void DiceBigTaserBatteryReset()
{
DeregisterEventHandler<EventWeaponFire>(EventDiceBigTaserBatteryOnWeaponFire);
_playersWithBigTaserBattery.Clear();
}

Expand Down
14 changes: 8 additions & 6 deletions src/RollTheDice+DiceFastBombAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ public partial class RollTheDice : BasePlugin

private Dictionary<string, string> DiceFastBombAction(CCSPlayerController player, CCSPlayerPawn playerPawn)
{
// create listener if not exists
if (_playersCanInstantDefuse.Count() == 0 && _playersCanInstantPlant.Count() == 0)
{
RegisterEventHandler<EventBombBegindefuse>(DiceFastBombActionEventBeginDefuse);
RegisterEventHandler<EventBombBeginplant>(DiceFastBombActionEventBeginPlant);
}
if (playerPawn.TeamNum == (int)CsTeam.Terrorist)
{
_playersCanInstantPlant.Add(player);
Expand Down Expand Up @@ -41,19 +47,15 @@ private Dictionary<string, string> DiceFastBombAction(CCSPlayerController player
}
}

private void DiceFastBombActionLoad()
{
RegisterEventHandler<EventBombBegindefuse>(DiceFastBombActionEventBeginDefuse);
RegisterEventHandler<EventBombBeginplant>(DiceFastBombActionEventBeginPlant);
}

private void DiceFastBombActionUnload()
{
DiceFastBombActionReset();
}

private void DiceFastBombActionReset()
{
DeregisterEventHandler<EventBombBegindefuse>(DiceFastBombActionEventBeginDefuse);
DeregisterEventHandler<EventBombBeginplant>(DiceFastBombActionEventBeginPlant);
_playersCanInstantDefuse.Clear();
_playersCanInstantPlant.Clear();
}
Expand Down
11 changes: 6 additions & 5 deletions src/RollTheDice+DiceIncreaseSpeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public partial class RollTheDice : BasePlugin

private Dictionary<string, string> DiceIncreaseSpeed(CCSPlayerController player, CCSPlayerPawn playerPawn)
{
// create listener if not exists
if (_playersWithIncreasedSpeed.Count() == 0)
{
RegisterEventHandler<EventPlayerHurt>(EventDiceIncreaseSpeedOnPlayerHurt);
}
_playersWithIncreasedSpeed.Add(player);
var speedIncrease = _random.NextDouble() * (2.0 - 1.5) + 1.5;
playerPawn.VelocityModifier *= (float)speedIncrease;
Expand All @@ -25,18 +30,14 @@ private Dictionary<string, string> DiceIncreaseSpeed(CCSPlayerController player,
};
}

private void DiceIncreaseSpeedLoad()
{
RegisterEventHandler<EventPlayerHurt>(EventDiceIncreaseSpeedOnPlayerHurt);
}

private void DiceIncreaseSpeedUnload()
{
DiceIncreaseSpeedReset();
}

private void DiceIncreaseSpeedReset()
{
DeregisterEventHandler<EventPlayerHurt>(EventDiceIncreaseSpeedOnPlayerHurt);
// iterate through all players
foreach (var player in _playersWithIncreasedSpeed)
{
Expand Down

0 comments on commit 320e006

Please sign in to comment.