Skip to content

Commit

Permalink
Merge pull request #5 from oscar-wos/beam
Browse files Browse the repository at this point in the history
Config + Beams
  • Loading branch information
oscar-wos authored Jul 3, 2024
2 parents fbc372e + 23108ab commit 8c611cc
Show file tree
Hide file tree
Showing 14 changed files with 408 additions and 194 deletions.
22 changes: 12 additions & 10 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ jobs:

- name: Create Directories
run: |
mkdir -p ./plugin/plugins/${{ github.event.repository.name }}
mkdir -p ./plugin/shared/Menu
mkdir -p plugin/plugins/${{ github.event.repository.name }}
mkdir -p plugin/shared/Menu
- name: Move Files
run: |
Expand All @@ -61,25 +61,27 @@ jobs:
- name: Zip
run: |
cd ./plugin/
cd ./plugin
zip -r ${{ github.event.repository.name }}-${{ github.sha }}.zip .
- name: Extract version and create tag
- name: Extract version
id: extract_version
run: |
version=$(grep -oP 'public override string ModuleVersion => "\K(.*)(?=";)' ./src/Globals.cs)
echo "Version found: $version"
git config --global user.email "[email protected]"
git config --global user.name "GitHub Actions"
git tag $version
git push origin $version
echo "::set-output name=version::$version"
- name: Publish
uses: actions/upload-artifact@v4
with:
name: ${{ github.event.repository.name }}-${{ github.sha }}
path: plugin
path: ./plugin

- name: Create Tag
run: |
git config --global user.email "[email protected]"
git config --global user.name "GitHub Actions"
git tag ${{ env.version }}
git push origin ${{ env.version }}
- name: Create Release
id: create_release
Expand Down
10 changes: 7 additions & 3 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
"ct": "CT",
"both": "Both",
"missingPermission": "Missing permission: {red}{0}",
"invalidInput": "Invalid input: '{lightred}{0}{default}' ({orange}{1}{default})",
"delayRemaining": "{0} in {1} second(s)",
"saving": "Saving zone '{0}' {1}",
"invalidInput": "Invalid input: \"{red}{0}{white}\" ({orange}{1}{white})",
"saving": "Saving {0} \"{1}\"",
"rushDisabled": "Rush Zones {red}disabled{white}",
"campEnabled": "Camp Zones {olive}enabled{white}",
"delayRemaining": "{0} in {green}{1}{white} second(s)",
"rushDelayRemaining": "{0} {red}disabled{white} in {green}{1}{white} second(s)",
"hurtDamage": "{0} for {green}{1}{white} damage",
"zone.Bounce": "Bounce",
"zone.Hurt": "Hurt",
"zone.Kill": "Kill",
Expand Down
122 changes: 68 additions & 54 deletions src/AntiRush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,36 @@

namespace AntiRush;

public partial class AntiRush : BasePlugin
public partial class AntiRush : BasePlugin, IPluginConfig<AntiRushConfig>
{
public void OnConfigParsed(AntiRushConfig config)
{
Config = config;
}

public override void Load(bool isReload)
{
RegisterListener<Listeners.OnTick>(OnTick);
RegisterListener<Listeners.OnMapStart>(OnMapStart);
RegisterListener<Listeners.OnClientPutInServer>(OnClientPutInServer);
RegisterEventHandler<EventRoundStart>(OnRoundStart);
RegisterEventHandler<EventBombPlanted>(OnBombPlanted);
RegisterEventHandler<EventPlayerSpawn>(OnPlayerSpawn);
RegisterEventHandler<EventBulletImpact>(OnBulletImpact);
RegisterEventHandler<EventPlayerConnectFull>(OnPlayerConnect);

AddCommand("css_antirush", "Anti-Rush", CommandAntiRush);
AddCommand("css_addzone", "Add Zone", CommandAddZone);
//AddCommand("css_viewzones", "View Zones", CommandViewZones);

if (!isReload)
return;
LoadJson(Server.MapName);

foreach (var controller in Utilities.GetPlayers())
_playerData[controller] = new PlayerData();
Server.NextFrame(() =>
{
foreach (var controller in Utilities.GetPlayers())
_playerData[controller] = new PlayerData();

LoadJson(Server.MapName);
Server.ExecuteCommand("mp_restartgame 1");
Server.ExecuteCommand("mp_restartgame 1");
});
}

private void SaveZone(CCSPlayerController controller)
Expand All @@ -45,13 +53,13 @@ private void SaveZone(CCSPlayerController controller)
var minPoint = new Vector(Math.Min(menu.Points[0].X, menu.Points[1].X), Math.Min(menu.Points[0].Y, menu.Points[1].Y), Math.Min(menu.Points[0].Z, menu.Points[1].Z));
var maxPoint = new Vector(Math.Max(menu.Points[0].X, menu.Points[1].X), Math.Max(menu.Points[0].Y, menu.Points[1].Y), Math.Max(menu.Points[0].Z, menu.Points[1].Z));
var delay = zoneType != ZoneType.Bounce && float.TryParse(menu.Items[3].DataString, out var valueDelay) ? (float)Math.Floor(valueDelay * 10) / 10 : 0;
var damage = zoneType == ZoneType.Hurt && int.TryParse(menu.Items[4].DataString, out var valueDamage) ? valueDamage : 10;
var damage = zoneType == ZoneType.Hurt && int.TryParse(menu.Items[4].DataString, out var valueDamage) ? valueDamage : 0;
var name = menu.Items[2].DataString;

if (name.Length == 0)
name = "noname";
var zone = new Zone(name, zoneType, delay, damage, teams, minPoint, maxPoint);
_zones.Add(zone);

var printMessage = $"{Prefix}{Localizer["saving", name, FormatZoneString(zoneType)]} | {Localizer["menu.Teams"]} [";
var printMessage = $"{Prefix}{Localizer["saving", zone.ToString(Localizer), name]} | {Localizer["menu.Teams"]} [";

if (teams.Contains(CsTeam.Terrorist))
printMessage += $"{ChatColors.LightYellow}{Localizer["t"]}{ChatColors.White}";
Expand All @@ -68,67 +76,73 @@ private void SaveZone(CCSPlayerController controller)
printMessage += $" | {Localizer["menu.Damage"]} {ChatColors.Green}{damage}{ChatColors.White}";

controller.PrintToChat(printMessage);

var zone = new Zone(zoneType, teams, minPoint, maxPoint, name, delay, damage);
_zones.Add(zone);
SaveJson(Server.MapName);

if (Config.DrawZones)
zone.Draw();
}

