using System.Collections.Generic;
using Oxide.Core;
using Oxide.Game.Rust.Cui;
using System.Linq;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("FurnaceGUI", "YourName", "1.0.0")]
class FurnaceGUI : RustPlugin
{
private Dictionary<ulong, CuiElementContainer> playerGUIs = new Dictionary<ulong, CuiElementContainer>();
private void OnServerInitialized()
{
foreach (BaseOven oven in BaseEntity.saveList.OfType<BaseOven>())
{
ulong ownerID = oven.OwnerID;
if (!playerGUIs.ContainsKey(ownerID))
{
CuiElementContainer elements = CreateGUIElement(ownerID);
playerGUIs.Add(ownerID, elements);
BasePlayer owner = BasePlayer.FindByID(ownerID);
if (owner != null)
{
CuiHelper.AddUi(owner, elements);
}
}
}
}
private CuiElementContainer CreateGUIElement(ulong ownerID)
{
CuiElementContainer elements = new CuiElementContainer();
string panelName = $"FurnacePanel_{ownerID}";
var guiPanel = elements.Add(new CuiPanel
{
CursorEnabled = true,
Image = { Color = "0 0 0 0" },
RectTransform = { AnchorMin = "0.1 0.1", AnchorMax = "0.3 0.3" }
}, "Hud", panelName);
var guiText = elements.Add(new CuiLabel
{
Text = { Text = "Furnace GUI Text", FontSize = 16, Align = TextAnchor.MiddleCenter },
RectTransform = { AnchorMin = "0 0", AnchorMax = "1 1" }
}, panelName);
return elements;
}
private void OnEntityEnter(BaseNetworkable entity, BasePlayer player)
{
if (entity is BaseOven && player != null)
{
BaseOven oven = entity as BaseOven;
ulong ownerID = oven.OwnerID;
if (playerGUIs.ContainsKey(ownerID))
{
CuiHelper.AddUi(player, playerGUIs[ownerID]);
}
}
}
private void OnEntityLeave(BaseNetworkable entity, BasePlayer player)
{
if (entity is BaseOven && player != null)
{
BaseOven oven = entity as BaseOven;
ulong ownerID = oven.OwnerID;
if (playerGUIs.ContainsKey(ownerID))
{
CuiHelper.DestroyUi(player, $"FurnacePanel_{ownerID}");
}
}
}
private void Unload()
{
foreach (var playerGUI in playerGUIs)
{
if (playerGUI.Value != null)
{
foreach (var element in playerGUI.Value)
{
CuiHelper.DestroyUi(BasePlayer.FindByID(playerGUI.Key), element.Name);
}
}
}
playerGUIs.Clear();
}
}
}