Skip to content

Commit

Permalink
Merge pull request #63 from schwarper/Playerskin-update
Browse files Browse the repository at this point in the history
Playerskin update
  • Loading branch information
schwarper committed Jun 24, 2024
2 parents 7877553 + a3626e0 commit 2fec184
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 53 deletions.
2 changes: 1 addition & 1 deletion Store/src/config/config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ public class Config_Setting
[JsonPropertyName("Menu")] public Config_Menu Menu { get; set; } = new Config_Menu();
[JsonPropertyName("Settings")] public Config_Setting Settings { get; set; } = new Config_Setting();
[JsonPropertyName("Items")] public Dictionary<string, Dictionary<string, Dictionary<string, string>>> Items { get; set; } = [];
}
}
114 changes: 62 additions & 52 deletions Store/src/item/items/playerskin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ public static bool OnEquip(CCSPlayerController player, Dictionary<string, string
return false;
}

SetPlayerModel(player, item["uniqueid"], item["disable_leg"] is "true" or "1", int.Parse(item["slot"]));
if (player.TeamNum == int.Parse(item["slot"]))
{
player.ChangeModelDelay(item["uniqueid"], item["disable_leg"] is "true" or "1", int.Parse(item["slot"]));
}

return true;
}
Expand All @@ -71,11 +74,17 @@ public static bool OnUnequip(CCSPlayerController player, Dictionary<string, stri

if (player.TeamNum == int.Parse(item["slot"]))
{
SetDefaultModel(player);
var defaultModel = GetDefaultModel(player);

if (defaultModel.HasValue)
{
player.ChangeModelDelay(defaultModel.Value.modelname, defaultModel.Value.disableleg, player.TeamNum);
}
}

return true;
}

public static HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info)
{
CCSPlayerController? player = @event.Userid;
Expand All @@ -90,10 +99,12 @@ public static HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo in
return HookResult.Continue;
}

Instance.AddTimer(0.1f, () =>
var modelData = GetModel(player, player.TeamNum);

if (modelData.HasValue)
{
ChangeModel(player, player.TeamNum);
});
player.ChangeModelDelay(modelData.Value.modelname, modelData.Value.disableleg, player.TeamNum);
}

return HookResult.Continue;
}
Expand All @@ -114,7 +125,12 @@ public static HookResult OnPlayerTeam(EventPlayerTeam @event, GameEventInfo info

if (@event.Team == 2 || @event.Team == 3)
{
ChangeModel(player, player.TeamNum);
var modelDatas = GetModel(player, @event.Team);

if (modelDatas.HasValue)
{
player.PlayerPawn.Value!.ChangeModel(modelDatas.Value.modelname, modelDatas.Value.disableleg);
}
}

return HookResult.Continue;
Expand All @@ -125,32 +141,6 @@ public static HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info
ForceModelDefault = false;
return HookResult.Continue;
}
public static void SetDefaultModel(CCSPlayerController player)
{
string[] modelsArray = player.Team == CsTeam.CounterTerrorist ? Instance.Config.DefaultModels.CT : Instance.Config.DefaultModels.T;
int maxIndex = modelsArray.Length;

if (maxIndex > 0)
{
int randomnumber = Instance.Random.Next(0, maxIndex - 1);

string model = modelsArray[randomnumber];

SetPlayerModel(player, model, Instance.Config.Settings.DefaultModelDisableLeg, player.TeamNum);
}
}
private static void SetPlayerModel(CCSPlayerController player, string model, bool disable_leg, int slotNumber)
{
float apply_delay = float.Max(Instance.Config.Settings.ApplyPlayerskinDelay, 0.1f);

Instance.AddTimer(apply_delay, () =>
{
if (player.IsValid && player.TeamNum == slotNumber && player.PawnIsAlive)
{
player.PlayerPawn.Value?.ChangeModel(model, disable_leg);
}
}, TimerFlags.STOP_ON_MAPCHANGE);
}

