Skip to content

Commit

Permalink
Organize code
Browse files Browse the repository at this point in the history
  • Loading branch information
razpbrry committed Mar 18, 2024
1 parent 9714b56 commit 76c501d
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 133 deletions.
93 changes: 93 additions & 0 deletions src/Commands/ColorCommands.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImperfectModels
{
public partial class ImperfectModels
{
// Command for changing the player model alpha (transparency)
[ConsoleCommand("css_selfmodelalpha", "Changes the alpha of your player model")]
[CommandHelper(minArgs: 1, usage: "<number for alpha ex. 50>", whoCanExecute: CommandUsage.CLIENT_ONLY)]
[RequiresPermissions("@css/root")]
public void ChangeSelfModelAlphaCommand(CCSPlayerController? player, CommandInfo commandInfo)
{
/// This argument is the number for the alpha
var alphaPercentage = commandInfo.GetArg(1);
int alphaPercentageInt = 0;

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

if (intParseSuccess)
{
try
{
player.PlayerPawn.Value.Render = Color.FromArgb(alphaPercentageInt, 255, 255, 255);
Utilities.SetStateChanged(player.PlayerPawn.Value, "CBaseModelEntity", "m_clrRender");

commandInfo.ReplyToCommand($"Player model alpha set to {alphaPercentage}");
}
catch (Exception ex)
{
Logger.LogWarning("Something went wrong when setting the player model alpha {message}", ex.Message);

commandInfo.ReplyToCommand("Something went wrong when setting the player model alpha. Check error logs for more info.");
}
}
else
{
Logger.LogWarning("The number that was input was not correct.");
commandInfo.ReplyToCommand("The number that you input was not correct. Try a number between 1 and 255.");
}
}

// Command for changing all players model alpha (transparency)
[ConsoleCommand("css_modelalpha", "Changes the alpha of all player models")]
[CommandHelper(minArgs: 1, usage: "<number for alpha ex. 50>", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)]
[RequiresPermissions("@css/root")]
public void ChangeModelAlphaCommand(CCSPlayerController? player, CommandInfo commandInfo)
{
/// This argument is the number for the alpha
var alphaPercentage = commandInfo.GetArg(1);
int alphaPercentageInt = 0;

var connectedPlayers = Utilities.GetPlayers();

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

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

commandInfo.ReplyToCommand($"All player models alpha set to {alphaPercentage}");
}
catch (Exception ex)
{
Logger.LogWarning("Something went wrong when setting the player model alpha {message}", ex.Message);

commandInfo.ReplyToCommand("Something went wrong when setting the player model alpha. Check error logs for more info.");
}
}
else
{
Logger.LogWarning("The number that was input was not correct.");
commandInfo.ReplyToCommand("The number that you input was not correct. Try a number between 1 and 255.");
}
}
}
}
68 changes: 68 additions & 0 deletions src/Events/PlayerEvents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImperfectModels
{
public partial class ImperfectModels
{
public void RegisterPlayerEvents()
{
RegisterEventHandler<EventPlayerConnectFull>((@event, info) =>
{
if (@event.Userid.IsValid)
{
var player = @event.Userid;

if (!player.IsValid || player.IsBot)
{
return HookResult.Continue;
}
else
{
OnPlayerConnect(player);
return HookResult.Continue;
}
}
else
{
return HookResult.Continue;
}
});
}
private void OnPlayerConnect(CCSPlayerController? player, bool isForBot = false)
{
try
{
if (player == null)
{
return;
}

if (player.PlayerPawn == null)
{
return;
}

try
{
player.PlayerPawn.Value.Render = Color.FromArgb(Config.DefaultAlpha, 255, 255, 255);
Utilities.SetStateChanged(player.PlayerPawn.Value, "CBaseModelEntity", "m_clrRender");
}
catch (Exception ex)
{
Console.WriteLine($"Something bad happened: {ex.Message}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Something bad happened: {ex.Message}");
}
}
}
}
136 changes: 3 additions & 133 deletions src/ImperfectModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,18 @@
namespace ImperfectModels;

[MinimumApiVersion(80)]
public class ImperfectModels : BasePlugin, IPluginConfig<Config>
public partial class ImperfectModels : BasePlugin, IPluginConfig<Config>
{
public override string ModuleName => "Imperfect-Models";
public override string ModuleVersion => "1.0.0";
public override string ModuleAuthor => "Imperfect Gamers - raz";
public override string ModuleDescription => "A plugin for handling player models.";

public Config Config { get; set; } = new Config();

public override void Load(bool hotReload)
{
RegisterEventHandler<EventPlayerConnectFull>((@event, info) =>
{
if (@event.Userid.IsValid)
{
var player = @event.Userid;

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

public override void Unload(bool hotReload)
Expand All @@ -60,115 +41,4 @@ public void OnConfigParsed(Config config)

Config = config;
}

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

if (player.PlayerPawn == null)
{
return;
}

try
{
player.PlayerPawn.Value.Render = Color.FromArgb(Config.DefaultAlpha, 255, 255, 255);
Utilities.SetStateChanged(player.PlayerPawn.Value, "CBaseModelEntity", "m_clrRender");
}
catch (Exception ex)
{
Console.WriteLine($"Something bad happened: {ex.Message}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Something bad happened: {ex.Message}");
}
}

// Command for changing the player model alpha (transparency)
[ConsoleCommand("css_selfmodelalpha", "Changes the alpha of your player model")]
[CommandHelper(minArgs: 1, usage: "<number for alpha ex. 50>", whoCanExecute: CommandUsage.CLIENT_ONLY)]
[RequiresPermissions("@css/root")]
public void ChangeSelfModelAlphaCommand(CCSPlayerController? player, CommandInfo commandInfo)
{
/// This argument is 'css_modelalpha'
commandInfo.GetArg(0);

/// This argument is the number for the alpha
var alphaPercentage = commandInfo.GetArg(1);
int alphaPercentageInt = 0;

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

if (intParseSuccess)
{
try
{
player.PlayerPawn.Value.Render = Color.FromArgb(alphaPercentageInt, 255, 255, 255);
Utilities.SetStateChanged(player.PlayerPawn.Value, "CBaseModelEntity", "m_clrRender");

commandInfo.ReplyToCommand($"Player model alpha set to {alphaPercentage}");
}
catch (Exception ex)
{
Logger.LogWarning("Something went wrong when setting the player model alpha {message}", ex.Message);

commandInfo.ReplyToCommand("Something went wrong when setting the player model alpha. Check error logs for more info.");
}
}
else
{
Logger.LogWarning("The number that was input was not correct.");
commandInfo.ReplyToCommand("The number that you input was not correct. Try a number between 1 and 255.");
}
}

// Command for changing all players model alpha (transparency)
[ConsoleCommand("css_modelalpha", "Changes the alpha of all player models")]
[CommandHelper(minArgs: 1, usage: "<number for alpha ex. 50>", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)]
[RequiresPermissions("@css/root")]
public void ChangeModelAlphaCommand(CCSPlayerController? player, CommandInfo commandInfo)
{
/// This argument is 'css_modelalpha'
commandInfo.GetArg(0);

/// This argument is the number for the alpha
var alphaPercentage = commandInfo.GetArg(1);
int alphaPercentageInt = 0;

var connectedPlayers = Utilities.GetPlayers();

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

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

commandInfo.ReplyToCommand($"All player models alpha set to {alphaPercentage}");
}
catch (Exception ex)
{
Logger.LogWarning("Something went wrong when setting the player model alpha {message}", ex.Message);

commandInfo.ReplyToCommand("Something went wrong when setting the player model alpha. Check error logs for more info.");
}
}
else
{
Logger.LogWarning("The number that was input was not correct.");
commandInfo.ReplyToCommand("The number that you input was not correct. Try a number between 1 and 255.");
}
}
}

0 comments on commit 76c501d

Please sign in to comment.