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 Minkner committed Jan 7, 2025
1 parent 346f11a commit 09cf7d8
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/RollTheDice+DiceNoExplosives.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ private Dictionary<string, string> DiceNoExplosives(CCSPlayerController player,

private void DiceNoExplosivesReset()
{
RemoveListener<Listeners.OnEntitySpawned>(DiceNoExplosivesOnEntitySpawned);
_playersWithoutExplosives.Clear();
}

private void DiceNoExplosivesUnload()
{
RemoveListener<Listeners.OnEntitySpawned>(DiceNoExplosivesOnEntitySpawned);
DiceNoExplosivesReset();
}

Expand Down
6 changes: 3 additions & 3 deletions src/RollTheDice+DicePlayerAsChicken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ private Dictionary<string, string> DicePlayerAsChicken(CCSPlayerController playe

private void DicePlayerAsChickenUnload()
{
// remove listeners
RemoveListener<Listeners.OnTick>(EventDicePlayerAsChickenOnTick);
RemoveListener<Listeners.CheckTransmit>(EventDicePlayerAsChickenCheckTransmit);
DicePlayerAsChickenReset();
}

private void DicePlayerAsChickenReset()
{
DeregisterEventHandler<EventPlayerDeath>(EventDicePlayerAsChickenOnPlayerDeath);
RemoveListener<Listeners.OnTick>(EventDicePlayerAsChickenOnTick);
RemoveListener<Listeners.CheckTransmit>(EventDicePlayerAsChickenCheckTransmit);
// iterate through all players
foreach (CCSPlayerController player in _playersAsChicken.Keys)
{
Expand Down
8 changes: 2 additions & 6 deletions src/RollTheDice+DicePlayerCloak.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ private Dictionary<string, string> DicePlayerCloak(CCSPlayerController player, C
{ "playerName", player.PlayerName }
};
// create listener if not exists
if (_playersWithCloak.Count() == 0)
{
RegisterListener<Listeners.OnTick>(EventDicePlayerCloakOnTick);
}
if (_playersWithCloak.Count() == 0) RegisterListener<Listeners.OnTick>(EventDicePlayerCloakOnTick);
// add player to list
_playersWithCloak.Add(player, 255);
return new Dictionary<string, string>
Expand All @@ -33,13 +30,12 @@ private Dictionary<string, string> DicePlayerCloak(CCSPlayerController player, C

private void DicePlayerCloakUnload()
{
// remove listeners
RemoveListener<Listeners.OnTick>(EventDicePlayerCloakOnTick);
DicePlayerCloakReset();
}

private void DicePlayerCloakReset()
{
RemoveListener<Listeners.OnTick>(EventDicePlayerCloakOnTick);
// iterate through all players
foreach (var (player, visibility) in _playersWithCloak)
{
Expand Down
15 changes: 8 additions & 7 deletions src/RollTheDice+DicePlayerDisguiseAsPlant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ private Dictionary<string, string> DicePlayerDisguiseAsPlant(CCSPlayerController
{ "playerName", player.PlayerName }
};
// create listener if not exists
if (_playersDisguisedAsPlants.Count == 0) RegisterListener<Listeners.OnTick>(EventDicePlayerDisguiseAsPlantOnTick);
if (_playersDisguisedAsPlants.Count == 0)
{
RegisterEventHandler<EventPlayerDeath>(EventDicePlayerDisguiseAsPlantOnPlayerDeath);
RegisterListener<Listeners.OnTick>(EventDicePlayerDisguiseAsPlantOnTick);
}
// add player to list
_playersDisguisedAsPlants.Add(player, new Dictionary<string, string>());
_playersDisguisedAsPlants[player]["status"] = "player";
Expand All @@ -49,19 +53,16 @@ private Dictionary<string, string> DicePlayerDisguiseAsPlant(CCSPlayerController
};
}

private void DicePlayerDisguiseAsPlantLoad()
{
RegisterEventHandler<EventPlayerDeath>(EventDicePlayerDisguiseAsPlantOnPlayerDeath);
}

private void DicePlayerDisguiseAsPlantUnload()
{
RemoveListener<Listeners.OnTick>(EventDicePlayerDisguiseAsPlantOnTick);
DicePlayerDisguiseAsPlantReset();
}

private void DicePlayerDisguiseAsPlantReset()
{
DeregisterEventHandler<EventPlayerDeath>(EventDicePlayerDisguiseAsPlantOnPlayerDeath);
RemoveListener<Listeners.OnTick>(EventDicePlayerDisguiseAsPlantOnTick);
// iterate through all players
foreach (CCSPlayerController player in _playersDisguisedAsPlants.Keys)
{
if (player == null || player.Pawn == null || player.Pawn.Value == null) continue;
Expand Down
2 changes: 1 addition & 1 deletion src/RollTheDice+DicePlayerMakeFakeGunSoundss.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ private Dictionary<string, string> DicePlayerMakeFakeGunSounds(CCSPlayerControll

private void DicePlayerMakeFakeGunSoundsUnload()
{
RemoveListener<Listeners.OnTick>(EventDicePlayerMakeFakeGunSoundsOnTick);
DicePlayerMakeFakeGunSoundsReset();
}

private void DicePlayerMakeFakeGunSoundsReset()
{
RemoveListener<Listeners.OnTick>(EventDicePlayerMakeFakeGunSoundsOnTick);
_playersWithFakeGunSounds.Clear();
}

Expand Down
2 changes: 1 addition & 1 deletion src/RollTheDice+DicePlayerMakeHostageSounds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ private Dictionary<string, string> DicePlayerMakeHostageSounds(CCSPlayerControll

private void DicePlayerMakeHostageSoundsUnload()
{
RemoveListener<Listeners.OnTick>(EventDicePlayerMakeHostageSoundsOnTick);
DicePlayerMakeHostageSoundsReset();
}

private void DicePlayerMakeHostageSoundsReset()
{
RemoveListener<Listeners.OnTick>(EventDicePlayerMakeHostageSoundsOnTick);
_playersWithHostageSounds.Clear();
}

Expand Down
17 changes: 9 additions & 8 deletions src/RollTheDice+DicePlayerRespawn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ public partial class RollTheDice : BasePlugin
private Dictionary<string, string> DicePlayerRespawn(CCSPlayerController player, CCSPlayerPawn playerPawn)
{
// create listener if not exists
if (_playersWithRespawnAbility.Count() == 0) RegisterListener<Listeners.OnTick>(EventDicePlayerRespawnOnTick);
if (_playersWithRespawnAbility.Count() == 0)
{
RegisterEventHandler<EventPlayerDeath>(EventDicePlayerRespawnOnPlayerDeath);
RegisterEventHandler<EventPlayerTeam>(EventDicePlayerRespawnOnPlayerTeam);
RegisterListener<Listeners.OnTick>(EventDicePlayerRespawnOnTick);
}
// add player to list
_playersWithRespawnAbility.Add(player, new Dictionary<string, string>());
return new Dictionary<string, string>
Expand All @@ -22,20 +27,16 @@ private Dictionary<string, string> DicePlayerRespawn(CCSPlayerController player,
};
}

private void DicePlayerRespawnLoad()
{
RegisterEventHandler<EventPlayerDeath>(EventDicePlayerRespawnOnPlayerDeath);
RegisterEventHandler<EventPlayerTeam>(EventDicePlayerRespawnOnPlayerTeam);
}

private void DicePlayerRespawnUnload()
{
RemoveListener<Listeners.OnTick>(EventDicePlayerRespawnOnTick);
DicePlayerRespawnReset();
}

private void DicePlayerRespawnReset()
{
DeregisterEventHandler<EventPlayerDeath>(EventDicePlayerRespawnOnPlayerDeath);
DeregisterEventHandler<EventPlayerTeam>(EventDicePlayerRespawnOnPlayerTeam);
RemoveListener<Listeners.OnTick>(EventDicePlayerRespawnOnTick);
_playersWithRespawnAbility.Clear();
}

Expand Down
8 changes: 3 additions & 5 deletions src/RollTheDice+DicePlayerVampire.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public partial class RollTheDice : BasePlugin

private Dictionary<string, string> DicePlayerVampire(CCSPlayerController player, CCSPlayerPawn playerPawn)
{
// create listener if not exists
if (_playerVampires.Count() == 0) RegisterEventHandler<EventPlayerHurt>(EventDicePlayerVampireOnPlayerHurt);
_playerVampires.Add(player);
return new Dictionary<string, string>
{
Expand All @@ -18,18 +20,14 @@ private Dictionary<string, string> DicePlayerVampire(CCSPlayerController player,
};
}

private void DicePlayerVampireLoad()
{
RegisterEventHandler<EventPlayerHurt>(EventDicePlayerVampireOnPlayerHurt);
}

private void DicePlayerVampireUnload()
{
DicePlayerVampireReset();
}

private void DicePlayerVampireReset()
{
DeregisterEventHandler<EventPlayerHurt>(EventDicePlayerVampireOnPlayerHurt);
_playerVampires.Clear();
}

Expand Down

0 comments on commit 09cf7d8

Please sign in to comment.