private static void Command_Model0(CCSPlayerController? player, CommandInfo info)
{
Expand All @@ -163,7 +153,12 @@ private static void Command_Model0(CCSPlayerController? player, CommandInfo info

foreach (CCSPlayerController target in Utilities.GetPlayers())
{
SetDefaultModel(target);
var modelDatas = GetDefaultModel(target);

if (modelDatas.HasValue)
{
target.PlayerPawn.Value!.ChangeModel(modelDatas.Value.modelname, modelDatas.Value.disableleg);
}
}

Server.PrintToChatAll(Instance.Config.Tag + Instance.Localizer["css_model0", player?.PlayerName ?? Instance.Localizer["Console"]]);
Expand All @@ -182,18 +177,11 @@ private static void Command_Model1(CCSPlayerController? player, CommandInfo info

foreach (CCSPlayerController target in Utilities.GetPlayers())
{
Store_Equipment? item = Instance.GlobalStorePlayerEquipments.FirstOrDefault(p => p.SteamID == target.SteamID && p.Type == "playerskin" && p.Slot == target.TeamNum);
var modelDatas = GetModel(target, target.TeamNum);

if (item != null)
if (modelDatas.HasValue)
{
Dictionary<string, string>? itemdata = Item.GetItem(item.Type, item.UniqueId);

if (itemdata == null)
{
continue;
}

SetPlayerModel(target, item.UniqueId, itemdata["disable_leg"] is "true" or "1", item.Slot);
target.PlayerPawn.Value!.ChangeModel(modelDatas.Value.modelname, modelDatas.Value.disableleg);
}
}

Expand All @@ -202,24 +190,46 @@ private static void Command_Model1(CCSPlayerController? player, CommandInfo info
ForceModelDefault = false;
}

private static void ChangeModel(CCSPlayerController player, int teamnum)
private static (string modelname, bool disableleg)? GetModel(CCSPlayerController player, int teamnum)
{
Store_Equipment? item = Instance.GlobalStorePlayerEquipments.FirstOrDefault(p => p.SteamID == player.SteamID && p.Type == "playerskin" && p.Slot == teamnum);

if (item == null || ForceModelDefault)
{
SetDefaultModel(player);
return GetDefaultModel(player);
}
else
{
Dictionary<string, string>? itemdata = Item.GetItem(item.Type, item.UniqueId);
return GetStoreModel(player, item);
}
}

if (itemdata == null)
{
return;
}
private static (string modelname, bool disableleg)? GetDefaultModel(CCSPlayerController player)
{
string[] modelsArray = player.Team == CsTeam.CounterTerrorist ? Instance.Config.DefaultModels.CT : Instance.Config.DefaultModels.T;
int maxIndex = modelsArray.Length;

SetPlayerModel(player, item.UniqueId, itemdata["disable_leg"] is "true" or "1", item.Slot);
if (maxIndex > 0)
{
int randomnumber = Instance.Random.Next(0, maxIndex - 1);

string model = modelsArray[randomnumber];

return (model, Instance.Config.Settings.DefaultModelDisableLeg);
}

return null;
}

private static (string modelname, bool disableleg)? GetStoreModel(CCSPlayerController player, Store_Equipment item)
{
Dictionary<string, string>? itemdata = Item.GetItem(item.Type, item.UniqueId);

if (itemdata == null)
{
return null;
}

return (item.UniqueId, itemdata["disable_leg"] is "true" or "1");
}
}
}
15 changes: 15 additions & 0 deletions Store/src/playerutils/playerutils.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Translations;
using CounterStrikeSharp.API.Modules.Timers;
using System.Drawing;
using System.Text;
using static Store.Store;
Expand All @@ -18,6 +19,20 @@ static public void PrintToChatMessage(this CCSPlayerController player, string me
player.PrintToChat(builder.ToString());
}
}

static public void ChangeModelDelay(this CCSPlayerController player, string model, bool disableleg, int slotNumber)
{
float apply_delay = float.Max(Instance.Config.Settings.ApplyPlayerskinDelay, 0.1f);

Instance.AddTimer(apply_delay, () =>
{
if (player.IsValid && player.TeamNum == slotNumber && player.PawnIsAlive)
{
player.PlayerPawn.Value?.ChangeModel(model, disableleg);
}
}, TimerFlags.STOP_ON_MAPCHANGE);
}

static public void ChangeModel(this CCSPlayerPawn pawn, string model, bool disableleg)
{
if (model == string.Empty)
Expand Down

0 comments on commit 2fec184

Please sign in to comment.