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

FREE NewItems : 1.0.0 1.0.0

Нет прав для скачивания
C#:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using ConVar;
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Core.Plugins;
using Oxide.Game.Rust.Cui;
using Rust;
using UnityEngine;
namespace Oxide.Plugins {
    [Info("NewItems", "OMHOMHOM", "1.0.0")]
    public class NewItems : RustPlugin {
#region Fields
        [PluginReference]
        private Plugin ImageLibrary;
        public string Layer = "UI_NewItems.Craft";
        public string LayerBlur = "UI.NewItems.Blur";
        public Dictionary<ulong, int> playerModifity = new Dictionary<ulong, int>();
#endregion
#region Command
        [ChatCommand("updatecraft")]
        void chatCmdAddNewItem(BasePlayer player, string command, string[] args) {
            if (!player.IsAdmin) return;
            var activeItem = player.GetActiveItem();
            if (activeItem == null) {
                if (player.inventory.containerBelt.itemList[0] != null) {
                    activeItem = player.inventory.containerBelt.itemList[0];
                }
                else return;
            }
            List<CraftInfo> craftInfo = new List<CraftInfo>();
            foreach (var item in player.inventory.containerMain.itemList) {
                if (craftInfo.Find(x => x.shortname.Equals(item.info.shortname)) != null) {
                    continue;
                }
                craftInfo.Add(new CraftInfo() {
                    shortname = item.info.shortname, amount = item.amount, skinID = item.skin, itemID = item.info.itemid
                }
                );
            }
            if (activeItem.name != null) {
                var item = _config.items.Find(x => x.name != null && x.name.Contains(activeItem.name));
                if (item != null) {
                    item.craftItems = craftInfo;
                }
            }
            SendReply(player, "Предмет добавлен или крафт был изменён!");
            SaveConfig();
        }
        public void UpdateItemsInfo(BasePlayer player, string name) {
            CuiHelper.DestroyUi(player, Layer + ".Items");
            CuiElementContainer container = new CuiElementContainer();
            container.Add(new CuiPanel() {
                RectTransform = {
                    AnchorMin = "0 0", AnchorMax = "1 1"
                }
                , Image = {
                    Color = "0 0 0 0"
                }
            }
            , Layer + ".Menu", Layer + ".Items");
            var craftItem = _config.items.Find(x => x.name.Contains(name));
            float minPosition = 0f;
            var itemWidth = (0.193633f - 0.054745f);
            var itemHeight = 0.421575f - 0.261575f;
            var itemCount = craftItem.craftItems.Count;
            var itemMargin = 0.019399f;
            if (itemCount >= 5) minPosition = 0.5f - 5 / 2f * itemWidth - (5 - 1) / 2f * itemMargin;
            else minPosition = 0.5f - itemCount / 2f * itemWidth - (itemCount - 1) / 2f * itemMargin;
            itemCount -= 5;
            var minHeight = 0.238401f;
            var modifity = playerModifity[player.userID];
            var countItem = 0;
            foreach (var item in craftItem.craftItems) {
                countItem += 1;
                var haveItem = HaveItem(player, item.itemID, item.skinID, item.amount * modifity);
                container.Add(new CuiElement() {
                    Parent = Layer + ".Items", Name = Layer + $".Item{item.shortname}", Components = {
                        new CuiImageComponent() {
                            Color = haveItem ? HexToRGB("#80A47DFF") : HexToRGB("#A47D7DFF"), Sprite = "Assets/Content/UI/UI.Background.Tile.psd"
                        }
                        , new CuiOutlineComponent() {
                            Color = haveItem ? HexToRGB("#A0D29CFF") : HexToRGB("#C29494FF"), Distance = "2 2",
                        }
                        , new CuiRectTransformComponent() {
                            AnchorMin = $"{minPosition} {minHeight}", AnchorMax = $"{minPosition + itemWidth} {minHeight + itemHeight}"
                        }
                        ,
                    }
                }
                );
                container.Add(new CuiElement() {
                    Parent = Layer + $".Item{item.shortname}", Name = Layer + $".Item{item.shortname}.Image", Components = {
                        new CuiRawImageComponent() {
                            Color = "1 1 1 1", Png = GetImage(item.shortname),
                        }
                        , new CuiRectTransformComponent() {
                            AnchorMin = "0 0", AnchorMax = "1 1"
                        }
                        ,
                    }
                }
                );
                container.Add(new CuiLabel() {
                    Text = {
                        Text = $"x{item.amount * modifity}", Align = TextAnchor.LowerRight, FontSize = 12
                    }
                    , RectTransform = {
                        AnchorMin = "0 0", AnchorMax = "1 1", OffsetMax = "-3 -3"
                    }
                }
                , Layer + $".Item{item.shortname}.Image");
                if (countItem % 5 == 0) {
                    if (itemCount > 5) {
                        minPosition = 0.5f - 5 / 2f * itemWidth - (5 - 1) / 2f * itemMargin;
                        itemCount -= 5;
                    }
                    else {
                        minPosition = 0.5f - itemCount / 2f * itemWidth - (itemCount - 1) / 2f * itemMargin;
                    }
                    minHeight -= ((itemMargin * 2) + itemHeight);
                }
                else {
                    minPosition += (itemWidth + itemMargin);
                }
            }
            CuiHelper.AddUi(player, container);
        }
        [ConsoleCommand("givenewitem")]
        void consoleCmdGiveNewItem(ConsoleSystem.Arg arg) {
            if (!arg.IsAdmin) return;
            var name = "";
            var amount = 0;
            foreach (var args in arg.Args) {
                if (args.Equals(arg.GetString(0))) {
                    continue;
                }
                if (args == "givenewitem") continue;
                if (name.Length == 0) {
                    name += args;
                }
                else name += $" {args}";
            }
            var findItem = _config.items.Find(x => x.name.Equals(name));
            var target = BasePlayer.FindByID(arg.GetULong(0));
            if (findItem != null) {
                var item = ItemManager.CreateByItemID(findItem.idItem, findItem.countCraft, findItem.skinID);
                item.name = findItem.name;
                target.GiveItem(item);
            }
            else {
                PrintError("Ошибка при выдаче предмета! Неправильно указано название предмета или оно было изменено!");
            }
        }
        [ConsoleCommand("UI_NewItems")]
        void consoleCmdNewItems(ConsoleSystem.Arg arg) {
            var player = arg.Connection?.player as BasePlayer;
            if (player == null) return;
            ItemInfo craftItem = null;
            var name = "";
            var amount = 0;
            foreach (var args in arg.Args) {
                if (args == "choicecraft" || args == "close" || args == "craftitem" || args == "addmodifity" || args == "removemodifity") continue;
                if (name.Length == 0) {
                    name += args;
                }
                else name += $" {args}";
            }
            if (arg.GetString(0).Equals("close") || arg.GetString(0).Equals("giveitems")) {
            }
            else {
                craftItem = _config.items.Find(x => x.name.Contains(name));
                if (craftItem == null) return;
            }
            CuiElementContainer container = new CuiElementContainer();
            switch (arg.GetString(0)) {
                case "giveitems" : foreach (var item in _config.items) {
                    Item newItem = ItemManager.CreateByItemID(item.idItem, item.countCraft, item.skinID);
                    newItem.name = item.name;
                    player.GiveItem(newItem);
                }
                break;
                case "craftitem" : var success = true;
                Dictionary<Item, int> items = new Dictionary<Item, int>();
                foreach (var craftedItem in craftItem.craftItems) {
                    var haveItem = HaveItem(player, craftedItem.itemID, craftedItem.skinID, craftedItem.amount);
                    if (!haveItem) {
                        success = false;
                        SendReply(player, "<color=#afafaf>Вы не можете скрафтить предмет! Не хватает ингредиента!</color>");
                        return;
                    }
                    var item = FindItem(player, craftedItem.itemID, craftedItem.skinID, craftedItem.amount);
                    items.Add(item, craftedItem.amount * playerModifity[player.userID]);
                }
                foreach (var item in items) {
                    item.Key.UseItem(item.Value);
                }
                if (success) {
                    player.SendConsoleCommand("UI_Craft close");
                    Item craft = ItemManager.CreateByName(craftItem.shortname, craftItem.countCraft * playerModifity[player.userID], craftItem.skinID);
                    craft.name = craftItem.name;
                    player.GiveItem(craft);
                }
                break;
                case "addmodifity" : playerModifity[player.userID] += 1;
                CuiHelper.DestroyUi(player, Layer + ".CountPanel.Text");
                amount = craftItem.countCraft * playerModifity[player.userID];
                container.Add(new CuiLabel() {
                    RectTransform = {
                        AnchorMin = "0.1 0", AnchorMax = "0.9 1"
                    }
                    , Text = {
                        Align = TextAnchor.MiddleCenter, Text = $"{amount} шт.", FontSize = 28
                    }
                }
                , Layer + ".CountPanel", Layer + ".CountPanel.Text");
                CuiHelper.AddUi(player, container);
                UpdateItemsInfo(player, name);
                break;
                case "removemodifity" : if (playerModifity[player.userID] == craftItem.countCraft) return;
                playerModifity[player.userID] -= 1;
                CuiHelper.DestroyUi(player, Layer + ".CountPanel.Text");
                amount = craftItem.countCraft * playerModifity[player.userID];
                container.Add(new CuiLabel() {
                    RectTransform = {
                        AnchorMin = "0.1 0", AnchorMax = "0.9 1"
                    }
                    , Text = {
                        Align = TextAnchor.MiddleCenter, Text = $"{amount} шт.", FontSize = 28
                    }
                }
                , Layer + ".CountPanel", Layer + ".CountPanel.Text");
                container.Add(new CuiButton() {
                    RectTransform = {
                        AnchorMin = $"0.463021 {0.246296 - 0.093519}", AnchorMax = $"0.504688 {0.32037 - 0.093519}"
                    }
                    , Button = {
                        Color = "0.9686275 0.9215686 0.8823529 0.03529412", Command = $"UI_NewItems removemodifity {craftItem.name}",
                    }
                    , Text = {
                        Align = TextAnchor.MiddleCenter, Color = "1 1 1 1", Text = "<b>-</b>", FontSize = 24
                    }
                }
                , Layer, Layer + ".Btn1");
                container.Add(new CuiButton() {
                    RectTransform = {
                        AnchorMin = $"0.615106 {0.246296 - 0.093519}", AnchorMax = $"0.656772 {0.32037 - 0.093519}"
                    }
                    , Button = {
                        Color = "0.9686275 0.9215686 0.8823529 0.03529412", Command = $"UI_NewItems addmodifity {craftItem.name}",
                    }
                    , Text = {
                        Align = TextAnchor.MiddleCenter, Color = "1 1 1 1", Text = "<b>+</b>", FontSize = 24
                    }
                }
                , Layer, Layer + ".Btn2");
                CuiHelper.AddUi(player, container);
                UpdateItemsInfo(player, name);
                break;
                case "close" : CuiHelper.DestroyUi(player, Layer);
                CuiHelper.DestroyUi(player, LayerBlur);
                break;
                case "choicecraft" : CuiHelper.DestroyUi(player, Layer + ".Items");
                CuiHelper.DestroyUi(player, Layer + ".TextBox");
                CuiHelper.DestroyUi(player, Layer + ".Menu.MainImage");
                CuiHelper.DestroyUi(player, Layer + ".Btn1");
                CuiHelper.DestroyUi(player, Layer + ".Btn2");
                CuiHelper.DestroyUi(player, Layer + ".Btn3");
                CuiHelper.DestroyUi(player, Layer + ".CountPanel.Text");
                playerModifity[player.userID] = 1;
                container.Add(new CuiLabel() {
                    RectTransform = {
                        AnchorMin = "0.1 0", AnchorMax = "0.9 1"
                    }
                    , Text = {
                        Align = TextAnchor.MiddleCenter, Text = $"{craftItem.countCraft * playerModifity[player.userID]} шт.", FontSize = 28
                    }
                }
                , Layer + ".CountPanel", Layer + ".CountPanel.Text");
                container.Add(new CuiButton() {
                    RectTransform = {
                        AnchorMin = $"0.6635427 {0.246296 - 0.093519}", AnchorMax = $"0.8380209 {0.32037 - 0.093519}"
                    }
                    , Button = {
                        Color = "0.9686275 0.9215686 0.8823529 0.03529412", Command = $"UI_NewItems craftitem {craftItem.name}"
                    }
                    , Text = {
                        Align = TextAnchor.MiddleCenter, Color = "1 1 1 1", Text = "<b>СКРАФТИТЬ</b>", FontSize = 24
                    }
                }
                , Layer, Layer + ".Btn3");
                container.Add(new CuiButton() {
                    RectTransform = {
                        AnchorMin = $"0.463021 {0.246296 - 0.093519}", AnchorMax = $"0.504688 {0.32037 - 0.093519}"
                    }
                    , Button = {
                        Color = "0.9686275 0.9215686 0.8823529 0.03529412", Command = $"UI_NewItems removemodifity {craftItem.name}",
                    }
                    , Text = {
                        Align = TextAnchor.MiddleCenter, Color = "1 1 1 1", Text = "<b>-</b>", FontSize = 24
                    }
                }
                , Layer, Layer + ".Btn1");
                container.Add(new CuiButton() {
                    RectTransform = {
                        AnchorMin = $"0.615106 {0.246296 - 0.093519}", AnchorMax = $"0.656772 {0.32037 - 0.093519}"
                    }
                    , Button = {
                        Color = "0.9686275 0.9215686 0.8823529 0.03529412", Command = $"UI_NewItems addmodifity {craftItem.name}",
                    }
                    , Text = {
                        Align = TextAnchor.MiddleCenter, Color = "1 1 1 1", Text = "<b>+</b>", FontSize = 24
                    }
                }
                , Layer, Layer + ".Btn2");
                container.Add(new CuiPanel() {
                    RectTransform = {
                        AnchorMin = "0 0", AnchorMax = "1 1"
                    }
                    , Image = {
                        Color = "0 0 0 0"
                    }
                }
                , Layer + ".Menu", Layer + ".Items");
                container.Add(new CuiPanel() {
                    RectTransform = {
                        AnchorMin = "0 0", AnchorMax = "1 1"
                    }
                    , Image = {
                        Color = "0 0 0 0"
                    }
                }
                , Layer + ".Menu", Layer + ".TextBox");
                container.Add(new CuiElement() {
                    Parent = Layer + ".Menu", Name = Layer + ".Menu.MainImage", Components = {
                        new CuiRawImageComponent() {
                            Color = "1 1 1 1", Png = craftItem.png == null ? GetImage(craftItem.shortname) : GetImage(craftItem.png),
                        }
                        , new CuiRectTransformComponent() {
                            AnchorMin = $"0.422221 0.453911", AnchorMax = $"0.83889 0.933911"
                        }
                        ,
                    }
                }
                );
                container.Add(new CuiLabel() {
                    RectTransform = {
                        AnchorMin = $"0.184722 0.9184", AnchorMax = "0.773611 0.9952"
                    }
                    , Text = {
                        Align = TextAnchor.MiddleCenter, Color = HexToRGB("#FFFFFFD6"), Text = $"<b>{craftItem.name}</b>", FontSize = 20
                    }
                }
                , Layer + ".TextBox");
                container.Add(new CuiLabel() {
                    RectTransform = {
                        AnchorMin = $"0.020833 0.435201", AnchorMax = "0.405555 0.904"
                    }
                    , Text = {
                        Align = TextAnchor.UpperCenter, Color = HexToRGB("#FFFFFFD6"), Text = $"<size=20>Описание предмета:</size>\n{craftItem.descriptionCraft}", FontSize = 16
                    }
                }
                , Layer + ".TextBox");
                float minPosition = 0f;
                var itemWidth = (0.193633f - 0.054745f);
                var itemHeight = 0.421575f - 0.261575f;
                var itemCount = craftItem.craftItems.Count;
                var itemMargin = 0.019399f;
                if (itemCount >= 5) minPosition = 0.5f - 5 / 2f * itemWidth - (5 - 1) / 2f * itemMargin;
                else minPosition = 0.5f - itemCount / 2f * itemWidth - (itemCount - 1) / 2f * itemMargin;
                itemCount -= 5;
                var minHeight = 0.238401f;
                var modifity = 1;
                var countItem = 0;
                foreach (var item in craftItem.craftItems) {
                    countItem += 1;
                    var haveItem = HaveItem(player, item.itemID, item.skinID, item.amount * modifity);
                    container.Add(new CuiElement() {
                        Parent = Layer + ".Items", Name = Layer + $".Item{item.shortname}", Components = {
                            new CuiImageComponent() {
                                Color = haveItem ? HexToRGB("#80A47DFF") : HexToRGB("#A47D7DFF"),
                            }
                            , new CuiOutlineComponent() {
                                Color = haveItem ? HexToRGB("#A0D29CFF") : HexToRGB("#C29494FF"), Distance = "2 2",
                            }
                            , new CuiRectTransformComponent() {
                                AnchorMin = $"{minPosition} {minHeight}", AnchorMax = $"{minPosition + itemWidth} {minHeight + itemHeight}"
                            }
                            ,
                        }
                    }
                    );
                    container.Add(new CuiElement() {
                        Parent = Layer + $".Item{item.shortname}", Name = Layer + $".Item{item.shortname}.Image", Components = {
                            new CuiRawImageComponent() {
                                Color = "1 1 1 1", Png = GetImage(item.shortname),
                            }
                            , new CuiRectTransformComponent() {
                                AnchorMin = "0 0", AnchorMax = "1 1"
                            }
                            ,
                        }
                    }
                    );
                    container.Add(new CuiLabel() {
                        Text = {
                            Text = $"x{item.amount * modifity}", Align = TextAnchor.LowerRight, FontSize = 12
                        }
                        , RectTransform = {
                            AnchorMin = "0 0", AnchorMax = "1 1", OffsetMax = "-3 -3"
                        }
                    }
                    , Layer + $".Item{item.shortname}.Image");
                    if (countItem % 5 == 0) {
                        if (itemCount > 5) {
                            minPosition = 0.5f - 5 / 2f * itemWidth - (5 - 1) / 2f * itemMargin;
                            itemCount -= 5;
                        }
                        else {
                            minPosition = 0.5f - itemCount / 2f * itemWidth - (itemCount - 1) / 2f * itemMargin;
                        }
                        minHeight -= ((itemMargin * 2) + itemHeight);
                    }
                    else {
                        minPosition += (itemWidth + itemMargin);
                    }
                }
                CuiHelper.AddUi(player, container);
                break;
            }
        }
        [ChatCommand("craft")]
        void chatCmdCraft(BasePlayer player, string command, string[] args) {
            CuiHelper.DestroyUi(player, Layer);
            CuiHelper.DestroyUi(player, LayerBlur);
            playerModifity[player.userID] = 1;
            CuiElementContainer container = new CuiElementContainer();
            container.Add(new CuiPanel() {
                CursorEnabled = true, RectTransform = {
                    AnchorMin = "0 0", AnchorMax = "1 1", OffsetMax = "0 0"
                }
                , Image = {
                    Color = HexToRGB("#202020C2"), Material = "assets/content/ui/uibackgroundblur.mat"
                }
            }
            , "Overlay", LayerBlur);
            container.Add(new CuiButton() {
                RectTransform = {
                    AnchorMin = "0 0", AnchorMax = "1 1"
                }
                , Button = {
                    Command = "UI_NewItems close", Color = "0 0 0 0"
                }
                , Text = {
                    Text = ""
                }
            }
            , "Overlay", Layer);
            container.Add(new CuiLabel() {
                RectTransform = {
                    AnchorMin = "0.2942708 0.9148148", AnchorMax = "0.6432291 0.9898146"
                }
                , Text = {
                    Align = TextAnchor.MiddleCenter, Text = "<b>СИСТЕМА УНИКАЛЬНЫХ ПРЕДМЕТОВ</b>", FontSize = 28
                }
            }
            , Layer);
            var buttonHeight = (0.926852f - 0.873148f);
            var buttonMargin = (0.873148f - 0.853706f);
            var buttonCount = _config.items.Count;
            var buttonMinHeight = 0.45f + buttonCount / 2f * buttonHeight + (buttonCount - 1) / 2f * buttonMargin;
            foreach (var craftItem in _config.items) {
                container.Add(new CuiButton() {
                    RectTransform = {
                        AnchorMin = $"{0.008854} {buttonMinHeight}", AnchorMax = $"{0.225521} {buttonMinHeight + buttonHeight}"
                    }
                    , Button = {
                        Color = "0.9686275 0.9215686 0.8823529 0.03529412", Command = $"UI_NewItems choicecraft {craftItem.name}",
                    }
                    , Text = {
                        Align = TextAnchor.MiddleCenter, Color = "1 1 1 1", Text = craftItem.textBtn, FontSize = 18
                    }
                }
                , Layer);
                buttonMinHeight -= (buttonHeight + buttonMargin);
            }
            var currentCraft = _config.items.ElementAt(0);
            container.Add(new CuiPanel() {
                RectTransform = {
                    AnchorMin = $"0.463021 {0.332407 - 0.093519}", AnchorMax = $"0.838021 {0.911111 - 0.093519}"
                }
                , Image = {
                    Color = "0.9686275 0.9215686 0.8823529 0.03529412",
                }
            }
            , Layer, Layer + ".Menu");
            container.Add(new CuiPanel() {
                RectTransform = {
                    AnchorMin = "0 0", AnchorMax = "1 1"
                }
                , Image = {
                    Color = "0 0 0 0"
                }
            }
            , Layer + ".Menu", Layer + ".Items");
            container.Add(new CuiElement() {
                Parent = Layer + ".Menu", Name = Layer + ".Menu.MainImage", Components = {
                    new CuiRawImageComponent() {
                        Color = "1 1 1 1", Png = currentCraft.png == null ? GetImage(currentCraft.shortname) : GetImage(currentCraft.png),
                    }
                    , new CuiRectTransformComponent() {
                        AnchorMin = $"0.422221 0.453911", AnchorMax = $"0.83889 0.933911"
                    }
                    ,
                }
            }
            );
            container.Add(new CuiPanel() {
                RectTransform = {
                    AnchorMin = $"0.510417 {0.246296 - 0.093519}", AnchorMax = $"0.609375 {0.32037 - 0.093519}"
                }
                , Image = {
                    Color = "0.9686275 0.9215686 0.8823529 0.03529412",
                }
                ,
            }
            , Layer, Layer + ".CountPanel");
            var modifity = 1;
            container.Add(new CuiLabel() {
                RectTransform = {
                    AnchorMin = "0.1 0", AnchorMax = "0.9 1"
                }
                , Text = {
                    Align = TextAnchor.MiddleCenter, Text = $"{currentCraft.countCraft * modifity} шт.", FontSize = 28
                }
            }
            , Layer + ".CountPanel", Layer + ".CountPanel.Text");
            container.Add(new CuiButton() {
                RectTransform = {
                    AnchorMin = $"0.463021 {0.246296 - 0.093519}", AnchorMax = $"0.504688 {0.32037 - 0.093519}"
                }
                , Button = {
                    Color = "0.9686275 0.9215686 0.8823529 0.03529412", Command = $"UI_NewItems removemodifity {currentCraft.name}",
                }
                , Text = {
                    Align = TextAnchor.MiddleCenter, Color = "1 1 1 1", Text = "<b>-</b>", FontSize = 24
                }
            }
            , Layer, Layer + ".Btn1");
            container.Add(new CuiButton() {
                RectTransform = {
                    AnchorMin = $"0.615106 {0.246296 - 0.093519}", AnchorMax = $"0.656772 {0.32037 - 0.093519}"
                }
                , Button = {
                    Color = "0.9686275 0.9215686 0.8823529 0.03529412", Command = $"UI_NewItems addmodifity {currentCraft.name}",
                }
                , Text = {
                    Align = TextAnchor.MiddleCenter, Color = "1 1 1 1", Text = "<b>+</b>", FontSize = 24
                }
            }
            , Layer, Layer + ".Btn2");
            container.Add(new CuiButton() {
                RectTransform = {
                    AnchorMin = $"0.6635427 {0.246296 - 0.093519}", AnchorMax = $"0.8380209 {0.32037 - 0.093519}"
                }
                , Button = {
                    Color = "0.9686275 0.9215686 0.8823529 0.03529412", Command = $"UI_NewItems craftitem {currentCraft.name}"
                }
                , Text = {
                    Align = TextAnchor.MiddleCenter, Color = "1 1 1 1", Text = "<b>СКРАФТИТЬ</b>", FontSize = 24
                }
            }
            , Layer, Layer + ".Btn3");
            container.Add(new CuiPanel() {
                RectTransform = {
                    AnchorMin = "0 0", AnchorMax = "1 1"
                }
                , Image = {
                    Color = "0 0 0 0"
                }
            }
            , Layer + ".Menu", Layer + ".TextBox");
            container.Add(new CuiLabel() {
                RectTransform = {
                    AnchorMin = $"0.184722 0.9184", AnchorMax = "0.773611 0.9952"
                }
                , Text = {
                    Align = TextAnchor.MiddleCenter, Color = HexToRGB("#FFFFFFD6"), Text = $"<b>{currentCraft.name}</b>", FontSize = 20
                }
            }
            , Layer + ".TextBox");
            container.Add(new CuiLabel() {
                RectTransform = {
                    AnchorMin = $"0.020833 0.435201", AnchorMax = "0.405555 0.904"
                }
                , Text = {
                    Align = TextAnchor.UpperCenter, Color = HexToRGB("#FFFFFFD6"), Text = $"<size=20>Описание предмета:</size>\n{currentCraft.descriptionCraft}", FontSize = 16
                }
            }
            , Layer + ".TextBox");
            float minPosition = 0f;
            var itemWidth = (0.193633f - 0.054745f);
            var itemHeight = 0.421575f - 0.261575f;
            var itemCount = currentCraft.craftItems.Count;
            var itemMargin = 0.019399f;
            if(itemCount >= 5) minPosition = 0.5f - 5 / 2f * itemWidth - (5 - 1) / 2f * itemMargin;
            else minPosition = 0.5f - itemCount / 2f * itemWidth - (itemCount - 1) / 2f * itemMargin;
            itemCount -= 5;
            var minHeight = 0.238401f;
            var countItem = 0;
            foreach (var item in currentCraft.craftItems) {
                countItem += 1;
                var haveItem = HaveItem(player, item.itemID, item.skinID, item.amount * modifity);
                container.Add(new CuiElement() {
                    Parent = Layer + ".Items", Name = Layer + $".Item{item.shortname}", Components = {
                        new CuiImageComponent() {
                            Color = haveItem ? HexToRGB("#80A47DFF") : HexToRGB("#A47D7DFF"), Sprite = "Assets/Content/UI/UI.Background.Tile.psd"
                        }
                        , new CuiOutlineComponent() {
                            Color = haveItem ? HexToRGB("#A0D29CFF") : HexToRGB("#C29494FF"), Distance = "2 2",
                        }
                        , new CuiRectTransformComponent() {
                            AnchorMin = $"{minPosition} {minHeight}", AnchorMax = $"{minPosition + itemWidth} {minHeight + itemHeight}"
                        }
                        ,
                    }
                }
                );
                container.Add(new CuiElement() {
                    Parent = Layer + $".Item{item.shortname}", Name = Layer + $".Item{item.shortname}.Image", Components = {
                        new CuiRawImageComponent() {
                            Color = "1 1 1 1", Png = GetImage(item.shortname),
                        }
                        , new CuiRectTransformComponent() {
                            AnchorMin = "0 0", AnchorMax = "1 1"
                        }
                        ,
                    }
                }
                );
                container.Add(new CuiLabel() {
                    Text = {
                        Text = $"x{item.amount * modifity}", Align = TextAnchor.LowerRight, FontSize = 12
                    }
                    , RectTransform = {
                        AnchorMin = "0 0", AnchorMax = "1 1", OffsetMax = "-3 -3"
                    }
                }
                , Layer + $".Item{item.shortname}.Image");
                if (countItem % 5 == 0) {
                    if (itemCount > 5) {
                        minPosition = 0.5f - 5 / 2f * itemWidth - (5 - 1) / 2f * itemMargin;
                        itemCount -= 5;
                    }
                    else {
                        minPosition = 0.5f - itemCount / 2f * itemWidth - (itemCount - 1) / 2f * itemMargin;
                    }
                    minHeight -= ((itemMargin * 2) + itemHeight);
                }
                else {
                    minPosition += (itemWidth + itemMargin);
                }
            }
            CuiHelper.AddUi(player, container);
        }
#endregion
#region Hooks
        object CanStackItem(Item item, Item item2) => CanStack(item, item2);
        object CanCombineDroppedItem(DroppedItem item, DroppedItem item2) => CanStack(item.item, item2.item);
        object OnItemSplit(Item item, int amount) {
            if (item.name == null) return null;
            var configItem = _config.items.Find(x => x.name.Contains(item.name));
            if (configItem == null) return null;
            item.amount -= amount;
            var byItem = ItemManager.Create(item.info, amount, item.skin);
            byItem.name = configItem.name;
            item.name = configItem.name;
            item.MarkDirty();
            return byItem;
        }
        public Item FindItem(BasePlayer player, int itemID, ulong skinID, int amount) {
            Item item = null;
            if (skinID == 0U) {
                if (player.inventory.FindItemID(itemID) != null && player.inventory.FindItemID(itemID).amount >= amount) return player.inventory.FindItemID(itemID);
            }
            else {
                List<Item> items = new List<Item>();
                items.AddRange(player.inventory.FindItemIDs(itemID));
                foreach (var findItem in items) {
                    if (findItem.skin == skinID && findItem.amount >= amount) {
                        return findItem;
                    }
                }
            }
            return item;
        }
        object OnDispenserGather(ResourceDispenser dispenser, BaseEntity entity, Item item) {
            var player = entity.ToPlayer();
            if (player == null) return null;
            if (player.IsNpc) return null;
            if (player.GetActiveItem().name != null && _config.items.Find(x => x.name.Contains(player.GetActiveItem().name)) != null) {
                item.amount *= 2;
            }
            return null;
        }
        object OnDispenserBonus(ResourceDispenser dispenser, BasePlayer player, Item item) {
            if (player == null) return null;
            if (player.IsNpc) return null;
            if (player.GetActiveItem().name != null && _config.items.Find(x => x.name.Contains(player.GetActiveItem().name)) != null) {
                item.amount *= 2;
            }
            return null;
        }
        private void OnEntityTakeDamage(BaseCombatEntity entity, HitInfo info) {
            try {
                if (entity is BasePlayer && entity.GetComponent<NPCPlayer>() == null && info != null) {
                    BasePlayer player = entity as BasePlayer;
                    BasePlayer target = null;
                    var item = player.inventory.containerWear.FindItemByItemID(1110385766);
                    if (item != null && item.name != null) {
                        return;
                    }
                    if (info.Initiator is BasePlayer && info.Initiator.GetComponent<NPCPlayer>() == null) target = info.InitiatorPlayer;
                    if (info.damageTypes.Total() < 5) return;
                    float protectMultiplier = 1 - (30 / 100);
                    info.damageTypes.ScaleAll(protectMultiplier);
                }
            }
            catch {
            }
        }
        object OnHealingItemUse(MedicalTool tool, BasePlayer player) {
            if (tool != null && player != null && tool.GetItem().info.itemid == 1079279582 && tool.GetItem().name != null) {
                player.Heal(100);
            }
            return null;
        }
        private bool? CanWearItem(PlayerInventory inventory, Item item) {
            var player = inventory.gameObject.ToBaseEntity() as BasePlayer;
            if (player == null) return false;
            if (player.IsNpc) return null;
            if (item.name != null && item.info.itemid == -194953424 && player.GetComponent<UpdateWaterOxygen>() != null) {
                var component = player.gameObject.GetComponent<UpdateWaterOxygen>();
                component.Destroy();
                return null;
            }
            if (item.name != null && item.info.itemid == -194953424) {
                player.gameObject.AddComponent<UpdateWaterOxygen>();
            }
            return null;
        }
        object OnItemAction(Item item, string action, BasePlayer player) {
            if (item.name != null) {
                var newItem = _config.items.Find(x => x.name.StartsWith(item.name));
                if (newItem != null) {
                    if (action.Equals("consume")) {
                        player.metabolism.calories.SetValue(500);
                        player.metabolism.hydration.SetValue(250);
                    }
                }
            }
            return null;
        }
        private void OnServerInitialized() {
            LoadConfig();
            foreach (var player in BasePlayer.activePlayerList) OnPlayerInit(player);
            SaveConfig();
            Dictionary<string, string> images = new Dictionary<string, string>();
            foreach (var craftItem in _config.items) {
                if (craftItem.png != null) {
                    ImageLibrary.Call("AddImage", craftItem.png, craftItem.png);
                }
                foreach (var item in craftItem.craftItems) {
                    ImageLibrary.Call("AddImage", $"https://rustlabs.com/img/items180/{item.shortname}.png", item.shortname);
                }
            }
        }
        void OnPlayerInit(BasePlayer player) {
            if(!playerModifity.ContainsKey(player.userID)) playerModifity.Add(player.userID, 1);
            var item = player.inventory.containerWear.FindItemByItemID(-194953424);
            if (item != null && item.name != null) {
                player.gameObject.AddComponent<UpdateWaterOxygen>();
            }
        }
        private void Unload() {
            foreach (var player in BasePlayer.activePlayerList) {
                CuiHelper.DestroyUi(player, Layer);
                CuiHelper.DestroyUi(player, LayerBlur);
                if (player.metabolism.GetComponent<UpdateWaterOxygen>()) {
                    var obj = player.metabolism.GetComponent<UpdateWaterOxygen>();
                    obj.Destroy();
                }
            }
        }
#endregion
#region Methods
        object CanStack(Item item, Item item2) {
            if (item.info.displayName.english == item2.info.displayName.english) {
                if (_config.items.Find(x => item.name != null && x.name.Contains(item.name)) != null) {
                    if (item.skin == item2.skin) return null;
                    return false;
                }
            }
            return null;
        }
        private static bool IsInWater(BasePlayer player) {
            var modelState = player.modelState;
            return modelState != null && modelState.waterLevel > 0f;
        }
        public bool HaveItem(BasePlayer player, int itemID, ulong skinID, int amount) {
            if (skinID == 0U) {
                if (player.inventory.FindItemID(itemID) != null && player.inventory.FindItemID(itemID).amount >= amount) return true;
                return false;
            }
            else {
                List<Item> items = new List<Item>();
                items.AddRange(player.inventory.FindItemIDs(itemID));
                foreach (var item in items) {
                    if (item.skin == skinID && item.amount >= amount) {
                        return true;
                    }
                }
            }
            return false;
        }
#endregion
#region Helper
        private bool ImportImageList(string title, Dictionary<string, string> images) {
            plugins.Find("ImageLibrary").Call("ImportImageList", title, images);
            return true;
        }
        private bool HasImage(string fileName, ulong imageId) {
            return (bool)plugins.Find("ImageLibrary").Call("HasImage", fileName, imageId);
        }
        private void AddImage(string url, string fileName) {
            plugins.Find("ImageLibrary").Call("AddImage", url, fileName, 0);
        }
        private string GetImage(string fileName, ulong skin = 0) {
            var imageId = (string) plugins.Find("ImageLibrary").CallHook("GetImage", fileName, skin);
            if (!string.IsNullOrEmpty(imageId)) return imageId;
            return string.Empty;
        }
        private static string HexToRGB(string hex) {
            if (string.IsNullOrEmpty(hex)) {
                hex = "#FFFFFFFF";
            }
            var str = hex.Trim('#');
            if (str.Length == 6) str += "FF";
            if (str.Length != 8) {
                throw new Exception(hex);
                throw new InvalidOperationException("Cannot convert a wrong format.");
            }
            var r = byte.Parse(str.Substring(0, 2), NumberStyles.HexNumber);
            var g = byte.Parse(str.Substring(2, 2), NumberStyles.HexNumber);
            var b = byte.Parse(str.Substring(4, 2), NumberStyles.HexNumber);
            var a = byte.Parse(str.Substring(6, 2), NumberStyles.HexNumber);
            Color color = new Color32(r, g, b, a);
            return $"{color.r:F2} {color.g:F2} {color.b:F2} {color.a:F2}";
        }
#endregion
#region Class
        public class UpdateWaterOxygen : MonoBehaviour {
            private BasePlayer player = null;
            void Awake() {
                player = GetComponent<BasePlayer>();
                InvokeHandler.InvokeRepeating(this, CheckWater, 1f, 1f);
            }
            void CheckWater() {
                if(!player.IsConnected) Destroy();
                var item = player.inventory.containerWear.FindItemByItemID(-194953424);
                if (item == null) {
                    Destroy();
                    return;
                }
                if (IsInWater(player)) {
                    player.metabolism.oxygen.SetValue(1);
                    item.condition -= 1;
                }
            }
            public void Destroy() {
                CancelInvoke();
                Destroy(this);
            }
        }
#endregion
#region Config
        protected override void SaveConfig() {
            Config.WriteObject(_config);
        }
        protected override void LoadConfig() {
            base.LoadConfig();
            _config = Config.ReadObject<Configuration>();
        }
        protected override void LoadDefaultConfig() {
            _config = new Configuration() {
                items = new List<ItemInfo>()
            }
            ;
        }
        public Configuration _config;
        public class Configuration {
            [JsonProperty("Новые предметы")]
            public List<ItemInfo> items = new List<ItemInfo>();
        }
        public class ItemInfo {
            [JsonProperty("Название предмета")]
            public string name = "";
            [JsonProperty("Текст кнопки")]
            public string textBtn = "";
            [JsonProperty("Шортнейм предмета")]
            public string shortname = "";
            [JsonProperty("Айди предмета")]
            public int idItem = 0;
            [JsonProperty("Крафтить шт")]
            public int countCraft = 1;
            [JsonProperty("СкинИД предмета[Не менять]")]
            public ulong skinID = 0U;
            [JsonProperty("Ссылка на иконку")]
            public string png = null;
            [JsonProperty("Отображать крафт предмета")]
            public bool isCrafted = true;
            [JsonProperty("Описание для крафта")]
            public string descriptionCraft = "Тестовый предмет";
            [JsonProperty("Предметы для крафта")]
            public List<CraftInfo> craftItems = new List<CraftInfo>();
        }
        public class CraftInfo {
            [JsonProperty("Шортнейм предмета")]
            public string shortname = "";
            [JsonProperty("Количество предмета")]
            public int amount = 0;
            [JsonProperty("СкинИД предмета")]
            public ulong skinID = 0U;
            [JsonProperty("АйтемИД предмета")]
            public int itemID = 0;
        }
#endregion
    }
}

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