C#:
using System.Collections.Generic;
using Oxide.Core;
using Oxide.Core.Plugins;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Welcome Gift", "0killrest", "1.0.0")]
[Description("Gives a starter kit to new players on their first join")]
internal class WelcomeGift : RustPlugin
{
#region Configuration
private static ConfigData config;
private class ConfigData
{
public bool Enabled { get; set; } = true;
public string WelcomeMessage { get; set; } = "<color=green>Welcome to the server!</color> You received a starter gift!";
public List<GiftItem> GiftItems { get; set; }
public ConfigData()
{
GiftItems = new List<GiftItem>
{
new GiftItem { ShortName = "rock", Amount = 1, SkinID = 0 },
new GiftItem { ShortName = "torch", Amount = 1, SkinID = 0 },
new GiftItem { ShortName = "hatchet", Amount = 1, SkinID = 0 },
new GiftItem { ShortName = "pickaxe", Amount = 1, SkinID = 0 },
new GiftItem { ShortName = "apple", Amount = 5, SkinID = 0 },
new GiftItem { ShortName = "bandage", Amount = 5, SkinID = 0 },
new GiftItem { ShortName = "wood", Amount = 500, SkinID = 0 },
new GiftItem { ShortName = "stones", Amount = 200, SkinID = 0 }
};
}
}
public class GiftItem
{
public string ShortName { get; set; }
public int Amount { get; set; }
public ulong SkinID { get; set; }
}
protected override void LoadConfig()
{
base.LoadConfig();
config = Config.ReadObject<ConfigData>();
if (config == null)
{
LoadDefaultConfig();
}
}
protected override void LoadDefaultConfig()
{
config = new ConfigData();
PrintWarning("Default configuration created.Edit it in oxide/config/WelcomeGift.json");
SaveConfig();
}
protected override void SaveConfig() => Config.WriteObject(config);
#endregion
#region Data Storage
private HashSet<ulong> giftedPlayers;
private void LoadData()
{
giftedPlayers = Interface.Oxide.DataFileSystem.ReadObject<HashSet<ulong>>("WelcomeGift_Data") ?? new HashSet<ulong>();
}
private void SaveData()
{
Interface.Oxide.DataFileSystem.WriteObject("WelcomeGift_Data", giftedPlayers);
}
#endregion
#region Hooks
private void Init()
{
LoadData();
}
private void OnServerSave() => SaveData();
private void Unload() => SaveData();
private void OnPlayerInit(BasePlayer player)
{
if (!config.Enabled) return;
if (player == null || !player.IsConnected) return;
ulong userId = player.userID;
if (!giftedPlayers.Contains(userId))
{
giftedPlayers.Add(userId);
GiveGift(player);
SaveData();
}
}
#endregion
#region Core Logic
private void GiveGift(BasePlayer player)
{
int totalItems = 0;
foreach (var itemConfig in config.GiftItems)
{
ItemDefinition itemDef = ItemManager.FindItemDefinition(itemConfig.ShortName);
if (itemDef == null)
{
PrintWarning($"Item '{itemConfig.ShortName}' not found! Skipping...");
continue;
}
Item item = ItemManager.Create(itemDef, itemConfig.Amount, itemConfig.SkinID);
if (item != null)
{
player.inventory.GiveItem(item);
totalItems++;
}
}
if (!string.IsNullOrEmpty(config.WelcomeMessage))
{
SendReply(player, config.WelcomeMessage);
}
Puts($"Gave welcome gift to {player.displayName} ({player.userID}) | {totalItems} item types");
}
[ConsoleCommand("welcomegift.reset")]
private void cmdResetGift(ConsoleSystem.Arg args)
{
if (args.Connection != null) return; // console only
if (args.Args == null || args.Args.Length == 0)
{
args.ReplyWith("Usage: welcomegift.reset <steamID> or 'all'");
return;
}
if (args.Args[0].ToLower() == "all")
{
giftedPlayers.Clear();
SaveData();
args.ReplyWith("Reset gift status for ALL players");
return;
}
if (ulong.TryParse(args.Args[0], out ulong steamId))
{
if (giftedPlayers.Remove(steamId))
{
SaveData();
args.ReplyWith($"Reset gift status for {steamId}");
}
else
{
args.ReplyWith($"Player {steamId} has not received a gift yet");
}
}
else
{
args.ReplyWith("Invalid SteamID");
}
}
#endregion
}
}