Skip to content

Commit

Permalink
Config option to limit spawn counts
Browse files Browse the repository at this point in the history
  • Loading branch information
joker-119 committed Feb 5, 2021
1 parent d889b06 commit 57f7e46
Show file tree
Hide file tree
Showing 24 changed files with 80 additions and 60 deletions.
2 changes: 1 addition & 1 deletion CustomItems/API/CustomGrenade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace CustomItems.API
{
public abstract class CustomGrenade : CustomItem
{
public CustomGrenade(ItemType type, int itemId) : base(type, itemId)
protected CustomGrenade(ItemType type, int itemId) : base(type, itemId)
{
}

Expand Down
35 changes: 14 additions & 21 deletions CustomItems/API/CustomItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ protected CustomItem(ItemType type, int itemId)
Id = itemId;
}

public int Id { get; set; }
public ItemType ItemType { get; set; }
public abstract string Name { get; set; }
public abstract string Description { get; set; }

public virtual int SpawnLimit { get; set; } = 0;
public virtual Dictionary<SpawnLocation, float> SpawnLocations { get; set; }

protected virtual void LoadEvents(){}
Expand Down Expand Up @@ -154,15 +154,9 @@ public virtual void GiveItem(Player player)

protected bool CheckItem(Pickup pickup) => ItemPickups.Contains(pickup);
protected bool CheckItem(Inventory.SyncItemInfo item) => ItemIds.Contains(item.uniq);

public static Vector3 RoomLocation(string roomName)
{
foreach (Room room in Map.Rooms)
if (room.Name == roomName)
return room.Position;

return Vector3.zero;
}

public int Id { get; set; }
public ItemType ItemType { get; set; }

public virtual void Init()
{
Expand Down Expand Up @@ -221,17 +215,16 @@ private void CheckAndUnloadSubclassEvent()

private void OnAddingClass(AddClassEventArgs ev)
{
if (Plugin.Singleton.Config.SubclassItems.ContainsKey(ev.Subclass.Name))
if (!Plugin.Singleton.Config.SubclassItems.ContainsKey(ev.Subclass.Name))
return;

foreach ((CustomItem item, float chance) in Plugin.Singleton.Config.SubclassItems[ev.Subclass.Name])
{
foreach (Tuple<CustomItem, float> item in Plugin.Singleton.Config.SubclassItems[ev.Subclass.Name])
{
if (item.Item1.Name == Name)
{
int r = Plugin.Singleton.Rng.Next(100);
if (r < item.Item2)
Timing.CallDelayed(1.5f, () => GiveItem(ev.Player));
}
}
if (item.Name != Name)
continue;
int r = Plugin.Singleton.Rng.Next(100);
if (r < chance)
Timing.CallDelayed(1.5f, () => GiveItem(ev.Player));
}
}

Expand Down
2 changes: 1 addition & 1 deletion CustomItems/API/CustomWeapon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@ public override void GiveItem(Player player)
ItemGiven(player);
}

protected void Reload(Player player) => player.ReferenceHub.weaponManager.RpcReload(player.ReferenceHub.weaponManager.curWeapon);
protected static void Reload(Player player) => player.ReferenceHub.weaponManager.RpcReload(player.ReferenceHub.weaponManager.curWeapon);
}
}
3 changes: 3 additions & 0 deletions CustomItems/Configs/ItemConfigs/EmpGrenadeConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@ public class EmpGrenadeConfig

[Description("The name of this item shown to players when they obtain it.")]
public string Name { get; set; } = "EM-119";

[Description("How many of this item are allowed to naturally spawn on the map when a round starts. 0 = unlimited")]
public int SpawnLimit { get; set; } = 1;
}
}
3 changes: 3 additions & 0 deletions CustomItems/Configs/ItemConfigs/GrenadeLauncherConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,8 @@ public class GrenadeLauncherConfig

[Description("The name of this item shown to players when they obtain it.")]
public string Name { get; set; } = "GL-119";

[Description("How many of this item are allowed to naturally spawn on the map when a round starts. 0 = unlimited")]
public int SpawnLimit { get; set; } = 1;
}
}
3 changes: 3 additions & 0 deletions CustomItems/Configs/ItemConfigs/ImplosionGrenadeConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@ public class ImplosionGrenadeConfig

[Description("The name of this item shown to players when they obtain it.")]
public string Name { get; set; } = "IG-119";

[Description("How many of this item are allowed to naturally spawn on the map when a round starts. 0 = unlimited")]
public int SpawnLimit { get; set; } = 1;
}
}
3 changes: 3 additions & 0 deletions CustomItems/Configs/ItemConfigs/LethalInjectionConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ public class LethalInjectionConfig

