Skip to content

Commit

Permalink
v1.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
NockyCZ committed May 1, 2024
1 parent e1d2468 commit 16b80b1
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 63 deletions.
10 changes: 6 additions & 4 deletions source/Common/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ namespace Deathmatch
public partial class Deathmatch
{
public DeathmatchConfig Config { get; set; } = null!;
public static int NextMode;
public static string ModeCenterMessage = "";
public static int ActiveCustomMode = 0;
public static int g_iModeTimer = 0;
public static int g_iRemainingTime = 500;
public static bool g_bIsActiveEditor = false;
public static bool g_bDefaultMapSpawnDisabled = false;
public static int ModeTimer = 0;
public static int RemainingTime = 500;
public static bool IsActiveEditor = false;
public static bool IsCasualGamemode;
public static bool DefaultMapSpawnDisabled = false;
public ModeData? ActiveMode;
public MemoryFunctionWithReturn<CCSPlayer_ItemServices, CEconItemView, AcquireMethod, NativeObject, AcquireResult>? CCSPlayer_CanAcquireFunc;
public MemoryFunctionWithReturn<int, string, CCSWeaponBaseVData>? GetCSWeaponDataFromKeyFunc;
Expand Down
11 changes: 7 additions & 4 deletions source/Configs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ public class Gameplay
[JsonPropertyName("Check Enemies Distance")] public bool CheckDistance { get; set; } = true;
[JsonPropertyName("Distance From Enemies for Respawn")] public int DistanceRespawn { get; set; } = 500;
[JsonPropertyName("Default Weapons")] public int DefaultModeWeapons { get; set; } = 2;
[JsonPropertyName("Contains Restricted Weapons As Default Weapons")] public bool RemoveRestrictedWeapons { get; set; } = true;
[JsonPropertyName("Switch Weapons")] public bool SwitchWeapons { get; set; } = true;
[JsonPropertyName("Allow Buymenu")] public bool AllowBuyMenu { get; set; } = true;
[JsonPropertyName("Use Default Spawns")] public bool DefaultSpawns { get; set; } = false;
[JsonPropertyName("Respawn Players After New Mode")] public bool RespawnPlayersAtNewMode { get; set; } = false;
[JsonPropertyName("Spawn Protection Color")] public string SpawnProtectionColor { get; set; } = "#000000";
}
public class General
{
[JsonPropertyName("Hide Round Seconds")] public bool HideRoundSeconds { get; set; } = true;
[JsonPropertyName("Hide New Mode Countdown")] public bool HideModeRemainingTime { get; set; } = false;
[JsonPropertyName("Block Radio Messages")] public bool BlockRadioMessage { get; set; } = true;
[JsonPropertyName("Remove Breakable Entities")] public bool RemoveBreakableEntities { get; set; } = true;
[JsonPropertyName("Remove Decals After Death")] public bool RemoveDecals { get; set; } = true;
Expand Down Expand Up @@ -174,7 +177,7 @@ public class CustomModes
OnlyHS = true,
KnifeDamage = false,
RandomWeapons = false,
CenterMessageText = "<font class='fontSize-l' color='orange'>Only Headshot</font>",
CenterMessageText = "<font class='fontSize-l' color='orange'>Only Headshot</font><br>Next Mode: {NEXTMODE} in {REMAININGTIME}<br>",
PrimaryWeapons = new List<string> {
"weapon_aug", "weapon_sg556", "weapon_xm1014",
"weapon_ak47", "weapon_famas", "weapon_galilar",
Expand All @@ -198,7 +201,7 @@ public class CustomModes
OnlyHS = false,
KnifeDamage = true,
RandomWeapons = false,
CenterMessageText = "<font class='fontSize-l' color='green'>Only Deagle</font>",
CenterMessageText = "<font class='fontSize-l' color='green'>Only Deagle</font><br>Next Mode: {NEXTMODE} in {REMAININGTIME}<br>",
PrimaryWeapons = new List<string>(),
SecondaryWeapons = new List<string> {
"weapon_deagle"
Expand All @@ -218,7 +221,7 @@ public class CustomModes
OnlyHS = false,
KnifeDamage = true,
RandomWeapons = false,
CenterMessageText = "<font class='fontSize-l' color='blue'>Only Pistols</font>",
CenterMessageText = "<font class='fontSize-l' color='blue'>Only Pistols</font><br>Next Mode: {NEXTMODE} in {REMAININGTIME}<br>",
PrimaryWeapons = new List<string>(),
SecondaryWeapons = new List<string> {
"weapon_usp_silencer", "weapon_p250", "weapon_glock",
Expand All @@ -238,7 +241,7 @@ public class CustomModes
OnlyHS = false,
KnifeDamage = true,
RandomWeapons = true,
CenterMessageText = "<font class='fontSize-l' color='yellow'>Only SMG (Random Weapons)</font>",
CenterMessageText = "<font class='fontSize-l' color='yellow'>Only SMG (Random Weapons)</font><br>Next Mode: {NEXTMODE} in {REMAININGTIME}<br>",
PrimaryWeapons = new List<string> {
"weapon_p90", "weapon_bizon", "weapon_mp5sd",
"weapon_mp7", "weapon_mp9", "weapon_mac10",
Expand Down
57 changes: 38 additions & 19 deletions source/Deathmatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public partial class Deathmatch : BasePlugin, IPluginConfig<DeathmatchConfig>
{
public override string ModuleName => "Deathmatch Core";
public override string ModuleAuthor => "Nocky";
public override string ModuleVersion => "1.1.2";
public override string ModuleVersion => "1.1.3";

public void OnConfigParsed(DeathmatchConfig config)
{
Expand Down Expand Up @@ -53,7 +53,7 @@ public override void Load(bool hotReload)

RegisterListener<Listeners.OnMapStart>(mapName =>
{
g_bDefaultMapSpawnDisabled = false;
DefaultMapSpawnDisabled = false;
Server.NextFrame(() =>
{
SetupCustomMode(Config.Gameplay.MapStartMode.ToString());
Expand All @@ -69,13 +69,22 @@ public override void Load(bool hotReload)
{
if (!GameRules().WarmupPeriod && ActiveMode != null)
{
g_iModeTimer++;
g_iRemainingTime = ActiveMode.Interval - g_iModeTimer;
if (g_iRemainingTime == 0)
ModeTimer++;
RemainingTime = ActiveMode.Interval - ModeTimer;
if (RemainingTime == 0)
{
SetupCustomMode(GetModeType().ToString());
SetupCustomMode(NextMode.ToString());
}
if (!string.IsNullOrEmpty(ActiveMode.CenterMessageText) && CustomModes.ContainsKey(NextMode.ToString()))
{
var time = TimeSpan.FromSeconds(RemainingTime);
var formattedTime = RemainingTime > 60 ? $"{time.Minutes}:{time.Seconds:D2}" : $"{time.Seconds}";
var NextModeData = CustomModes[NextMode.ToString()];
ModeCenterMessage = ActiveMode.CenterMessageText.Replace("{REMAININGTIME}", formattedTime);
ModeCenterMessage = ModeCenterMessage.Replace("{NEXTMODE}", NextModeData.Name);
}
}
}, TimerFlags.REPEAT | TimerFlags.STOP_ON_MAPCHANGE);
}
Expand All @@ -100,7 +109,7 @@ public override void Load(bool hotReload)
});
RegisterListener<Listeners.OnTick>(() =>
{
if (g_bIsActiveEditor)
if (IsActiveEditor)
{
foreach (var p in Utilities.GetPlayers().Where(p => p.IsValid && !p.IsBot && !p.IsHLTV && AdminManager.PlayerHasPermissions(p, "@css/root")))
{
Expand All @@ -113,33 +122,32 @@ public override void Load(bool hotReload)
{
foreach (var p in Utilities.GetPlayers().Where(p => playerData.ContainsPlayer(p) && playerData[p].HudMessages))
{
if (ActiveMode != null && !string.IsNullOrEmpty(ActiveMode.CenterMessageText) && MenuManager.GetActiveMenu(p) == null)
if (ActiveMode != null && !string.IsNullOrEmpty(ModeCenterMessage) && MenuManager.GetActiveMenu(p) == null)
{
p.PrintToCenterHtml($"{ActiveMode.CenterMessageText}");
p.PrintToCenterHtml(ModeCenterMessage);
}
if (g_iRemainingTime <= Config.Gameplay.NewModeCountdown && Config.Gameplay.NewModeCountdown > 0)
if (Config.General.HideModeRemainingTime && RemainingTime <= Config.Gameplay.NewModeCountdown && Config.Gameplay.NewModeCountdown > 0)
{
if (g_iRemainingTime == 0)
if (RemainingTime == 0)
{
p.PrintToCenter($"{Localizer["Hud.NewModeStarted"]}");
}
else
{
p.PrintToCenter($"{Localizer["Hud.NewModeStarting", g_iRemainingTime]}");
p.PrintToCenter($"{Localizer["Hud.NewModeStarting", RemainingTime]}");
}
}
}
}
});
}

public override void Unload(bool hotReload)
{
VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Unhook(OnTakeDamage, HookMode.Pre);
CCSPlayer_CanAcquireFunc?.Unhook(OnWeaponCanAcquire, HookMode.Pre);
playerData.Clear();
AllowedPrimaryWeaponsList.Clear();
AllowedSecondaryWeaponsList.Clear();
}

public void SetupCustomMode(string modeId)
{
ActiveMode = CustomModes[modeId];
Expand All @@ -159,24 +167,32 @@ public void SetupCustomMode(string modeId)
bNewmode = false;

ActiveCustomMode = int.Parse(modeId);
NextMode = GetModeType();

if (CustomModes.ContainsKey(NextMode.ToString()) && !string.IsNullOrEmpty(ActiveMode.CenterMessageText))
{
var NextModeData = CustomModes[NextMode.ToString()];
ModeCenterMessage = ActiveMode.CenterMessageText.Replace("{NEXTMODE}", NextModeData.Name);
ModeCenterMessage = ModeCenterMessage.Replace("{REMAININGTIME}", RemainingTime.ToString());
}
SetupDeathmatchConfiguration(ActiveMode, bNewmode);
}

public void SetupDeathmatchConfiguration(ModeData mode, bool isNewMode)
{
g_iModeTimer = 0;
ModeTimer = 0;

if (isNewMode)
Server.PrintToChatAll($"{Localizer["Chat.Prefix"]} {Localizer["Chat.NewModeStarted", mode.Name]}");

Server.ExecuteCommand($"mp_free_armor {mode.Armor};mp_damage_headshot_only {mode.OnlyHS};mp_ct_default_primary \"\";mp_t_default_primary \"\";mp_ct_default_secondary \"\";mp_t_default_secondary \"\"");

if (mode.ExecuteCommands != null && mode.ExecuteCommands.Count > 0)
{
foreach (var cmd in mode.ExecuteCommands)
Server.ExecuteCommand(cmd);
}

foreach (var p in Utilities.GetPlayers().Where(p => p != null && p.IsValid && p.PawnIsAlive))
{
p.RemoveWeapons();
Expand Down Expand Up @@ -206,6 +222,7 @@ public void LoadCustomConfigFile()
{
var content = @"
// Things you can customize and add your own cvars
sv_cheats 1
mp_timelimit 30
mp_maxrounds 0
sv_disable_radar 1
Expand All @@ -218,6 +235,7 @@ mp_death_drop_healthshot 0
mp_drop_grenade_enable 0
mp_death_drop_c4 0
mp_death_drop_taser 0
sv_infinite_ammo 0
mp_defuser_allocation 0
mp_solid_teammates 1
mp_give_player_c4 0
Expand All @@ -226,13 +244,14 @@ mp_teamcashawards 0
cash_team_bonus_shorthanded 0
mp_autokick 0
mp_match_restart_delay 10
mp_weapons_allow_zeus 1
//Do not change or delete!!
mp_max_armor 0
mp_weapons_allow_typecount -1
mp_hostages_max 0
mp_weapons_allow_zeus 0
mp_buy_allow_grenades 0
sv_cheats 0
";

using (StreamWriter writer = new StreamWriter(path + "deathmatch.cfg"))
Expand Down
2 changes: 1 addition & 1 deletion source/Deathmatch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.216" />
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.228" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

Expand Down
51 changes: 34 additions & 17 deletions source/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public partial class Deathmatch
[GameEventHandler]
public HookResult OnPlayerConnectFull(EventPlayerConnectFull @event, GameEventInfo info)
{
CCSPlayerController player = @event.Userid;
if (IsPlayerValid(player) && !playerData.ContainsPlayer(player))
var player = @event.Userid;
if (IsPlayerValid(player!) && !playerData.ContainsPlayer(player!))
{
bool IsVIP = AdminManager.PlayerHasPermissions(player, Config.PlayersSettings.VIPFlag);
DeathmatchPlayerData setupPlayerData = new DeathmatchPlayerData
Expand All @@ -32,15 +32,15 @@ public HookResult OnPlayerConnectFull(EventPlayerConnectFull @event, GameEventIn
OpenedMenu = 0,
LastSpawn = null!
};
playerData[player] = setupPlayerData;
playerData[player!] = setupPlayerData;
}
return HookResult.Continue;
}

[GameEventHandler(HookMode.Post)]
public HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info)
{
CCSPlayerController player = @event.Userid;
var player = @event.Userid;
if (player != null && player.IsValid)
GivePlayerWeapons(player, false);

Expand All @@ -52,9 +52,9 @@ public HookResult OnPlayerHurt(EventPlayerHurt @event, GameEventInfo info)
if (@event.Userid == null || !@event.Userid.IsValid)
return HookResult.Continue;

CCSPlayerController attacker = @event.Attacker;
CCSPlayerController player = @event.Userid;
if (ActiveMode != null && playerData.ContainsPlayer(attacker))
var attacker = @event.Attacker;
var player = @event.Userid;
if (ActiveMode != null && attacker != null && playerData.ContainsPlayer(attacker))
{
if (ActiveMode.OnlyHS)
{
Expand All @@ -80,8 +80,8 @@ public HookResult OnPlayerHurt(EventPlayerHurt @event, GameEventInfo info)
[GameEventHandler(HookMode.Pre)]
public HookResult OnPlayerDeath(EventPlayerDeath @event, GameEventInfo info)
{
CCSPlayerController player = @event.Userid;
CCSPlayerController attacker = @event.Attacker;
var player = @event.Userid;
var attacker = @event.Attacker;
info.DontBroadcast = true;

if (player == null || !player.IsValid)
Expand All @@ -107,6 +107,9 @@ public HookResult OnPlayerDeath(EventPlayerDeath @event, GameEventInfo info)
PerformRespawn(player, player.Team, IsBot);
}, TimerFlags.STOP_ON_MAPCHANGE);

if (attacker == null || !attacker.IsValid)
return HookResult.Continue;

if (attacker != player && playerData.ContainsPlayer(attacker) && attacker.PlayerPawn.Value != null)
{
playerData[attacker].KillStreak++;
Expand Down Expand Up @@ -145,7 +148,7 @@ public HookResult OnPlayerDeath(EventPlayerDeath @event, GameEventInfo info)
attacker.PlayerPawn.Value.Health += giveHP;
Utilities.SetStateChanged(attacker.PlayerPawn.Value, "CBaseEntity", "m_iHealth");
}

if (ActiveMode != null && ActiveMode.Armor != 0)
{
string armor = ActiveMode.Armor == 1 ? "item_kevlar" : "item_assaultsuit";
Expand All @@ -168,7 +171,7 @@ public HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info)
[GameEventHandler]
public HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info)
{
g_bIsActiveEditor = false;
IsActiveEditor = false;
return HookResult.Continue;
}
private HookResult OnPlayerRadioMessage(CCSPlayerController? player, CommandInfo info)
Expand All @@ -187,8 +190,15 @@ private HookResult OnTakeDamage(DynamicHook hook)
{
var damageInfo = hook.GetParam<CTakeDamageInfo>(1);

var player = new CCSPlayerController(new CCSPlayerPawn(hook.GetParam<CEntityInstance>(0).Handle).Controller.Value!.Handle);
var attacker = new CCSPlayerController(new CCSPlayerPawn(damageInfo.Attacker.Value!.Handle).Controller.Value!.Handle);
var playerPawn = hook.GetParam<CCSPlayerPawn>(0);
CCSPlayerController? player = null;
if (playerPawn.Controller.Value != null)
player = playerPawn.Controller.Value.As<CCSPlayerController>();

var attackerHandle = damageInfo.Attacker;
CCSPlayerController? attacker = null;
if (attackerHandle.Value != null)
attacker = attackerHandle.Value.As<CCSPlayerController>();

if (player == null || !player.IsValid || attacker == null || !attacker.IsValid || ActiveMode == null)
return HookResult.Continue;
Expand All @@ -198,7 +208,7 @@ private HookResult OnTakeDamage(DynamicHook hook)
damageInfo.Damage = 0;
return HookResult.Continue;
}
if (!ActiveMode.KnifeDamage && damageInfo.Ability.IsValid && (damageInfo.Ability.Value!.DesignerName.Contains("knife") || damageInfo.Ability.Value!.DesignerName.Contains("bayonet")))
if (!ActiveMode.KnifeDamage && damageInfo.Ability.Value != null && damageInfo.Ability.IsValid && (damageInfo.Ability.Value.DesignerName.Contains("knife") || damageInfo.Ability.Value.DesignerName.Contains("bayonet")))
{
attacker.PrintToCenter(Localizer["Hud.KnifeDamageIsDisabled"]);
damageInfo.Damage = 0;
Expand All @@ -208,14 +218,21 @@ private HookResult OnTakeDamage(DynamicHook hook)
private HookResult OnWeaponCanAcquire(DynamicHook hook)
{
var vdata = GetCSWeaponDataFromKeyFunc?.Invoke(-1, hook.GetParam<CEconItemView>(1).ItemDefinitionIndex.ToString());
var player = hook.GetParam<CCSPlayer_ItemServices>(0).Pawn.Value!.Controller.Value!.As<CCSPlayerController>();
var controller = hook.GetParam<CCSPlayer_ItemServices>(0).Pawn.Value.Controller.Value;

if (vdata == null)
return HookResult.Continue;

CCSPlayerController? player = null;
if (controller != null)
player = controller.As<CCSPlayerController>();

if (player == null || !player.IsValid || !player.PawnIsAlive)
return HookResult.Continue;

if (hook.GetParam<AcquireMethod>(2) == AcquireMethod.PickUp)
{
if (!AllowedPrimaryWeaponsList.Contains(vdata!.Name!) && !AllowedSecondaryWeaponsList.Contains(vdata.Name))
if (!AllowedPrimaryWeaponsList.Contains(vdata.Name) && !AllowedSecondaryWeaponsList.Contains(vdata.Name))
{
if (vdata.Name.Contains("knife") || vdata.Name.Contains("bayonet"))
return HookResult.Continue;
Expand All @@ -241,7 +258,7 @@ private HookResult OnWeaponCanAcquire(DynamicHook hook)
return HookResult.Stop;
}

if (!AllowedPrimaryWeaponsList.Contains(vdata!.Name!) && !AllowedSecondaryWeaponsList.Contains(vdata.Name))
if (!AllowedPrimaryWeaponsList.Contains(vdata.Name) && !AllowedSecondaryWeaponsList.Contains(vdata.Name))
{
if (!player.IsBot)
{
Expand Down
Loading

0 comments on commit 16b80b1

Please sign in to comment.