organize esse código:
using System.Collections.Generic;
using Oxide.Core.Plugins;
using Oxide.Game.Rust.Cui;
using UnityEngine;

namespace Oxide.Plugins
{
    [Info("QuarryComputerUi", "Marte6", "1.0.0")]
    public class QuarryComputerUi : RustPlugin
    {
        [PluginReference]
        Plugin ImageLibrary;

        private static readonly HashSet<BasePlayer> playersWithPopup = new();

        #region Initialization

        private void Init()
        {
            DestroyAllUi();
        }

        #endregion

        #region Commands

        [ChatCommand("ui")]
        private void CmdOpenUi(BasePlayer player)
        {
            DestroyUi(player);
            CreateUi(player);
        }

        [ConsoleCommand("quarrycompui.close")]
        private void CmdClose(ConsoleSystem.Arg arg)
        {
            var player = arg.Player();
            if (player != null)
            {
                DestroyUi(player);
                PrintToChat(player, "Close button clicked.");
            }
        }

        [ConsoleCommand("quarrycompui.remove")]
        private void CmdRemoveQuarry(ConsoleSystem.Arg arg)
        {
            var player = arg.Player();
            TogglePopup(player, "Remove the Quarry?", "quarrycompui.confirmremove", "quarrycompui.cancelremove");
        }

        [ConsoleCommand("quarrycompui.confirmremove")]
        private void CmdConfirmRemove(ConsoleSystem.Arg arg)
        {
            var player = arg.Player();
            if (player != null)
            {
                ClosePopupAndNotify(player, "Quarry removed.");
            }
        }

        [ConsoleCommand("quarrycompui.cancelremove")]
        private void CmdCancelRemove(ConsoleSystem.Arg arg)
        {
            var player = arg.Player();
            if (player != null)
            {
                ClosePopupAndNotify(player, "Removal cancelled.");
            }
        }

        [ConsoleCommand("quarrycompui.upgrade")]
        private void CmdUpgradeQuarry(ConsoleSystem.Arg arg)
        {
            var player = arg.Player();
            TogglePopup(player, "Upgrade the Quarry?", "quarrycompui.confirmupgrade", "quarrycompui.cancelupgrade");
        }

        [ConsoleCommand("quarrycompui.confirmupgrade")]
        private void CmdConfirmUpgrade(ConsoleSystem.Arg arg)
        {
            var player = arg.Player();
            if (player != null)
            {
                ClosePopupAndNotify(player, "Quarry upgraded.");
            }
        }

        [ConsoleCommand("quarrycompui.cancelupgrade")]
        private void CmdCancelUpgrade(ConsoleSystem.Arg arg)
        {
            var player = arg.Player();
            if (player != null)
            {
                ClosePopupAndNotify(player, "Upgrade cancelled.");
            }
        }

        #endregion


        private void Texts(string[] texts, float anchorX, float initialAnchorY, float spacing, CuiElementContainer container, string parent)
        {
            for (int i = 0; i < texts.Length; i++)
            {
                float anchorY = initialAnchorY - (spacing * i);
                AddText(container, parent, $"{anchorX} {anchorY}", $"{anchorX + 0.8f} {anchorY + 0.06}", texts[i], 12, TextAnchor.MiddleLeft);
            }
        }

