organize melhor esse código e padronize as nomenclaturas, não altere o funcionamento de…
- User
organize melhor esse código e padronize as nomenclaturas, não altere o funcionamento de nada: using System.Collections.Generic; using Oxide.Core.Plugins; using Oxide.Game.Rust.Cui; using UnityEngine;
namespace Oxide.Plugins { [Info("QuarryComputerUi", "Marte6", "1.0.11")] public class QuarryComputerUi : RustPlugin { [PluginReference] private Plugin ImageLibrary;
private static readonly string[] UiElements = { "QuarryUi_Background", "QuarryUi_CloseButton", "Overlay_Block", "Popup_Shadow", "Popup_Border", "Popup_Background" }; private static HashSet<BasePlayer> playersWithPopup = new HashSet<BasePlayer>(); private const string CommandPrefix = "quarrycomputerui"; private readonly Dictionary<string, string> colorOptions = new Dictionary<string, string> { { "Gray", "0.5 0.5 0.5 0.9" }, { "LightGray", "0.60 0.60 0.60 0.95" }, { "DarkGray", "0.25 0.25 0.25 0.95" }, { "Red", "1 0 0 1" }, { "DarkRed", "0.55 0 0 1" }, { "FireBrick", "0.7 0.13 0.13 1" }, { "Crimson", "0.86 0.08 0.24 1" }, { "Maroon", "0.5 0 0 1" }, { "Burgundy", "0.5 0 0.13 1" }, { "DeepPink", "1 0.08 0.58 1" }, { "Fuchsia", "1 0 1 1" }, { "Magenta", "1 0 1 1" }, { "Brown", "0.6 0.3 0 1" }, { "DarkMagenta", "0.55 0 0.55 1" }, { "IndianRed", "0.8 0.36 0.36 1" }, { "RosyBrown", "0.74 0.56 0.56 1" }, { "MediumVioletRed", "0.78 0.08 0.52 1" }, { "Purple", "0.5 0 0.5 1" }, { "Blue", "0 0 1 1" }, { "Green", "0 1 0 1" }, { "Yellow", "1 1 0 1" }, { "Cyan", "0 1 1 1" }, { "Orange", "1 0.5 0 1" }, { "Pink", "1 0.41 0.71 1" }, { "Lime", "0.75 1 0 1" }, { "Teal", "0 0.5 0.5 1" }, { "Lavender", "0.9 0.9 0.98 1" }, { "White", "1 1 1 1" }, { "Black", "0 0 0 1" }, { "SkyBlue", "0.53 0.81 0.98 1" }, { "Ivory", "1 1 0.94 1" }, { "Goldenrod", "0.85 0.65 0.13 1" }, { "Coral", "1 0.5 0.31 1" }, { "Salmon", "0.98 0.5 0.45 1" }, { "Navy", "0 0 0.5 1" }, { "Turquoise", "0.25 0.88 0.82 1" }, { "Chartreuse", "0.5 1 0 1" }, { "SlateGray", "0.44 0.5 0.56 1" }, { "ForestGreen", "0.13 0.55 0.13 1" }, { "Khaki", "0.94 0.9 0.55 1" }, }; private void Init() => RemoveUiFromAllPlayers(); [ChatCommand("ui")] private void OpenUiCommand(BasePlayer player) { RemoveUiFromPlayer(player); BuildAndSendUI(player, 1); } [ConsoleCommand(CommandPrefix + ".close")] private void CloseUiCommand(ConsoleSystem.Arg args) { var player = args.Player(); if (player != null) { RemoveUiFromPlayer(player); PrintToChat(player, "Close button was clicked."); } } [ConsoleCommand(CommandPrefix + ".remove")] private void RemoveQuarryCommand(ConsoleSystem.Arg args) { var player = args.Player(); if (player != null) { if (!playersWithPopup.Contains(player)) { playersWithPopup.Add(player); ShowConfirmationPopup(player, "Do you want to remove the Quarry?", CommandPrefix + ".confirmremove", CommandPrefix + ".cancelremove"); } } } [ConsoleCommand(CommandPrefix + ".confirmremove")] private void ConfirmRemoveQuarryCommand(ConsoleSystem.Arg args) { var player = args.Player(); if (player != null) { CloseConfirmationPopup(player); PrintToChat(player, "Quarry removed."); } } [ConsoleCommand(CommandPrefix + ".cancelremove")] private void CancelRemoveQuarryCommand(ConsoleSystem.Arg args) { var player = args.Player(); if (player != null) { CloseConfirmationPopup(player); PrintToChat(player, "Remove action cancelled."); } } [ConsoleCommand(CommandPrefix + ".upgrade")] private void UpgradeQuarryCommand(ConsoleSystem.Arg args) { var player = args.Player(); if (player != null) { if (!playersWithPopup.Contains(player)) { playersWithPopup.Add(player); ShowConfirmationPopup(player, "Do you want to upgrade the Quarry?", CommandPrefix + ".confirmupgrade", CommandPrefix + ".cancelupgrade"); } } } [ConsoleCommand(CommandPrefix + ".confirmupgrade")] private void ConfirmUpgradeQuarryCommand(ConsoleSystem.Arg args) { var player = args.Player(); if (player != null) { CloseConfirmationPopup(player); PrintToChat(player, "Quarry upgraded."); } } [ConsoleCommand(CommandPrefix + ".cancelupgrade")] private void CancelUpgradeQuarryCommand(ConsoleSystem.Arg args) { var player = args.Player(); if (player != null) { CloseConfirmationPopup(player); PrintToChat(player, "Upgrade cancelled."); } } private void ShowConfirmationPopup(BasePlayer player, string message, string confirmCommand, string cancelCommand) { var container = new CuiElementContainer(); // Overlay to block interaction with the background UI AddPanel(container, "0 0", "1 1", "Overlay_Block", "Overlay", "0 0 0 0.9", cursorEnabled: true); // Shadow AddPanel(container, "0.4 0.4", "0.6 0.6", "Popup_Shadow", "Overlay_Block", "0 0 0 0.85"); // Border AddPanel(container, "0.0 0.09", "0.95 1.0", "Popup_Border", "Popup_Shadow", colorOptions["White"]); // Background string popupBackground = "Popup_Background"; AddPanel(container, "0.01 0.01", "0.991 0.985", popupBackground, "Popup_Border", colorOptions["Black"]); AddText(container, popupBackground, "0.1 0.6", "0.9 0.9", message, 16, TextAnchor.MiddleCenter); AddButton(container, popupBackground, "Yes", confirmCommand, "0.25", "0.05", "0.45", "0.15", colorOptions["LightGray"], colorOptions["Green"]); AddButton(container, popupBackground, "No", cancelCommand, "0.55", "0.05", "0.75", "0.15", colorOptions["LightGray"], colorOptions["Red"]); CuiHelper.AddUi(player, container); } private void CloseConfirmationPopup(BasePlayer player) { foreach (var element in UiElements) { if (element.StartsWith("Popup_") || element == "Overlay_Block") { CuiHelper.DestroyUi(player, element); } } playersWithPopup.Remove(player); } private void RemoveUiFromPlayer(BasePlayer player) { if (player == null) return; foreach (var element in UiElements) { CuiHelper.DestroyUi(player, element); } player.SetPlayerFlag(BasePlayer.PlayerFlags.Spectating, false); player.UpdateActiveItem(default); playersWithPopup.Remove(player); } private void RemoveUiFromAllPlayers() { foreach (var player in BasePlayer.activePlayerList) { RemoveUiFromPlayer(player); } } private void BuildAndSendUI(BasePlayer player, int level) { var container = new CuiElementContainer(); string parentPanel = "QuarryUi_Background"; AddPanel(container, "0 0", "1 1", parentPanel, color: "0 0 0 0", cursorEnabled: true); AddMainCombinedPanel(container, level, parentPanel); AddCloseButton(container, "QuarryUi_MainCombined"); AddImage(container, parentPanel, "crude.oil", "0.1 0.1", "0.2 0.2", "ResourceImage"); CuiHelper.AddUi(player, container); player.SetPlayerFlag(BasePlayer.PlayerFlags.Spectating, true); } private void AddMainCombinedPanel(CuiElementContainer container, int level, string parent) { var mainPanelName = "QuarryUi_MainCombined"; AddPanel(container, "0.235 0.26", "0.765 0.724", mainPanelName, parent, color: colorOptions["LightGray"], isBackground: true); AddQuarryLevelPanel(container, level, mainPanelName); AddUpgradePanel(container, level, mainPanelName); } private void AddQuarryLevelPanel(CuiElementContainer container, int level, string parent) { var panelName = "QuarryUi_Full_Left"; AddPanel(container, "0.0 0", "0.5 1", panelName, parent, color: "0 0 0 0"); AddInnerSquare(container, panelName, "0.10 0.05", "0.95 0.90", "0 0 0 0.97"); AddInnerSquare(container, panelName, "0.054 0.1033", "0.895 0.945", "0.8 0.8 0.8 1"); AddInnerSquare(container, panelName, "0.06 0.108", "0.89 0.94", colorOptions["DarkRed"]); AddInnerSquare(container, panelName, "0.06 0.865", "0.89 0.870", "0.8 0.8 0.8 1"); AddText(container, panelName, "0.1 0.84", "0.9 0.98", $"Quarry Level {level}", 15, TextAnchor.MiddleCenter); AddSeparator(container, panelName, "0.06 0.82", "0.89 0.825"); AddText(container, panelName, "0.1 0.78", "0.9 0.82", "Extraction Settings:", 12, TextAnchor.MiddleLeft); AddText(container, panelName, "0.1 0.74", "0.9 0.78", "Extraction Interval: 1 second", 10, TextAnchor.MiddleLeft); AddText(container, panelName, "0.1 0.7", "0.9 0.74", "Fuel per extraction: 1 Low Grade Fuel", 10, TextAnchor.MiddleLeft); AddSeparator(container, panelName, "0.06 0.65", "0.89 0.655"); AddText(container, panelName, "0.1 0.6", "0.9 0.65", "Storage:", 12, TextAnchor.MiddleLeft); AddText(container, panelName, "0.1 0.56", "0.9 0.6", "Fuel Storage: 3 slots", 10, TextAnchor.MiddleLeft); AddText(container, panelName, "0.1 0.52", "0.9 0.56", "Resource Storage: 6 slots", 10, TextAnchor.MiddleLeft); AddSeparator(container, panelName, "0.06 0.47", "0.89 0.475"); AddText(container, panelName, "0.1 0.42", "0.9 0.47", "Resources Output Per Extraction:", 12, TextAnchor.MiddleLeft); AddText(container, panelName, "0.1 0.38", "0.9 0.42", "Crude Oil: 10", 10, TextAnchor.MiddleLeft); AddText(container, panelName, "0.1 0.34", "0.9 0.38", "HQM Ore: 60", 10, TextAnchor.MiddleLeft); AddText(container, panelName, "0.1 0.3", "0.9 0.34", "Metal Ore: 100", 10, TextAnchor.MiddleLeft); AddText(container, panelName, "0.1 0.26", "0.9 0.3", "Stones: 250", 10, TextAnchor.MiddleLeft); AddText(container, panelName, "0.1 0.22", "0.9 0.26", "Sulfur Ore: 160", 10, TextAnchor.MiddleLeft); AddButton(container, panelName, "Remove Quarry", CommandPrefix + ".remove", "0.35", "0.11", "0.65", "0.16", colorOptions["LightGray"], colorOptions["Red"]); } private void AddUpgradePanel(CuiElementContainer container, int level, string parent) { var panelName = "QuarryUi_Full_Right"; AddPanel(container, "0.5 0", "1 1", panelName, parent, color: "0 0 0 0"); AddInnerSquare(container, panelName, "0.10 0.05", "0.95 0.90", "0 0 0 0.97"); AddInnerSquare(container, panelName, "0.054 0.1033", "0.895 0.945", "0.8 0.8 0.8 1"); AddInnerSquare(container, panelName, "0.06 0.108", "0.89 0.94", "0 0 1 1"); AddInnerSquare(container, panelName, "0.06 0.865", "0.89 0.870", "0.8 0.8 0.8 1"); AddText(container, panelName, "0.1 0.84", "0.9 0.98", $"Upgrade to Level {level + 1}", 15, TextAnchor.MiddleCenter); AddSeparator(container, panelName, "0.06 0.82", "0.89 0.825"); AddText(container, panelName, "0.1 0.78", "0.9 0.82", "Requirements:", 12, TextAnchor.MiddleLeft); AddText(container, panelName, "0.1 0.74", "0.9 0.78", "Scrap: 5000", 10, TextAnchor.MiddleLeft); AddText(container, panelName, "0.1 0.7", "0.9 0.74", "Bone Fragments: 10000", 10, TextAnchor.MiddleLeft); AddSeparator(container, panelName, "0.06 0.65", "0.89 0.655"); AddText(container, panelName, "0.1 0.6", "0.9 0.65", "Upgrade Effects:", 12, TextAnchor.MiddleLeft); AddText(container, panelName, "0.1 0.56", "0.9 0.6", "Fuel Storage: 6 slots", 10, TextAnchor.MiddleLeft); AddText(container, panelName, "0.1 0.52", "0.9 0.56", "Resource Storage: 12 slots", 10, TextAnchor.MiddleLeft); AddSeparator(container, panelName, "0.06 0.47", "0.89 0.475"); AddText(container, panelName, "0.1 0.42", "0.9 0.47", "Crude Oil: 20", 10, TextAnchor.MiddleLeft); AddText(container, panelName, "0.1 0.38", "0.9 0.42", "HQM Ore: 120", 10, TextAnchor.MiddleLeft); AddText(container, panelName, "0.1 0.34", "0.9 0.38", "Metal Ore: 150", 10, TextAnchor.MiddleLeft); AddText(container, panelName, "0.1 0.3", "0.9 0.34", "Stones: 350", 10, TextAnchor.MiddleLeft); AddText(container, panelName, "0.1 0.26", "0.9 0.3", "Sulfur Ore: 200", 10, TextAnchor.MiddleLeft); AddButton(container, panelName, "Upgrade", CommandPrefix + ".upgrade", "0.35", "0.11", "0.65", "0.16", colorOptions["LightGray"], colorOptions["Green"]); } private void AddSeparator(CuiElementContainer container, string parent, string anchorMin, string anchorMax) { AddInnerSquare(container, parent, anchorMin, anchorMax, "0.8 0.8 0.8 1"); } private void AddPanel(CuiElementContainer container, string anchorMin, string anchorMax, string panelName, string parent = "Overlay", string color = "0 0 0 1", bool cursorEnabled = false, bool isBackground = false) { var panel = new CuiPanel { Image = { Color = color }, RectTransform = { AnchorMin = anchorMin, AnchorMax = anchorMax }, CursorEnabled = cursorEnabled, }; if (isBackground) { panel.Image.Material = "assets/content/ui/uibackgroundblur.mat"; panel.Image.Sprite = "assets/content/ui/ui.background.transparent.radial.psd"; } container.Add(panel, parent, panelName); } private void AddInnerSquare(CuiElementContainer container, string parent, string anchorMin, string anchorMax, string color) { container.Add(new CuiPanel { Image = { Color = color }, RectTransform = { AnchorMin = anchorMin, AnchorMax = anchorMax } }, parent); } private void AddText(CuiElementContainer container, string parent, string anchorMin, string anchorMax, string text, int fontSize, TextAnchor align) { container.Add( new CuiLabel { Text = { Text = text, FontSize = fontSize, Font = "DroidSansMono.ttf", Align = align, Color = colorOptions["White"], }, RectTransform = { AnchorMin = anchorMin, AnchorMax = anchorMax }, }, parent ); } private void AddButton(CuiElementContainer container, string parent, string text, string command, string anchorMinX, string anchorMinY, string anchorMaxX, string anchorMaxY, string backgroundColor, string textColor) { container.Add( new CuiButton { Button = { Color = backgroundColor, Command = command, Close = "", }, RectTransform = { AnchorMin = $"{anchorMinX} {anchorMinY}", AnchorMax = $"{anchorMaxX} {anchorMaxY}" }, Text = { Text = text, FontSize = 14, Align = TextAnchor.MiddleCenter, Color = textColor, }, }, parent ); } private void AddImage(CuiElementContainer container, string parent, string resourceName, string anchorMin, string anchorMax, string imageName) { string imageUrl = (string)ImageLibrary?.Call("GetImage", resourceName); if (imageUrl == null) { PrintError($"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 = CommandPrefix + ".close", Close = "QuarryUi_Background", }, RectTransform = { AnchorMin = "0.95 0.95", AnchorMax = "1 1" }, Text = { Text = "✖", FontSize = 13, Align = TextAnchor.MiddleCenter, Color = "1 1 1 0.5", }, }, parent, "QuarryUi_CloseButton" ); } } - Assistant
- User
ao invés de definier variáveis contantes nos métodos e etc, coloque todas configes em outra classe de config
- Assistant
- User
quero que todas private void AddUpgradePanel(CuiElementContainer container, int level, string parent) { const string PanelName = "QuarryUi_Full_Right";
fiquem em uma classe de config
- Assistant
- User
ficou muito grande pra chamar assim Config.QuarryPanelSettings.PanelName, divida as configs cada umnuma classe separada e não nested, e tente deixar osnomes mais curtos, além disso tente colocar tudo que achar viável de valores em classes de configs
- Assistant
- User
'QuarryComputerUi._playersWithPopup' is inaccessible due to its protection levelCS0122 The name 'PrintError' does not exist in the current contextCS0103
além desses erros, padronize mais os nomes, como config e settings que são a mesma coisa
- Assistant
- User
An object reference is required for the non-static field, method, or property 'Plugin.Config'CS0120 [ConsoleCommand(Config.CommandPrefix + ".close")] private void CloseUiCommand(ConsoleSystem.Arg args) {
- Assistant
- User
'QuarryComputerUi.playersWithPopup' is inaccessible due to its protection levelCS0122
mande só o que preciso fazer
- Assistant
To resolve the accessibility issue with
playersWithPopup, you need to ensure that it is accessible from static methods like those inUiHelper. You can do this by either making itpublic staticor by providing a static method in theQuarryComputerUiclass that allows controlled access to theplayersWithPopupset. Here's the step you need to take:- Make
playersWithPopupPublic and Static:
public static HashSet<BasePlayer> PlayersWithPopup = new HashSet<BasePlayer>();This change will make
PlayersWithPopupaccessible throughout theUiHelperclass and other static contexts. Update your references to useQuarryComputerUi.PlayersWithPopupappropriately. - Make