[Description("The name of this item shown to players when they obtain it.")]
public string Name { get; set; } = "LJ-119";

[Description("How many of this item are allowed to naturally spawn on the map when a round starts. 0 = unlimited")]
public int SpawnLimit { get; set; } = 1;
}
}
3 changes: 3 additions & 0 deletions CustomItems/Configs/ItemConfigs/LuckCoinConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ public class LuckCoinConfig

[Description("The name of this item shown to players when they obtain it.")]
public string Name { get; set; } = "LC-119";

[Description("How many of this item are allowed to naturally spawn on the map when a round starts. 0 = unlimited")]
public int SpawnLimit { get; set; } = 1;
}
}
3 changes: 3 additions & 0 deletions CustomItems/Configs/ItemConfigs/MediGunConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,8 @@ public class MediGunConfig

[Description("The name of this item shown to players when they obtain it.")]
public string Name { get; set; } = "MG-119";

[Description("How many of this item are allowed to naturally spawn on the map when a round starts. 0 = unlimited")]
public int SpawnLimit { get; set; } = 1;
}
}
3 changes: 3 additions & 0 deletions CustomItems/Configs/ItemConfigs/Scp127Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@ public class Scp127Config

[Description("The name of this item shown to players when they obtain it.")]
public string Name { get; set; } = "SCP-127";

[Description("How many of this item are allowed to naturally spawn on the map when a round starts. 0 = unlimited")]
public int SpawnLimit { get; set; } = 1;
}
}
3 changes: 3 additions & 0 deletions CustomItems/Configs/ItemConfigs/ShotgunConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@ public class ShotgunConfig

[Description("The name of this item shown to players when they obtain it.")]
public string Name { get; set; } = "SG-119";

[Description("How many of this item are allowed to naturally spawn on the map when a round starts. 0 = unlimited")]
public int SpawnLimit { get; set; } = 1;
}
}
3 changes: 3 additions & 0 deletions CustomItems/Configs/ItemConfigs/SniperRifleConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@ public class SniperRifleConfig

[Description("The name of this item shown to players when they obtain it.")]
public string Name { get; set; } = "SR-119";

[Description("How many of this item are allowed to naturally spawn on the map when a round starts. 0 = unlimited")]
public int SpawnLimit { get; set; } = 1;
}
}
3 changes: 3 additions & 0 deletions CustomItems/Configs/ItemConfigs/TranqGunConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,8 @@ public class TranqGunConfig

[Description("The name of this item shown to players when they obtain it.")]
public string Name { get; set; } = "TG-119";