private bool DoAction(CCSPlayerController controller, Zone zone)
private bool PrintAction(CCSPlayerController controller, Zone zone)
{
if (zone.Type != ZoneType.Bounce && Server.CurrentTime - _playerData[controller].LastMessage >= 1)
controller.PrintToChat($"{Prefix}{FormatZoneString(zone.Type)}");
if (!controller.IsValid(true) || !(Server.CurrentTime - _playerData[controller].LastMessage >= 1))
return false;

_playerData[controller].LastMessage = Server.CurrentTime;
if (zone.Type == ZoneType.Hurt && Server.CurrentTime % 1 != 0)
return false;

switch (zone.Type)
switch (Config.Messages)
{
case ZoneType.Bounce:
var speed = Math.Sqrt(_playerData[controller].LastVelocity.X * _playerData[controller].LastVelocity.X + _playerData[controller].LastVelocity.Y * _playerData[controller].LastVelocity.Y);
case "simple":
controller.PrintToChat($"{Prefix}{zone.ToString(Localizer)}");
return true;

_playerData[controller].LastVelocity *= (-350 / (float)speed);
_playerData[controller].LastVelocity.Z = _playerData[controller].LastVelocity.Z <= 0f ? 150f : Math.Min(_playerData[controller].LastVelocity.Z, 150f);
controller.PlayerPawn.Value!.Teleport(_playerData[controller].LastPosition, controller.PlayerPawn.Value.EyeAngles, _playerData[controller].LastVelocity);
case "detailed":
if (zone.Type is (ZoneType.Bounce or ZoneType.Teleport))
{
controller.PrintToChat(Config.NoRushTime != 0
? $"{Prefix}{Localizer["rushDelayRemaining", zone.ToString(Localizer), (_roundStart + Config.NoRushTime - Server.CurrentTime).ToString("0")]}"
: $"{Prefix}{zone.ToString(Localizer)}");

return true;
}

if (zone.Type == ZoneType.Hurt)
{
controller.PrintToChat($"{Prefix}{Localizer["hurtDamage", zone.ToString(Localizer), zone.Damage]}");
return true;
}

controller.PrintToChat($"{Prefix}{zone.ToString(Localizer)}");
return true;
}

case ZoneType.Hurt:
if (Server.CurrentTime % 1 != 0)
return false;
return false;
}

controller.PlayerPawn.Value!.Health -= zone.Damage;
Utilities.SetStateChanged(controller.PlayerPawn.Value, "CBaseEntity", "m_iHealth");
private void DoAction(CCSPlayerController controller, Zone zone)
{
if (PrintAction(controller, zone))
_playerData[controller].LastMessage = Server.CurrentTime;

switch (zone.Type)
{
case ZoneType.Bounce:
controller.Bounce();
return;

if (controller.PlayerPawn.Value.Health <= 0)
controller.PlayerPawn.Value.CommitSuicide(true, true);
case ZoneType.Hurt:
if (Server.CurrentTime % 1 == 0)
controller.Damage(zone.Damage);

return false;
return;

case ZoneType.Kill:
controller.PlayerPawn.Value!.CommitSuicide(true, true);
return false;
return;

case ZoneType.Teleport:
controller.PlayerPawn.Value!.Teleport(_playerData[controller].SpawnPos, controller.PlayerPawn.Value.EyeAngles, Vector.Zero);
return false;
return;
}

return false;
}

