C#:
using Newtonsoft.Json;
using Oxide.Core.Libraries.Covalence;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace Oxide.Plugins {
[Info("NameRewards", "Vlad-00003", "1.0.2", ResourceId =197)]
[Description("Gives the players rewards if their nickname contains desired word")]
/*
* Author info:
* E-mail: Vlad-00003@mail.ru
* Vk: vk.com/vlad_00003
*/
class NameRewards : CovalencePlugin {
#region Vars
private PluginConfig _config;
#endregion
#region Config
private class PluginConfig {
[JsonProperty("Текст, который должен быть в нике")]
public string Text;
[JsonProperty("Оповещать о получении привилегий/необходимости добавления текста")]
public bool Notify;
[JsonProperty("Формат сообщений в чате")]
public string ChatFormat;
[JsonProperty("Привилегии, которые будут выданы")]
public List<string> Permissions;
[JsonProperty("Группы, в которые игрок будет добавлен")]
public List<string> Groups;
[JsonIgnore]
public Regex Regex;
#region Default Config
public static PluginConfig DefaultConfig => new PluginConfig {
Text = "#OLDISLAND", Notify = true, ChatFormat = "[#86d3db][OLD ISLAND][/#] {0}", Permissions = new List<string> {
"kits.nickname", "nteleportation.nickname"
}
, Groups = new List<string> {
"nickname"
}
}
;
#endregion
}
#endregion
#region Config init
protected override void LoadDefaultConfig() {
_config = PluginConfig.DefaultConfig;
}
protected override void LoadConfig() {
base.LoadConfig();
_config = Config.ReadObject<PluginConfig>();
_config.Regex =new Regex(Regex.Escape(_config.Text), RegexOptions.IgnoreCase | RegexOptions.Compiled);
}
protected override void SaveConfig() {
Config.WriteObject(_config);
}
#endregion
#region Localization
private void SendResponse(IPlayer player, string langKey, params object[] args) {
var format = GetMsg(langKey, player.Id);
format = args.Length != 0 ? string.Format(format, args) : format;
player.Reply(string.Format(_config.ChatFormat, format));
}
private string GetMsg(string key, string playerid) => lang.GetMessage(key, this, playerid);
private string GetMsg(string key, ulong playerid = 3552256) => GetMsg(key, playerid == 0 ? null : playerid.ToString());
protected override void LoadDefaultMessages() {
lang.RegisterMessages(new Dictionary<string, string>() {
["Granted"]
= "У вас в нике есть текст \"{0}\"!\nНаслаждайтесь вашими привилегиями!", ["Not"] = "У вас в нике нет текста \"{0}\"!\nЕсли вы хотите получить подарки - добавьте этот текст к нику и перезайдите на сервер!"
}
, this);
lang.RegisterMessages(new Dictionary<string, string>() {
["Granted"]
= "У вас в нике есть текст \"{0}\"!\nНаслаждайтесь вашими привилегиями!", ["Not"] = "У вас в нике нет текста \"{0}\"!\nЕсли вы хотите получить подарки - добавьте этот текст к нику и перезайдите на сервер!"
}
, this, "ru");
}
#endregion
private void Init() {
foreach (var player in covalence.Players.Connected) {
OnUserConnected(player);
}
}
void OnUserConnected(IPlayer player) {
if (_config.Regex.IsMatch(player.Name)) {
Grant(player);
if(_config.Notify) SendResponse(player, "Granted", _config.Text);
return;
}
Revoke(player);
if (_config.Notify) SendResponse(player, "Not", _config.Text);
}
#region Grant|Revoke methods
private void Grant(IPlayer player) {
foreach(var perm in _config.Permissions) {
if (!permission.PermissionExists(perm)) {
PrintWarning($"Permission \"{perm}\" doesn't exists and wasn't granted to the player \"{player.Name}\"");
continue;
}
player.GrantPermission(perm);
}
foreach(var group in _config.Groups) {
if (!permission.GroupExists(group)) {
PrintWarning($"Group \"{group}\" doesn't exists and the player \"{player.Name}\" wasn't added to it.");
continue;
}
player.AddToGroup(group);
}
}
private void Revoke(IPlayer player) {
foreach (var perm in _config.Permissions) {
if (!permission.PermissionExists(perm)) {
PrintWarning($"Permission \"{perm}\" doesn't exists and wasn't removed form the player \"{player.Name}\"");
continue;
}
player.RevokePermission(perm);
}
foreach (var group in _config.Groups) {
if (!permission.GroupExists(group)) {
PrintWarning($"Group \"{group}\" doesn't exists and the player \"{player.Name}\" wasn't removed from it.");
continue;
}
player.RemoveFromGroup(group);
}
}
#endregion
}
}