C#:
using System;
using System.Collections.Generic;
using System.Globalization;
using ConVar;
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Game.Rust.Cui;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Birthdays", "yung", "1.0.1")]
[Description("Unique way to make your players feel special")]
public class Birthdays : RustPlugin
{
#region Fields
private PluginData _pluginData;
private PluginConfig _pluginConfig;
#endregion
#region Hooks
private void Init()
{
LoadData();
}
private void OnServerInitialized()
{
cmd.AddConsoleCommand("birthdays.remove", this, "RemovePlayer");
cmd.AddConsoleCommand("b.remove", this, "RemovePlayer");
cmd.AddConsoleCommand("birthdays.check" , this, "CheckPlayer");
cmd.AddConsoleCommand("b.check" , this, "CheckPlayer");
}
private void Unload()
{
SaveData();
}
private void OnPlayerConnected(BasePlayer player)
{
Dictionary<ulong, PlayerData> data = _pluginData.PlayersData;
if (!data.ContainsKey(player.userID))
{
Message(player,"Your birthday is not added! Use /birthday set (month/day) to receive a present on your birthday. You can only set it once!");
}
else if (data.ContainsKey(player.userID))
{
if (DateTime.Today.Day == data[player.userID].Birthday.Day &&
DateTime.Today.Month == data[player.userID].Birthday.Month)
{
Message(player,$"Happy Birthday, {data[player.userID].Name} ! Use /birthday claim to get your present!");
foreach (var plyer in BasePlayer.activePlayerList)
{
Message(plyer,$"Today is {data[player.userID].Name}'s birthday!");
}
}
else
{
data[player.userID].PresentClaimed = false;
}
}
}
private void Message(BasePlayer player, string args)
{
Player.Reply(player, args, ulong.Parse(_pluginConfig.PluginIcon));
}
private void RemovePlayer(ConsoleSystem.Arg arg)
{
if (arg.IsRcon)
{
Dictionary<ulong, PlayerData> data = _pluginData.PlayersData;
if (!arg.HasArgs())
{
return;
}
else if (arg.Args.Length > 0)
{
ulong playerID = Convert.ToUInt64(arg.Args[0]);
if (data.ContainsKey(playerID))
{
Puts($"Player {data[playerID].Name} was removed!");
data.Remove(playerID);
SaveData();
}
else
{
Puts("Invalid player or doesn't exist in database");
}
}
else
{
Puts("Invalid syntax! Use birthdays.remove userID");
}
}
}
private void CheckPlayer(ConsoleSystem.Arg arg)
{
if (arg.IsRcon)
{
Dictionary<ulong, PlayerData> data = _pluginData.PlayersData;
if (!arg.HasArgs())
{
return;
}
else if (arg.Args.Length > 0)
{
ulong playerID = Convert.ToUInt64(arg.Args[0]);
if (data.ContainsKey(playerID))
{
if (data[playerID].PresentClaimed)
{
Puts($"Player {data[playerID].Name} has claimed his present!");
}
else
{
Puts($"Player {data[playerID].Name} has not claimed his present!");
}
}
else
{
Puts("Invalid player or doesn't exist in database");
}
}
else
{
Puts("Invalid syntax! Use birthdays.check userID");
}
}
}
#endregion
#region Commands
[ChatCommand("birthday")]
private void CommandHandler(BasePlayer player, string command, string[] args)
{
Dictionary<ulong, PlayerData> data = _pluginData.PlayersData;
switch (args[0])
{
case "set":
{
{
if (args.Length > 1 && args.Length < 3 && args[0] == "set")
{
if (!data.ContainsKey(player.userID))
{
DateTime birthday;
if (DateTime.TryParse(args[1], out birthday))
{
data[player.userID] = new PlayerData(player);
data[player.userID].Birthday = birthday;
data[player.userID].PresentClaimed = false;
Message(player,$"Your birthday is set on {args[1]}. If you think this is a mistake, please contact an Owner");
SaveData();
}
else
{
Message(player,"Invalid date");
}
}
else
{
Message(player,"You have already set your birthday!");
}
}
else
{
Message(player,"Syntax error! Use /birthday set (month/day). Exemple: /birthday add 01/05");
}
}
break;
}
case "claim":
{
if (args.Length != 0 && args.Length < 2 && args[0] == "claim")
{
if (data.ContainsKey(player.userID))
{
if (DateTime.Today.Day == data[player.userID].Birthday.Day &&
DateTime.Today.Month == data[player.userID].Birthday.Month)
{
if (data[player.userID].PresentClaimed == false)
{
Message(player,"Enjoy your present!");
foreach (KeyValuePair<string, int> item in _pluginConfig.Items)
{
Item give = ItemManager.CreateByName(item.Key, item.Value);
if (give == null)
{
continue;
}
player.GiveItem(give);
}
data[player.userID].PresentClaimed = true;
}
else
{
Message(player,"You have already claimed your present!");
}
}
else
{
Message(player,"It's not your birthday today!");
}
}
else
{
Message(player,"You didn't set your birthday date!");
}
}
else
{
Message(player,"Syntax error! Use /birthday claim ");
}
break;
}
}
}
#endregion
#region Data
private void SaveData() => Interface.Oxide.DataFileSystem.WriteObject(Name, _pluginData);
private void LoadData()
{
try
{
_pluginData = Interface.Oxide.DataFileSystem.ReadObject<PluginData>(Name);
}
catch (Exception e)
{
PrintError(e.ToString());
}
if (_pluginData == null)
{
_pluginData = new PluginData();
}
}
private class PluginData
{
[JsonProperty(PropertyName = "Players Data")]
public Dictionary<ulong, PlayerData> PlayersData = new Dictionary<ulong, PlayerData>();
}
private class PlayerData
{
[JsonProperty(PropertyName = "Player Name")]
public string Name { get; set; }
[JsonProperty(PropertyName = "Birthday Date")]
public DateTime Birthday { get; set; }
[JsonProperty(PropertyName = "Present claimed")]
public bool PresentClaimed { get; set; }
public PlayerData(BasePlayer player)
{
Name = player.displayName;
PresentClaimed = false;
}
[JsonConstructor]
public PlayerData()
{
}
}
#endregion
#region Configuration
private class PluginConfig
{
[JsonProperty(PropertyName = "Items to give ")]
public Dictionary<string, int> Items;
[JsonProperty(PropertyName = "Custom Icon")]
public string PluginIcon { get; set; }
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_pluginConfig = Config.ReadObject<PluginConfig>();
if (_pluginConfig == null)
{
throw new Exception();
}
SaveConfig();
}
catch
{
PrintError("Your configuration file contains an error. Using default configuration values.");
LoadDefaultConfig();
}
}
protected override void SaveConfig() => Config.WriteObject(_pluginConfig);
protected override void LoadDefaultConfig()
{
_pluginConfig = new PluginConfig
{
Items = new Dictionary<string, int>
{
["shortname"] = 10,
},
PluginIcon = "76561199105971351"
};
}
#endregion
}
}