        private void CreateUi(BasePlayer player)
        {
            var container = new CuiElementContainer();

            AddPanel(container, "0 0", "1 1", "QuarryCompUi_Background", "Overlay", "0 0 0 0.9", true);
            AddPanel(container, "0.235 0.18", "0.765 0.784", "QuarryCompUi_MainPanel", "QuarryCompUi_Background", "0.6 0.6 0.6 0.95", true);
            AddInnerSquare(container, "QuarryCompUi_MainPanel", "0 0.9570", "1 1", "0.39 0.39 0.39 1");
            AddText(container, "QuarryCompUi_MainPanel", "0.02 0.96", "1 1", "Extraction Interval: 1 sec | Fuel per extraction: 1 Low Grade Fuel", 13, TextAnchor.MiddleLeft);
            AddCloseButton(container, "QuarryCompUi_MainPanel");

            AddImage(container, "QuarryCompUi_Background", "crude.oil", "0.1 0.1", "0.2 0.2", "ResImg");

            AddPanel(container, "0.0 0", "0.5 0.94", "QuarryCompUi_Full_Left", "QuarryCompUi_MainPanel", "0 0 0 0");
            AddInnerSquare(container, "QuarryCompUi_Full_Left", "0.10 0.05", "0.95 0.90", "0 0 0 0.97");
            AddInnerSquare(container, "QuarryCompUi_Full_Left", "0.054 0.1033", "0.895 0.945", "0.8 0.8 0.8 1");
            AddInnerSquare(container, "QuarryCompUi_Full_Left", "0.06 0.108", "0.89 0.94", "0 0 1 1");
            AddInnerSquare(container, "QuarryCompUi_Full_Left", "0.06 0.865", "0.89 0.870", "0.8 0.8 0.8 1");
            AddText(container, "QuarryCompUi_Full_Left", "0.1 0.84", "0.9 0.98", "Quarry Level 1", 15, TextAnchor.MiddleCenter);

            Texts(new[] { "Storage", "Fuel: 3 slots", "Resource: 6 slots" }, 0.1f, 0.8f, 0.04f, container, "QuarryCompUi_Full_Left");

            AddInnerSquare(container, "QuarryCompUi_Full_Left", "0.06 0.70", "0.89 0.705", "0.8 0.8 0.8 1");

            Texts(new[] { "Extraction", "Crude Oil: 10", "HQM Ore: 60", "Metal Ore: 100", "Stones: 250", "Sulfur Ore: 160" }, 0.1f, 0.635f, 0.04f, container, "QuarryCompUi_Full_Left");

            AddButton(container, "QuarryCompUi_Full_Left", "Remove Quarry", "quarrycompui.remove", "0.28", "0.11", "0.70", "0.16", "0.6 0.6 0.6 0.95", "1 0 0 1");

            AddPanel(container, "0.5 0", "1 0.94", "QuarryCompUi_Full_Right", "QuarryCompUi_MainPanel", "0 0 0 0");
            AddInnerSquare(container, "QuarryCompUi_Full_Right", "0.10 0.05", "0.95 0.90", "0 0 0 0.97");
            AddInnerSquare(container, "QuarryCompUi_Full_Right", "0.054 0.1033", "0.895 0.945", "0.8 0.8 0.8 1");
            AddInnerSquare(container, "QuarryCompUi_Full_Right", "0.06 0.108", "0.89 0.94", "0.55 0 0 1");
            AddInnerSquare(container, "QuarryCompUi_Full_Right", "0.06 0.865", "0.89 0.870", "0.8 0.8 0.8 1");

            AddText(container, "QuarryCompUi_Full_Right", "0.05 0.84", "0.9 0.98", "Upgrade to Level 2", 15, TextAnchor.MiddleCenter);

            Texts(new[] { "Storage", "Fuel: 6 slots", "Resource: 12 slots" }, 0.1f, 0.8f, 0.04f, container, "QuarryCompUi_Full_Right");
            AddInnerSquare(container, "QuarryCompUi_Full_Right", "0.06 0.70", "0.89 0.705", "0.8 0.8 0.8 1");

            Texts(new[] { "Extraction", "Crude Oil: 20", "HQM Ore: 100", "Metal Ore: 150", "Stones: 300", "Sulfur Ore: 200" }, 0.1f, 0.635f, 0.04f, container, "QuarryCompUi_Full_Right");

            AddInnerSquare(container, "QuarryCompUi_Full_Right", "0.06 0.425", "0.89 0.430", "0.8 0.8 0.8 1");

            Texts(new[] { "Requirements", "Scrap: 5000", "Bone Fragments: 10000" }, 0.1f, 0.360f, 0.04f, container, "QuarryCompUi_Full_Right");

            AddButton(container, "QuarryCompUi_Full_Right", "Upgrade", "quarrycompui.upgrade", "0.35", "0.11", "0.65", "0.16", "0.6 0.6 0.6 0.95", "0 1 0 1");

            CuiHelper.AddUi(player, container);
        }

        private void AddImage(CuiElementContainer container, string parent, string resourceName, string anchorMin, string anchorMax, string imageName)
        {
            string imageUrl = ImageLibrary?.Call("GetImage", resourceName) as string;
            if (string.IsNullOrEmpty(imageUrl))
            {
                PrintWarning($"Failed to get image for item: {resourceName}");
                return;
            }

            container.Add(
                new CuiElement
                {
                    Name = imageName,
                    Parent = parent,
                    Components =
                    {
                        new CuiRawImageComponent { Png = imageUrl },
                        new CuiRectTransformComponent { AnchorMin = anchorMin, AnchorMax = anchorMax },
                    },
                }
            );
        }

        private void AddCloseButton(CuiElementContainer container, string parent)
        {
            container.Add(
                new CuiButton
                {
                    Button =
                    {
                        Color = "1 0 0 1",
                        Command = "quarrycompui.close",
                        Close = "QuarryCompUi_Background",
                    },
                    RectTransform = { AnchorMin = "0.965 0.957", AnchorMax = "1 1" },
                    Text =
                    {
                        Text = "✖",
                        FontSize = 13,
                        Align = TextAnchor.MiddleCenter,
                        Color = "1 1 1 0.5",
                    },
                },
                parent
            );
        }

