/**
* OxidationClock - In-game clock
* Copyright (C) 2021 kasvoton [kasvoton@projectoxidation.com]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
*/
using System;
using System.Linq;
using Oxide.Game.Rust.Cui;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("OxidationClock", "kasvoton", "1.3.1")]
[Description("Project Oxidation :: In-game clock")]
public class OxidationClock : RustPlugin
{
//
// --- UMOD EVENTS -----------------------------------------------------
//
private void OnServerInitialized()
{
foreach (BasePlayer Player in BasePlayer.activePlayerList)
OnPlayerConnected(Player);
}
private void Unload()
{
foreach (BasePlayer Player in BasePlayer.activePlayerList)
OnPlayerDisconnected(Player, string.Empty);
}
private void OnPlayerConnected(BasePlayer Player)
{
if (Player.HasPlayerFlag(BasePlayer.PlayerFlags.ReceivingSnapshot))
{
float wait = UnityEngine.Random.Range(0.5f, 1.0f);
timer.Once(wait, () => OnPlayerConnected(Player));
return;
}
Player.gameObject.AddComponent();
}
private void OnPlayerDisconnected(BasePlayer Player, string Reason)
=> UnityEngine.Object.Destroy(Player.GetComponent());
//
// --- BEHAVIOURS ------------------------------------------------------
//
public class OxidationClockBehaviour : MonoBehaviour
{
protected BasePlayer Player;
protected string LastTextUsed;
protected float LastClientUpdate;
protected CuiElementContainer Container;
internal void Awake()
=> Player = GetComponent();
internal void OnEnable()
{
Container = new CuiElementContainer();
Container.Add(new CuiElement
{
Name = $"UIClock.text",
Parent = "Hud",
Components =
{
new CuiTextComponent
{
Color = "1.0 1.0 1.0 0.2",
Font = "RobotoCondensed-Bold.ttf",
Align = TextAnchor.MiddleCenter,
FontSize = 12
},
new CuiRectTransformComponent
{
AnchorMin = "0.960 0.970",
AnchorMax = "1.000 1.000"
}
}
});
}
internal void FixedUpdate()
{
if (Time.realtimeSinceStartup - LastClientUpdate < 3f) return;
LastClientUpdate = Time.realtimeSinceStartup;
string dt = TOD_Sky.Instance.Cycle.DateTime.ToString("HH:mm");
if (LastTextUsed == dt) return;
LastTextUsed = dt;
foreach (CuiElement Element in Container.Reverse())
CuiHelper.DestroyUi(Player, Element.Name);
(Container[0].Components[0] as CuiTextComponent).Text = dt;
CuiHelper.AddUi(Player, Container);
}
internal void OnDisable()
{
foreach (CuiElement Element in Container.Reverse())
CuiHelper.DestroyUi(Player, Element.Name);
}
}
}
}