Skip to content

Commit

Permalink
fix: remove listener only on unloading
Browse files Browse the repository at this point in the history
  • Loading branch information
derkalle4 committed Jan 5, 2025
1 parent dc9a40b commit fa1bf22
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 54 deletions.
6 changes: 1 addition & 5 deletions src/RollTheDice+DiceNoExplosives.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ private void DiceNoExplosivesUnload()
private void DiceNoExplosivesOnEntitySpawned(CEntityInstance entity)
{
// remove listener if no players to save resources
if (_playersWithoutExplosives.Count == 0)
{
RemoveListener<Listeners.OnEntitySpawned>(DiceNoExplosivesOnEntitySpawned);
return;
}
if (_playersWithoutExplosives.Count == 0) return;
// handle smoke grenades
if (entity.DesignerName == "smokegrenade_projectile") DiceNoExplosivesHandleSmokeGrenade(entity.Handle);
// handle HE grenades
Expand Down
18 changes: 5 additions & 13 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()
{
// remove listeners
RemoveListener<Listeners.OnTick>(EventDicePlayerAsChickenOnTick);
RemoveListener<Listeners.CheckTransmit>(EventDicePlayerAsChickenCheckTransmit);
// iterate through all players
foreach (CCSPlayerController player in _playersAsChicken.Keys)
{
Expand All @@ -65,11 +65,7 @@ private void DicePlayerAsChickenReset()
private void EventDicePlayerAsChickenOnTick()
{
// remove listener if no players to save resources
if (_playersAsChicken.Count() == 0)
{
RemoveListener<Listeners.OnTick>(EventDicePlayerAsChickenOnTick);
return;
}
if (_playersAsChicken.Count() == 0) return;
// worker
Dictionary<CCSPlayerController, Dictionary<string, string>> _playersAsChickenCopy = new(_playersAsChicken);
foreach (var (player, playerData) in _playersAsChickenCopy)
Expand Down Expand Up @@ -109,11 +105,7 @@ private void EventDicePlayerAsChickenOnTick()
private void EventDicePlayerAsChickenCheckTransmit(CCheckTransmitInfoList infoList)
{
// remove listener if no players to save resources
if (_playersAsChicken.Count() == 0)
{
RemoveListener<Listeners.CheckTransmit>(EventDicePlayerAsChickenCheckTransmit);
return;
}
if (_playersAsChicken.Count() == 0) return;
// worker
foreach ((CCheckTransmitInfo info, CCSPlayerController? player) in infoList)
{
Expand Down
11 changes: 3 additions & 8 deletions src/RollTheDice+DicePlayerCloak.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ private Dictionary<string, string> DicePlayerCloak(CCSPlayerController player, C

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

private void DicePlayerCloakReset()
{
// remove listeners
RemoveListener<Listeners.OnTick>(EventDicePlayerCloakOnTick);
// iterate through all players
foreach (var (player, visibility) in _playersWithCloak)
{
Expand All @@ -56,12 +56,7 @@ private void DicePlayerCloakReset()

private void EventDicePlayerCloakOnTick()
{
// remove listener if no players to save resources
if (_playersWithCloak.Count() == 0)
{
RemoveListener<Listeners.OnTick>(EventDicePlayerCloakOnTick);
return;
}
if (_playersWithCloak.Count() == 0) return;
// worker
Dictionary<CCSPlayerController, int> _playersWithCloakCopy = new(_playersWithCloak);
foreach (var (player, visibility) in _playersWithCloakCopy)
Expand Down
9 changes: 2 additions & 7 deletions src/RollTheDice+DicePlayerDisguiseAsPlant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ private void DicePlayerDisguiseAsPlantLoad()

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

private void DicePlayerDisguiseAsPlantReset()
{
RemoveListener<Listeners.OnTick>(EventDicePlayerDisguiseAsPlantOnTick);
foreach (CCSPlayerController player in _playersDisguisedAsPlants.Keys)
{
if (player == null || player.Pawn == null || player.Pawn.Value == null) continue;
Expand All @@ -73,12 +73,7 @@ private void DicePlayerDisguiseAsPlantReset()

private void EventDicePlayerDisguiseAsPlantOnTick()
{
// remove listener if no players to save resources
if (_playersDisguisedAsPlants.Count == 0)
{
RemoveListener<Listeners.OnTick>(EventDicePlayerDisguiseAsPlantOnTick);
return;
}
if (_playersDisguisedAsPlants.Count == 0) return;
// worker
Dictionary<CCSPlayerController, Dictionary<string, string>> _playersDisguisedAsPlantsCopy = new(_playersDisguisedAsPlants);
foreach (var (player, playerData) in _playersDisguisedAsPlantsCopy)
Expand Down
9 changes: 2 additions & 7 deletions src/RollTheDice+DicePlayerMakeFakeGunSoundss.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,18 @@ private Dictionary<string, string> DicePlayerMakeFakeGunSounds(CCSPlayerControll

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

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

private void EventDicePlayerMakeFakeGunSoundsOnTick()
{
// remove listener if no players to save resources
if (_playersWithFakeGunSounds.Count() == 0)
{
DicePlayerMakeFakeGunSoundsReset();
return;
}
if (_playersWithFakeGunSounds.Count() == 0) return;
// worker
Dictionary<CCSPlayerController, int> _playersWithFakeGunSoundsCopy = new(_playersWithFakeGunSounds);
foreach (var (player, last_sound) in _playersWithFakeGunSoundsCopy)
Expand Down
9 changes: 2 additions & 7 deletions src/RollTheDice+DicePlayerMakeHostageSounds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,18 @@ private Dictionary<string, string> DicePlayerMakeHostageSounds(CCSPlayerControll

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

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

private void EventDicePlayerMakeHostageSoundsOnTick()
{
// remove listener if no players to save resources
if (_playersWithHostageSounds.Count() == 0)
{
DicePlayerMakeHostageSoundsReset();
return;
}
if (_playersWithHostageSounds.Count() == 0) return;
// worker
Dictionary<CCSPlayerController, int> _playersWithHostageSoundsCopy = new(_playersWithHostageSounds);
foreach (var (player, playerStatus) in _playersWithHostageSoundsCopy)
Expand Down
9 changes: 2 additions & 7 deletions src/RollTheDice+DicePlayerRespawn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,18 @@ private void DicePlayerRespawnLoad()

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

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

private void EventDicePlayerRespawnOnTick()
{
// remove listener if no players to save resources
if (_playersWithRespawnAbility.Count() == 0)
{
DicePlayerRespawnReset();
return;
}
if (_playersWithRespawnAbility.Count() == 0) return;
// worker
Dictionary<CCSPlayerController, Dictionary<string, string>> _playersWithRespawnAbilityCopy = new(_playersWithRespawnAbility);
foreach (var (player, playerData) in _playersWithRespawnAbilityCopy)
Expand Down

0 comments on commit fa1bf22

Please sign in to comment.