-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
367 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace HealthBar_GoldKingZ.Config | ||
{ | ||
public static class Configs | ||
{ | ||
public static class Shared { | ||
public static string? CookiesModule { get; set; } | ||
} | ||
|
||
private static readonly string ConfigDirectoryName = "config"; | ||
private static readonly string ConfigFileName = "config.json"; | ||
private static string? _configFilePath; | ||
private static ConfigData? _configData; | ||
|
||
private static readonly JsonSerializerOptions SerializationOptions = new() | ||
{ | ||
Converters = | ||
{ | ||
new JsonStringEnumConverter() | ||
}, | ||
WriteIndented = true, | ||
AllowTrailingCommas = true, | ||
ReadCommentHandling = JsonCommentHandling.Skip, | ||
}; | ||
|
||
public static bool IsLoaded() | ||
{ | ||
return _configData is not null; | ||
} | ||
|
||
public static ConfigData GetConfigData() | ||
{ | ||
if (_configData is null) | ||
{ | ||
throw new Exception("Config not yet loaded."); | ||
} | ||
|
||
return _configData; | ||
} | ||
|
||
public static ConfigData Load(string modulePath) | ||
{ | ||
var configFileDirectory = Path.Combine(modulePath, ConfigDirectoryName); | ||
if(!Directory.Exists(configFileDirectory)) | ||
{ | ||
Directory.CreateDirectory(configFileDirectory); | ||
} | ||
|
||
_configFilePath = Path.Combine(configFileDirectory, ConfigFileName); | ||
if (File.Exists(_configFilePath)) | ||
{ | ||
_configData = JsonSerializer.Deserialize<ConfigData>(File.ReadAllText(_configFilePath), SerializationOptions); | ||
} | ||
else | ||
{ | ||
_configData = new ConfigData(); | ||
} | ||
|
||
if (_configData is null) | ||
{ | ||
throw new Exception("Failed to load configs."); | ||
} | ||
|
||
SaveConfigData(_configData); | ||
|
||
return _configData; | ||
} | ||
|
||
private static void SaveConfigData(ConfigData configData) | ||
{ | ||
if (_configFilePath is null) | ||
{ | ||
throw new Exception("Config not yet loaded."); | ||
} | ||
string json = JsonSerializer.Serialize(configData, SerializationOptions); | ||
|
||
|
||
json = System.Text.RegularExpressions.Regex.Unescape(json); | ||
File.WriteAllText(_configFilePath, json, System.Text.Encoding.UTF8); | ||
} | ||
|
||
public class ConfigData | ||
{ | ||
public bool DisableOnWarmUp { get; set; } | ||
public int DisplayHealthBarStyle { get; set; } | ||
public bool ShowHealthBarToAll { get; set; } | ||
|
||
public string empty { get; set; } | ||
public string Information_For_You_Dont_Delete_it { get; set; } | ||
|
||
public ConfigData() | ||
{ | ||
DisableOnWarmUp = false; | ||
DisplayHealthBarStyle = 0; | ||
ShowHealthBarToAll = false; | ||
empty = "----------------------------[ ↓ Info For All Configs Above ↓ ]----------------------------"; | ||
Information_For_You_Dont_Delete_it = " Vist [https://github.com/oqyh/cs2-HealthBar-GoldKingZ/tree/main?tab=readme-ov-file#-configuration-] To Understand All Above"; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
using CounterStrikeSharp.API.Core; | ||
using CounterStrikeSharp.API.Modules.Admin; | ||
using CounterStrikeSharp.API; | ||
using CounterStrikeSharp.API.Modules.Utils; | ||
using System.Text.RegularExpressions; | ||
using System.Text.Json; | ||
using HealthBar_GoldKingZ.Config; | ||
using System.Text.Encodings.Web; | ||
|
||
namespace HealthBar_GoldKingZ; | ||
|
||
public class Helper | ||
{ | ||
public static void AdvancedPlayerPrintToChat(CCSPlayerController player, string message, params object[] args) | ||
{ | ||
if (string.IsNullOrEmpty(message))return; | ||
|
||
for (int i = 0; i < args.Length; i++) | ||
{ | ||
message = message.Replace($"{{{i}}}", args[i].ToString()); | ||
} | ||
if (Regex.IsMatch(message, "{nextline}", RegexOptions.IgnoreCase)) | ||
{ | ||
string[] parts = Regex.Split(message, "{nextline}", RegexOptions.IgnoreCase); | ||
foreach (string part in parts) | ||
{ | ||
string messages = part.Trim(); | ||
player.PrintToChat(" " + messages); | ||
} | ||
}else | ||
{ | ||
player.PrintToChat(message); | ||
} | ||
} | ||
public static void AdvancedServerPrintToChatAll(string message, params object[] args) | ||
{ | ||
if (string.IsNullOrEmpty(message))return; | ||
|
||
for (int i = 0; i < args.Length; i++) | ||
{ | ||
message = message.Replace($"{{{i}}}", args[i].ToString()); | ||
} | ||
if (Regex.IsMatch(message, "{nextline}", RegexOptions.IgnoreCase)) | ||
{ | ||
string[] parts = Regex.Split(message, "{nextline}", RegexOptions.IgnoreCase); | ||
foreach (string part in parts) | ||
{ | ||
string messages = part.Trim(); | ||
Server.PrintToChatAll(" " + messages); | ||
} | ||
}else | ||
{ | ||
Server.PrintToChatAll(message); | ||
} | ||
} | ||
public static void AdvancedPlayerPrintToConsole(CCSPlayerController player, string message, params object[] args) | ||
{ | ||
if (string.IsNullOrEmpty(message))return; | ||
|
||
for (int i = 0; i < args.Length; i++) | ||
{ | ||
message = message.Replace($"{{{i}}}", args[i].ToString()); | ||
} | ||
if (Regex.IsMatch(message, "{nextline}", RegexOptions.IgnoreCase)) | ||
{ | ||
string[] parts = Regex.Split(message, "{nextline}", RegexOptions.IgnoreCase); | ||
foreach (string part in parts) | ||
{ | ||
string messages = part.Trim(); | ||
player.PrintToConsole(" " + messages); | ||
} | ||
}else | ||
{ | ||
player.PrintToConsole(message); | ||
} | ||
} | ||
|
||
public static bool IsPlayerInGroupPermission(CCSPlayerController player, string groups) | ||
{ | ||
var excludedGroups = groups.Split(','); | ||
foreach (var group in excludedGroups) | ||
{ | ||
if(group.StartsWith("#")) | ||
{ | ||
if (AdminManager.PlayerInGroup(player, group)) | ||
{ | ||
return true; | ||
} | ||
|
||
}else if(group.StartsWith("@")) | ||
{ | ||
if (AdminManager.PlayerHasPermissions(player, group)) | ||
{ | ||
return true; | ||
} | ||
}else | ||
{ | ||
if (AdminManager.PlayerInGroup(player, group)) | ||
{ | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
public static List<CCSPlayerController> GetPlayersController(bool IncludeBots = false, bool IncludeSPEC = true, bool IncludeCT = true, bool IncludeT = true) | ||
{ | ||
var playerList = Utilities | ||
.FindAllEntitiesByDesignerName<CCSPlayerController>("cs_player_controller") | ||
.Where(p => p != null && p.IsValid && | ||
(IncludeBots || (!p.IsBot && !p.IsHLTV)) && | ||
p.Connected == PlayerConnectedState.PlayerConnected && | ||
((IncludeCT && p.TeamNum == (byte)CsTeam.CounterTerrorist) || | ||
(IncludeT && p.TeamNum == (byte)CsTeam.Terrorist) || | ||
(IncludeSPEC && p.TeamNum == (byte)CsTeam.Spectator))) | ||
.ToList(); | ||
|
||
return playerList; | ||
} | ||
public static int GetPlayersCount(bool IncludeBots = false, bool IncludeSPEC = true, bool IncludeCT = true, bool IncludeT = true) | ||
{ | ||
return Utilities.GetPlayers().Count(p => | ||
p != null && | ||
p.IsValid && | ||
p.Connected == PlayerConnectedState.PlayerConnected && | ||
(IncludeBots || (!p.IsBot && !p.IsHLTV)) && | ||
((IncludeCT && p.TeamNum == (byte)CsTeam.CounterTerrorist) || | ||
(IncludeT && p.TeamNum == (byte)CsTeam.Terrorist) || | ||
(IncludeSPEC && p.TeamNum == (byte)CsTeam.Spectator)) | ||
); | ||
} | ||
|
||
public static void ClearVariables() | ||
{ | ||
|
||
} | ||
|
||
public static string ReplaceMessages(string Message, string date, string time, string PlayerName, string SteamId, string ipAddress, string reason) | ||
{ | ||
var replacedMessage = Message | ||
.Replace("{TIME}", time) | ||
.Replace("{DATE}", date) | ||
.Replace("{PLAYERNAME}", PlayerName.ToString()) | ||
.Replace("{STEAMID}", SteamId.ToString()) | ||
.Replace("{IP}", ipAddress.ToString()) | ||
.Replace("{REASON}", reason); | ||
return replacedMessage; | ||
} | ||
public static string RemoveLeadingSpaces(string content) | ||
{ | ||
string[] lines = content.Split('\n'); | ||
for (int i = 0; i < lines.Length; i++) | ||
{ | ||
lines[i] = lines[i].TrimStart(); | ||
} | ||
return string.Join("\n", lines); | ||
} | ||
public static CCSGameRules? GetGameRules() | ||
{ | ||
try | ||
{ | ||
var gameRulesEntities = Utilities.FindAllEntitiesByDesignerName<CCSGameRulesProxy>("cs_gamerules"); | ||
return gameRulesEntities.First().GameRules; | ||
} | ||
catch | ||
{ | ||
return null; | ||
} | ||
} | ||
public static bool IsWarmup() | ||
{ | ||
return GetGameRules()?.WarmupPeriod ?? false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using CounterStrikeSharp.API; | ||
using CounterStrikeSharp.API.Core; | ||
using CounterStrikeSharp.API.Modules.Commands; | ||
using System.Text.Json.Serialization; | ||
using Microsoft.Extensions.Localization; | ||
using CounterStrikeSharp.API.Modules.Admin; | ||
using CounterStrikeSharp.API.Core.Attributes.Registration; | ||
using CounterStrikeSharp.API.Core.Attributes; | ||
using HealthBar_GoldKingZ.Config; | ||
using CounterStrikeSharp.API.Modules.UserMessages; | ||
using CounterStrikeSharp.API.Modules.Entities; | ||
|
||
namespace HealthBar_GoldKingZ; | ||
|
||
|
||
public class HealthBarGoldKingZ : BasePlugin | ||
{ | ||
public override string ModuleName => "Show HealthBar To Attacker Or All Players"; | ||
public override string ModuleVersion => "1.0.0"; | ||
public override string ModuleAuthor => "Gold KingZ"; | ||
public override string ModuleDescription => "https://github.com/oqyh"; | ||
public static HealthBarGoldKingZ Instance { get; set; } = new(); | ||
|
||
public override void Load(bool hotReload) | ||
{ | ||
Instance = this; | ||
Configs.Load(ModuleDirectory); | ||
Configs.Shared.CookiesModule = ModuleDirectory; | ||
|
||
RegisterEventHandler<EventPlayerHurt>(OnEventPlayerHurt); | ||
} | ||
|
||
public HookResult OnEventPlayerHurt(EventPlayerHurt @event, GameEventInfo info) | ||
{ | ||
if (@event == null || Configs.GetConfigData().DisableOnWarmUp && Helper.IsWarmup()) return HookResult.Continue; | ||
|
||
var victim = @event.Userid; | ||
var dmgHealth = @event.DmgHealth; | ||
var health = @event.Health; | ||
|
||
if (victim == null || !victim.IsValid) return HookResult.Continue; | ||
var victimHealth = victim.PlayerPawn.Value!.MaxHealth; | ||
|
||
var attacker = @event.Attacker; | ||
if (attacker == null || !attacker.IsValid) return HookResult.Continue; | ||
|
||
float oldHealth = health + dmgHealth; | ||
if (oldHealth == health) return HookResult.Continue; | ||
|
||
float oldHealthRatio = oldHealth / victimHealth; | ||
float newHealthRatio = (float)health / victimHealth; | ||
|
||
var message = UserMessage.FromPartialName("UpdateScreenHealthBar"); | ||
message.SetInt("entidx", (int)victim.PlayerPawn.Index); | ||
message.SetFloat("healthratio_old", oldHealthRatio); | ||
message.SetFloat("healthratio_new", newHealthRatio); | ||
message.SetInt("style", Configs.GetConfigData().DisplayHealthBarStyle); | ||
if(Configs.GetConfigData().ShowHealthBarToAll) | ||
{ | ||
message.Recipients.AddAllPlayers(); | ||
message.Send(); | ||
}else | ||
{ | ||
message.Send(attacker); | ||
} | ||
|
||
return HookResult.Continue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> | ||
<CopyLocalLockFileAssemblies>false</CopyLocalLockFileAssemblies> | ||
<OutputPath>bin\Release\addons\counterstrikesharp\plugins\HealthBar-GoldKingZ\</OutputPath> | ||
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> | ||
<Configuration>Release</Configuration> | ||
<DebugType>none</DebugType> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="CounterStrikeSharp.API" Version="*" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="lang\**\*.*" CopyToOutputDirectory="PreserveNewest" /> | ||
</ItemGroup> | ||
</Project> |