Skip to content

Commit

Permalink
v0.0.4
Browse files Browse the repository at this point in the history
added trails
player alive check (the models could spawn before player joined a team on connect)
  • Loading branch information
exkludera committed Jul 7, 2024
1 parent 115be8e commit 21fc696
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 21 deletions.
9 changes: 5 additions & 4 deletions Equipments/Equipments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ namespace Equipments;
public class Equipments : BasePlugin, IPluginConfig<EquipmentsConfig>
{
public override string ModuleName => "Equipments";
public override string ModuleVersion => "0.0.3";
public override string ModuleVersion => "0.0.4";
public override string ModuleAuthor => "exkludera";

public EquipmentsConfig Config { get; set; } = new EquipmentsConfig();
public List<Equipments_Items> GlobalEquipmentsItems { get; set; } = [];
public List<Equipments_Item_Types> GlobalEquipmentsItemTypes { get; set; } = [];

public int GlobalTickrate { get; set; } = 0;
public static Equipments Instance { get; set; } = new();

public override void Load(bool hotReload)
Expand All @@ -29,8 +29,9 @@ public override void Load(bool hotReload)
Command.Load();

Item_Hat.OnPluginStart();
Item_Wing.OnPluginStart();
Item_Wings.OnPluginStart();
Item_Backpack.OnPluginStart();
Item_Trail.OnPluginStart();

if (hotReload)
{
Expand All @@ -48,7 +49,7 @@ public override void Unload(bool hotReload)

public void OnConfigParsed(EquipmentsConfig config)
{
if (string.IsNullOrEmpty(config.Database.Host) || string.IsNullOrEmpty(config.Database.Name) || string.IsNullOrEmpty(config.Database.User))
if (string.IsNullOrEmpty(config.Database.Host) || string.IsNullOrEmpty(config.Database.DBName) || string.IsNullOrEmpty(config.Database.User))
throw new Exception("Setup Database in config!");

Task.Run(async () => {
Expand Down
7 changes: 6 additions & 1 deletion Equipments/Items/backpack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ public static bool OnUnequip(CCSPlayerController player, Dictionary<string, stri
public static void Equip(CCSPlayerController player)
{
UnEquip(player);

Instance.AddTimer(0.1f, () => {

if (!Functions.PlayerAlive(player))
return;

Equipments_Items? playerItems = Instance.GlobalEquipmentsItems.FirstOrDefault(p => p.SteamID == player.SteamID && p.Type == "backpack");
if (playerItems == null) return;

Expand All @@ -63,7 +68,7 @@ public static void UnEquip(CCSPlayerController player)

public static void CreateItem(CCSPlayerController player, string itemName)
{
var entity = Utilities.CreateEntityByName<CBaseModelEntity>("prop_dynamic_override");
var entity = Utilities.CreateEntityByName<CDynamicProp>("prop_dynamic_override");

Instance.AddTimer(0.1f, () => {
entity!.Globalname = $"{player.SteamID}({itemName})#{Functions.RandomString(6)}";
Expand Down
7 changes: 6 additions & 1 deletion Equipments/Items/hat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ public static bool OnUnequip(CCSPlayerController player, Dictionary<string, stri
public static void Equip(CCSPlayerController player)
{
UnEquip(player);

Instance.AddTimer(0.1f, () => {

if (!Functions.PlayerAlive(player))
return;

Equipments_Items? playerItems = Instance.GlobalEquipmentsItems.FirstOrDefault(p => p.SteamID == player.SteamID && p.Type == "hat");
if (playerItems == null) return;

Expand All @@ -63,7 +68,7 @@ public static void UnEquip(CCSPlayerController player)

public static void CreateItem(CCSPlayerController player, string itemName)
{
var entity = Utilities.CreateEntityByName<CBaseModelEntity>("prop_dynamic_override");
var entity = Utilities.CreateEntityByName<CDynamicProp>("prop_dynamic_override");

Instance.AddTimer(0.1f, () => {
entity!.Globalname = $"{player.SteamID}({itemName})#{Functions.RandomString(6)}";
Expand Down
93 changes: 93 additions & 0 deletions Equipments/Items/trail.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Utils;
using System.Globalization;

using static Equipments.Equipments;
using static EquipmentsAPI.Equipments;

namespace Equipments;

public static class Item_Trail
{
private static bool trailExists = false;
public static void OnPluginStart()
{
Item.RegisterType("trail", OnServerPrecacheResources, OnEquip, OnUnequip, true, null);
if (Item.GetItemsByType("trail").Count > 0) trailExists = true;
}
public static void OnServerPrecacheResources(ResourceManifest manifest)
{
List<KeyValuePair<string, Dictionary<string, string>>> items = Item.GetItemsByType("trail");

foreach (KeyValuePair<string, Dictionary<string, string>> item in items)
{
manifest.AddResource(item.Value["uniqueid"]);
}
}
public static bool OnEquip(CCSPlayerController player, Dictionary<string, string> item)
{
return true;
}
public static bool OnUnequip(CCSPlayerController player, Dictionary<string, string> item)
{
return true;
}

public static void OnTick(CCSPlayerController player)
{
if (!trailExists)
return;

Equipments_Items? playertrail = Instance.GlobalEquipmentsItems.FirstOrDefault(p => p.SteamID == player.SteamID && p.Type == "trail");
if (playertrail == null) return;

Dictionary<string, string>? itemdata = Item.GetItem(playertrail.Type, playertrail.UniqueId);
if (itemdata == null) return;

Vector? absorigin = player.PlayerPawn.Value?.AbsOrigin;
if (absorigin == null) return;

float lifetime = 1.3f;

if (itemdata.TryGetValue("lifetime", out string? ltvalue) && float.TryParse(ltvalue, CultureInfo.InvariantCulture, out float lt))
lifetime = lt;

CreateParticle(absorigin, playertrail.UniqueId, lifetime, itemdata, player);
}

public static void CreateParticle(Vector absOrigin, string effectName, float lifetime, Dictionary<string, string> itemdata, CCSPlayerController player)
{
CParticleSystem? entity = Utilities.CreateEntityByName<CParticleSystem>("info_particle_system");

if (entity == null || !entity.IsValid) return;

if (!itemdata.TryGetValue("acceptInputValue", out string? acceptinputvalue) || string.IsNullOrEmpty(acceptinputvalue))
acceptinputvalue = "Start";

QAngle angle = new();

if (!itemdata.TryGetValue("angleValue", out string? angleValue) || string.IsNullOrEmpty(angleValue))
angle.X = 90;
else
{
string[] angleValues = angleValue.Split(' ');

angle.X = int.Parse(angleValues[0]);
angle.Y = int.Parse(angleValues[0]);
angle.Z = int.Parse(angleValues[0]);
}

entity.EffectName = effectName;
entity.DispatchSpawn();
entity.Teleport(absOrigin, angle, new Vector());
entity.AcceptInput(acceptinputvalue!);
entity.AcceptInput("FollowEntity", player.PlayerPawn?.Value!, player.PlayerPawn?.Value!, "!activator");

Instance.AddTimer(lifetime, () =>
{
if (entity != null && entity.IsValid)
entity.Remove();
});
}
}
15 changes: 10 additions & 5 deletions Equipments/Items/wings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@

namespace Equipments;

public static class Item_Wing
public static class Item_Wings
{
private static Dictionary<ulong, CBaseModelEntity> Equipment = new();

public static void OnPluginStart()
{
Item.RegisterType("wing", OnServerPrecacheResources, OnEquip, OnUnequip, true, null);
Item.RegisterType("wings", OnServerPrecacheResources, OnEquip, OnUnequip, true, null);
Instance.RegisterEventHandler<EventPlayerSpawn>(OnPlayerSpawn);
}
public static void OnServerPrecacheResources(ResourceManifest manifest)
{
List<KeyValuePair<string, Dictionary<string, string>>> items = Item.GetItemsByType("wing");
List<KeyValuePair<string, Dictionary<string, string>>> items = Item.GetItemsByType("wings");

foreach (KeyValuePair<string, Dictionary<string, string>> item in items)
{
Expand All @@ -39,8 +39,13 @@ public static bool OnUnequip(CCSPlayerController player, Dictionary<string, stri
public static void Equip(CCSPlayerController player)
{
UnEquip(player);

Instance.AddTimer(0.1f, () => {
Equipments_Items? playerItems = Instance.GlobalEquipmentsItems.FirstOrDefault(p => p.SteamID == player.SteamID && p.Type == "wing");

if (!Functions.PlayerAlive(player))
return;

Equipments_Items? playerItems = Instance.GlobalEquipmentsItems.FirstOrDefault(p => p.SteamID == player.SteamID && p.Type == "wings");
if (playerItems == null) return;

Dictionary<string, string>? itemdata = Item.GetItem(playerItems.Type, playerItems.UniqueId);
Expand All @@ -63,7 +68,7 @@ public static void UnEquip(CCSPlayerController player)

public static void CreateItem(CCSPlayerController player, string itemName)
{
var entity = Utilities.CreateEntityByName<CBaseModelEntity>("prop_dynamic_override");
var entity = Utilities.CreateEntityByName<CDynamicProp>("prop_dynamic_override");

Instance.AddTimer(0.1f, () => {
entity!.Globalname = $"{player.SteamID}({itemName})#{Functions.RandomString(6)}";
Expand Down
20 changes: 20 additions & 0 deletions Equipments/Player/events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ public static void Unload()
{
Instance.RemoveListener<OnServerPrecacheResources>(OnServerPrecacheResources);
Instance.RemoveListener<OnClientAuthorized>(OnClientAuthorized);
Instance.RemoveListener<OnTick>(OnTick);
}

public static void Load()
{
Instance.RegisterListener<OnServerPrecacheResources>(OnServerPrecacheResources);
Instance.RegisterListener<OnClientAuthorized>(OnClientAuthorized);
Instance.RegisterEventHandler<EventPlayerDisconnect>(OnPlayerDisconnect);
Instance.RegisterListener<OnTick>(OnTick);
}

public static void OnServerPrecacheResources(ResourceManifest manifest)
Expand All @@ -30,6 +32,24 @@ public static void OnServerPrecacheResources(ResourceManifest manifest)
});
}

public static void OnTick()
{
Instance.GlobalTickrate++;

if (Instance.GlobalTickrate % 10 != 0)
return;

Instance.GlobalTickrate = 0;

foreach (CCSPlayerController player in Utilities.GetPlayers())
{
if (!Functions.PlayerAlive(player))
continue;

Item_Trail.OnTick(player);
}
}

private static void OnClientAuthorized(int playerSlot, SteamID steamId)
{
CCSPlayerController? player = Utilities.GetPlayerFromSlot(playerSlot);
Expand Down
14 changes: 7 additions & 7 deletions Equipments/Plugin/config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ public class EquipmentsConfig : BasePluginConfig
{
public class Config_Database
{
public string Host { get; set; } = string.Empty;
public uint Port { get; set; } = 3306;
public string User { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public string Host { get; set; } = "localhost";
public string User { get; set; } = "username";
public string Password { get; set; } = "password";
public string DBName { get; set; } = "database";
public string DBTable { get; set; } = "equipments";
public uint Port { get; set; } = 3306;
}
[JsonPropertyName("Database")] public Config_Database Database { get; set; } = new Config_Database();

public class Config_Command
{
public string[] OpenMenu { get; set; } = ["equipments", "equipment", "hats", "wings"];
public string[] OpenMenu { get; set; } = ["equipments", "equipment"];
public string[] ResetPlayer { get; set; } = ["equipments-resetplayer"];
public string[] ResetDatabase { get; set; } = ["equipments-resetdatabase"];
}
Expand All @@ -27,7 +27,7 @@ public class Config_Command
public class Config_Settings
{
public string Flag { get; set; } = "@css/reservation";
public bool HudMenu { get; set; }
public bool HudMenu { get; set; } = true;
}
[JsonPropertyName("Settings")] public Config_Settings Settings { get; set; } = new Config_Settings();

Expand Down
2 changes: 1 addition & 1 deletion Equipments/Plugin/database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static async Task CreateDatabaseAsync(EquipmentsConfig config)
MySqlConnectionStringBuilder builder = new()
{
Server = config.Database.Host,
Database = config.Database.Name,
Database = config.Database.DBName,
UserID = config.Database.User,
Password = config.Database.Password,
Port = config.Database.Port,
Expand Down
8 changes: 8 additions & 0 deletions Equipments/Plugin/functions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ static public void PrintToChatMessage(this CCSPlayerController player, string me
player.PrintToChat($" {Instance.Localizer["prefix"]} {Instance.Localizer[message, args]}");
}

public static bool PlayerAlive(CCSPlayerController player)
{
if (player == null || !player.IsValid || player.Pawn == null || !player.PlayerPawn.IsValid || !player.PawnIsAlive)
return false;

return true;
}

public static string RandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
Expand Down
5 changes: 3 additions & 2 deletions Equipments/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

"menu<title>": "Equipments",
"menu<back>": "-> Back",

"menu_item<hat>": "Hats",
"menu_item<wing>": "Wings",
"menu_item<wings>": "Wings",
"menu_item<backpack>": "Backpacks",
"menu_item<other>": "Other",
"menu_item<trail>": "Trails",

"No matching client": "{white}There is no matching client",
"More than one client matched": "{white}There are more than one client matched",
Expand Down

0 comments on commit 21fc696

Please sign in to comment.