Skip to content

Commit

Permalink
1.6.6a
Browse files Browse the repository at this point in the history
```diff
+ Reapply gravity/speed with timer
+ Added shake effect for slap
+ Fixed css_gravity and css_speed command
+ Fixed css_give command, for example weapon_knife returns weapon_knife instead of weapon_knife and weapon_knife_t
+ Small code improvements
```
  • Loading branch information
daffyyyy committed Oct 30, 2024
1 parent 82b8272 commit 7a69c53
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 86 deletions.
2 changes: 1 addition & 1 deletion CS2-SimpleAdmin/CS2-SimpleAdmin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
public override string ModuleName => "CS2-SimpleAdmin" + (Helper.IsDebugBuild ? " (DEBUG)" : " (RELEASE)");
public override string ModuleDescription => "Simple admin plugin for Counter-Strike 2 :)";
public override string ModuleAuthor => "daffyy & Dliix66";
public override string ModuleVersion => "1.6.5a";
public override string ModuleVersion => "1.6.6a";

public override void Load(bool hotReload)
{
Expand Down
2 changes: 1 addition & 1 deletion CS2-SimpleAdmin/CS2-SimpleAdmin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.276" />
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.284" />
<PackageReference Include="Dapper" Version="2.1.35" />
<PackageReference Include="MySqlConnector" Version="2.3.7" />
<PackageReference Include="Newtonsoft.Json" Version="*" />
Expand Down
28 changes: 19 additions & 9 deletions CS2-SimpleAdmin/Commands/playercommands.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Globalization;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Entities.Constants;
Expand All @@ -10,6 +10,9 @@ namespace CS2_SimpleAdmin;

public partial class CS2_SimpleAdmin
{
internal static readonly Dictionary<int, float> SpeedPlayers = [];
internal static readonly Dictionary<CCSPlayerController, float> GravityPlayers = [];

[RequiresPermissions("@css/slay")]
[CommandHelper(minArgs: 1, usage: "<#userid or name>", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)]
public void OnSlayCommand(CCSPlayerController? caller, CommandInfo command)
Expand Down Expand Up @@ -265,8 +268,7 @@ internal static void SetHp(CCSPlayerController? caller, CCSPlayerController play
[CommandHelper(minArgs: 1, usage: "<#userid or name> <speed>", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)]
public void OnSpeedCommand(CCSPlayerController? caller, CommandInfo command)
{
var callerName = caller == null ? _localizer?["sa_console"] ?? "Console" : caller.PlayerName;
float.TryParse(command.GetArg(2), out var speed);
float.TryParse(command.GetArg(2), NumberStyles.Float, CultureInfo.InvariantCulture, out var speed);

var targets = GetTarget(command);
if (targets == null) return;
Expand Down Expand Up @@ -294,6 +296,11 @@ internal static void SetSpeed(CCSPlayerController? caller, CCSPlayerController p

// Set player's speed
player.SetSpeed(speed);

if (speed == 1f)
SpeedPlayers.Remove(player.Slot);
else
SpeedPlayers[player.Slot] = speed;

// Log the command
if (command == null)
Expand All @@ -313,14 +320,12 @@ internal static void SetSpeed(CCSPlayerController? caller, CCSPlayerController p
}
}

[ConsoleCommand("css_gravity")]
[RequiresPermissions("@css/slay")]
[CommandHelper(minArgs: 1, usage: "<#userid or name> <gravity>", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)]
public void OnGravityCommand(CCSPlayerController? caller, CommandInfo command)
{
var callerName = caller == null ? _localizer?["sa_console"] ?? "Console" : caller.PlayerName;
float.TryParse(command.GetArg(2), out var gravity);

float.TryParse(command.GetArg(2), NumberStyles.Float, CultureInfo.InvariantCulture, out var gravity);

var targets = GetTarget(command);
if (targets == null) return;

Expand All @@ -347,7 +352,12 @@ internal static void SetGravity(CCSPlayerController? caller, CCSPlayerController

// Set player's gravity
player.SetGravity(gravity);


if (gravity == 1f)
GravityPlayers.Remove(player);
else
GravityPlayers[player] = gravity;

// Log the command
if (command == null)
Helper.LogCommand(caller, $"css_gravity {(string.IsNullOrEmpty(player.PlayerName) ? player.SteamID.ToString() : player.PlayerName)} {gravity}");
Expand Down Expand Up @@ -452,7 +462,7 @@ internal static void Slap(CCSPlayerController? caller, CCSPlayerController playe

// Set default caller name if not provided
var callerName = caller != null ? caller.PlayerName : _localizer?["sa_console"] ?? "Console";

// Apply slap damage to the player
player.Pawn.Value?.Slap(damage);

Expand Down
3 changes: 3 additions & 0 deletions CS2-SimpleAdmin/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ public class OtherSettings

[JsonPropertyName("ShowBanMenuIfNoTime")]
public bool ShowBanMenuIfNoTime { get; set; } = true;

[JsonPropertyName("UserMessageGagChatType")]
public bool UserMessageGagChatType { get; set; } = false;
}

public class CS2_SimpleAdminConfig : BasePluginConfig
Expand Down
106 changes: 77 additions & 29 deletions CS2-SimpleAdmin/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,41 @@
using Microsoft.Extensions.Logging;
using System.Text;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.UserMessages;

namespace CS2_SimpleAdmin;

public partial class CS2_SimpleAdmin
{
private void RegisterEvents()
{
RegisterListener<Listeners.OnMapStart>(OnMapStart);
RegisterListener<Listeners.OnMapStart>(OnMapStart);
RegisterListener<Listeners.OnGameServerSteamAPIActivated>(OnGameServerSteamAPIActivated);
AddCommandListener(null, OnCommandSayNew);
AddCommandListener("callvote", OnCommandCallVote);
if (Config.OtherSettings.UserMessageGagChatType)
HookUserMessage(118, HookUmChat);

AddCommandListener(null, ComamndListenerHandler);
// AddCommandListener("callvote", OnCommandCallVote);
// AddCommandListener("say", OnCommandSay);
// AddCommandListener("say_team", OnCommandTeamSay);
}

private HookResult OnCommandCallVote(CCSPlayerController? caller, CommandInfo info)
{
var voteType = info.GetArg(1).ToLower();

if (voteType != "kick")
return HookResult.Continue;

var target = int.TryParse(info.GetArg(2), out var userId)
? Utilities.GetPlayerFromUserid(userId)
: null;

if (target == null || !target.IsValid || target.Connected != PlayerConnectedState.PlayerConnected)
return HookResult.Continue;

return !AdminManager.CanPlayerTarget(caller, target) ? HookResult.Stop : HookResult.Continue;
}
// private HookResult OnCommandCallVote(CCSPlayerController? caller, CommandInfo info)
// {
// var voteType = info.GetArg(1).ToLower();
//
// if (voteType != "kick")
// return HookResult.Continue;
//
// var target = int.TryParse(info.GetArg(2), out var userId)
// ? Utilities.GetPlayerFromUserid(userId)
// : null;
//
// if (target == null || !target.IsValid || target.Connected != PlayerConnectedState.PlayerConnected)
// return HookResult.Continue;
//
// return !AdminManager.CanPlayerTarget(caller, target) ? HookResult.Stop : HookResult.Continue;
// }

private void OnGameServerSteamAPIActivated()
{
Expand Down Expand Up @@ -88,7 +91,9 @@ public HookResult OnClientDisconnect(EventPlayerDisconnect @event, GameEventInfo

SilentPlayers.Remove(player.Slot);
GodPlayers.Remove(player.Slot);

SpeedPlayers.Remove(player.Slot);
GravityPlayers.Remove(player);

if (player.UserId.HasValue)
PlayersInfo.Remove(player.UserId.Value);

Expand Down Expand Up @@ -123,13 +128,16 @@ public HookResult OnPlayerFullConnect(EventPlayerConnectFull @event, GameEventIn
}

[GameEventHandler]
public HookResult OnRoundEnd(EventRoundStart @event, GameEventInfo info)
public HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info)
{
#if DEBUG
Logger.LogCritical("[OnRoundEnd]");
#endif

GodPlayers.Clear();
SpeedPlayers.Clear();
GravityPlayers.Clear();

foreach (var player in PlayersInfo.Values)
{
player.DiePosition = null;
Expand All @@ -153,22 +161,57 @@ public HookResult OnRoundEnd(EventRoundStart @event, GameEventInfo info)

return HookResult.Continue;
}

private HookResult HookUmChat(UserMessage um)
{
var author = Utilities.GetPlayerFromIndex(um.ReadInt("entityindex"));
if (author == null || !author.IsValid || author.IsBot)
return HookResult.Continue;

if (PlayerPenaltyManager.IsPenalized(author.Slot, PenaltyType.Gag) || PlayerPenaltyManager.IsPenalized(author.Slot, PenaltyType.Silence))
return HookResult.Stop;

// um.Recipients.Clear();

return HookResult.Continue;
}

private HookResult OnCommandSayNew(CCSPlayerController? player, CommandInfo info)
private HookResult ComamndListenerHandler(CCSPlayerController? player, CommandInfo info)
{
if (player == null || !player.IsValid || player.IsBot)
return HookResult.Continue;

var command = info.GetArg(0).ToLower();

if (command == "css_admins_reload")
switch (command)
{
AddTimer(1.0f, () => ReloadAdmins(null));
return HookResult.Continue;
case "css_admins_reload":
AddTimer(1.0f, () => ReloadAdmins(null));
return HookResult.Continue;
case "callvote":
{
var voteType = info.GetArg(1).ToLower();

if (voteType != "kick")
return HookResult.Continue;

var target = int.TryParse(info.GetArg(2), out var userId)
? Utilities.GetPlayerFromUserid(userId)
: null;

if (target == null || !target.IsValid || target.Connected != PlayerConnectedState.PlayerConnected)
return HookResult.Continue;

return !AdminManager.CanPlayerTarget(player, target) ? HookResult.Stop : HookResult.Continue;
}
}

if (!command.Contains("say"))
return HookResult.Continue;

if (!Config.OtherSettings.UserMessageGagChatType)
if (PlayerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Gag) || PlayerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Silence))
return HookResult.Stop;

if (info.GetArg(1).StartsWith($"/")
|| info.GetArg(1).StartsWith($"!"))
Expand All @@ -177,9 +220,6 @@ private HookResult OnCommandSayNew(CCSPlayerController? player, CommandInfo info
if (info.GetArg(1).Length == 0)
return HookResult.Stop;

if (PlayerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Gag) || PlayerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Silence))
return HookResult.Stop;

if (command == "say" && info.GetArg(1).StartsWith($"@") &&
AdminManager.PlayerHasPermissions(player, "@css/chat"))
{
Expand Down Expand Up @@ -282,6 +322,8 @@ private void OnMapStart(string mapName)

GodPlayers.Clear();
SilentPlayers.Clear();
SpeedPlayers.Clear();
GravityPlayers.Clear();

PlayerPenaltyManager.RemoveAllPenalties();
}
Expand All @@ -294,6 +336,9 @@ public HookResult OnPlayerHurt(EventPlayerHurt @event, GameEventInfo info)
if (player is null || @event.Attacker is null || !player.PawnIsAlive || player.PlayerPawn.Value == null)
return HookResult.Continue;

if (SpeedPlayers.TryGetValue(player.Slot, out var speedPlayer))
player.SetSpeed(speedPlayer);

if (!GodPlayers.Contains(player.Slot)) return HookResult.Continue;

player.PlayerPawn.Value.Health = player.PlayerPawn.Value.MaxHealth;
Expand All @@ -310,6 +355,9 @@ public HookResult OnPlayerDeath(EventPlayerDeath @event, GameEventInfo info)
if (player?.UserId == null || player.IsBot || player.Connected != PlayerConnectedState.PlayerConnected)
return HookResult.Continue;

SpeedPlayers.Remove(player.Slot);
GravityPlayers.Remove(player);

PlayersInfo[player.UserId.Value].DiePosition =
new DiePosition(
new Vector(player.PlayerPawn.Value?.AbsOrigin?.X, player.PlayerPawn.Value?.AbsOrigin?.Y,
Expand Down
18 changes: 16 additions & 2 deletions CS2-SimpleAdmin/Extensions/PlayerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using CounterStrikeSharp.API.Modules.Memory;
using Microsoft.Extensions.Localization;
using System.Text;
using CounterStrikeSharp.API.Modules.UserMessages;
using Vector = CounterStrikeSharp.API.Modules.Utils.Vector;

namespace CS2_SimpleAdmin;
Expand Down Expand Up @@ -93,8 +94,8 @@ public static void Unbury(this CBasePlayerPawn pawn, float depth = 15f)

public static void Freeze(this CBasePlayerPawn pawn)
{
pawn.MoveType = MoveType_t.MOVETYPE_OBSOLETE;
Schema.SetSchemaValue(pawn.Handle, "CBaseEntity", "m_nActualMoveType", 1); // obsolete
pawn.MoveType = MoveType_t.MOVETYPE_INVALID;
Schema.SetSchemaValue(pawn.Handle, "CBaseEntity", "m_nActualMoveType", 11); // invalid
Utilities.SetStateChanged(pawn, "CBaseEntity", "m_MoveType");
}

Expand Down Expand Up @@ -171,6 +172,8 @@ private static void PerformSlap(CBasePlayerPawn pawn, int damage = 0)
if (pawn.LifeState != (int)LifeState_t.LIFE_ALIVE)
return;

var controller = pawn.Controller.Value?.As<CCSPlayerController>();

/* Teleport in a random direction - thank you, Mani!*/
/* Thank you AM & al!*/
var random = new Random();
Expand All @@ -184,6 +187,17 @@ private static void PerformSlap(CBasePlayerPawn pawn, int damage = 0)
pawn.AbsVelocity.Y = vel.Y;
pawn.AbsVelocity.Z = vel.Z;

if (controller != null && controller.IsValid)
{
var shakeMessage = UserMessage.FromPartialName("Shake");
shakeMessage.SetFloat("duration", 1);
shakeMessage.SetFloat("amplitude", 10);
shakeMessage.SetFloat("frequency", 1f);
shakeMessage.SetInt("command", 0);
shakeMessage.Recipients.Add(controller);
shakeMessage.Send();
}

if (damage <= 0)
return;

Expand Down
25 changes: 22 additions & 3 deletions CS2-SimpleAdmin/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -690,11 +690,30 @@ private static Dictionary<string, CsItem> BuildEnumMemberMap()

public static List<(string EnumMemberValue, CsItem EnumValue)> GetWeaponsByPartialName(string input)
{
// Normalize input for case-insensitive comparison
var normalizedInput = input.ToLowerInvariant();

// Find all matching weapons based on the input
var matchingWeapons = WeaponsEnumCache.Value
.Where(kvp => kvp.Key.Contains(input))
.Select(kvp => (kvp.Key, kvp.Value))
.Where(kvp => kvp.Key.Contains(normalizedInput, StringComparison.InvariantCultureIgnoreCase))
.Select(kvp => (EnumMemberValue: kvp.Key, EnumValue: kvp.Value))
.ToList();

// Check for an exact match first
var exactMatch = matchingWeapons
.FirstOrDefault(m => m.EnumMemberValue.Equals(input, StringComparison.OrdinalIgnoreCase));

if (exactMatch.EnumMemberValue != null)
{
// Return a list containing only the exact match
return [exactMatch];
}

// If no exact match, get all matches that start with the input
var filteredWeapons = matchingWeapons
.Where(m => m.EnumMemberValue.StartsWith(normalizedInput, StringComparison.InvariantCultureIgnoreCase))
.ToList();

return matchingWeapons;
return filteredWeapons; // Return all relevant matches for the partial input
}
}
Loading

0 comments on commit 7a69c53

Please sign in to comment.