Иконка ресурса

FREE CNewItemManager : 2.1.1 2.1.1

Нет прав для скачивания
C#:
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

            /// <summary>Rename all created item, for all player</summary>
            private void OnServerInitialized()
            {
                foreach (var item in cfg.item)
                    AddCovalenceCommand(item.command, "CmdGive");

                foreach (BasePlayer player in BasePlayer.activePlayerList)
                    foreach (Item item in player.inventory.AllItems())
                        ChangeItemName(item, player);
            }

            /// <summary>Rename all created item, for player</summary>
            private void OnPlayerConnected(BasePlayer player) => player.inventory.AllItems().ToList().ForEach(it => ChangeItemName(it, player));

            /// <summary>Rename item when is added to container</summary>
            private void OnItemAddedToContainer(ItemContainer container, Item item)
            {
                if (container == null || item == null || container.playerOwner == null)
                    return;

                ChangeItemName(item, container.playerOwner);
            }

            /// <summary>Check if object can pickup</summary>
            /// <return>False or True</return>
            private bool CanPickupEntity(BasePlayer player, BaseEntity entity)
            {
                if(CheckPickup(player, entity.GetComponent<BaseEntity>()))
                    return false;

                return true;
            }

        #endregion

        #region Config         

            /// <summary>Config data class</summary>
            private class ConfigData
            {
                [JsonProperty(PropertyName = "ITEM")]
                public List<NewItem> item { get; set; }

                [JsonProperty(PropertyName = "VERSION")]
                public VersionNumber version { get; set; }
            }

            /// <summary>Item class</summary>
            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;}
            }

            /// <summary>Item data class</summary>
            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;}

            }

            /// <summary>Create default config data</summary>
            private ConfigData GetBaseConfig()
            {
                return new ConfigData
                {
                    item = new List<NewItem>
                    {
                        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"}
                                }
                            }
                        }
                    }
                };
            }

            /// <summary>Load config data</summary>
            protected override void LoadConfig()
            {
                base.LoadConfig();
                cfg = Config.ReadObject<ConfigData>();

                if (cfg.version < Version)
                    UpdateConfigValues();

                Config.WriteObject(cfg, true);
            }

            /// <summary>Load default config data</summary>
            protected override void LoadDefaultConfig() => cfg = GetBaseConfig();

            /// <summary>Save config data</summary>
            protected override void SaveConfig() => Config.WriteObject(cfg, true);

            /// <summary>Update config data</summary>
            private void UpdateConfigValues()
            {
                PrintWarning("Config update detected! Updating config values...");
                ConfigData baseConfig = GetBaseConfig();
                cfg.version = Version;
                PrintWarning("Config update completed!");
            }

        #endregion

        #region Function

            /// <summary>Check if item can pickup</summary>
            /// <return>Return item to player inventory if true</return>
            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;
            }

            /// <summary>Get item by command</summary>
            /// <return>Return NewItem</return>
            public NewItem GetItemByCommand(string args)
            {
                if(!cfg.item.Any(x => x.command == args))
                    return null;

                return cfg.item.Single(x => x.command == args);
            }

            /// <summary>Get item by skin</summary>
            /// <return>Return NewItem</return>
            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);
            }

            /// <summary>Check if item exist in config file</summary>
            /// <return>Return true if exist</return>
            public bool ExistByShortName(string args) => cfg.item.Any(item => item.data.shortname == args);

            /// <summary>Check if item exist in config file</summary>
            /// <return>Return true if exist</return>
            public bool ExistBySkin(ulong args) => cfg.item.Any(item => item.data.skin == args);

            /// <summary>Get item name</summary>
            /// <return>Return item name</return>
            public string GetItemName(ulong args) => cfg.item.Single(item => item.data.skin == args).displayName;

            /// <summary>Change item display name</summary>
            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;
            }

            /// <summary>Create new custom item</summary>
            /// <param name="item">NewItem: contain item informations</param>
            /// <param name="quantity">Amount to give to player</param>
            /// <return>Return item</return>
            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;
            }

            /// <summary>Send item in player inventory</summary>
            /// <param name="player">BasePlayer: contain informations about player</param>
            /// <param name="item">NewItem: contain item informations</param>
            /// <param name="quantity">Amount to give to player</param>
            private void GiveCraft(BasePlayer player, NewItem newItem, int quantity)
            {
                var item = CreateCraft(newItem, quantity);
                player.GiveItem(item);
            }
  
        #endregion

        #region Command

            /// <summary>
            /// Command for give item
            /// </summary>
            /// <param name="player">Contains informations of player</param>
            /// <param name="command">Command name</param>
            /// <param name="args">Contains playername or id in args1, amount in args2 </param>
            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

    }
}

Автор
15FPS
Скачиваний
0
Просмотры
1
Первый выпуск
Обновление
Оценка
0.00 звёзд 0 оценок
Сверху