using System; using System.Linq; using Newtonsoft.Json; using Oxide.Core; using UnityEngine; using Oxide.Core.Libraries.Covalence; using System.Collections.Generic; namespace Oxide.Plugins { [Info("CNewItemManager", "Crazy", "2.1.1")] [Description("Create new item")] public class CNewItemManager : RustPlugin { #region Fields private static ConfigData cfg; #endregion #region Server private void OnServerInitialized() { foreach (var item in cfg.item) AddCovalenceCommand(item.command, "CmdGive"); foreach (BasePlayer player in BasePlayer.activePlayerList) { // Parcourir les objets du conteneur principal foreach (Item item in player.inventory.containerMain?.itemList ?? Enumerable.Empty()) ChangeItemName(item, player); // Parcourir les objets de la ceinture foreach (Item item in player.inventory.containerBelt?.itemList ?? Enumerable.Empty()) ChangeItemName(item, player); // Parcourir les objets que le joueur porte foreach (Item item in player.inventory.containerWear?.itemList ?? Enumerable.Empty()) ChangeItemName(item, player); } } /// Rename all created item, for player private void OnPlayerConnected(BasePlayer player) { // Parcourir les items dans le conteneur principal if (player.inventory.containerMain != null) { foreach (Item item in player.inventory.containerMain.itemList) ChangeItemName(item, player); } // Parcourir les items dans le conteneur de la ceinture if (player.inventory.containerBelt != null) { foreach (Item item in player.inventory.containerBelt.itemList) ChangeItemName(item, player); } // Parcourir les items dans le conteneur de port if (player.inventory.containerWear != null) { foreach (Item item in player.inventory.containerWear.itemList) ChangeItemName(item, player); } } /// Rename item when is added to container private void OnItemAddedToContainer(ItemContainer container, Item item) { if (container == null || item == null || container.playerOwner == null) return; ChangeItemName(item, container.playerOwner); } /// Check if object can pickup /// False or True private bool CanPickupEntity(BasePlayer player, BaseEntity entity) { if(CheckPickup(player, entity.GetComponent())) return false; return true; } #endregion #region Config /// Config data class private class ConfigData { [JsonProperty(PropertyName = "ITEM")] public List item { get; set; } [JsonProperty(PropertyName = "VERSION")] public VersionNumber version { get; set; } } /// Item class public class NewItem { [JsonProperty("» Display name")] public string displayName{ get; set;} [JsonProperty("» Command for give")] public string command{ get; set;} [JsonProperty("» Quantity")] public int quantity{ get; set;} [JsonProperty("» Data")] public ItemData data{ get; set;} } /// Item data class public class ItemData { [JsonProperty("• Item shortname")] public string shortname { get; set;} [JsonProperty("• Skin")] public ulong skin { get; set;} [JsonProperty("• Amount")] public int amount { get; set;} [JsonProperty("• Condition")] public float condition { get; set;} [JsonProperty("• Max condition")] public float maxCondition { get; set;} [JsonProperty("• Ammo")] public int ammo { get; set;} [JsonProperty("• Ammotype shortname")] public string ammotype { get; set;} [JsonProperty("• Content (mods)")] public ItemData[] contents { get; set;} } /// Create default config data private ConfigData GetBaseConfig() { return new ConfigData { item = new List { new NewItem { displayName = "Money", command = "money.give", quantity = 1, data = new ItemData() { shortname = "researchpaper", skin = 2103444307, amount = 0, condition = 0, maxCondition = 0, ammo = 0, ammotype = null, contents = null } }, new NewItem { displayName = "Radio transceiver", command = "radio.give", quantity = 1, data = new ItemData() { shortname = "rf.detonator", skin = 2067425750, amount = 0, condition = 10, maxCondition = 50, ammo = 0, ammotype = null, contents = null } }, new NewItem { displayName = "M249 Custom", command = "m249.give", quantity = 1, data = new ItemData() { shortname = "lmg.m249", skin = 1712378771, amount = 0, condition = 100, maxCondition = 75, ammo = 500, ammotype = "ammo.rifle.explosive", contents = new ItemData[] { new ItemData{shortname = "weapon.mod.flashlight"} } } } } }; } /// Load config data protected override void LoadConfig() { base.LoadConfig(); cfg = Config.ReadObject(); if (cfg.version < Version) UpdateConfigValues(); Config.WriteObject(cfg, true); } /// Load default config data protected override void LoadDefaultConfig() => cfg = GetBaseConfig(); /// Save config data protected override void SaveConfig() => Config.WriteObject(cfg, true); /// Update config data private void UpdateConfigValues() { PrintWarning("Config update detected! Updating config values..."); ConfigData baseConfig = GetBaseConfig(); cfg.version = Version; PrintWarning("Config update completed!"); } #endregion #region Function /// Check if item can pickup /// Return item to player inventory if true private bool CheckPickup(BasePlayer player, BaseEntity entity) { if (entity == null) return false; if (entity.OwnerID != player.userID) return false; NewItem item = GetItemBySkin(entity.skinID); if (item == null) return false; entity.Kill(); GiveCraft(player, item, 1); return true; } /// Get item by command /// Return NewItem public NewItem GetItemByCommand(string args) { if(!cfg.item.Any(x => x.command == args)) return null; return cfg.item.Single(x => x.command == args); } /// Get item by skin /// Return NewItem public NewItem GetItemBySkin(ulong args) { if(!cfg.item.Any(x => x.data.skin == args)) return null; return cfg.item.Single(x => x.data.skin == args); } /// Check if item exist in config file /// Return true if exist public bool ExistByShortName(string args) => cfg.item.Any(item => item.data.shortname == args); /// Check if item exist in config file /// Return true if exist public bool ExistBySkin(ulong args) => cfg.item.Any(item => item.data.skin == args); /// Get item name /// Return item name public string GetItemName(ulong args) => cfg.item.Single(item => item.data.skin == args).displayName; /// Change item display name private void ChangeItemName(Item item, BasePlayer player) { if (item == null || player == null || !ExistByShortName(item.info.shortname) || !ExistBySkin(item.skin) || item.skin == 0) return; string displayName = GetItemName(item.skin); item.name = displayName; } /// Create new custom item /// NewItem: contain item informations /// Amount to give to player /// Return item private Item CreateCraft(NewItem newItem, int quantity) { var item = ItemManager.CreateByName(newItem.data.shortname, quantity, newItem.data.skin); if (item == null) return null; item.name = newItem.displayName; if(newItem.data.amount != 0) item.amount = newItem.data.amount; if(newItem.data.condition != 0) item._condition = newItem.data.condition; if(newItem.data.maxCondition != 0) item._maxCondition = newItem.data.maxCondition; BaseProjectile weapon = item.GetHeldEntity() as BaseProjectile; if (weapon != null) { if(newItem.data.ammo != 0) weapon.primaryMagazine.contents = newItem.data.ammo; if (!string.IsNullOrEmpty(newItem.data.ammotype)) weapon.primaryMagazine.ammoType = ItemManager.FindItemDefinition(newItem.data.ammotype); } FlameThrower flameThrower = item.GetHeldEntity() as FlameThrower; if (flameThrower != null) flameThrower.ammo = newItem.data.ammo; if (newItem.data.contents != null && item.contents != null) { foreach (ItemData contentData in newItem.data.contents) { Item newContent = ItemManager.CreateByName(contentData.shortname, Mathf.Max(1, contentData.amount)); if (newContent != null) { newContent.condition = contentData.condition; newContent.MoveToContainer(item.contents); } } } return item; } /// Send item in player inventory /// BasePlayer: contain informations about player /// NewItem: contain item informations /// Amount to give to player private void GiveCraft(BasePlayer player, NewItem newItem, int quantity) { var item = CreateCraft(newItem, quantity); player.GiveItem(item); } #endregion #region Command /// /// Command for give item /// /// Contains informations of player /// Command name /// Contains playername or id in args1, amount in args2 private void CmdGive(IPlayer player, string command, string[] args) { NewItem item = GetItemByCommand(command); if (item == null) return; if (!player.IsAdmin && args.Length < 0) return; var targetPlayer = BasePlayer.Find(args[0]) ?? BasePlayer.FindSleeping(args[0]); if (targetPlayer == null) { PrintWarning($"Player [ {args[0]} ] not found!"); return; } int quantity = 1; if(args.Count() == 2 ) { int asInt; if (int.TryParse(args[1], out asInt)) quantity = Int32.Parse(args[1]); } GiveCraft(targetPlayer, item, quantity); } #endregion } }