[Description("How many of this item are allowed to naturally spawn on the map when a round starts. 0 = unlimited")]
public int SpawnLimit { get; set; } = 1;
}
}
23 changes: 13 additions & 10 deletions CustomItems/EventHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,20 @@ public void OnRoundStart()
{
foreach (CustomItem item in plugin.ItemManagers)
{
if (item.SpawnLocations != null)
if (item.SpawnLocations == null)
continue;

int count = 0;

foreach (KeyValuePair<SpawnLocation, float> spawn in item.SpawnLocations)
{
foreach (KeyValuePair<SpawnLocation, float> spawn in item.SpawnLocations)
{
Log.Debug($"Attempting to spawn {item.Name} at {spawn.Key}", plugin.Config.Debug);
if (plugin.Rng.Next(100) <= spawn.Value)
{
item.SpawnItem(spawn.Key.TryGetLocation());
Log.Debug($"Spawned {item.Name} at {spawn.Key}", plugin.Config.Debug);
}
}
Log.Debug($"Attempting to spawn {item.Name} at {spawn.Key}", plugin.Config.Debug);
if (plugin.Rng.Next(100) >= spawn.Value || (item.SpawnLimit > 0 && count >= item.SpawnLimit))
continue;

count++;
item.SpawnItem(spawn.Key.TryGetLocation());
Log.Debug($"Spawned {item.Name} at {spawn.Key}", plugin.Config.Debug);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion CustomItems/Items/EmpGrenade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public EmpGrenade(ItemType type, int itemId) : base(type, itemId)

public override string Name { get; set; } = Plugin.Singleton.Config.ItemConfigs.EmpCfg.Name;
public override Dictionary<SpawnLocation, float> SpawnLocations { get; set; } = Plugin.Singleton.Config.ItemConfigs.EmpCfg.SpawnLocations;

public override string Description { get; set; } = Plugin.Singleton.Config.ItemConfigs.EmpCfg.Description;
public override int SpawnLimit { get; set; } = Plugin.Singleton.Config.ItemConfigs.EmpCfg.SpawnLimit;

protected override bool ExplodeOnCollision { get; set; } = Plugin.Singleton.Config.ItemConfigs.EmpCfg.ExplodeOnCollision;
protected override float FuseTime { get; set; } = Plugin.Singleton.Config.ItemConfigs.EmpCfg.FuseDuration;
Expand Down
2 changes: 1 addition & 1 deletion CustomItems/Items/GrenadeLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public GrenadeLauncher(ItemType type, int clipSize, int itemId) : base(type, cli
public override string Name { get; set; } = Plugin.Singleton.Config.ItemConfigs.GlCfg.Name;
public override Dictionary<SpawnLocation, float> SpawnLocations { get; set; } =
Plugin.Singleton.Config.ItemConfigs.GlCfg.SpawnLocations;

public override string Description { get; set; } = Plugin.Singleton.Config.ItemConfigs.GlCfg.Description;
public override int SpawnLimit { get; set; } = Plugin.Singleton.Config.ItemConfigs.GlCfg.SpawnLimit;

protected override void LoadEvents()
{
Expand Down
5 changes: 2 additions & 3 deletions CustomItems/Items/ImplosionGrenade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ public ImplosionGrenade(ItemType type, int itemId) : base(type, itemId)
}

public override string Name { get; set; } = Plugin.Singleton.Config.ItemConfigs.ImpCfg.Name;
public override Dictionary<SpawnLocation, float> SpawnLocations { get; set; } =
Plugin.Singleton.Config.ItemConfigs.ImpCfg.SpawnLocations;

public override Dictionary<SpawnLocation, float> SpawnLocations { get; set; } = Plugin.Singleton.Config.ItemConfigs.ImpCfg.SpawnLocations;
public override string Description { get; set; } = Plugin.Singleton.Config.ItemConfigs.ImpCfg.Description;
public override int SpawnLimit { get; set; } = Plugin.Singleton.Config.ItemConfigs.ImpCfg.SpawnLimit;

protected override bool ExplodeOnCollision { get; set; } = true;

Expand Down
5 changes: 2 additions & 3 deletions CustomItems/Items/LethalInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ public LethalInjection(ItemType type, int itemId) : base(type, itemId)
}

public override string Name { get; set; } = Plugin.Singleton.Config.ItemConfigs.LethalCfg.Name;
public override Dictionary<SpawnLocation, float> SpawnLocations { get; set; } =
Plugin.Singleton.Config.ItemConfigs.LethalCfg.SpawnLocations;

public override Dictionary<SpawnLocation, float> SpawnLocations { get; set; } = Plugin.Singleton.Config.ItemConfigs.LethalCfg.SpawnLocations;
public override string Description { get; set; } = Plugin.Singleton.Config.ItemConfigs.LethalCfg.Description;
public override int SpawnLimit { get; set; } = Plugin.Singleton.Config.ItemConfigs.LethalCfg.SpawnLimit;

protected override void LoadEvents()
{
Expand Down
5 changes: 2 additions & 3 deletions CustomItems/Items/LuckyCoin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ public LuckyCoin(ItemType type, int itemId) : base(type, itemId)
}

public override string Name { get; set; } = Plugin.Singleton.Config.ItemConfigs.LuckyCfg.Name;
public override Dictionary<SpawnLocation, float> SpawnLocations { get; set; } =
Plugin.Singleton.Config.ItemConfigs.LuckyCfg.SpawnLocations;

public override Dictionary<SpawnLocation, float> SpawnLocations { get; set; } = Plugin.Singleton.Config.ItemConfigs.LuckyCfg.SpawnLocations;
public override string Description { get; set; } = Plugin.Singleton.Config.ItemConfigs.LuckyCfg.Description;
public override int SpawnLimit { get; set; } = Plugin.Singleton.Config.ItemConfigs.LuckyCfg.SpawnLimit;

protected override void LoadEvents()
{
Expand Down
5 changes: 2 additions & 3 deletions CustomItems/Items/MediGun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ public MediGun(ItemType type, int clipSize, int itemId) : base(type, clipSize, i
}

public override string Name { get; set; } = Plugin.Singleton.Config.ItemConfigs.MediCfg.Name;
public override Dictionary<SpawnLocation, float> SpawnLocations { get; set; } =
Plugin.Singleton.Config.ItemConfigs.MediCfg.SpawnLocations;

public override Dictionary<SpawnLocation, float> SpawnLocations { get; set; } = Plugin.Singleton.Config.ItemConfigs.MediCfg.SpawnLocations;
public override string Description { get; set; } = Plugin.Singleton.Config.ItemConfigs.MediCfg.Description;
public override int SpawnLimit { get; set; } = Plugin.Singleton.Config.ItemConfigs.MediCfg.SpawnLimit;

protected override void LoadEvents()
{
Expand Down
12 changes: 6 additions & 6 deletions CustomItems/Items/Scp-127.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ public Scp127(ItemType type, int clipSize, int itemId) : base(type, clipSize, it
}

public override string Name { get; set; } = Plugin.Singleton.Config.ItemConfigs.Scp127Cfg.Name;
public override Dictionary<SpawnLocation, float> SpawnLocations { get; set; } =
Plugin.Singleton.Config.ItemConfigs.Scp127Cfg.SpawnLocations;
private List<CoroutineHandle> Coroutines { get; } = new List<CoroutineHandle>();

public override Dictionary<SpawnLocation, float> SpawnLocations { get; set; } = Plugin.Singleton.Config.ItemConfigs.Scp127Cfg.SpawnLocations;
public override string Description { get; set; } = Plugin.Singleton.Config.ItemConfigs.Scp127Cfg.Description;
public override int SpawnLimit { get; set; } = Plugin.Singleton.Config.ItemConfigs.Scp127Cfg.SpawnLimit;

protected override void LoadEvents()
{
Expand Down Expand Up @@ -52,7 +50,9 @@ private void OnPickingUp(PickingUpItemEventArgs ev)
Coroutines.Add(Timing.RunCoroutine(DoInventoryRegeneration(ev.Player)));
}

IEnumerator<float> DoInventoryRegeneration(Player player)
private List<CoroutineHandle> Coroutines { get; } = new List<CoroutineHandle>();

private IEnumerator<float> DoInventoryRegeneration(Player player)
{
for (;;)
{
Expand Down Expand Up @@ -80,7 +80,7 @@ IEnumerator<float> DoInventoryRegeneration(Player player)
}
}

IEnumerator<float> DoAmmoRegeneration()
private IEnumerator<float> DoAmmoRegeneration()
{
for (;;)
{
Expand Down
5 changes: 2 additions & 3 deletions CustomItems/Items/Shotgun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ public Shotgun(ItemType type, int clipSize, int itemId) : base(type, clipSize, i
}

public override string Name { get; set; } = Plugin.Singleton.Config.ItemConfigs.ShotgunCfg.Name;
public override Dictionary<SpawnLocation, float> SpawnLocations { get; set; } =
Plugin.Singleton.Config.ItemConfigs.ShotgunCfg.SpawnLocations;

public override Dictionary<SpawnLocation, float> SpawnLocations { get; set; } = Plugin.Singleton.Config.ItemConfigs.ShotgunCfg.SpawnLocations;
public override string Description { get; set; } = Plugin.Singleton.Config.ItemConfigs.ShotgunCfg.Description;
public override int SpawnLimit { get; set; } = Plugin.Singleton.Config.ItemConfigs.ShotgunCfg.SpawnLimit;

protected override void LoadEvents()
{
Expand Down
5 changes: 2 additions & 3 deletions CustomItems/Items/SniperRifle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ public SniperRifle(ItemType type, int clipSize, int itemId) : base(type, clipSiz
}

public override string Name { get; set; } = Plugin.Singleton.Config.ItemConfigs.SniperCfg.Name;
public override Dictionary<SpawnLocation, float> SpawnLocations { get; set; } =
Plugin.Singleton.Config.ItemConfigs.GlCfg.SpawnLocations;

public override Dictionary<SpawnLocation, float> SpawnLocations { get; set; } = Plugin.Singleton.Config.ItemConfigs.GlCfg.SpawnLocations;
public override string Description { get; set; } = Plugin.Singleton.Config.ItemConfigs.SniperCfg.Description;
public override int SpawnLimit { get; set; } = Plugin.Singleton.Config.ItemConfigs.SniperCfg.SpawnLimit;

protected override int ModBarrel { get; set; } = 3;

Expand Down
2 changes: 1 addition & 1 deletion CustomItems/Items/TranqGun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public TranqGun(ItemType type, int clipSize, int itemId) : base(type, clipSize,
}

public override string Name { get; set; } = Plugin.Singleton.Config.ItemConfigs.TranqCfg.Name;

public override string Description { get; set; } = Plugin.Singleton.Config.ItemConfigs.TranqCfg.Description;
public override int SpawnLimit { get; set; } = Plugin.Singleton.Config.ItemConfigs.TranqCfg.SpawnLimit;

protected override void LoadEvents()
{
Expand Down

0 comments on commit 57f7e46

Please sign in to comment.