Skip to content

Commit

Permalink
Bring back connected player dict, add tick/spawn listeners to set alp…
Browse files Browse the repository at this point in the history
…ha, create alpha prop and set mthd
  • Loading branch information
razpbrry committed Mar 19, 2024
1 parent 9dc8fea commit 7757cd3
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 27 deletions.
16 changes: 7 additions & 9 deletions src/Commands/ColorCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,22 @@ public partial class ImperfectModels
public void ChangeModelAlphaCommand(CCSPlayerController? player, CommandInfo commandInfo)
{
/// This argument is the number for the alpha
var alphaPercentage = commandInfo.GetArg(1);
int alphaPercentageInt = 0;
var newModelAlpha = commandInfo.GetArg(1);
int newModelAlphaInt = 0;

var connectedPlayers = Utilities.GetPlayers();

var intParseSuccess = int.TryParse(alphaPercentage, out alphaPercentageInt);
var intParseSuccess = int.TryParse(newModelAlpha, out newModelAlphaInt);

if (intParseSuccess)
{
try
{
foreach (var connectedPlayer in connectedPlayers)
foreach (var connectedPlayer in ConnectedPlayers)
{
connectedPlayer.PlayerPawn.Value.Render = Color.FromArgb(alphaPercentageInt, 255, 255, 255);
Utilities.SetStateChanged(connectedPlayer.PlayerPawn.Value, "CBaseModelEntity", "m_clrRender");
ModelAlpha = newModelAlphaInt;
player.PlayerPawn.Value.Render = SetPlayerPawnColor(player, ModelAlpha);

Check warning on line 31 in src/Commands/ColorCommands.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 31 in src/Commands/ColorCommands.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 31 in src/Commands/ColorCommands.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 31 in src/Commands/ColorCommands.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 31 in src/Commands/ColorCommands.cs

View workflow job for this annotation

GitHub Actions / publish

Dereference of a possibly null reference.

Check warning on line 31 in src/Commands/ColorCommands.cs

View workflow job for this annotation

GitHub Actions / publish

Dereference of a possibly null reference.
}

commandInfo.ReplyToCommand($"All player models alpha set to {alphaPercentage}");
commandInfo.ReplyToCommand($"All player models alpha set to {newModelAlpha}");
}
catch (Exception ex)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Configuration/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public class Config : BasePluginConfig
[JsonPropertyName("ConfigVersion")]
public override int Version { get; set; } = 1;

public int DefaultAlpha { get; set; } = 254;
public int DefaultAlpha { get; set; } = 255;
}
}
99 changes: 88 additions & 11 deletions src/Events/PlayerEvents.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using System;
using System.Collections.Generic;
using CounterStrikeSharp.API.Core;
using Microsoft.Extensions.Logging;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImperfectModels
{
Expand Down Expand Up @@ -34,7 +29,42 @@ public void RegisterPlayerEvents()
return HookResult.Continue;
}
});

RegisterEventHandler<EventPlayerDisconnect>((@event, info) =>
{
var player = @event.Userid;

if (player.IsBot || !player.IsValid)
{
return HookResult.Continue;
}
else
{
OnPlayerDisconnect(player);
return HookResult.Continue;
}
});

RegisterEventHandler<EventPlayerSpawned>((@event, info) =>
{
if (@event.Userid == null) return HookResult.Continue;

var player = @event.Userid;

if (player.IsBot || !player.IsValid || player == null)
{
return HookResult.Continue;
}
else
{
player.PlayerPawn.Value.Render = SetPlayerPawnColor(player, ModelAlpha);

Check warning on line 60 in src/Events/PlayerEvents.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 60 in src/Events/PlayerEvents.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 60 in src/Events/PlayerEvents.cs

View workflow job for this annotation

GitHub Actions / publish

Dereference of a possibly null reference.
return HookResult.Continue;
}
});

RegisterListener<Listeners.OnTick>(PlayerOnTick);
}

private void OnPlayerConnect(CCSPlayerController? player, bool isForBot = false)
{
try
Expand All @@ -49,20 +79,67 @@ private void OnPlayerConnect(CCSPlayerController? player, bool isForBot = false)
return;
}

int playerSlot = player.Slot;

try
{
player.PlayerPawn.Value.Render = Color.FromArgb(Config.DefaultAlpha, 255, 255, 255);
Utilities.SetStateChanged(player.PlayerPawn.Value, "CBaseModelEntity", "m_clrRender");
ConnectedPlayers[playerSlot] = new CCSPlayerController(player.Handle);

player.PlayerPawn.Value.Render = SetPlayerPawnColor(player, ModelAlpha);

Check warning on line 88 in src/Events/PlayerEvents.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 88 in src/Events/PlayerEvents.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 88 in src/Events/PlayerEvents.cs

View workflow job for this annotation

GitHub Actions / publish

Dereference of a possibly null reference.
}
catch (Exception ex)
{
Console.WriteLine($"Something bad happened: {ex.Message}");
Logger.LogWarning($"Something bad happened: {ex.Message}");
}
finally
{
if (ConnectedPlayers[playerSlot] == null)
{
ConnectedPlayers.Remove(playerSlot);
}
}
}
catch (Exception ex)
{
Logger.LogWarning($"Something bad happened: {ex.Message}");
}
}

private void OnPlayerDisconnect(CCSPlayerController? player, bool isForBot = false)
{
if (player == null) return;

try
{
if (ConnectedPlayers.TryGetValue(player.Slot, out var connectedPlayer))
{
ConnectedPlayers.Remove(player.Slot);
}
}
catch (Exception ex)
{
Console.WriteLine($"Something bad happened: {ex.Message}");
Logger.LogWarning($"Something bad happened: {ex.Message}");
}
}

private void PlayerOnTick()
{
try
{
foreach (CCSPlayerController player in ConnectedPlayers.Values)
{
player.PlayerPawn.Value.Render = SetPlayerPawnColor(player, ModelAlpha);

Check warning on line 131 in src/Events/PlayerEvents.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 131 in src/Events/PlayerEvents.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 131 in src/Events/PlayerEvents.cs

View workflow job for this annotation

GitHub Actions / publish

Dereference of a possibly null reference.
}
}
catch (Exception ex)
{
Logger.LogWarning($"Something bad happened: {ex.Message}");
}
}

private Color SetPlayerPawnColor(CCSPlayerController player, int alpha)
{
return Color.FromArgb(alpha, 255, 255, 255);
}
}
}
12 changes: 6 additions & 6 deletions src/ImperfectModels.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Commands;
using ImperfectModels.Configuration;
using Microsoft.Extensions.Logging;
using System.Drawing;


namespace ImperfectModels;
Expand All @@ -20,6 +15,9 @@ public partial class ImperfectModels : BasePlugin, IPluginConfig<Config>
public override string ModuleDescription => "A plugin for handling player models.";

public Config Config { get; set; } = new Config();
public Dictionary<int, CCSPlayerController> ConnectedPlayers = new Dictionary<int, CCSPlayerController>();
public int ModelAlpha { get; set; }


public override void Load(bool hotReload)
{
Expand All @@ -39,6 +37,8 @@ public void OnConfigParsed(Config config)
Logger.LogWarning("The config version does not match current version: Expected: {0} | Current: {1}", Config.Version, config.Version);
}

ModelAlpha = Config.DefaultAlpha;

Config = config;
}
}

0 comments on commit 7757cd3

Please sign in to comment.