        private void DestroyUi(BasePlayer player)
        {
            if (player == null)
                return;

            var componentNames = new[]
            {
                "QuarryCompUi_Background",
                "QuarryCompUi_MainPanel",
                "Overlay_Block",
                "Popup_Shadow",
                "Popup_Border",
                "Popup_Background",
                "QuarryCompUi_Full_Left",
                "QuarryCompUi_Full_Right",
            };

            foreach (var name in componentNames)
            {
                CuiHelper.DestroyUi(player, name);
            }

            playersWithPopup.Remove(player);
        }

        private void DestroyAllUi()
        {
            foreach (var player in BasePlayer.activePlayerList)
            {
                DestroyUi(player);
                timer.Once(1f, () => CreateUi(player));
            }
        }

        private void TogglePopup(BasePlayer player, string message, string confirmCmd, string cancelCmd)
        {
            if (player != null && !playersWithPopup.Contains(player))
            {
                playersWithPopup.Add(player);
                ShowPopup(player, message, confirmCmd, cancelCmd);
            }
        }

        private void ShowPopup(BasePlayer player, string message, string confirmCmd, string cancelCmd)
        {
            var container = new CuiElementContainer();
            AddPanel(container, "0 0", "1 1", "Overlay_Block", "Overlay", "0 0 0 0.9", true);
            AddPanel(container, "0.4 0.4", "0.6 0.6", "Popup_Shadow", "Overlay_Block", "0 0 0 0.85");
            AddPanel(container, "0.0 0.09", "0.95 1.0", "Popup_Border", "Popup_Shadow", "1 1 1 1");
            AddPanel(container, "0.01 0.01", "0.991 0.985", "Popup_Background", "Popup_Border", "0 0 0 0.97");
            AddText(container, "Popup_Background", "0.1 0.6", "0.9 0.9", message, 16, TextAnchor.MiddleCenter);
            AddButton(container, "Popup_Background", "Yes", confirmCmd, "0.25", "0.05", "0.45", "0.15", "0.6 0.6 0.6 0.95", "0 1 0 1");
            AddButton(container, "Popup_Background", "No", cancelCmd, "0.55", "0.05", "0.75", "0.15", "0.6 0.6 0.6 0.95", "1 0 0 1");
            CuiHelper.AddUi(player, container);
        }

        private void ClosePopupAndNotify(BasePlayer player, string message)
        {
            if (player != null)
            {
                foreach (var elem in new[] { "Overlay_Block", "Popup_Shadow", "Popup_Border", "Popup_Background" })
                {
                    CuiHelper.DestroyUi(player, elem);
                }

                playersWithPopup.Remove(player);
                PrintToChat(player, message);
            }
        }

        #region UI Helpers

        private void AddPanel(CuiElementContainer container, string anchorMin, string anchorMax, string panelName, string parent, string color, bool isCursorEnabled = false)
        {
            var panel = new CuiPanel
            {
                Image = { Color = color },
                RectTransform = { AnchorMin = anchorMin, AnchorMax = anchorMax },
                CursorEnabled = isCursorEnabled,
            };

            container.Add(panel, parent, panelName);
        }

        private void AddInnerSquare(CuiElementContainer container, string parent, string anchorMin, string anchorMax, string color)
        {
            container.Add(new CuiPanel { Image = { Color = color }, RectTransform = { AnchorMin = anchorMin, AnchorMax = anchorMax } }, parent);
        }

        private void AddText(CuiElementContainer container, string parent, string anchorMin, string anchorMax, string text, int fontSize, TextAnchor alignment)
        {
            container.Add(
                new CuiLabel
                {
                    Text =
                    {
                        Text = text,
                        FontSize = fontSize,
                        Font = "DroidSansMono.ttf",
                        Align = alignment,
                        Color = "1 1 1 1",
                    },
                    RectTransform = { AnchorMin = anchorMin, AnchorMax = anchorMax },
                },
                parent
            );
        }

        private void AddButton(
            CuiElementContainer container,
            string parent,
            string text,
            string command,
            string anchorMinX,
            string anchorMinY,
            string anchorMaxX,
            string anchorMaxY,
            string bgColor,
            string textColor
        )
        {
            container.Add(
                new CuiButton
                {
                    Button =
                    {
                        Color = bgColor,
                        Command = command,
                        Close = "",
                    },
                    RectTransform = { AnchorMin = $"{anchorMinX} {anchorMinY}", AnchorMax = $"{anchorMaxX} {anchorMaxY}" },
                    Text =
                    {
                        Text = text,
                        FontSize = 14,
                        Font = "DroidSansMono.ttf",
                        Align = TextAnchor.MiddleCenter,
                        Color = textColor,
                    },
                },
                parent
            );
        }
        #endregion
    }
}