using System; using System.Linq; using System.Reflection; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; using Oxide.Core; using Oxide.Core.Plugins; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Facepunch; #region Changelogs and ToDo /********************************************************************** * * 1.0.0 - Initial release * 1.1.0 - Added a cfg for melee weapon settings * - Added support for skins * - Can set Random skins * - Frankie will equip a sicle first then decides to draw another item instead * **********************************************************************/ #endregion namespace Oxide.Plugins { [Info("FrankiesWeapon", "Krungh Crow", "1.1.0")] [Description("Make Frankensteins pet wield different random melee weapons")] class FrankiesWeapon : RustPlugin { bool Debug = false; #region Configuration private ConfigData configData; class ConfigData { [JsonProperty(PropertyName = "Main config")] public SettingsPlugin PlugCFG = new SettingsPlugin(); } class SettingsPlugin { [JsonProperty(PropertyName = "Debug")] public bool Debug = false; [JsonProperty(PropertyName = "Use Random Skins")] public bool RandomSkins { get; set; } = false; [JsonProperty(PropertyName = "Melee weapons" , ObjectCreationHandling = ObjectCreationHandling.Replace)] public List Loot { get; set; } = DefaultList; } private bool LoadConfigVariables() { try { configData = Config.ReadObject(); } catch { return false; } SaveConf(); return true; } protected override void LoadDefaultConfig() { Puts("Fresh install detected Creating a new config file."); configData = new ConfigData(); SaveConf(); } void SaveConf() => Config.WriteObject(configData , true); #endregion #region Hooks void Init() { if (!LoadConfigVariables()) { Puts("Config file issue detected. Please delete file, or check syntax and fix."); return; } Debug = configData.PlugCFG.Debug; if (Debug) Puts($"[Debug] trigger for Debug is true"); } void OnEntitySpawned(FrankensteinPet frankie) { if (frankie == null) return; var inv_belt = frankie.inventory.containerBelt; var inv_wear = frankie.inventory.containerWear; timer.Once(3f , () => { inv_belt.Clear(); GiveRandom(inv_belt , configData.PlugCFG.Loot.ToList()); Item eyes = ItemManager.CreateByName("gloweyes" , 1 , 0); if (eyes != null) eyes.MoveToContainer(inv_wear); frankie.EquipWeapon(); }); } #endregion #region Melee weapon random System private Dictionary> Skins { get; set; } = new Dictionary>(); private static List DefaultList { get { return new List { new MeleeList { shortname = "machete", skin = 0 }, new MeleeList { shortname = "pitchfork", skin = 0 }, new MeleeList { shortname = "knife.bone", skin = 0 }, new MeleeList { shortname = "knife.butcher", skin = 0 }, new MeleeList { shortname = "longsword", skin = 0 }, new MeleeList { shortname = "mace", skin = 0 }, new MeleeList { shortname = "salvaged.cleaver", skin = 0 }, new MeleeList { shortname = "salvaged.sword", skin = 0 }, new MeleeList { shortname = "knife.combat", skin = 0 }, new MeleeList { shortname = "bone.club", skin = 0 }, new MeleeList { shortname = "spear.wooden", skin = 0 }, new MeleeList { shortname = "spear.stone", skin = 0 }, }; } } public class MeleeList { public string shortname { get; set; } public ulong skin { get; set; } } private void GiveRandom(ItemContainer container , List loot) { if (Debug) Puts($"[Debug] Started GiveRandom routine"); ItemDefinition def; List skins; MeleeList meleeItem; if (loot.Count == 0) { return; } meleeItem = loot.GetRandom(); loot.Remove(meleeItem); string shortname = meleeItem.shortname; def = ItemManager.FindItemDefinition(shortname); if (def == null) { if (Debug) Puts("Invalid shortname: {0}" , meleeItem.shortname); return; } ulong skin = meleeItem.skin; if (configData.PlugCFG.RandomSkins && skin == 0) { skins = GetItemSkins(def); if (skins.Count > 0) { skin = skins.GetRandom(); } } Item item = ItemManager.Create(def , 1 , skin); if (Debug) Puts("[Debug] {0} with skin {1} was used" , meleeItem.shortname, skin); if (!item.MoveToContainer(container , -1 , false)) { item.Remove(); } return; } private List GetItemSkins(ItemDefinition def) { List skins; if (!Skins.TryGetValue(def.shortname , out skins)) { Skins[def.shortname] = skins = ExtractItemSkins(def , skins); } return skins; } private List ExtractItemSkins(ItemDefinition def , List skins) { skins = new List(); foreach (var skin in def.skins) { skins.Add(Convert.ToUInt64(skin.id)); } foreach (var asi in Rust.Workshop.Approved.All.Values) { if (!string.IsNullOrEmpty(asi.Skinnable.ItemName) && asi.Skinnable.ItemName == def.shortname) { skins.Add(Convert.ToUInt64(asi.WorkshopdId)); } } return skins; } #endregion } }