Skip to content
The Chat Archive
gpt-4o-2024-08-06· OpenAI WildChat-4.8M (AI2)

using System; using System.Collections.Generic; using System.Linq; using Facepunch;…

  1. User

    using System; using System.Collections.Generic; using System.Linq; using Facepunch; using Newtonsoft.Json; using Oxide.Core; using Oxide.Core.Libraries.Covalence; using Oxide.Core.Plugins; using Oxide.Game.Rust.Cui; using UnityEngine;

    namespace Oxide.Plugins { [Info("Quarry Computer", "Marte6", "1.0.0")] [Description("Quarry in a Computer with configurations and UI")] public class QuarryComputer : CovalencePlugin { [PluginReference] private Plugin ImageLibrary = null, LangAPI = null, Economics = null, ServerRewards = null, Notify = null;

        #region Constants
        private const ulong QuarrySkinId = 3341965622;
    
        private const string BaseItemName = "arcade.machine.chippy";
        private const string BaseDeployedItemName = "chippyarcademachine";
        private const string QuarryPrefab = "assets/content/structures/excavator/prefabs/excavatorsignalcomputer.prefab";
        private const string QuarryOutputStoragePrefab = "assets/prefabs/deployable/hot air balloon/subents/hab_storage.prefab";
        private const string QuarryFuelStoragePrefab = "assets/content/vehicles/boats/rowboat/subents/rowboat_storage.prefab";
        private const string StorageAdaptorPrefab = "assets/prefabs/deployable/playerioents/industrialadaptors/storageadaptor.deployed.prefab";
        private const string ButtonPrefab = "assets/prefabs/io/electric/switches/pressbutton/pressbutton.prefab";
        private const string GroundLossFx = "assets/prefabs/tools/pager/effects/beep.prefab";
        private const string QuarryTurnedOnFx = "assets/prefabs/npc/autoturret/effects/targetacquired.prefab";
        private const string QuarryUpgradedFx = "assets/bundled/prefabs/fx/build/promote_toptier.prefab";
    
        private const string UiBackground = "6iYcl0UppZxwaotNu6kghxTF7s8a4r4d";
        private const string UiMainPanel = "VsUNAYUweeIfW5qvG1zHcQ09hy06GGFi";
        private const string UiLeftPanel = "6k3PPUUOyd8A6RgqGBan9cMLn59GTzRS";
        private const string UiRightPanel = "o9C5OcIYWCoOsRpcULoyfKPGGxb8LAoJ";
        private const string UiPopupOverlayBlock = "Mu8AqzfN17Djw3TstzIsjnJqKi4W5n5j";
        private const string UiPopupShadow = "JVQZBnIR9vmF844dkfYsuOymwWBTCeJV";
        private const string UiPopupBorder = "tmZZ1WqIBuBlqvNeabQH6D6wvK5Gfz7u";
        private const string UiPopupBackground = "3kNfPEASr9RkcBTB9vtMLoxkpKfqc4FU";
        private const string UiButtonStart = "TB9vtMLoxk3kNfPEASr9RkcBpKfqc4FU";
    
        private const string CommandUiClose = "3bizteteO5ic8z82GJHeQx77hlDMLcI9";
        private const string CommandUiStart = "n4DHuNvEGFpIW2gCuZ4ZftJzQBLHpZUx";
        private const string CommandUiRemove = "fl6n4DHugCuZKCrLHpZUF4ZNvEGIW2px";
        private const string CommandUiConfirmRemove = "PTZdQzOalxnZFiwDkiKgMdk1wbuG6T64";
        private const string CommandUiCancelRemove = "09stuljgGWtJzQBvFevq2chgWvZNvodr";
        private const string CommandUiUpgrade = "mSjiZxaTD4hdq9hUHcLGdQviIPzfksgK";
        private const string CommandUiConfirmUpgrade = "gTpJiMwJn7JSbSWFr8fSjY4nHg72Fryl";
        private const string CommandUiCancelUpgrade = "B3xei8Zq8VVr4zptxJxYxEfW3sh40x82";
    
        private const Item.Flag ExtractedItemFlag = (Item.Flag)(1 << 78);
        #endregion
    
        #region Fields
        private static QuarryComputer _instance;
        private int _maxLevel;
        private readonly List<string> _permissions = new() { "quarrycomputer.give", "quarrycomputer.industrial", "quarrycomputer.vip", "quarrycomputer.norequirements", "quarrycomputer.admin" };
        #endregion
    
        #region Cache
        private readonly HashSet<ulong> _playersWithPopup = new();
        private readonly HashSet<ulong> _itemContainersFuel = new();
        private readonly HashSet<ulong> _itemContainersOutput = new();
        private readonly HashSet<ulong> _buttons = new();
        private readonly HashSet<ulong> _adaptors = new();
        private readonly Dictionary<ulong, QuarryComputerBehaviour> _openedQuarryInterfaces = new();
        #endregion
    
        #region Game Hooks
        private void OnServerInitialized()
        {
            _instance = this;
    
            if (ImageLibrary == null)
            {
                PrintError("ImageLibrary is not loaded!");
                PrintError("Plugin has been disabled!");
                Interface.Oxide.UnloadPlugin(Title);
                return;
            }
    
            var items = GetItemsForValidation();
    
            if (!ValidateItemsCounts())
            {
                PrintError("Invalid Configuration File!");
                PrintError("The maximum number of items per configuration must not exceed 12.");
                PrintError("Plugin has been disabled!");
                Interface.Oxide.UnloadPlugin(Title);
                return;
            }
    
            if (!ValidateConfigItems(items))
            {
                PrintError("Invalid Configuration File!");
                PrintError("Plugin has been disabled!");
                Interface.Oxide.UnloadPlugin(Title);
                return;
            }
    
            ImageLibrary?.Call("LoadImageList", Title, items, null);
            ImageLibrary?.Call("AddImage", "https://i.ibb.co/C22VZDj/money.png", "Economics");
            ImageLibrary?.Call("AddImage", "https://i.ibb.co/C22VZDj/money.png", "ServerRewards");
    
            RegisterPermissions();
            RegisterCommands();
            SetMaxLevel();
            LoadData();
            InitializeExistingQuarries();
        }
    
        private void OnServerSave() => SaveData();
    
        private void Unload()
        {
            DestroyAllUis();
            UnloadAllQuarryBehaviours();
            SaveData();
        }
    
        private void OnEntityBuilt(Planner planner, GameObject builtObject)
        {
            var player = planner?.GetOwnerPlayer();
            var entity = builtObject.GetComponent<BaseEntity>();
    
            if (player == null || entity == null || entity.ShortPrefabName != BaseDeployedItemName || entity.skinID != QuarrySkinId)
                return;
    
            int playerQuarryLimit = _configs.Profiles.Default.QuarryLimit;
    
            if (permission.UserHasPermission(player.UserIDString, "quarrycomputer.vip"))
                playerQuarryLimit = _configs.Profiles.VIP.QuarryLimit;
    
            int level = RemoveItemData(player.GetActiveItem().uid.Value);
    
            if (!player.IsBuildingAuthed() || GetPlayerQuarryCount(player.userID) >= playerQuarryLimit)
            {
                entity?.Kill();
    
                NotifyPlayer(player.IPlayer, !player.IsBuildingAuthed() ? "NoBuildingAuth" : "QuarryLimitReached", 1);
                timer.Once(1, () => GiveQuarryItem(player, level));
                return;
            }
    
            if (!DeployQuarry(player, entity, level))
            {
                entity?.Kill();
    
                NotifyPlayer(player.IPlayer, "NoFundation", 1);
                timer.Once(1, () => GiveQuarryItem(player, level));
                return;
            }
    
            NextTick(() => entity?.Kill());
        }
    
        private object OnEntityKill(ExcavatorSignalComputer signalComputer)
        {
            if (signalComputer.OwnerID != 0)
            {
                RemoveQuarryData(signalComputer.OwnerID, signalComputer.net.ID.Value);
    
                if (_configs.DropItem)
                {
                    var quarry = signalComputer.GetComponent<QuarryComputerBehaviour>();
                    if (quarry != null)
                    {
                        var item = CreateQuarryItem(signalComputer.OwnerID, quarry.Level);
                        item.Drop(signalComputer.transform.position, new Vector3(0, 1, 0));
                        Effect.server.Run(GroundLossFx, signalComputer.transform.position);
                    }
                }
            }
    
            return null;
        }
    
        private ItemContainer.CanAcceptResult? CanAcceptItem(ItemContainer container, Item item)
        {
            if (_itemContainersFuel.Contains(container.uid.Value) && item.info.shortname != _configs.FuelConfiguration.Shortname)
                return ItemContainer.CanAcceptResult.CannotAccept;
    
            if (_itemContainersOutput.Contains(container.uid.Value) && !item.flags.HasFlag(ExtractedItemFlag))
                return ItemContainer.CanAcceptResult.CannotAccept;
    
            return null;
        }
    
        private object CanPickupEntity(BasePlayer player, IndustrialStorageAdaptor entity)
        {
            return _adaptors.Contains(entity.net.ID.Value) ? (object)false : null;
        }
    
        private object OnExcavatorSuppliesRequest(ExcavatorSignalComputer computer, BasePlayer player)
        {
            return _pluginData.QuarriesData.ContainsKey(computer.net.ID.Value) ? (object)false : null;
        }
    
        private object OnButtonPress(PressButton button, BasePlayer player)
        {
            if (_buttons.Contains(button.net.ID.Value))
            {
                var outputContainer = button.GetParentEntity();
                var quarry = outputContainer.GetComponentInParent<QuarryComputerBehaviour>();
    
                if (quarry.OwnerID != player.userID && !permission.UserHasPermission(player.UserIDString, "quarrycomputer.admin"))
                {
                    NotifyPlayer(player, "NotYours", 1);
                    return null;
                }
    
                _openedQuarryInterfaces.Add(player.userID, quarry);
                CreateUi(player, quarry);
            }
    
            return null;
        }
        #endregion
    
        #region Data
        private StoredData _pluginData;
    
        private class StoredData
        {
            [JsonProperty("Quarries")]
            public Dictionary<ulong, QuarriesData> QuarriesData = new();
    
            [JsonProperty("Items")]
            public Dictionary<ulong, ItemsData> ItemsData = new();
        }
    
        private class QuarriesData
        {
            [JsonProperty("OwnerId")]
            public ulong OwnerId { get; set; }
    
            [JsonProperty("Level")]
            public int Level { get; set; }
        }
    
        private class ItemsData
        {
            [JsonProperty("OwnerId")]
            public ulong OwnerId { get; set; }
    
            [JsonProperty("Level")]
            public int Level { get; set; }
        }
    
        private void SaveData()
        {
            if (_pluginData != null)
            {
                Interface.Oxide.DataFileSystem.WriteObject($"{Name}_Data", _pluginData, true);
            }
        }
    
        private void LoadData()
        {
            _pluginData = Interface.Oxide.DataFileSystem.ReadObject<StoredData>($"{Name}_Data");
            if (_pluginData == null)
            {
                _pluginData = new StoredData();
                SaveData();
            }
        }
    
        private void AddQuarryData(ulong ownerId, ulong netId, int level = 1)
        {
            if (!_pluginData.QuarriesData.ContainsKey(netId))
            {
                _pluginData.QuarriesData[netId] = new QuarriesData { OwnerId = ownerId, Level = level };
            }
            else
            {
                var quarryData = _pluginData.QuarriesData[netId];
                quarryData.OwnerId = ownerId;
                quarryData.Level = level;
            }
        }
    
        private void UpdateQuarryLevelData(ulong netId, int newLevel)
        {
            if (_pluginData.QuarriesData.ContainsKey(netId))
            {
                var quarryData = _pluginData.QuarriesData[netId];
                quarryData.Level = newLevel;
            }
        }
    
        private int GetQuarryLevelData(ulong netId)
        {
            return _pluginData.QuarriesData.ContainsKey(netId) ? _pluginData.QuarriesData[netId].Level : 1;
        }
    
        private int GetPlayerQuarryCount(ulong ownerId)
        {
            return _pluginData.QuarriesData.Values.Count(quarry => quarry.OwnerId == ownerId);
        }
    
        private void RemoveQuarryData(ulong ownerId, ulong netId)
        {
            _pluginData.QuarriesData.Remove(netId);
        }
    
        private void AddItemData(ulong itemId, ulong userId, int level)
        {
            if (level > 1)
            {
                _pluginData.ItemsData[itemId] = new ItemsData { OwnerId = userId, Level = level };
            }
        }
    
        private int RemoveItemData(ulong itemId)
        {
            if (_pluginData.ItemsData.TryGetValue(itemId, out ItemsData data))
            {
                _pluginData.ItemsData.Remove(itemId);
                return data.Level;
            }
    
            return 1;
        }
        #endregion
    
        #region Configuration
        private Configuration _configs;
    
        private class Configuration
        {
            [JsonProperty("Use LangAPI Plugin")]
            public bool UseLangAPI { get; set; }
    
            [JsonProperty("Use Notify Plugin")]
            public bool UseNotify { get; set; }
    
            [JsonProperty("Give Quarry Command")]
            public string[] GiveQuarryCommand { get; set; }
    
            [JsonProperty("Drop Quarry Item on Ground Loss")]
            public bool DropItem { get; set; }
    
            [JsonProperty("Fuel Configuration")]
            public FuelConfiguration FuelConfiguration { get; set; }
    
            [JsonProperty("No Upgrades (Level 1)")]
            public NoUpgrades NoUpgrades { get; set; }
    
            [JsonProperty("Upgrades (Level 2+)")]
            public List<UpgradeOption> Upgrades { get; set; }
    
            [JsonProperty("Keep Upgrades On Remove Quarry")]
            public bool KeepUpgradesOnRemoval { get; set; }
    
            [JsonProperty("Profiles")]
            public UserProfiles Profiles { get; set; }
    
            [JsonProperty("Version")]
            public VersionNumber Version { get; set; }
        }
    
        private class FuelConfiguration
        {
            [JsonProperty("Shortname")]
            public string Shortname { get; set; }
    
            [JsonProperty("Resource Extraction Interval In Seconds")]
            public int ResourceExtractionInterval { get; set; }
    
            [JsonProperty("Fuel Required Per Extraction")]
            public int FuelRequiredPerExtraction { get; set; }
        }
    
        private class NoUpgrades
        {
            [JsonProperty("Storage Slots")]
            public StorageSlots StorageSlots { get; set; }
    
            [JsonProperty("Resources Output Per Extraction")]
            public List<ResourceItem> ResourcesOutput { get; set; }
        }
    
        private class StorageSlots
        {
            [JsonProperty("Fuel")]
            public int FuelSlots { get; set; }
    
            [JsonProperty("Resources")]
            public int OutputSlots { get; set; }
        }
    
        private class ResourceItem
        {
            [JsonProperty("Shortname")]
            public string Shortname { get; set; }
    
            [JsonProperty("Amount")]
            public int Amount { get; set; }
        }
    
        private class UpgradeOption
        {
            [JsonProperty("Storage Slots")]
            public StorageSlots StorageSlots { get; set; }
    
            [JsonProperty("Resources Output Per Extraction")]
            public List<ResourceItem> ResourcesOutput { get; set; }
    
            [JsonProperty("Required Items To Upgrade")]
            public List<ResourceItem> UpgradeItems { get; set; }
        }
    
        private class UserProfiles
        {
            [JsonProperty("Default (No Permission)")]
            public UserProfile Default { get; set; }
    
            [JsonProperty("VIP (quarrycomputer.vip Permission)")]
            public UserProfile VIP { get; set; }
        }
    
        private class UserProfile
        {
            [JsonProperty("Quarry Limit")]
            public int QuarryLimit { get; set; }
        }
    
        protected override void LoadConfig()
        {
            base.LoadConfig();
            try
            {
                _configs = Config.ReadObject<Configuration>();
                if (_configs != null)
                {
                    if (_configs.Version < Version)
                        UpdateConfigValues();
    
                    SaveConfig();
                }
                else
                    throw new Exception();
            }
            catch
            {
                PrintError("There is an error in your configuration file. Default values will be used.");
                LoadDefaultConfig();
            }
        }
    
        protected override void LoadDefaultConfig() => _configs = CreateDefaultConfig();
    
        protected override void SaveConfig() => Config.WriteObject(_configs);
    
        private void UpdateConfigValues()
        {
            PrintWarning("Configuration change detected! Updating settings.");
    
            _configs.Version = Version;
    
            PrintWarning("Configuration update completed!");
        }
    
        private Configuration CreateDefaultConfig()
        {
            return new Configuration
            {
                GiveQuarryCommand = new[] { "gq", "givequarry" },
                DropItem = true,
                UseLangAPI = true,
                UseNotify = true,
                FuelConfiguration = new FuelConfiguration
                {
                    Shortname = "charcoal",
                    ResourceExtractionInterval = 40,
                    FuelRequiredPerExtraction = 100,
                },
                NoUpgrades = new NoUpgrades
                {
                    StorageSlots = new StorageSlots { FuelSlots = 1, OutputSlots = 6 },
                    ResourcesOutput = new List<ResourceItem>
                    {
                        new ResourceItem { Shortname = "crude.oil", Amount = 4 },
                        new ResourceItem { Shortname = "stones", Amount = 35 },
                        new ResourceItem { Shortname = "metal.ore", Amount = 7 },
                        new ResourceItem { Shortname = "sulfur.ore", Amount = 7 },
                    },
                },
                Upgrades = new List<UpgradeOption>
                {
                    new UpgradeOption
                    {
                        StorageSlots = new StorageSlots { FuelSlots = 3, OutputSlots = 12 },
                        ResourcesOutput = new List<ResourceItem>
                        {
                            new ResourceItem { Shortname = "crude.oil", Amount = 6 },
                            new ResourceItem { Shortname = "stones", Amount = 50 },
                            new ResourceItem { Shortname = "metal.ore", Amount = 10 },
                            new ResourceItem { Shortname = "sulfur.ore", Amount = 10 },
                        },
                        UpgradeItems = new List<ResourceItem>
                        {
                            new ResourceItem { Shortname = "Economics", Amount = 25000 },
                            new ResourceItem { Shortname = "scrap", Amount = 1000 },
                            new ResourceItem { Shortname = "pumpkin", Amount = 100 },
                        },
                    },
                    new UpgradeOption
                    {
                        StorageSlots = new StorageSlots { FuelSlots = 6, OutputSlots = 24 },
                        ResourcesOutput = new List<ResourceItem>
                        {
                            new ResourceItem { Shortname = "crude.oil", Amount = 10 },
                            new ResourceItem { Shortname = "hq.metal.ore", Amount = 10 },
                            new ResourceItem { Shortname = "metal.ore", Amount = 30 },
                            new ResourceItem { Shortname = "stones", Amount = 100 },
                            new ResourceItem { Shortname = "sulfur.ore", Amount = 25 },
                        },
                        UpgradeItems = new List<ResourceItem>
                        {
                            new ResourceItem { Shortname = "scrap", Amount = 5000 },
                            new ResourceItem { Shortname = "pumpkin", Amount = 1000 },
                            new ResourceItem { Shortname = "cloth", Amount = 10000 },
                            new ResourceItem { Shortname = "red.berry", Amount = 1000 },
                            new ResourceItem { Shortname = "bone.fragments", Amount = 1000 },
                        },
                    },
                },
                KeepUpgradesOnRemoval = true,
                Profiles = new UserProfiles
                {
                    Default = new UserProfile { QuarryLimit = 1 },
                    VIP = new UserProfile { QuarryLimit = 3 },
                },
            };
        }
        #endregion
    
        #region Localization
        protected override void LoadDefaultMessages()
        {
            lang.RegisterMessages(
                new Dictionary<string, string>
                {
                    ["NoPermission"] = "You don't have permission to use this command.",
                    ["NoBuildingAuth"] = "You must have building auth to use this.",
                    ["NoFundation"] = "Must be placed on a foundation.",
                    ["QuarryLimitReached"] = "Quarry Limit Reached.",
                    ["NotMiningQuarry"] = "This object is not a Mining Quarry.",
                    ["NotOwner"] = "This object belongs to another player.",
                    ["QuarryRemoved"] = "Mining Quarry removed.",
                    ["NoQuarriesFound"] = "No quarries found.",
                    ["QuarrySend"] = "You have sent a Mining Quarry to the player.",
                    ["QuarryReceived"] = "You have received a Mining Quarry.",
                    ["QuarryDropped"] = "No space in inventory! Quarry dropped on the ground.",
                    ["PlayerNotFound"] = "Player not found.",
                    ["TurnedOn"] = "Quarry turned on.",
                    ["AlreadyOn"] = "The Quarry is already on.",
                    ["NoFuel"] = "Not enough fuel to start extraction.",
                    ["NoSpace"] = "Not enough space in output for extracted resources.",
                    ["NotEmpty"] = "The Quarry still contains items. Empty it before removal.",
                    ["UpgradeRequirements"] = "You do not have the required items to upgrade.",
                    ["QuarryTurnedOn"] = "Quarry turned on.",
                    ["ExtractionIntervalText"] = "Extraction Interval: {0} seconds | Fuel per extraction: {1} {2}",
                    ["StartButton"] = "Start",
                    ["RemoveButton"] = "Remove",
                    ["UpgradeButton"] = "Upgrade",
                    ["UpgradeToLevel"] = "Upgrade to Level {0}",
                    ["StorageHeader"] = "Storage",
                    ["FuelSlots"] = "Fuel: {0} slots",
                    ["ResourceSlots"] = "Resource: {0} slots",
                    ["ExtractionHeader"] = "Extraction",
                    ["RequirementsHeader"] = "Requirements",
                    ["YesButton"] = "Yes",
                    ["NoButton"] = "No",
                    ["ConfirmRemove"] = "Remove the Quarry?",
                    ["ConfirmUpgrade"] = "Upgrade the Quarry?",
                    ["QuarryLevel"] = "Quarry Level {0}",
                },
                this
            );
    
            lang.RegisterMessages(
                new Dictionary<string, string>
                {
                    ["NoPermission"] = "Você não tem permissão.",
                    ["NoBuildingAuth"] = "Você precisa de permissão para construir.",
                    ["NoFundation"] = "Deve ser colocado em uma fundação.",
                    ["QuarryLimitReached"] = "Limite de Quarries alcançado.",
                    ["NotMiningQuarry"] = "Este não é uma Quarry.",
                    ["NotOwner"] = "Isto pertence a outro jogador.",
                    ["QuarryRemoved"] = "Quarry removida.",
                    ["NoQuarriesFound"] = "Nenhuma Quarry encontrada.",
                    ["QuarrySend"] = "Quarry enviada ao jogador.",
                    ["QuarryReceived"] = "Você recebeu uma Quarry.",
                    ["QuarryDropped"] = "Inventário cheio! Quarry no chão.",
                    ["PlayerNotFound"] = "Jogador não encontrado.",
                    ["TurnedOn"] = "Quarry ligada.",
                    ["AlreadyOn"] = "A Quarry já está ligada.",
                    ["NoFuel"] = "Combustível insuficiente.",
                    ["NoSpace"] = "Espaço insuficiente para recursos.",
                    ["NotEmpty"] = "Esvazie a Quarry antes de remover.",
                    ["UpgradeRequirements"] = "Itens insuficientes para atualização.",
                    ["QuarryTurnedOn"] = "Quarry ligada.",
                    ["ExtractionIntervalText"] = "Intervalo: {0}s | Combustível: {1} {2}",
                    ["StartButton"] = "Iniciar",
                    ["RemoveButton"] = "Remover",
                    ["UpgradeButton"] = "Atualizar",
                    ["UpgradeToLevel"] = "Atualizar para Nível {0}",
                    ["StorageHeader"] = "Armazém",
                    ["FuelSlots"] = "Fuel: {0} slots",
                    ["ResourceSlots"] = "Recursos: {0} slots",
                    ["ExtractionHeader"] = "Extração",
                    ["RequirementsHeader"] = "Requisitos",
                    ["YesButton"] = "Sim",
                    ["NoButton"] = "Não",
                    ["ConfirmRemove"] = "Remover a Quarry?",
                    ["ConfirmUpgrade"] = "Atualizar a Quarry?",
                    ["QuarryLevel"] = "Quarry Nível: {0}",
                },
                this,
                "pt-BR"
            );
    
            lang.RegisterMessages(
                new Dictionary<string, string>
                {
                    ["NoPermission"] = "Du hast keine Berechtigung.",
                    ["NoBuildingAuth"] = "Du benötigst eine Baugenehmigung.",
                    ["NoFundation"] = "Muss auf einem Fundament platziert werden.",
                    ["QuarryLimitReached"] = "Steinbruchgrenze Erreicht",
                    ["NotMiningQuarry"] = "Dies ist keine Quarry.",
                    ["NotOwner"] = "Dies gehört einem anderen Spieler.",
                    ["QuarryRemoved"] = "Quarry entfernt.",
                    ["NoQuarriesFound"] = "Keine Quarry gefunden.",
                    ["QuarrySend"] = "Quarry an den Spieler gesendet.",
                    ["QuarryReceived"] = "Du hast eine Quarry erhalten.",
                    ["QuarryDropped"] = "Inventar voll! Quarry auf dem Boden.",
                    ["PlayerNotFound"] = "Spieler nicht gefunden.",
                    ["TurnedOn"] = "Quarry eingeschaltet.",
                    ["AlreadyOn"] = "Die Quarry ist bereits eingeschaltet.",
                    ["NoFuel"] = "Nicht genug Treibstoff.",
                    ["NoSpace"] = "Nicht genug Platz für Ressourcen.",
                    ["NotEmpty"] = "Entleere die Quarry, bevor du sie entfernst.",
                    ["UpgradeRequirements"] = "Du hast nicht die notwendigen Gegenstände zum Aufrüsten.",
                    ["QuarryTurnedOn"] = "Quarry eingeschaltet.",
                    ["ExtractionIntervalText"] = "Intervall: {0}s | Treibstoff: {1} {2}",
                    ["StartButton"] = "Starten",
                    ["RemoveButton"] = "Entfernen",
                    ["UpgradeButton"] = "Aufrüsten",
                    ["UpgradeToLevel"] = "Aufrüsten auf Stufe {0}",
                    ["StorageHeader"] = "Lager",
                    ["FuelSlots"] = "Treibstoff: {0} Plätze",
                    ["ResourceSlots"] = "Ressourcen: {0} Plätze",
                    ["ExtractionHeader"] = "Extraktion",
                    ["RequirementsHeader"] = "Anforderungen",
                    ["YesButton"] = "Ja",
                    ["NoButton"] = "Nein",
                    ["ConfirmRemove"] = "Quarry entfernen?",
                    ["ConfirmUpgrade"] = "Quarry aufrüsten?",
                    ["QuarryLevel"] = "Stufe: {0}",
                },
                this,
                "de"
            );
        }
    
        public string GetItemDisplayName(BasePlayer player, string itemShortName)
        {
            var itemDefinition = ItemManager.FindItemDefinition(itemShortName);
            var displayName = itemDefinition.displayName.translated;
    
            if (_configs.UseLangAPI && LangAPI != null && LangAPI.Call<bool>("IsDefaultDisplayName", displayName))
            {
                var displayNameTranslated = LangAPI.Call<string>("GetItemDisplayName", itemShortName, displayName, player.UserIDString);
                if (!string.IsNullOrEmpty(displayNameTranslated))
                    return displayNameTranslated;
            }
    
            return displayName;
        }
        #endregion
    
        #region UiCommands
        [Command(CommandUiStart)]
        private void UiStartCommand(IPlayer player)
        {
            var basePlayer = player.Object as BasePlayer;
            if (!_openedQuarryInterfaces.TryGetValue(basePlayer.userID, out var quarry))
                return;
    
            if (!quarry.HasFuel())
            {
                DestroyUi(basePlayer);
                NotifyPlayer(player, "NoFuel", 1);
                return;
            }
    
            if (!quarry.HasSpace())
            {
                DestroyUi(basePlayer);
                NotifyPlayer(player, "NoSpace", 1);
                return;
            }
    
            if (quarry.TurnOn())
            {
                NotifyPlayer(player, "TurnedOn", 0);
                Effect.server.Run(QuarryTurnedOnFx, basePlayer.GetNetworkPosition());
            }
            else
            {
                DestroyUi(basePlayer);
                NotifyPlayer(player, "AlreadyOn", 0);
            }
    
            CuiHelper.DestroyUi(basePlayer, UiButtonStart);
        }
    
        [Command(CommandUiClose)]
        private void UiCloseCommand(IPlayer player)
        {
            var basePlayer = player.Object as BasePlayer;
            DestroyUi(basePlayer);
        }
    
        [Command(CommandUiRemove)]
        private void UiRemoveCommand(IPlayer player)
        {
            var basePlayer = player.Object as BasePlayer;
            ToggleConfirmationPopup(basePlayer, Msg(basePlayer.UserIDString, "ConfirmRemove"), CommandUiConfirmRemove, CommandUiCancelRemove);
        }
    
        [Command(CommandUiConfirmRemove)]
        private void UiConfirmRemoveCommand(IPlayer player)
        {
            var basePlayer = player.Object as BasePlayer;
            if (!_openedQuarryInterfaces.TryGetValue(basePlayer.userID, out var quarry))
                return;
    
            if (!quarry.IsEmpty())
            {
                DestroyUi(basePlayer);
                NotifyPlayer(basePlayer, "NotEmpty", 1);
                return;
            }
    
            RemoveQuarryData(quarry.SignalComputer.OwnerID, quarry.SignalComputer.net.ID.Value);
    
            Unsubscribe(nameof(OnEntityKill));
            quarry.SignalComputer.Kill();
            Subscribe(nameof(OnEntityKill));
    
            GiveQuarryItem(basePlayer, quarry.Level);
            NotifyPlayer(basePlayer, "QuarryRemoved", 0);
            DestroyUi(basePlayer);
        }
    
        [Command(CommandUiCancelRemove)]
        private void UiCancelRemoveCommand(IPlayer player)
        {
            var basePlayer = player.Object as BasePlayer;
            ClosePopup(basePlayer);
        }
    
        [Command(CommandUiUpgrade)]
        private void UiUpgradeCommand(IPlayer player)
        {
            var basePlayer = player.Object as BasePlayer;
            ToggleConfirmationPopup(basePlayer, Msg(basePlayer.UserIDString, "ConfirmUpgrade"), CommandUiConfirmUpgrade, CommandUiCancelUpgrade);
        }
    
        [Command(CommandUiConfirmUpgrade)]
        private void UiConfirmUpgradeCommand(IPlayer player)
        {
            var basePlayer = player.Object as BasePlayer;
            if (!_openedQuarryInterfaces.TryGetValue(basePlayer.userID, out var quarry))
                return;
    
            if (!permission.UserHasPermission(basePlayer.UserIDString, "quarrycomputer.norequirements"))
            {
                if (quarry.UpgradeRequirements != null && !CheckAndRemoveUpgradeItems(basePlayer, quarry.UpgradeRequirements.UpgradeItems))
                {
                    NotifyPlayer(player, "UpgradeRequirements", 1);
                    ClosePopup(basePlayer);
                    return;
                }
            }
    
            quarry.Upgrade();
            Effect.server.Run(QuarryUpgradedFx, basePlayer.GetNetworkPosition());
    
            DestroyUi(basePlayer);
        }
    
        private bool CheckAndRemoveUpgradeItems(BasePlayer player, List<ResourceItem> requiredItems)
        {
            var requiredItemMap = requiredItems.ToDictionary(item => item.Shortname, item => item.Amount);
    
            int economicsValue = ExtractAndRemoveValue(requiredItemMap, "Economics");
            int serverRewardsValue = ExtractAndRemoveValue(requiredItemMap, "ServerRewards");
    
            var allItems = Pool.Get<List<Item>>();
            try
            {
                player.inventory.GetAllItems(allItems);
    
                if (!HasAllRequiredItems(allItems, requiredItemMap))
                    return false;
    
                if (!CheckBalance(player, economicsValue, serverRewardsValue))
                    return false;
    
                if (!WithdrawFunds(player, economicsValue, serverRewardsValue))
                    return false;
    
                RemoveItems(allItems, player, requiredItemMap);
                return true;
            }
            finally
            {
                Pool.FreeList(ref allItems);
            }
        }
    
        private int ExtractAndRemoveValue(Dictionary<string, int> itemMap, string key)
        {
            if (itemMap.Remove(key, out var value))
                return value;
    
            return 0;
        }
    
        private bool CheckBalance(BasePlayer player, int economicsValue, int serverRewardsValue)
        {
            return (economicsValue == 0 || CheckEconomicsBalance(player, economicsValue)) && (serverRewardsValue == 0 || CheckServerRewardsBalance(player, serverRewardsValue));
        }
    
        private bool WithdrawFunds(BasePlayer player, int economicsValue, int serverRewardsValue)
        {
            return (economicsValue == 0 || WithdrawEconomicsFunds(player, economicsValue)) && (serverRewardsValue == 0 || WithdrawServerRewardsFunds(player, serverRewardsValue));
        }
    
        private bool CheckEconomicsBalance(BasePlayer player, int economicsValue)
        {
            if (Economics == null)
                return true;
    
            double balance = Convert.ToDouble(Economics?.CallHook("Balance", player.userID));
            if (balance < economicsValue)
            {
                return false;
            }
            return true;
        }
    
        private bool CheckServerRewardsBalance(BasePlayer player, int serverRewardsValue)
        {
            if (ServerRewards == null)
                return true;
    
            double balance = Convert.ToDouble(ServerRewards?.CallHook("CheckPoints", player.userID));
            if (balance < serverRewardsValue)
            {
                return false;
            }
            return true;
        }
    
        private bool WithdrawEconomicsFunds(BasePlayer player, int economicsValue)
        {
            if (Economics == null)
                return true;
    
            if (!Convert.ToBoolean(Economics?.CallHook("Withdraw", player.userID, (double)economicsValue)))
            {
                return false;
            }
            return true;
        }
    
        private bool WithdrawServerRewardsFunds(BasePlayer player, int serverRewardsValue)
        {
            if (ServerRewards == null)
                return true;
    
            if (!Convert.ToBoolean(ServerRewards?.CallHook("TakePoints", player.userID, serverRewardsValue)))
            {
                return false;
            }
            return true;
        }
    
        private bool HasAllRequiredItems(List<Item> allItems, Dictionary<string, int> requiredItemMap)
        {
            var playerItemMap = allItems.GroupBy(item => item.info.shortname).ToDictionary(g => g.Key, g => g.Sum(item => item.amount));
    
            foreach (var requirement in requiredItemMap)
                if (!playerItemMap.TryGetValue(requirement.Key, out int count) || count < requirement.Value)
                    return false;
    
            return true;
        }
    
        private void RemoveItems(List<Item> allItems, BasePlayer player, Dictionary<string, int> requiredItemMap)
        {
            foreach (var requirement in requiredItemMap)
            {
                int amountToRemove = requirement.Value;
    
                foreach (var item in allItems.Where(item => item.info.shortname == requirement.Key))
                {
                    if (amountToRemove <= 0)
                        break;
    
                    if (item.amount <= amountToRemove)
                    {
                        amountToRemove -= item.amount;
                        player.inventory.Take(null, item.info.itemid, item.amount);
                        item.Remove();
                    }
                    else
                    {
                        item.amount -= amountToRemove;
                        item.MarkDirty();
                        amountToRemove = 0;
                    }
                }
            }
        }
    
        [Command(CommandUiCancelUpgrade)]
        private void UiCancelUpgradeCommand(IPlayer player)
        {
            var basePlayer = player.Object as BasePlayer;
            ClosePopup(basePlayer);
        }
        #endregion
    
        #region Commands
        private void GiveQuarryCommand(IPlayer iPlayer, string command, string[] args)
        {
            var player = iPlayer?.Object as BasePlayer;
            if (player == null || args == null || args.Length < 1)
                return;
    
            if (!iPlayer.HasPermission("quarrycomputer.give"))
            {
                NotifyPlayer(iPlayer, "NoPermission", 1);
                return;
            }
    
            var targetPlayer = BasePlayer.Find(args[0]);
            if (targetPlayer == null)
            {
                NotifyPlayer(iPlayer, "PlayerNotFound", 1);
                return;
            }
    
            if (GiveQuarryItem(targetPlayer, 1))
            {
                NotifyPlayer(iPlayer, "QuarrySend", 0);
                NotifyPlayer(targetPlayer, "QuarryReceived", 0);
            }
            else
            {
                NotifyPlayer(iPlayer, "QuarrySend", 0);
                NotifyPlayer(targetPlayer, "QuarryReceived", 0);
                NotifyPlayer(targetPlayer, "QuarryDropped", 1);
            }
        }
        #endregion
    
        #region UI Handling
        private void CreateUi(BasePlayer player, QuarryComputerBehaviour quarry)
        {
            var fuelName = GetItemDisplayName(player, _configs.FuelConfiguration.Shortname);
            var container = new CuiElementContainer();
            AddBackground(container);
            AddMainPanel(player, container, fuelName);
            AddLeftPanel(player, container, quarry);
    
            if (quarry.UpgradeRequirements != null)
                AddRightPanel(player, container, quarry);
    
            CuiHelper.AddUi(player, container);
        }
    
        private void AddBackground(CuiElementContainer container)
        {
            AddPanel(container, "0 0", "1 1", UiBackground, "Overlay", "0 0 0 0.9", true);
        }
    
        private void AddMainPanel(BasePlayer player, CuiElementContainer container, string fuelName)
        {
            string resourceExtractionInterval = Msg(
                player.UserIDString,
                "ExtractionIntervalText",
                _configs.FuelConfiguration.ResourceExtractionInterval,
                _configs.FuelConfiguration.FuelRequiredPerExtraction,
                fuelName
            );
    
            AddPanel(container, "0.235 0.18", "0.765 0.784", UiMainPanel, UiBackground, "0.6 0.6 0.6 0.95", true);
            AddInnerSquare(container, UiMainPanel, "0 0.9570", "1 1", "0.39 0.39 0.39 1");
            AddText(container, UiMainPanel, "0.02 0.96", "1 1", resourceExtractionInterval, 13, TextAnchor.MiddleLeft);
            AddCloseButton(container, UiMainPanel);
        }
    
        private void AddLeftPanel(BasePlayer player, CuiElementContainer container, QuarryComputerBehaviour quarry)
        {
            var outputSlots = quarry.OutputContainer.inventory.capacity;
            var fuelSlots = quarry.FuelContainer.inventory.capacity;
    
            var parent = UiLeftPanel;
            AddPanel(container, "0 0", "0.5 0.94", parent, UiMainPanel, "0 0 0 0");
            AddPanelStructure(container, parent, Msg(player.UserIDString, "QuarryLevel", quarry.Level), "0 0 1 1");
    
            AddText(container, parent, "0.38 0.679", "1 1", Msg(player.UserIDString, "StorageHeader"), 13, TextAnchor.MiddleLeft);
            AddText(container, parent, "0.09 0.590", "1 1", Msg(player.UserIDString, "FuelSlots", fuelSlots), 13, TextAnchor.MiddleLeft);
            AddText(container, parent, "0.45 0.590", "1 1", Msg(player.UserIDString, "ResourceSlots", outputSlots), 13, TextAnchor.MiddleLeft);
    
            AddHorizontalLine(container, parent, "0.06 0.769", "0.89 0.77");
    
            AddText(container, parent, "0.34 0.490", "1 1", Msg(player.UserIDString, "ExtractionHeader"), 13, TextAnchor.MiddleLeft);
    
            AddDictionaryWithSpacing(container, parent, quarry.ResourcesOutput, 0.18f, 0.725f, 0.065f);
    
            AddButton(container, parent, Msg(player.UserIDString, "StartButton"), "", "0.15", "0.11", "0.45", "0.16", "0.6 0.6 0.6 0.95", "0.4 0.4 0.4 1");
            if (!quarry.IsOn)
                AddButton(container, parent, Msg(player.UserIDString, "StartButton"), CommandUiStart, "0.15", "0.11", "0.45", "0.16", "0.6 0.6 0.6 0.95", "0 1 0 1", buttonName: UiButtonStart);
    
            AddButton(container, parent, Msg(player.UserIDString, "RemoveButton"), CommandUiRemove, "0.50", "0.11", "0.80", "0.16", "0.6 0.6 0.6 0.95", "1 0 0 1");
        }
    
        private void AddRightPanel(BasePlayer player, CuiElementContainer container, QuarryComputerBehaviour quarry)
        {
            var parent = UiRightPanel;
            AddPanel(container, "0.5 0", "1 0.94", parent, UiMainPanel, "0 0 0 0");
    
            string upgradeTitle = Msg(player.UserIDString, "UpgradeToLevel", quarry.Level + 1);
            AddPanelStructure(container, parent, upgradeTitle, "0.5 0 0 1");
    
            AddText(container, parent, "0.38 0.679", "1 1", Msg(player.UserIDString, "StorageHeader"), 13, TextAnchor.MiddleLeft);
            AddText(container, parent, "0.09 0.590", "1 1", Msg(player.UserIDString, "FuelSlots", quarry.UpgradeRequirements.StorageSlots.FuelSlots), 13, TextAnchor.MiddleLeft);
            AddText(container, parent, "0.45 0.590", "1 1", Msg(player.UserIDString, "ResourceSlots", quarry.UpgradeRequirements.StorageSlots.OutputSlots), 13, TextAnchor.MiddleLeft);
    
            AddHorizontalLine(container, parent, "0.06 0.769", "0.89 0.77");
    
            AddText(container, parent, "0.34 0.490", "1 1", Msg(player.UserIDString, "ExtractionHeader"), 13, TextAnchor.MiddleLeft);
    
            AddDictionaryWithSpacing(container, parent, quarry.UpgradeRequirements.ResourcesOutput, 0.18f, 0.725f, 0.065f);
    
            AddHorizontalLine(container, parent, "0.06 0.464", "0.89 0.465");
    
            AddText(container, parent, "0.34 0.0", "1 0.88", Msg(player.UserIDString, "RequirementsHeader"), 13, TextAnchor.MiddleLeft);
    
            AddDictionaryWithSpacing(container, parent, quarry.UpgradeRequirements.UpgradeItems, 0.18f, 0.419f, 0.065f);
    
            AddButton(container, parent, Msg(player.UserIDString, "UpgradeButton"), CommandUiUpgrade, "0.35", "0.11", "0.65", "0.16", "0.6 0.6 0.6 0.95", "0 1 0 1");
        }
    
        private void AddPanelStructure(CuiElementContainer container, string parent, string title, string titleColor)
        {
            AddInnerSquare(container, parent, "0.10 0.05", "0.95 0.90", "0 0 0 0.97");
            AddInnerSquare(container, parent, "0.054 0.1033", "0.895 0.945", "0.8 0.8 0.8 1");
            AddInnerSquare(container, parent, "0.06 0.108", "0.89 0.94", titleColor);
            AddHorizontalLine(container, parent, "0.06 0.865", "0.89 0.865");
    
            AddText(container, parent, "0.1 0.84", "0.84 0.98", title, 15, TextAnchor.MiddleCenter);
        }
    
        private void AddDictionaryWithSpacing(CuiElementContainer container, string parent, List<ResourceItem> items, float initialAnchorX, float initialAnchorY, float spacing)
        {
            float anchorX = initialAnchorX;
    
            int i = 0;
            foreach (var item in items)
            {
                i++;
    
                float anchorY = initialAnchorY - (spacing * i);
    
                if (i >= 5 && i < 9)
                {
                    anchorY = initialAnchorY - (spacing * (i - 4));
                    anchorX = initialAnchorX + 0.23F;
                }
    
                if (i >= 9)
                {
                    anchorY = initialAnchorY - (spacing * (i - 8));
                    anchorX = initialAnchorX + 0.47F;
                }
    
                float imageSize = 0.07f;
                float imageAnchorMinX = anchorX - (imageSize / 2);
                float imageAnchorMaxX = anchorX + (imageSize / 2);
                float imageAnchorMinY = anchorY + 0.04f - (imageSize / 2);
                float imageAnchorMaxY = anchorY + 0.02f + (imageSize / 2);
    
                AddImage(container, parent, item.Shortname, $"{imageAnchorMinX} {imageAnchorMinY}", $"{imageAnchorMaxX} {imageAnchorMaxY}", "ResourceImage");
                AddText(container, parent, $"{anchorX + 0.05f} {anchorY}", $"{anchorX + 0.05f + 0.8f} {anchorY + 0.06}", item.Amount.ToString(), 12, TextAnchor.MiddleLeft);
            }
        }
    
        private void AddHorizontalLine(CuiElementContainer container, string parent, string anchorMin, string anchorMax)
        {
            AddInnerSquare(container, parent, anchorMin, anchorMax, "0.8 0.8 0.8 1");
        }
    
        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 = CommandUiClose,
                        Close = UiBackground,
                    },
                    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;
    
            CuiHelper.DestroyUi(player, UiBackground);
            CuiHelper.DestroyUi(player, UiMainPanel);
            CuiHelper.DestroyUi(player, UiLeftPanel);
            CuiHelper.DestroyUi(player, UiRightPanel);
            CuiHelper.DestroyUi(player, UiPopupOverlayBlock);
            CuiHelper.DestroyUi(player, UiPopupShadow);
            CuiHelper.DestroyUi(player, UiPopupBorder);
            CuiHelper.DestroyUi(player, UiPopupBackground);
    
            _openedQuarryInterfaces.Remove(player.userID);
            _playersWithPopup.Remove(player.userID);
        }
    
        private void DestroyAllUis()
        {
            foreach (BasePlayer player in BasePlayer.activePlayerList)
                DestroyUi(player);
        }
    
        private void ToggleConfirmationPopup(BasePlayer player, string message, string confirmCommand, string cancelCommand)
        {
            if (player != null && !_playersWithPopup.Contains(player.userID))
            {
                _playersWithPopup.Add(player.userID);
                ShowConfirmationPopup(player, message, confirmCommand, cancelCommand);
            }
        }
    
        private void ShowConfirmationPopup(BasePlayer player, string message, string confirmCommand, string cancelCommand)
        {
            var container = new CuiElementContainer();
            AddPanel(container, "0 0", "1 1", UiPopupOverlayBlock, "Overlay", "0 0 0 0.9", true);
            AddPanel(container, "0.4 0.4", "0.6 0.6", UiPopupShadow, UiPopupOverlayBlock, "0 0 0 0.85");
            AddPanel(container, "0.0 0.09", "0.95 1.0", UiPopupBorder, UiPopupShadow, "1 1 1 1");
            AddPanel(container, "0.01 0.01", "0.991 0.985", UiPopupBackground, UiPopupBorder, "0 0 0 1");
            AddText(container, UiPopupBackground, "0 0", "1 1", message, 14, TextAnchor.MiddleCenter);
            AddButton(container, UiPopupBackground, Msg(player.UserIDString, "YesButton"), confirmCommand, "0.25", "0.05", "0.45", "0.20", "0.6 0.6 0.6 0.95", "0 1 0 1");
            AddButton(container, UiPopupBackground, Msg(player.UserIDString, "NoButton"), cancelCommand, "0.55", "0.05", "0.75", "0.20", "0.6 0.6 0.6 0.95", "1 0 0 1");
            CuiHelper.AddUi(player, container);
        }
    
        private void ClosePopup(BasePlayer player)
        {
            if (player != null)
            {
                CuiHelper.DestroyUi(player, UiPopupOverlayBlock);
                CuiHelper.DestroyUi(player, UiPopupShadow);
                CuiHelper.DestroyUi(player, UiPopupBorder);
                CuiHelper.DestroyUi(player, UiPopupBackground);
    
                _playersWithPopup.Remove(player.userID);
            }
        }
        #endregion
    
        #region UI Helpers
        private static 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 static 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 static 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 static void AddButton(
            CuiElementContainer container,
            string parent,
            string text,
            string command,
            string anchorMinX,
            string anchorMinY,
            string anchorMaxX,
            string anchorMaxY,
            string bgColor,
            string textColor,
            string buttonName = ""
        )
        {
            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,
                buttonName
            );
        }
        #endregion
    
        #region Quarry Management Methods
        private bool GiveQuarryItem(BasePlayer player, int level)
        {
            var item = CreateQuarryItem(player.userID, level);
    
            if (player.inventory.GiveItem(item))
                return true;
    
            item.Drop(player.inventory.containerMain.dropPosition, player.inventory.containerMain.dropVelocity);
            return false;
        }
    
        private Item CreateQuarryItem(ulong userId, int level)
        {
            var item = ItemManager.CreateByName(BaseItemName, 1, QuarrySkinId);
            item.name = "Quarry Computer";
    
            AddItemData(item.uid.Value, userId, level);
    
            return item;
        }
    
        private BuildingBlock GetBuildingBlock(BaseEntity entity)
        {
            Vector3 origin = entity.transform.position + Vector3.up * 0.1f;
            Ray ray = new Ray(origin, Vector3.down);
    
            if (Physics.Raycast(ray, out RaycastHit hitInfo, 4f, LayerMask.GetMask("Construction")))
            {
                var groundEntity = hitInfo.GetEntity();
    
                if (groundEntity is BuildingBlock buildingBlock)
                {
                    return buildingBlock;
                }
            }
    
            return null;
        }
    
        private bool DeployQuarry(BasePlayer player, BaseEntity entity, int level)
        {
            var buildingBlock = GetBuildingBlock(entity);
            if (buildingBlock == null)
                return false;
    
            var quarry = CreateAndPositionQuarry(player, entity, buildingBlock);
            ConfigureQuarry(player, quarry, level);
    
            return true;
        }
    
        private BaseEntity CreateAndPositionQuarry(BasePlayer player, BaseEntity entity, BaseEntity groundEntity)
        {
            var quarry = GameManager.server.CreateEntity(QuarryPrefab);
            quarry.OwnerID = player.userID;
            quarry.SetParent(groundEntity);
            quarry.transform.position = entity.transform.position;
            quarry.transform.rotation = entity.transform.rotation * Quaternion.Euler(0, 90, 0);
            quarry.Spawn();
            quarry.SendNetworkUpdateImmediate();
            return quarry;
        }
    
        private void ConfigureQuarry(BasePlayer player, BaseEntity quarry, int level)
        {
            AttachContainer(quarry, QuarryOutputStoragePrefab, new Vector3(0.05f, 0.75f, -0.21f), new Vector3(0, 180, 0), false, player);
            AttachContainer(quarry, QuarryFuelStoragePrefab, new Vector3(0.05f, 0.75f, 0.32f), new Vector3(90, 0, 0), true, player);
    
            AddQuarryData(player.userID, quarry.net.ID.Value, level);
            quarry.gameObject.AddComponent<QuarryComputerBehaviour>();
        }
    
        private void AttachContainer(BaseEntity entity, string prefab, Vector3 localPosition, Vector3 localEulerAngles, bool isFuelContainer, BasePlayer player)
        {
            var container = GameManager.server.CreateEntity(prefab) as StorageContainer;
            container.SetParent(entity);
            container.OwnerID = player.userID;
            container.transform.localPosition = localPosition;
            container.transform.localRotation = Quaternion.Euler(localEulerAngles);
            container.Spawn();
            container.SendNetworkUpdateImmediate();
    
            if (permission.UserHasPermission(player.UserIDString, "quarrycomputer.industrial"))
            {
                var adaptor = GameManager.server.CreateEntity(StorageAdaptorPrefab, container.transform.position, container.transform.rotation) as IndustrialStorageAdaptor;
                adaptor.SetParent(container, true, true);
                adaptor.OwnerID = player.userID;
                adaptor.transform.localPosition = isFuelContainer ? new Vector3(0.00f, -0.06f, -0.26f) : new Vector3(0.00f, 0.26f, 0.05f);
                adaptor.transform.rotation = container.transform.rotation * Quaternion.Euler(isFuelContainer ? new Vector3(0, 270, 90) : new Vector3(0, 90, 0));
                adaptor.Spawn();
                adaptor.SendNetworkUpdateImmediate();
            }
    
            if (isFuelContainer)
                return;
    
            var button = GameManager.server.CreateEntity(ButtonPrefab) as PressButton;
            button.SetParent(container, true, true);
            button.OwnerID = player.userID;
            button.transform.localPosition = new Vector3(0.40f, 1.60f, -0.13f);
            button.transform.rotation = container.transform.rotation * Quaternion.Euler(new Vector3(10, 90, 180));
            button.Spawn();
            button.SendNetworkUpdateImmediate();
        }
    
        private void HandleFuelConsumption(Item fuel, int requiredFuelAmount)
        {
            if (fuel.amount > requiredFuelAmount)
                SafelyRemoveItem(fuel.SplitItem(requiredFuelAmount));
            else
                SafelyRemoveItem(fuel);
        }
    
        public static class DictionaryPool<TKey, TValue>
        {
            private static readonly Stack<Dictionary<TKey, TValue>> _pool = new();
    
            public static Dictionary<TKey, TValue> Get()
            {
                lock (_pool)
                {
                    return _pool.Count > 0 ? _pool.Pop() : new Dictionary<TKey, TValue>();
                }
            }
    
            public static void Free(Dictionary<TKey, TValue> dictionary)
            {
                dictionary.Clear();
                lock (_pool)
                {
                    _pool.Push(dictionary);
                }
            }
        }
    
        private bool CanTransferAllResources(StorageContainer outputContainer, List<ResourceItem> resources)
        {
            Dictionary<string, int> stackableItems = DictionaryPool<string, int>.Get();
            int emptySlots = outputContainer.inventory.capacity - outputContainer.inventory.itemList.Count;
    
            foreach (var item in outputContainer.inventory.itemList)
            {
                if (!stackableItems.ContainsKey(item.info.shortname))
                    stackableItems[item.info.shortname] = 0;
    
                stackableItems[item.info.shortname] += item.MaxStackable() - item.amount;
            }
    
            foreach (var resource in resources)
            {
                var itemDef = ItemManager.FindItemDefinition(resource.Shortname);
                if (itemDef == null)
                {
                    DictionaryPool<string, int>.Free(stackableItems);
                    return false;
                }
    
                int remainingAmount = resource.Amount;
                if (stackableItems.TryGetValue(resource.Shortname, out int currentStackableSpace))
                    remainingAmount -= currentStackableSpace;
    
                while (remainingAmount > 0 && emptySlots > 0)
                {
                    remainingAmount -= itemDef.stackable;
                    emptySlots--;
                }
    
                if (remainingAmount > 0)
                {
                    DictionaryPool<string, int>.Free(stackableItems);
                    return false;
                }
            }
    
            DictionaryPool<string, int>.Free(stackableItems);
            return true;
        }
    
        private bool TransferResourcesIfPossible(StorageContainer outputContainer, List<ResourceItem> resources)
        {
            if (!CanTransferAllResources(outputContainer, resources))
                return false;
    
            foreach (var resource in resources)
            {
                var item = ItemManager.CreateByName(resource.Shortname, resource.Amount, 0uL);
                item.SetFlag(ExtractedItemFlag, true);
    
                if (!item.MoveToContainer(outputContainer.inventory))
                {
                    item.Remove();
                    return false;
                }
    
                item.SetFlag(ExtractedItemFlag, false);
            }
    
            return true;
        }
    
        private void SafelyRemoveItem(Item item)
        {
            item?.RemoveFromWorld();
            item?.RemoveFromContainer();
            item?.Remove();
        }
        #endregion
    
        #region Utility Methods
        private bool ValidateItemsCounts()
        {
            if (_configs.NoUpgrades.ResourcesOutput.Count > 12)
            {
                return false;
            }
    
            foreach (var upgrade in _configs.Upgrades)
            {
                if (upgrade.ResourcesOutput.Count > 12)
                {
                    return false;
                }
                if (upgrade.UpgradeItems.Count > 12)
                {
                    return false;
                }
            }
    
            return true;
        }
    
        private List<KeyValuePair<string, ulong>> GetItemsForValidation()
        {
            var itemIcons = new List<KeyValuePair<string, ulong>>();
    
            foreach (var item in _configs.NoUpgrades.ResourcesOutput)
                itemIcons.Add(new KeyValuePair<string, ulong>(item.Shortname, 0));
    
            foreach (var upgrade in _configs.Upgrades)
            {
                foreach (var item in upgrade.ResourcesOutput)
                    itemIcons.Add(new KeyValuePair<string, ulong>(item.Shortname, 0));
    
                foreach (var item in upgrade.UpgradeItems)
                {
                    if (item.Shortname != "Economics" && item.Shortname != "ServerRewards")
                        itemIcons.Add(new KeyValuePair<string, ulong>(item.Shortname, 0));
                }
            }
    
            itemIcons.Add(new KeyValuePair<string, ulong>(_configs.FuelConfiguration.Shortname, 0));
    
            return itemIcons;
        }
    
        private bool ValidateConfigItems(List<KeyValuePair<string, ulong>> items)
        {
            foreach (var item in items)
            {
                if (ItemManager.FindItemDefinition(item.Key) == null)
                {
                    PrintError($"Item '{item.Key}' does not exist.");
                    return false;
                }
            }
    
            return true;
        }
    
        private string Msg(string userId, string key, params object[] obj)
        {
            return string.Format(lang.GetMessage(key, this, userId), obj);
        }
    
        private void NotifyPlayer(IPlayer player, string key, int type, params object[] obj)
        {
            var userIdString = player.Id;
            if (_configs.UseNotify && Notify != null)
                Interface.Oxide.CallHook("SendNotify", userIdString, type, Msg(userIdString, key, obj));
            else
                player.Message(Msg(player.Id, key, obj));
        }
    
        private void NotifyPlayer(BasePlayer player, string key, int type, params object[] obj)
        {
            var userIdString = player.UserIDString;
            if (_configs.UseNotify && Notify != null)
                Interface.Oxide.CallHook("SendNotify", userIdString, type, Msg(userIdString, key, obj));
            else
                player.ChatMessage(Msg(userIdString, key, obj));
        }
    
        private void RegisterPermissions()
        {
            foreach (string permissionName in _permissions)
                permission.RegisterPermission(permissionName, this);
        }
    
        private void RegisterCommands()
        {
            AddCovalenceCommand(_configs.GiveQuarryCommand, nameof(GiveQuarryCommand));
        }
    
        private void InitializeExistingQuarries()
        {
            var existingNetIds = new HashSet<ulong>();
    
            foreach (var signalComputer in UnityEngine.Object.FindObjectsOfType<ExcavatorSignalComputer>())
            {
                if (signalComputer.OwnerID != 0)
                {
                    ulong netId = signalComputer.net.ID.Value;
                    ulong ownerId = signalComputer.OwnerID;
                    int level = GetQuarryLevelData(netId);
    
                    AddQuarryData(ownerId, netId, level);
                    existingNetIds.Add(netId);
    
                    signalComputer.gameObject.AddComponent<QuarryComputerBehaviour>();
                }
            }
    
            RemoveMissingQuarries(existingNetIds);
        }
    
        private void RemoveMissingQuarries(HashSet<ulong> existingNetIds)
        {
            var quarriesToRemove = _pluginData.QuarriesData.Keys.Where(id => !existingNetIds.Contains(id)).ToList();
    
            foreach (ulong quarryId in quarriesToRemove)
            {
                _pluginData.QuarriesData.Remove(quarryId);
            }
        }
    
        private void UnloadAllQuarryBehaviours()
        {
            var behaviours = UnityEngine.Object.FindObjectsOfType<QuarryComputerBehaviour>();
            foreach (var behaviour in behaviours)
            {
                behaviour.Unload();
                UnityEngine.Object.Destroy(behaviour);
            }
        }
    
        private void SetMaxLevel() => _maxLevel = _configs.Upgrades.Count + 1;
        #endregion
    
        #region Quarry Behaviour Class
        private class QuarryComputerBehaviour : MonoBehaviour
        {
            public ExcavatorSignalComputer SignalComputer { get; private set; }
            public StorageContainer OutputContainer { get; private set; }
            public StorageContainer FuelContainer { get; private set; }
            public List<ResourceItem> ResourcesOutput { get; private set; }
            public UpgradeOption UpgradeRequirements { get; private set; }
            public int Level { get; private set; }
            public ulong OwnerID { get; private set; }
            public bool IsOn { get; private set; } = true;
    
            private void Awake()
            {
                InitializeSignalComputer();
                UpdateQuarryOwnerId();
                InitializeContainers();
                UpdateQuarryLevel();
                UpdateQuarryResourcesOutput();
                UpdateQuarryCapacity();
                UpdateUpgradeRequirements();
                ScheduleRoutineTasks();
            }
    
            public bool TurnOn()
            {
                if (!IsInvoking(nameof(PerformResourceExtraction)))
                {
                    IsOn = true;
                    InvokeRepeating(nameof(PerformResourceExtraction), 0f, _instance._configs.FuelConfiguration.ResourceExtractionInterval);
                    return true;
                }
    
                return false;
            }
    
            private void InitializeSignalComputer()
            {
                SignalComputer = GetComponent<ExcavatorSignalComputer>();
                SignalComputer.SetFlag(BaseEntity.Flags.Reserved8, false);
                SignalComputer.chargePower = 0f;
            }
    
            private void UpdateQuarryOwnerId() => OwnerID = SignalComputer.OwnerID;
    
            private void InitializeContainers()
            {
                foreach (var container in SignalComputer.children.Cast<StorageContainer>())
                {
                    if (container.name == QuarryOutputStoragePrefab || container.name == QuarryFuelStoragePrefab)
                    {
                        if (container.name == QuarryOutputStoragePrefab)
                        {
                            OutputContainer = container;
                            _instance._itemContainersOutput.Add(container.inventory.uid.Value);
                        }
                        else
                        {
                            FuelContainer = container;
                            _instance._itemContainersFuel.Add(container.inventory.uid.Value);
                        }
    
                        foreach (var entity in container.children)
                        {
                            if (entity is PressButton)
                                _instance._buttons.Add(entity.net.ID.Value);
    
                            if (entity is IndustrialStorageAdaptor)
                                _instance._adaptors.Add(entity.net.ID.Value);
                        }
                    }
                }
            }
    
            public void Upgrade()
            {
                var newLevel = Level + 1;
                if (newLevel <= _instance._maxLevel)
                    Level = newLevel;
    
                _instance.UpdateQuarryLevelData(SignalComputer.net.ID.Value, newLevel);
                UpdateQuarryResourcesOutput();
                UpdateQuarryCapacity();
                UpdateUpgradeRequirements();
            }
    
            private void UpdateQuarryLevel() => Level = _instance.GetQuarryLevelData(SignalComputer.net.ID.Value);
    
            private void UpdateQuarryResourcesOutput()
            {
                ResourcesOutput = Level == 1 ? _instance._configs.NoUpgrades.ResourcesOutput : _instance._configs.Upgrades[Level - 2].ResourcesOutput;
            }
    
            private void UpdateQuarryCapacity()
            {
                if (Level == 1)
                {
                    OutputContainer.inventory.capacity = _instance._configs.NoUpgrades.StorageSlots.OutputSlots;
                    FuelContainer.inventory.capacity = _instance._configs.NoUpgrades.StorageSlots.FuelSlots;
                }
                else
                {
                    OutputContainer.inventory.capacity = _instance._configs.Upgrades[Level - 2].StorageSlots.OutputSlots;
                    FuelContainer.inventory.capacity = _instance._configs.Upgrades[Level - 2].StorageSlots.FuelSlots;
                }
            }
    
            private void UpdateUpgradeRequirements() => UpgradeRequirements = _instance._configs.Upgrades.ElementAtOrDefault(Level - 1);
    
            private void ScheduleRoutineTasks()
            {
                InvokeRepeating(nameof(PerformResourceExtraction), 0f, _instance._configs.FuelConfiguration.ResourceExtractionInterval);
            }
    
            public bool HasFuel()
            {
                var fuelItem = FuelContainer.inventory.itemList.Find(item => item.info.shortname == _instance._configs.FuelConfiguration.Shortname);
                int fuelRequired = _instance._configs.FuelConfiguration.FuelRequiredPerExtraction;
    
                return fuelItem != null && fuelItem.amount >= fuelRequired;
            }
    
            public bool HasSpace()
            {
                return _instance.CanTransferAllResources(OutputContainer, ResourcesOutput);
            }
    
            public bool IsEmpty() => FuelContainer.inventory.itemList.Count == 0 && OutputContainer.inventory.itemList.Count == 0;
    
            private void PerformResourceExtraction()
            {
                var fuelItem = FuelContainer.inventory.itemList.Find(item => item.info.shortname == _instance._configs.FuelConfiguration.Shortname);
                int fuelRequired = _instance._configs.FuelConfiguration.FuelRequiredPerExtraction;
    
                if (!HasSufficientFuel(fuelItem, fuelRequired))
                {
                    DeactivateQuarry();
                    return;
                }
    
                if (!_instance.TransferResourcesIfPossible(OutputContainer, ResourcesOutput))
                {
                    DeactivateQuarry();
                    return;
                }
    
                _instance.HandleFuelConsumption(fuelItem, fuelRequired);
                ActivateQuarry();
            }
    
            private bool HasSufficientFuel(Item fuelItem, int fuelRequired) => fuelItem != null && fuelItem.amount >= fuelRequired;
    
            private void ActivateQuarry()
            {
                SetSignalComputerState(true);
                IsOn = true;
            }
    
            private void DeactivateQuarry()
            {
                SetSignalComputerState(false);
                IsOn = false;
                CancelInvoke(nameof(PerformResourceExtraction));
            }
    
            private void SetSignalComputerState(bool isActive)
            {
                SignalComputer.SetFlag(BaseEntity.Flags.Reserved8, isActive);
                SignalComputer.chargePower = isActive ? 1 : 0;
            }
    
            private void OnDestroy()
            {
                CancelInvoke(nameof(PerformResourceExtraction));
            }
    
            public void Unload()
            {
                SignalComputer.SetFlag(BaseEntity.Flags.Reserved8, false);
                SignalComputer.chargePower = 0f;
            }
        }
        #endregion
    }
    

    }

  2. Assistant

    This script is a comprehensive Rust game plugin called QuarryComputer. It is developed for the Oxide game modding platform, adding functionalities that allow players to manage quarry operations using a computer interface within the game. Here’s a high-level explanation of the features and components of the plugin:

    Key Features:

    1. Quarry Deployment: Players can deploy a special computer to control quarry operations. This is conducted by placing a specific item configured to represent the quarry computer.

    2. UI Management: The plugin includes a customizable user interface (UI). Players can interact with this UI to control and configure their quarry operations—starting and stopping quarries, upgrading operations, and removing the quarry machine.

    3. Upgrades and Requirements: Players can upgrade their quarry systems using predefined items, enhancing the quarry's capabilities such as increasing resource output and storage slots.

    4. Command & Permission System: The plugin defines several commands for players to interact with the plugin's functionality, also checking for specific permissions before executing these commands.

    5. Integration with Other Plugins: The plugin can interface with other popular Rust plugins like EconLib for economy management and Notify to deliver structured messages to players.

    Main Components:

    • Configuration & Data Management: The plugin manages its configurations and data through JSON objects which are stored and loaded at runtime. This includes setup for items, fuel, upgrades, profiles, and more.

    • Game Hooks: The plugin makes use of game hooks such as OnServerInitialized, OnEntityBuilt, CanAcceptItem, etc., to tie into the Rust game engine’s events. This allows it to respond to various in-game events, such as when a player tries to place a quarry or interact with an existing one.

    • UI Handling: The plugin uses several UI commands to manage interface interactions. Functions create, update, and destroy the UI components on the player's screen based on their interaction.

    • Quarry Behaviour Class: This encapsulates the operations related to a single quarry, managing its state, interactions, fuel consumption, resource extraction, and upgrades.

    • Localization: The script provides messages in multiple languages, supporting international players by allowing the plugin to display messages in their chosen language.

    Workflow Description:

    1. Initialization: On server start, OnServerInitialized() sets up the plugin, loading configurations, registering commands and permissions, and initializing any persistent data.

    2. Placement Hook: When a player tries to place an item, OnEntityBuilt checks the item against quarry-related properties. If valid, it deploys the quarry, checking authorization and limits.

    3. Resource Management: The plugin manages the resources using inventories. Certain operations check the fuel and storage conditions before allowing extraction operations to continue.

    4. UI Interaction: Players can interact with an in-game UI to control quarry actions. Commands handle various actions like start, stop, upgrade, and confirm or cancel actions.

    5. Periodic Save & Unloads: Hooks for saving the state regularly, updating data structures and gracefully handling server unloads ensure the plugin state remains consistent across server restarts.

    This plugin is a robust solution to automate and manage quarry operations, providing a useful interface to players to interact with game mechanics in an organized manner. Additionally, it exemplifies leveraging the Oxide API to create complex systems that blend well into the Rust gameplay.

  3. User

    como eu poderia encontrar no mundo quantas caixas de madeira com uma skin um player tem?

  4. Assistant

    Para encontrar no mundo quantas caixas de madeira (ou qualquer outro item) com uma skin específica um jogador possui em Rust, você pode usar um script no ambiente do Oxide mod. Isso envolve percorrer todas as StorageContainers no mundo e verificar o proprietário e o identificador de skin.

    Aqui está um exemplo de como você poderia implementar essa funcionalidade em um plugin Rust:

    using Oxide.Core.Plugins;
    using Oxide.Core;
    using System.Collections.Generic;
    using UnityEngine;
    
    namespace Oxide.Plugins
    {
        [Info("Wooden Box Skin Checker", "AuthorName", "1.0.0")]
        [Description("Checks the number of wooden boxes with a specific skin owned by a player.")]
        public class WoodenBoxSkinChecker : RustPlugin
        {
            [Command("checkwoodenboxes")]
            private void CheckWoodenBoxesCommand(IPlayer iPlayer, string command, string[] args)
            {
                if (!iPlayer.IsAdmin)
                {
                    iPlayer.Reply("Você não tem permissão para usar este comando.");
                    return;
                }
    
                if (args.Length != 2)
                {
                    iPlayer.Reply("Uso: /checkwoodenboxes <SteamID> <SkinID>");
                    return;
                }
    
                if (!ulong.TryParse(args[0], out ulong playerId))
                {
                    iPlayer.Reply("O SteamID fornecido não é válido.");
                    return;
                }
    
                if (!ulong.TryParse(args[1], out ulong skinId))
                {
                    iPlayer.Reply("O SkinID fornecido não é válido.");
                    return;
                }
    
                int boxCount = CountWoodenBoxesWithSkin(playerId, skinId);
    
                iPlayer.Reply($"O jogador com SteamID {playerId} possui {boxCount} caixas de madeira com a skin {skinId}.");
            }
    
            private int CountWoodenBoxesWithSkin(ulong playerId, ulong skinId)
            {
                int count = 0;
                List<StorageContainer> containers = new List<StorageContainer>(UnityEngine.Object.FindObjectsOfType<StorageContainer>());
    
                foreach (var container in containers)
                {
                    if (container.OwnerID == playerId && container.skinID == skinId && container.PrefabName == "assets/prefabs/deployable/woodenbox/woodbox_deployed.prefab")
                    {
                        count++;
                    }
                }
    
                return count;
            }
        }
    }
    

    Explicação do Script:

    1. Comando checkwoodenboxes: Este é o comando que os administradores podem usar para verificar quantas caixas de madeira com uma skin específica um jogador possui. Ele requer dois argumentos: o ID do Steam do jogador e o ID da skin.

    2. Verificações de Permissão e Parâmetros: O script verifica se o usuário do comando é um administrador e se os argumentos fornecidos são válidos.

    3. Método CountWoodenBoxesWithSkin: Este método percorre todos os StorageContainer no jogo, verifica se o proprietário e a skin correspondem aos fornecidos e conta quantas caixas de madeira atendem aos critérios.

    4. Resposta do Comando: Após a contagem, o plugin retorna a quantidade de caixas encontradas com o proprietário e a skin especificados.

    Considerações:

    • Performance: Percorrer todos os StorageContainer pode ser um pouco pesado em servidores muito populosos ou com muitos objetos, mas geralmente é gerenciável para usos administrativos ocasionais.

    • Personalização: Você pode ajustar o script para incluir outros tipos de caixas ou considerar skins diferentes, alterando os identificadores e as lógicas de comparação conforme necessário.

    • Segurança: Este script é executado apenas para jogadores com permissões administrativas para evitar abusos ou sobrecarga por players normais.

  5. User

    não tem como melhorar isso? usar linq ou algo assim private int CountWoodenBoxesWithSkin(ulong playerId, ulong skinId, ulong skinIdSmall) { int count = 0; List containers = new List(UnityEngine.Object.FindObjectsOfType());

            foreach (var container in containers)
            {
                if (container.OwnerID == playerId && container.skinID == skinId && container.PrefabName == "assets/prefabs/deployable/large wood storage/box.wooden.large.prefab")
                {
                    count++;
                }
    
                if (container.OwnerID == playerId && container.skinID == skinIdSmall && container.PrefabName == "assets/prefabs/deployable/woodenbox/woodbox_deployed.prefab")
                {
                    count++;
                }
            }
    
            return count;
        }
    
  6. Assistant

    Você pode usar LINQ para tornar o código mais conciso e legível. O LINQ permite filtrar e projetar coleções de forma mais expressiva. No seu caso, você pode usar Enumerable.Where para filtrar as caixas, e Enumerable.Count para contar as caixas que atendem aos critérios. Aqui está um exemplo aprimorado usando LINQ:

    using System.Linq;
    using UnityEngine;
    
    namespace Oxide.Plugins
    {
        public partial class WoodenBoxSkinChecker : RustPlugin
        {
            private int CountWoodenBoxesWithSkin(ulong playerId, ulong skinId, ulong skinIdSmall)
            {
                var containers = UnityEngine.Object.FindObjectsOfType<StorageContainer>();
    
                return containers.Count(container =>
                    container.OwnerID == playerId &&
                    ((container.skinID == skinId && container.PrefabName == "assets/prefabs/deployable/large wood storage/box.wooden.large.prefab") ||
                     (container.skinID == skinIdSmall && container.PrefabName == "assets/prefabs/deployable/woodenbox/woodbox_deployed.prefab"))
                );
            }
        }
    }
    

    Explicação:

    • Uso de FindObjectsOfType<StorageContainer>: Mantém a busca de todos os contêineres de armazenamento no mundo.

    • Uso de LINQ Count: O método Count é usado diretamente na coleção de contêineres enquanto aplica a condição especificada, economizando a necessidade de um loop foreach explícito.

    • Condições Combinadas com ||: As condições para os dois tipos de caixa e skins são combinadas usando o operador lógico || (ou), tornando o filtro mais eficiente e o código mais compacto.

    Isso não apenas melhora a legibilidade do código, mas também o mantém eficiente, aproveitando os métodos de extensão do LINQ para realizar operações de coleção de maneira expressiva.

  7. User

    não tem como ser direto aqui? var containers = UnityEngine.Object.FindObjectsOfType();

  8. Assistant

    Você pode otimizar a maneira como percorre os objetos no mundo do Unity, especialmente se sua intenção é focar diretamente nos identificadores de skin e prefabs desejados. Enquanto UnityEngine.Object.FindObjectsOfType<StorageContainer>() é bastante utilizado, ele pode, de fato, ser um pouco mais custoso em termos de desempenho se houver muitos objetos no mundo. Ainda assim, ele é a abordagem padrão para encontrar todos os objetos de um determinado tipo.

    Se o objetivo é melhorar o acesso direto sem percorrer todos os objetos quando desnecessário, infelizmente, não há uma maneira embutida no Unity/C# para filtrar os objetos diretamente na fase de busca. No entanto, podemos aplicar o seguinte:

    • Usar LINQ para tornar o código mais direto e filtrado pós-busca: Enquanto a obtenção inicial de todos os StorageContainer ainda é necessária, o uso efetivo do LINQ pode filtrar e contar os objetos de interesse rapidamente.

    Aqui está como isso pode ser escrito de forma concisa usando LINQ:

    using System.Linq;
    using UnityEngine;
    
    namespace Oxide.Plugins
    {
        public partial class WoodenBoxSkinChecker : RustPlugin
        {
            private int CountWoodenBoxesWithSkin(ulong playerId, ulong largeBoxSkinId, ulong smallBoxSkinId)
            {
                return UnityEngine.Object
                    .FindObjectsOfType<StorageContainer>()
                    .Count(container =>
                        container.OwnerID == playerId && 
                        (
                            (container.skinID == largeBoxSkinId && container.PrefabName == "assets/prefabs/deployable/large wood storage/box.wooden.large.prefab") ||
                            (container.skinID == smallBoxSkinId && container.PrefabName == "assets/prefabs/deployable/woodenbox/woodbox_deployed.prefab")
                        )
                    );
            }
        }
    }
    

    Observações:

    • Abordagem Direta: Ainda usamos FindObjectsOfType<StorageContainer>(), pois é a forma padrão de buscar todos os objetos de um tipo específico no mundo do Unity. Esta chamada deve existir a menos que você tenha uma arquitetura de sistema diferente que rastreie ou registre objetos conforme são criados.

    • Eficiência com LINQ: Utilizamos os métodos de filtragem do LINQ diretamente na coleção obtida para um código mais limpo e eficiente.

    Eventuais melhorias no desempenho nos jogos multiplayer como Rust podem exigir mudanças no nível do próprio servidor ou na arquitetura do plugin para evitar buscas globais completas, mas isso geralmente demanda mudanças significativas na forma como os objetos são geridos.

  9. User

    gimme a msg for this error: LimitReached

  10. Assistant

    Certainly! When designing an error message for a "LimitReached" scenario, it's essential to be clear and concise, while also providing the user with enough context about the situation. Here’s a suggestion:


    Error: Limit Reached

    You have reached the allowed limit for this operation. Please check your current limits and try again later. If you believe this is an error, contact support for assistance.


    Explanation:

    • Clear Header: The message starts with a clear and direct header ("Error: Limit Reached") to immediately inform the user of the issue.
    • Description: It provides a brief explanation that the user has reached a limit.
    • Action: It suggests the user check their current limits, implying there may be set boundaries they are aware of.
    • Support Suggestion: It offers a course of action (contact support) if the user believes there is a mistake, providing a way to seek resolution.

    Feel free to adjust the language and specific advice according to the exact application and context in which this error might occur.

  11. User

    limit os recyclers, shorter

  12. Assistant

    Certainly! Here’s a concise error message tailored for limiting the number of recyclers:


    Recycler Limit Reached

    You have reached the maximum number of allowed recyclers.


    Explanation:

    • Direct and Clear: The message quickly informs the user of the exact issue.
    • Specific Context: It specifies that the limit pertains to recyclers, eliminating ambiguity.
    • Brevity: The message is kept short to convey the necessary information efficiently.

    This approach ensures that users are promptly and clearly informed about the constraint without unnecessary detail.

  13. User

    em portugues e alemão: Recycler Limit Reached.

  14. Assistant

    Claro! Aqui estão as traduções para português e alemão:

    Português:

    Limite de Recicladores Atingido.
    

    Alemão:

    Recycler-Limit erreicht.
    

    Essas mensagens são diretas e mantêm o mesmo tom claro e conciso do original em inglês.

  15. User

    preciso checar se tbm não é outro prefab com outra skin

            if (player == null || entity == null || entity.ShortPrefabName != LargeBoxPrefab || entity.skinID != config.LargeBoxSkinIdRecycler)
                return;
    
  16. Assistant

    Para verificar se uma entidade não corresponde a um ou mais prefabs e skins, você pode expandir a condição com operadores lógicos para abranger todas as possibilidades que deseja excluir. Se você está lidando com mais de um prefab e skin que precisam ser verificados, utilize uma estrutura como a abaixo:

    Supondo que você tenha mais de um Prefab e SkinId para verificar, você pode fazer algo assim:

    // Define os prefabs e skins que deseja evitar
    string[] forbiddenPrefabs = { "Prefab1", "Prefab2" }; // substitua pelos nomes reais dos prefabs
    ulong[] forbiddenSkinIds = { 1111, 2222 }; // substitua pelos IDs reais das skins
    
    // Verifica se o prefab ou a skin da entidade não são os proibidos
    if (player == null || entity == null || 
        forbiddenPrefabs.Contains(entity.ShortPrefabName) || 
        forbiddenSkinIds.Contains(entity.skinID))
    {
        return; 
    }
    

    Explicação:

    • Arrays para Prefabs e Skins Proibidos: Eu defino dois arrays, forbiddenPrefabs e forbiddenSkinIds, que contêm os nomes de prefab e IDs de skin que deseja bloquear. Altere-os conforme necessário para que representem os prefabs e skins que você está interessado em evitar.

    • Uso de Contains: Usar o método Contains em arrays facilita verificar se um elemento faz parte dos arrays proibidos. Isso ajuda a manter o código conciso e flexível caso precise aumentar as listas no futuro.

    Este formato permite que você adicione quantos prefabs e skin IDs precisar sem complicar a lógica condicional e mantém seu código legível e fácil de manter.

  17. User

    está correto? private void OnEntityBuilt(Planner planner, GameObject builtObject) { var player = planner?.GetOwnerPlayer(); var entity = builtObject.GetComponent();

            if (player == null || entity == null || (entity.skinID != config.LargeBoxSkinIdRecycler && entity.skinID != config.SmallBoxSkinIdRecycler))
                return;
    
            var count = CountRecyclers(player.userID);
            player.ChatMessage("count: " + count);
            if (count >= config.RecyclerLimit)
            {
                player.ChatMessage(lang.GetMessage("LimitReached", this, player.UserIDString));
                entity?.Kill();
                return;
            }
        }
    
  18. Assistant

    Your code snippet appears mostly correct, but let’s go through it to ensure clarity and functionality, especially focusing on checking both the skinID and handling the null values properly:

    1. Null Checks: You are safely checking if planner and entity are null before proceeding, which is good for avoiding potential null reference exceptions.

    2. Skin ID Check: The condition (entity.skinID != config.LargeBoxSkinIdRecycler && entity.skinID != config.SmallBoxSkinIdRecycler) ensures that the code only proceeds if the entity's skin ID matches one of the specified IDs. This logic will continue if the skin ID doesn't match either of the provided IDs.

    3. Count and Limit Check: The snippet checks a recycler count against a configured limit and sends appropriate messages to the player. If the limit is reached, the entity is destroyed.

    To ensure correctness and clarity, here’s a slightly refined version of your code with comments:

    private void OnEntityBuilt(Planner planner, GameObject builtObject)
    {
        var player = planner?.GetOwnerPlayer();
        var entity = builtObject.GetComponent<BaseEntity>();
    
        // Check if either player or entity is null, or if the entity skin doesn't match the allowed ones
        if (player == null || entity == null || 
            (entity.skinID != config.LargeBoxSkinIdRecycler && entity.skinID != config.SmallBoxSkinIdRecycler))
        {
            return;
        }
    
        // Count the number of recyclers the player currently has
        var count = CountRecyclers(player.userID);
        player.ChatMessage("count: " + count);
    
        // Check if the player's recycler count exceeds the allowed limit
        if (count >= config.RecyclerLimit)
        {
            // Notify player about reaching the limit and remove the entity
            player.ChatMessage(lang.GetMessage("LimitReached", this, player.UserIDString));
            entity?.Kill();
            return;
        }
    
        // Note: Add any additional logic you might need here for when the player hasn't reached the recycler limit
    }
    

    Points to Consider:

    • Functionality Check (CountRecyclers): Ensure that the CountRecyclers function correctly counts the recyclers specific to the player's user ID, based on whatever criteria or data structure you're maintaining.

    • Configuration (config): Double-check that the config object is properly initialized with LargeBoxSkinIdRecycler, SmallBoxSkinIdRecycler, and RecyclerLimit.

    • Localization: Make sure that the localization key "LimitReached" exists in your language file so that the lang.GetMessage method retrieves a valid string.

    • Additional Logic: If there are actions to be taken when the limit isn't reached, this is where you can handle those scenarios in your function.

  19. User

    how can i give the item back to the player?

  20. Assistant

    To give an item back to a player in Rust using the Oxide API, you need to create the item and then attempt to add it to the player's inventory. If the inventory is full, you might want to drop the item at the player's position. Here's a step-by-step example of how you can implement this:

    Step-by-Step Guide:

    1. Create the Item: Use ItemManager.CreateByName to create an instance of the item you want to give back to the player.

    2. Add the Item to the Player's Inventory: Try to place the item in the player's inventory using player.inventory.GiveItem. If the inventory is full, the function will return false.

    3. Drop the Item if Inventory is Full: If the player has no space in their inventory, drop the item on the ground using the player's position and velocity.

    Example Code:

    private void GiveItemBackToPlayer(BasePlayer player, string itemName, ulong skinID = 0)
    {
        // Step 1: Create the item
        Item item = ItemManager.CreateByName(itemName, 1, skinID);
        if (item == null)
        {
            Puts($"Failed to create item with name {itemName}");
            return;
        }
    
        // Step 2: Attempt to add the item to the player's inventory
        if (!player.inventory.GiveItem(item))
        {
            // Step 3: Inventory is full, drop the item at the player's position
            Vector3 dropPosition = player.transform.position + Vector3.up * 0.5f; // Slight offset to drop the item above the ground
            Vector3 dropVelocity = player.eyes.BodyForward() * 2; // Forward direction to give it a little throw effect
            item.Drop(dropPosition, dropVelocity);
            player.ChatMessage("Your inventory is full. The item has been dropped on the ground.");
        }
        else
        {
            player.ChatMessage("The item has been returned to your inventory.");
        }
    }
    

    Explanation:

    • Item Creation: The ItemManager.CreateByName method creates an item by its short name. Optionally, you can specify a skinID if your item has a custom skin.

    • Inventory Check: player.inventory.GiveItem(item) attempts to place the item in the player's inventory. If it returns false, the inventory was full.

    • Item Drop: If inventory space is not available, item.Drop is used to drop the item at a calculated position slightly above the player's position. The drop velocity is intended to visibly move the item forward from the player's position.

    • Player Feedback: Chat messages provide feedback so the player knows what happened to their item.

    This example provides a complete transaction where an item is either returned to a player's inventory or dropped near them if space isn't available, providing a smooth user experience. Adjust the vectors and messages as necessary for your specific needs.

  21. User

    here, how can i give it back? private void OnEntityBuilt(Planner planner, GameObject builtObject) { var player = planner?.GetOwnerPlayer(); var entity = builtObject.GetComponent();

            // Check if either player or entity is null, or if the entity skin doesn't match the allowed ones
            if (player == null || entity == null || (entity.skinID != config.LargeBoxSkinIdRecycler && entity.skinID != config.SmallBoxSkinIdRecycler))
            {
                return;
            }
    
            // Count the number of recyclers the player currently has
            var count = CountRecyclers(player.userID);
            player.ChatMessage("count: " + count);
    
            // Check if the player's recycler count exceeds the allowed limit
            if (count >= config.RecyclerLimit)
            {
                // Notify player about reaching the limit and remove the entity
                player.ChatMessage(lang.GetMessage("LimitReached", this, player.UserIDString));
                entity?.Kill();
                return;
            }
    
            // Note: Add any additional logic you might need here for when the player hasn't reached the recycler limit
        }
    
  22. Assistant

    To give the item back to the player in your OnEntityBuilt method, you will need to create the item again using ItemManager.CreateByName() and attempt to add it to the player's inventory. If the inventory is full, you can drop the item near the player's location. Here's how you can integrate this logic into your existing method:

    private void OnEntityBuilt(Planner planner, GameObject builtObject)
    {
        var player = planner?.GetOwnerPlayer();
        var entity = builtObject.GetComponent<BaseEntity>();
    
        // Check if either player or entity is null, or if the entity skin doesn't match the allowed ones
        if (player == null || entity == null || 
            (entity.skinID != config.LargeBoxSkinIdRecycler && entity.skinID != config.SmallBoxSkinIdRecycler))
        {
            return;
        }
    
        // Count the number of recyclers the player currently has
        var count = CountRecyclers(player.userID);
        player.ChatMessage("count: " + count);
    
        // Check if the player's recycler count exceeds the allowed limit
        if (count >= config.RecyclerLimit)
        {
            // Notify player about reaching the limit
            player.ChatMessage(lang.GetMessage("LimitReached", this, player.UserIDString));
            
            // Kill the entity to prevent its existence
            entity?.Kill();
    
            // Use the appropriate item name based on the entity's prefab
            string itemName = entity.ShortPrefabName == "box.wooden.large.prefab" 
                ? "woodbox.large" 
                : "woodbox";
    
            // Give the item back to the player
            GiveItemBackToPlayer(player, itemName, entity.skinID);
    
            return;
        }
    
        // Note: Add any additional logic you might need here for when the player hasn't reached the recycler limit
    }
    
    private void GiveItemBackToPlayer(BasePlayer player, string itemName, ulong skinID)
    {
        // Step 1: Create the item
        Item item = ItemManager.CreateByName(itemName, 1, skinID);
        if (item == null)
        {
            Puts($"Failed to create item with name {itemName}");
            return;
        }
    
        // Step 2: Attempt to add the item to the player's inventory
        if (!player.inventory.GiveItem(item))
        {
            // Step 3: Inventory is full, drop the item at the player's position
            Vector3 dropPosition = player.transform.position + Vector3.up * 0.5f; // Slight offset to drop the item above the ground
            item.Drop(dropPosition, Vector3.zero); // Drop with no velocity
            player.ChatMessage("Your inventory is full. The item has been dropped on the ground.");
        }
        else
        {
            player.ChatMessage("The item has been returned to your inventory.");
        }
    }
    

    Key Points:

    • Entity Check: Ensure that you correctly identify the prefab and related item name to recreate the item.
    • Item Recreation: ItemManager.CreateByName is used to recreate the item with the appropriate skin.
    • Inventory Management: Attempt to add the item back to the player's inventory and handle the case where the inventory might be full.
    • Feedback: Send chat messages to inform the player about what happened to their item.

    Ensure the item names used (e.g., "woodbox.large", "woodbox") match the actual short names used in the game for those items, and adjust as needed based on your specific use case and game data.

  23. User

    faça um texto para atualização do meu plugin dizendo: adicionado limite de recyclers na configuração

  24. Assistant

    Claro! Aqui está um texto para a atualização do seu plugin que menciona a adição do limite de recicladores na configuração:


    Atualização do Plugin - Versão X.X.X

    Temos o prazer de anunciar uma nova atualização para o seu plugin, que agora inclui uma funcionalidade muito solicitada: a capacidade de configurar um limite para o número de recicladores.

    Novidade:

    • Limite de Recicladores Adicionado: Agora é possível definir um número máximo de recicladores que cada jogador pode construir simultaneamente. Este recurso foi implementado para ajudar a manter o equilíbrio do jogo e melhorar a performance do servidor ao limitar a quantidade de recicladores em uso.

    Como Configurar:

    1. Localize o arquivo de configuração do plugin.
    2. Encontre a nova opção RecyclerLimit.
    3. Defina o valor desejado para o limite de recicladores por jogador.

    Esperamos que esta atualização melhore ainda mais a jogabilidade e a gestão do servidor. Agradecemos o seu feedback contínuo, que ajuda a guiar o desenvolvimento de funcionalidades que atendem melhor às suas necessidades.

    Para qualquer dúvida ou sugestão, entre em contato com nossa equipe de suporte. Agradecemos por usar nosso plugin!


    Esse texto busca ser claro e informativo, informando aos usuários sobre a nova funcionalidade e como usá-la, ao mesmo tempo em que oferece suporte contínuo.

  25. User

    não... em uma linha curta, em ingles

  26. Assistant

    Added recycler limit configuration.

  27. User

    faça uma msg para eu avisa o cliente que subi o que ele pediu

  28. Assistant

    Certainly! Here's a concise message you can send to your client to inform them that you've implemented their requested change:


    Hello [Client's Name],

    I've just implemented the feature you requested. Please check it out and let me know if everything meets your expectations!

    Best regards,

    [Your Name]


    Be sure to personalize it with the client's name and your own details before sending.