C#:
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Core.Plugins;
using Rust;
namespace Oxide.Plugins
{
[Info("RandomTips" , "Krungh Crow" , "2.0.0")]
[Description("Send random gametips or queuedpopups.")]
class RandomTips : RustPlugin
{
private const string NoTip_Perm = "randomtips.exclude";
[PluginReference] private Plugin QueuedPopups;
private ConfigData configData;
private readonly System.Random rng = new System.Random();
#region Configuration
private class QueuedPopupsSettings
{
[JsonProperty(PropertyName = "Duration (seconds)")]
public float Duration = 10f;
// Uses PopupType.Icon (typeInt=1)
[JsonProperty(PropertyName = "Icon Sprite")]
public string IconSprite = "assets/icons/info.png";
[JsonProperty(PropertyName = "Background Color (RGBA)")]
public string BackgroundColor = "0 0 0.65 0.6";
[JsonProperty(PropertyName = "Text Color (RGBA)")]
public string TextColor = "1 1 1 1";
[JsonProperty(PropertyName = "Icon Color (RGBA)")]
public string IconColor = "1 1 1 1";
[JsonProperty(PropertyName = "Height (Pixels)")]
public float? Height = 38;
[JsonProperty(PropertyName = "Font Size")]
public int? FontSize = 14;
}
private class ConfigData
{
[JsonProperty(PropertyName = "Prefix")]
public string Prefix = "[YourServer]";
[JsonProperty(PropertyName = "Interval (seconds)")]
public int Interval = 600;
[JsonProperty(PropertyName = "Enable Debug")]
public bool Debug = false;
[JsonProperty(PropertyName = "QueuedPopups")]
public QueuedPopupsSettings QP = new QueuedPopupsSettings();
[JsonProperty(PropertyName = "Messages")]
public List<string> Messages = new List<string>();
}
#endregion
#region Debug
private void DebugLog(string msg)
{
if (configData?.Debug == true)
Puts($"[Debug] {msg}");
}
#endregion
#region Init / Config
private void Init()
{
if (!LoadConfigVariables())
{
Puts("Config error detected. Fix or delete the config file.");
return;
}
permission.RegisterPermission(NoTip_Perm , this);
}
private bool LoadConfigVariables()
{
try
{
configData = Config.ReadObject<ConfigData>();
}
catch
{
return false;
}
if (configData.QP == null)
configData.QP = new QueuedPopupsSettings();
if (configData.QP.Duration <= 0f)
configData.QP.Duration = 6f;
SaveConfig();
return true;
}
protected override void LoadDefaultConfig()
{
configData = new ConfigData
{
Messages = new List<string>
{
"Join our Discord, click <color=orange>[view webpage]</color> button.",
"We test several cool new plugins!",
"Some spawns like animals and their stats can be set high.",
"<color=red>Bradley Tiers</color> is tested here.",
"Hitting barrels/nodes/trees or item pickup can trigger events, like npc animal bomb spawning."
}
};
SaveConfig();
}
private void SaveConfig() => Config.WriteObject(configData , true);
#endregion
#region Hooks
private void OnServerInitialized()
{
DebugLog($"Initialized. Interval={configData.Interval}s Messages={configData.Messages.Count}");
if (configData.Messages == null || configData.Messages.Count == 0)
{
Puts("Message list empty. Unloading plugin.");
timer.Once(3f , () => Interface.Oxide.UnloadPlugin(nameof(RandomTips)));
return;
}
timer.Every(configData.Interval , SendRandomMessage);
}
#endregion
#region Random Sender
private void SendRandomMessage()
{
DebugLog($"Tick. Sending to {BasePlayer.activePlayerList.Count} active players.");
var msg = configData.Messages[rng.Next(configData.Messages.Count)];
var full = $"{configData.Prefix}{msg}";
foreach (var player in BasePlayer.activePlayerList)
{
if (player == null) continue;
if (permission.UserHasPermission(player.UserIDString , NoTip_Perm)) continue;
TIP(player , full , 0f , false);
}
}
#endregion
#region API
private void TIP(BasePlayer player , string message , float dur , bool warning)
{
if (player == null || string.IsNullOrEmpty(message)) return;
float finalDuration = dur > 0f ? dur : configData.QP.Duration;
DebugLog($"TIP -> {player.displayName} ({player.userID}) dur_in={dur} dur_out={finalDuration} warn={warning} : {message}");
if (QueuedPopups != null && QueuedPopups.IsLoaded)
{
QueuedPopups.Call(
"AddPopup" ,
player ,
message ,
1 , // PopupType.Icon
configData.QP.IconSprite ,
configData.QP.BackgroundColor ,
configData.QP.TextColor ,
configData.QP.IconColor ,
configData.QP.Height ,
configData.QP.FontSize ,
finalDuration
);
return;
}
// Fallback: toast (duration not controllable)
player.ShowToast(
GameTip.Styles.Blue_Normal ,
new Translate.Phrase("randomtips.tip" , message) ,
false
);
}
#endregion
}
}