C#:
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
namespace Oxide.Plugins
{
[Info("kryAutoturretLimit", "xkrystalll", "1.0.0")]
[Description("Added the limit of placed autoturrets for privilegies")]
class kryAutoturretLimit : RustPlugin
{
#region Fields
private bool isReady = false;
#endregion
#region Hooks
private void OnServerInitialized()
{
isReady = true;
foreach (var x in cfg._privilegies)
{
permission.RegisterPermission(x.Key, this);
}
}
void OnConstructionPlace(BaseEntity entity, Construction component, Construction.Target constructionTarget, BasePlayer player)
{
if (entity == null || component == null || player == null)
return;
NextTick(() =>
{
try
{
if (!isReady)
return;
if (!entity.name.Contains("autoturret") || entity.OwnerID == 0)
return;
if (GetAllObjectsOfPlayer<AutoTurret>(player).Count() > GetMayCountDeployedTurrets(player))
{
player.ChatMessage($"Вы достигли <color=red>лимита</color> установленных турелей! ({GetMayCountDeployedTurrets(player)} шт.)");
entity.Kill(BaseNetworkable.DestroyMode.None);
ReturnTurret(player);
}
else
{
player.ChatMessage($"Осталось турелей - <color=red>{GetMayCountDeployedTurrets(player) - GetAllObjectsOfPlayer<AutoTurret>(player).Count()}</color> шт.");
}
}
catch (Exception e)
{
LogToFile("errors", $"{e.Message}", this, true);
}
});
}
#endregion
#region Methods
private void ReturnTurret(BasePlayer p)
{
Item item = ItemManager.CreateByName("autoturret", 1, 0);
p.GiveItem(item);
}
private int GetMayCountDeployedTurrets(BasePlayer p)
{
foreach (var x in cfg._privilegies.OrderByDescending(x => x.Value))
{
if (p.IPlayer.HasPermission(x.Key))
return x.Value;
}
return int.MaxValue;
}
private List<BaseEntity> GetAllObjectsOfPlayer<T>(BasePlayer p) where T : BaseEntity
{
var resultList = new List<BaseEntity>();
foreach (var x in BaseNetworkable.serverEntities)
{
if ((x as T)?.OwnerID == p.userID)
resultList.Add(x as BaseEntity);
}
return resultList;
}
#endregion
#region Config
private ConfigData cfg;
public class ConfigData
{
[JsonProperty("Привилегии и их кол-во турелей")]
public Dictionary<string, int> _privilegies;
}
protected override void LoadDefaultConfig()
{
var config = new ConfigData()
{
_privilegies = new Dictionary<string, int>()
{
{"kryAutoturretLimit.default", 1},
{"kryAutoturretLimit.vip", 10},
}
};
SaveConfig(config);
}
protected override void LoadConfig()
{
base.LoadConfig();
cfg = Config.ReadObject<ConfigData>();
SaveConfig(cfg);
}
private void SaveConfig(object config)
{
Config.WriteObject(config, true);
}
#endregion
}
}