private string FormatZoneString(ZoneType type)
{
return type switch
{
ZoneType.Bounce => $"{ChatColors.Yellow}{Localizer["zone.Bounce"]}{ChatColors.White}",
ZoneType.Hurt => $"{ChatColors.Orange}{Localizer["zone.Hurt"]}{ChatColors.White}",
ZoneType.Kill => $"{ChatColors.Red}{Localizer["zone.Kill"]}{ChatColors.White}",
ZoneType.Teleport => $"{ChatColors.Magenta}{Localizer["zone.Teleport"]}{ChatColors.White}",
_ => ""
};
}

private static bool IsValidPlayer(CCSPlayerController? player)
{
return player != null && player is { IsValid: true, Connected: PlayerConnectedState.PlayerConnected, PawnIsAlive: true, IsBot: false };
}
}
35 changes: 8 additions & 27 deletions src/Commands.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Commands;
using AntiRush.Enums;

Expand All @@ -9,45 +8,27 @@ public partial class AntiRush
{
public void CommandAntiRush(CCSPlayerController? controller, CommandInfo info)
{
if (!IsValidPlayer(controller))
if (controller == null || !controller.IsValid(true) || !controller.HasPermission("@css/generic"))
return;

if (!AdminManager.PlayerHasPermissions(controller, "@css/generic"))
{
controller!.PrintToChat($"{Prefix}{Localizer["missingPermission", "@css/generic"]}");
return;
}

BuildMenu(controller!);
BuildMenu(controller);
}

public void CommandAddZone(CCSPlayerController? controller, CommandInfo info)
{
if (!IsValidPlayer(controller))
return;

if (!AdminManager.PlayerHasPermissions(controller, "@css/root"))
{
controller!.PrintToChat($"{Prefix}{Localizer["missingPermission", "@css/root"]}");
if (controller == null || !controller.IsValid(true) || !controller.HasPermission("@css/root"))
return;
}

BuildMenu(controller!);
BuildMenu(controller!, MenuType.Add);
BuildMenu(controller);
BuildMenu(controller, MenuType.Add);
}

public void CommandViewZones(CCSPlayerController? controller, CommandInfo info)
{
if (!IsValidPlayer(controller))
return;

if (!AdminManager.PlayerHasPermissions(controller, "@css/generic"))
{
controller!.PrintToChat($"{Prefix}{Localizer["missingPermission", "@css/generic"]}");
if (controller == null || !controller.IsValid(true) || !controller.HasPermission("@css/generic"))
return;
}

BuildMenu(controller!);
BuildMenu(controller!, MenuType.View);
BuildMenu(controller);
BuildMenu(controller, MenuType.View);
}
}
14 changes: 14 additions & 0 deletions src/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using CounterStrikeSharp.API.Core;
using System.Text.Json.Serialization;

namespace AntiRush;

public class AntiRushConfig : BasePluginConfig
{
public override int Version { get; set; } = 3;
[JsonPropertyName("Messages")] public string Messages { get; set; } = "simple";
[JsonPropertyName("DrawZones")] public bool DrawZones { get; set; } = false;
[JsonPropertyName("Warmup")] public bool Warmup { get; set; } = false;
[JsonPropertyName("NoRushTime")] public int NoRushTime { get; set; } = 0;
[JsonPropertyName("NoCampTime")] public int NoCampTime { get; set; } = 0;
}
46 changes: 46 additions & 0 deletions src/ControllerExtends.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Admin;

namespace AntiRush;

public static class ControllerExtends
{
public static bool IsValid(this CCSPlayerController? controller, bool checkBot = false)
{
if (checkBot)
return controller is { IsValid: true, IsBot: false };

return controller is { IsValid: true };
}

public static void Damage(this CCSPlayerController? controller, int damage)
{
if (controller == null || !controller.IsValid() || controller!.PlayerPawn.Value == null)
return;

controller.PlayerPawn.Value.Health -= damage;
Utilities.SetStateChanged(controller.PlayerPawn.Value, "CBaseEntity", "m_iHealth");

if (controller.PlayerPawn.Value.Health <= 0)
controller.PlayerPawn.Value.CommitSuicide(true, true);
}

public static void Bounce(this CCSPlayerController? controller)
{
if (controller == null || !controller.IsValid() || controller!.PlayerPawn.Value == null)
return;

var vel = controller.PlayerPawn.Value.AbsVelocity;
var speed = Math.Sqrt(vel.X * vel.X + vel.Y * vel.Y);

vel *= (-350 / (float)speed);
vel.Z = vel.Z <= 0 ? 150 : Math.Min(vel.Z, 150);
controller.PlayerPawn.Value.Teleport(controller.PlayerPawn.Value.AbsOrigin, controller.PlayerPawn.Value.EyeAngles, vel);
}

public static bool HasPermission(this CCSPlayerController? controller, string permission)
{
return controller != null && controller.IsValid() && AdminManager.PlayerHasPermissions(controller, permission);
}
}
Loading

0 comments on commit 8c611cc

Please sign in to comment.