C#:
using Newtonsoft.Json;
using Oxide.Core;
using System.Collections.Generic;
namespace Oxide.Plugins
{
[Info("TargetingAttachmentExtension", "Scalbox", "1.0.0")]
[Description("Weapons Targeting Attachment Extension")]
public class TargetingAttachmentExtension : RustPlugin
{
#region Fields
private Configuration _config;
//ItemID : ItemDefinition, ItemName
private readonly Dictionary<int, (ItemDefinition ItemDef, string ItemName)> _allowedWeapons = new();
#endregion
#region Hook
// private void Init()
// {
// }
private void OnServerInitialized()
{
if (_config.AllowedWeapons.IsNullOrEmpty() || _config.AllowedWeapons.Count == 0)
{
Puts("No allowed weapons configured. Please check your config.");
Interface.Oxide.UnloadPlugin(Name);
return;
}
foreach (var allowedWeapon in _config.AllowedWeapons)
{
var itemDef = ItemManager.FindItemDefinition(allowedWeapon);
if (!itemDef) continue;
_allowedWeapons.Add(itemDef.itemid, (itemDef, itemDef.shortname));
}
}
private void OnItemAddedToContainer(ItemContainer container, Item item)
{
ExtendWeaponAttachment(item);
}
private void Unload()
{
}
protected override void LoadConfig()
{
base.LoadConfig();
_config = Config.ReadObject<Configuration>();
if (!Equals(_config.VersionNumber, Version))
{
UpdateConfigValues();
_config.VersionNumber = Version;
}
SaveConfig();
}
private void UpdateConfigValues()
{
}
protected override void LoadDefaultConfig()
{
_config = Configuration.CreateConfig();
}
protected override void SaveConfig() => Config.WriteObject(_config);
#endregion
#region Core
private void ExtendWeaponAttachment(Item weaponItem)
{
if (weaponItem == null || weaponItem.info == null) return;
if (weaponItem.info.category != ItemCategory.Weapon) return;
if (!_allowedWeapons.TryGetValue(weaponItem.info.itemid, out var allowedWeapon)) return;
if (weaponItem.contents?.availableSlots == null || weaponItem.contents.availableSlots.IsEmpty()) return;
// Check if the weapon is allowed
if (allowedWeapon.ItemDef.itemid != weaponItem.info.itemid) return;
// Add the targeting slot if it doesn't exist
if (!weaponItem.contents.availableSlots.Contains(ItemSlot.InternalTargeting))
{
weaponItem.contents.availableSlots.Add(ItemSlot.InternalTargeting);
// weaponItem.MarkDirty();
}
}
#endregion
#region Class
private class Configuration
{
[JsonProperty("Allowed Weapons")] public HashSet<string> AllowedWeapons;
public VersionNumber VersionNumber;
public static Configuration CreateConfig()
{
return new Configuration
{
AllowedWeapons = new HashSet<string>
{
"lmg.m249",
"rifle.l96",
"rifle.bolt",
"hmlmg",
"smg.mp5"
},
VersionNumber = OxideMod.Version
};
}
}
#endregion
}
}