using System;
using System.Collections.Generic;
using Network;
using Oxide.Core;
using Oxide.Core.Libraries.Covalence;
using UnityEngine;
using Random = UnityEngine.Random;
namespace Oxide.Plugins
{
[Info("CharmsBase", "Marte6", "1.0.0")]
[Description("Special charms and effects with random elements.")]
class CharmsBase : RustPlugin
{
#region Fields
private const int CharmItemId = 1789825282;
private const string CharmPermission = "charms.use";
private const string CharmGrantPermission = "charms.grant";
private Dictionary<ulong, double> playerCharmUsageTimes = new Dictionary<ulong, double>();
private Configuration config;
private List<CharmType> charmTypes;
#endregion
#region Configuration
private class Configuration
{
public float CharmEffectDuration { get; set; } = 300f; // Default to 5 minutes
public string PluginVersion { get; set; } = "3.3.0";
// Configuration ranges for various charm effects
public CharmSettings HealthCharm { get; set; } = new CharmSettings { Min = 150f, Max = 200f };
public CharmSettings GatherMultiplier { get; set; } = new CharmSettings { Min = 1.1f, Max = 2.0f };
public CharmSettings SpeedIncrease { get; set; } = new CharmSettings { Min = 1.1f, Max = 1.5f };
public CharmSettings DamageReduction { get; set; } = new CharmSettings { Min = 0.1f, Max = 0.5f };
// Add more settings for each charm type here
}
private class CharmSettings
{
public float Min { get; set; }
public float Max { get; set; }
}
private Configuration GetDefaultConfig() => new Configuration();
protected override void LoadConfig()
{
try
{
base.LoadConfig();
config = Config.ReadObject<Configuration>() ?? GetDefaultConfig();
SaveConfig();
CheckConfigUpdates();
}
catch (Exception ex)
{
LogError($"Configuration file is corrupt: {ex.Message}. Using default configuration.");
config = GetDefaultConfig();
SaveConfig();
}
}
protected override void SaveConfig()
{
Config.WriteObject(config, true);
}
private void CheckConfigUpdates()
{
bool updated = false;
if (config.PluginVersion != "3.3.0")
{
config.PluginVersion = "3.3.0";
updated = true;
}
if (updated)
{
Puts(Lang("ConfigUpdated"));
SaveConfig();
}
}
private void LogError(string message)
{
UnityEngine.Debug.LogError("[Charms] " + message);
}
#endregion
#region Oxide Hooks
private void Init()
{
permission.RegisterPermission(CharmPermission, this);
permission.RegisterPermission(CharmGrantPermission, this);
LoadData();
LoadDefaultMessages();
InitializeCharmEffects();
}
private void OnItemAddedToContainer(ItemContainer container, Item item)
{
if (item == null || container == null || container.playerOwner == null)
return;
if (item.info.itemid == CharmItemId)
{
BasePlayer player = container.playerOwner;
if (player?.userID.IsSteamId() == true && (container == player.inventory.containerBelt || container == player.inventory.containerMain))
{
NotifyPlayer(player, Lang("SpecialItemAdded"));
}
}
}
private void OnActiveItemChanged(BasePlayer player, Item oldItem, Item newItem)
{
if (player?.userID.IsSteamId() == true && newItem != null)
{
var charmType = charmTypes.Find(c => c.SkinId == newItem.skin);
if (charmType != null)
{
NotifyPlayer(player, Lang("CharmActivated", null, charmType.Name));
charmType.ApplyEffect(player);
ActivateCharmEffect(player);
}
}
}
private void OnEntityDeath(Stag entity, HitInfo hitInfo)
{
if (hitInfo?.Initiator is BasePlayer player && !player.IsNpc && player.userID.IsSteamId())
{
Puts("OnEntityDeath: " + entity.ShortPrefabName);
DropRandomCharmAt(entity.transform.position);
}
}
private void OnGrowableGathered(GrowableEntity plant, Item item, BasePlayer player)
{
if (player != null && !player.IsNpc && player.userID.IsSteamId() && HasActiveCharmEffect(player))
{
float randomMultiplier = Random.Range(config.GatherMultiplier.Min, config.GatherMultiplier.Max);
item.amount = (int)(item.amount * randomMultiplier);
NotifyPlayer(player, Lang("GatherBonus", null, randomMultiplier));
}
}
private void OnDispenserGather(ResourceDispenser dispenser, BaseEntity entity, Item item)
{
if (entity.ToPlayer() is BasePlayer player && !player.IsNpc && HasActiveCharmEffect(player))
{
item.amount = (int)(item.amount * Random.Range(config.GatherMultiplier.Min, config.GatherMultiplier.Max));
NotifyPlayer(player, Lang("ResourceBonus"));
}
}
#endregion
#region Charm Effects
private class CharmType
{
public string Name { get; set; }
public ulong SkinId { get; set; }
public Action<BasePlayer> ApplyEffect { get; set; }
}
private void InitializeCharmEffects()
{
charmTypes = new List<CharmType>
{
new CharmType
{
Name = "Health Charm",
SkinId = 3354623665,
ApplyEffect = player => CharmEffects.BoostHealth(player, config.HealthCharm),
},
new CharmType
{
Name = "Swift Charm",
SkinId = 3354623666,
ApplyEffect = player => CharmEffects.IncreaseSpeed(player, config.SpeedIncrease),
},
new CharmType
{
Name = "Protector's Charm",
SkinId = 3354623667,
ApplyEffect = player => CharmEffects.ReduceDamageTaken(player, config.DamageReduction),
},
new CharmType
{
Name = "Miner's Charm",
SkinId = 3354623668,
ApplyEffect = player => NotifyPlayer(player, Lang("MiningEfficiency")),
},
new CharmType
{
Name = "Lumberjack's Charm",
SkinId = 3354623669,
ApplyEffect = player => NotifyPlayer(player, Lang("LumberEfficiency")),
},
new CharmType
{
Name = "Fisherman's Charm",
SkinId = 3354623670,
ApplyEffect = player => NotifyPlayer(player, Lang("FishingLuck")),
},
new CharmType
{
Name = "Hunter's Charm",
SkinId = 3354623671,
ApplyEffect = player => NotifyPlayer(player, Lang("HuntingSkill")),
},
new CharmType
{
Name = "Visionary's Charm",
SkinId = 3354623672,
ApplyEffect = player => NotifyPlayer(player, Lang("NightVision")),
},
new CharmType
{
Name = "Charisma Charm",
SkinId = 3354623673,
ApplyEffect = player => NotifyPlayer(player, Lang("SocialBoost")),
},
new CharmType
{
Name = "Trader's Charm",
SkinId = 3354623674,
ApplyEffect = player => NotifyPlayer(player, Lang("TradingProfit")),
},
};
}
#endregion
#region Charm Effects Logic
private static class CharmEffects
{
public static void BoostHealth(BasePlayer player, CharmSettings settings)
{
float randomHealthBoost = Random.Range(settings.Min, settings.Max);
player.health = Mathf.Min(randomHealthBoost, player.MaxHealth());
player.ChatMessage($"Your health has been boosted to {randomHealthBoost}.");
}
public static void IncreaseSpeed(BasePlayer player, CharmSettings settings)
{
float speedBonus = Random.Range(settings.Min, settings.Max);
player.ChatMessage($"Your speed has been increased by {speedBonus}x!");
// Example function for tracking speed increases
}
public static void ReduceDamageTaken(BasePlayer player, CharmSettings settings)
{
float reduction = Random.Range(settings.Min, settings.Max);
player.ChatMessage($"You now take {reduction * 100}% less damage!");
// Example function for applying damage reduction
}
// Additional effects can be placed here
}
#endregion
#region Helper Methods
private void NotifyPlayer(BasePlayer player, string message)
{
player.SendConsoleCommand("echo " + message);
}
private Item CreateCharm(string charmName)
{
var charmType = charmTypes.Find(c => c.Name.Equals(charmName, StringComparison.OrdinalIgnoreCase));
if (charmType == null)
{
PrintWarning($"Charm type '{charmName}' not found.");
return null;
}
var charmItem = ItemManager.CreateByItemID(CharmItemId, 1, charmType.SkinId);
charmItem.name = charmType.Name;
SaveData(charmItem);
return charmItem;
}
private void GiveCharm(BasePlayer player, string charmName)
{
if (!HasPermission(player, CharmPermission))
{
NotifyPlayer(player, Lang("NoPermission"));
return;
}
var charmItem = CreateCharm(charmName);
if (charmItem != null)
{
player.inventory.GiveItem(charmItem);
NotifyPlayer(player, Lang("CharmReceived", null, charmName));
}
}
private void DropRandomCharmAt(Vector3 position)
{
int index = Random.Range(0, charmTypes.Count);
var charmItem = CreateCharm(charmTypes[index].Name);
if (charmItem != null)
{
charmItem.Drop(position, new Vector3(0, 1, 0));
}
}
private bool HasPermission(BasePlayer player, string perm)
{
return permission.UserHasPermission(player.UserIDString, perm);
}
private void ActivateCharmEffect(BasePlayer player)
{
playerCharmUsageTimes[player.userID] = TimeEx.currentTimestamp;
}
private bool HasActiveCharmEffect(BasePlayer player)
{
return playerCharmUsageTimes.TryGetValue(player.userID, out double usageTime) && TimeEx.currentTimestamp - usageTime <= config.CharmEffectDuration;
}
private void SaveData(Item charmItem)
{
var data = Interface.Oxide.DataFileSystem.GetFile("charmItemsData");
data.WriteObject(charmItem);
}
private void LoadData()
{
var data = Interface.Oxide.DataFileSystem.GetFile("charmItemsData");
if (data.Exists())
{
// Load previously saved data if necessary
}
}
#endregion
#region Commands
[ChatCommand("charm")]
private void GiveCharmCommand(BasePlayer player, string command, string[] args)
{
if (!HasPermission(player, CharmGrantPermission))
{
NotifyPlayer(player, Lang("NoPermissionToGive"));
return;
}
if (args.Length > 0)
{
string charmName = args[0];
GiveCharm(player, charmName);
}
else
{
NotifyPlayer(player, Lang("InvalidCharm"));
}
}
#endregion
#region Localization
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(
new Dictionary<string, string>
{
["NoPermission"] = "You do not have permission to use this command.",
["CharmReceived"] = "You have received a {0}.",
["NoPermissionToGive"] = "You do not have permission to give charms.",
["SpecialItemAdded"] = "Special item added.",
["CharmActivated"] = "{0} activated.",
["ConfigUpdated"] = "Configuration file updated to latest version.",
["InvalidCharm"] = "Invalid charm name provided. Please try again.",
["ResourceBonus"] = "Your gather has been enhanced by the charm effect!",
["GatherBonus"] = "Your gather has been increased by a multiplier of {0}!",
["MiningEfficiency"] = "Your mining is now more efficient!",
["LumberEfficiency"] = "Your woodcutting is now more efficient!",
["FishingLuck"] = "Your fishing luck just increased!",
["HuntingSkill"] = "Your hunting skills have been sharpened!",
["NightVision"] = "Night vision enabled!",
["SocialBoost"] = "Your charisma has been boosted!",
["TradingProfit"] = "Trade profits increased!",
},
this
);
}
private string Lang(string key, string userId = null, params object[] args) => string.Format(lang.GetMessage(key, this, userId), args);
#endregion
}
}