Skip to content

Commit

Permalink
New staff hiding feature and code cleanup!
Browse files Browse the repository at this point in the history
  • Loading branch information
ItzNxthaniel committed Sep 14, 2023
1 parent 051dd3e commit 5399dfb
Showing 1 changed file with 171 additions and 121 deletions.
292 changes: 171 additions & 121 deletions TrueWelcomer.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,60 @@
using Newtonsoft.Json;
using Oxide.Core;
using UnityEngine;
using Newtonsoft.Json;
using Oxide.Core.Configuration;
using System.Collections.Generic;
using UnityEngine;

namespace Oxide.Plugins
{
namespace Oxide.Plugins {

[Info("True Welcomer", "ItzNxthaniel", "1.0.2")]
[Info("True Welcomer", "ItzNxthaniel", "1.1.0")]
[Description("This plugin makes it easy to welcome new users and welcome back users rejoining! Also supports Disconnect Messages.")]
public class TrueWelcomer : RustPlugin
{
#region // Vars/Fields \\
public class TrueWelcomer : RustPlugin {

#region Vars/Fields

private static List<string> OnlinePlayers = new List<string>();
#endregion

#region // Config \\
private Configuration _config;
#endregion

protected override void LoadConfig()
{
#region Config

private Configuration _config;

protected override void LoadConfig() {
base.LoadConfig();
_config = Config.ReadObject<Configuration>();
SaveConfig();
}

protected override void SaveConfig() => Config.WriteObject(_config);

protected override void LoadDefaultConfig()
{
_config = new Configuration
{
showJoinMsgs = true,
showWelcomeMessages = true,
showLeaveMsgs = true,
clearOnWipe = true,
steamIconID = 0
protected override void SaveConfig() =>
Config.WriteObject(_config);

protected override void LoadDefaultConfig() {
_config = new Configuration {
debugMode = false,
showJoinMsgs = true,
showWelcomeMessages = true, showLeaveMsgs = true,
clearOnWipe = true,
steamIconID = 0,
hidePlayersWithAuthlevel = false,
authLevelToHide = 0,
hidePlayersWithPermission = false
};

SaveConfig();
}

private class Configuration
{

private enum AuthLevelEnum {
Both = 0,
AuthLevel1 = 1,
AuthLevel2 = 2
}

private class Configuration {
[JsonProperty("Debug Mode")]
internal bool debugMode;

[JsonProperty("Show Join Messages")]
internal bool showJoinMsgs;

Expand All @@ -57,155 +69,193 @@ private class Configuration

[JsonProperty("Steam User Icon ID")]
internal ulong steamIconID;

[JsonProperty("Hide Players with AuthLevel")]
internal bool hidePlayersWithAuthlevel;

[JsonProperty("AuthLevel to Hide. 0 - Both, 1 - AuthLevel1, 2 - AuthLevel2 ")]
internal AuthLevelEnum authLevelToHide;

[JsonProperty("Hide Players With Permission")]
internal bool hidePlayersWithPermission;
}

#endregion

#region // Language \\
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(new Dictionary<string, string>
{
["OnWelcome"] = "Welcome, <color=#ff7675>{0}</color>, to the server!",
["OnJoin"] = "Welcome back, <color=#ff7675>{0}</color>, to the server!",
["OnLeave"] = "Goodbye, <color=#ff7675>{0}</color>!"
}, this);
#region Language

protected override void LoadDefaultMessages() {
lang.RegisterMessages(new Dictionary<string, string> {
["OnWelcome"] = "Welcome, <color=#ff7675>{0}</color>, to the server!",
["OnJoin"] = "Welcome back, <color=#ff7675>{0}</color>, to the server!",
["OnLeave"] = "Goodbye, <color=#ff7675>{0}</color>!",
["NoPermission"] = "You do not have permission to run this command!",
["NowHiding"] = "Users will no longer be alerted when you join or leave the server.",
["NowShowing"] = "Users will now be alerted when you join or leave the server.",
["ServerConfigAlert"] = "Due to the Server's Config, your preference will be ignored."
},
this);
}

private void Broadcast(BasePlayer player, string messageKey, params object[] args)
{
private void Broadcast(BasePlayer player, string messageKey, params object[] args) {
if (player == null) return;

var message = GetMessage(messageKey, player.UserIDString, args);
if (_config.debugMode) Puts(message);
Server.Broadcast(message, null, _config.steamIconID);
}

private string GetMessage(string messageKey, string playerID, params object[] args)
{
return string.Format(lang.GetMessage(messageKey, this, playerID), args);
}
#endregion
private string GetMessage(string key, string id = null, params object[] args) =>
string.Format(lang.GetMessage(key, this, id), args);

#endregion

#region // Data \\
DynamicConfigFile dataFile = Interface.Oxide.DataFileSystem.GetDatafile("TrueWelcomer");
#endregion
#region Data
private DynamicConfigFile cachedPlayers = Interface.Oxide.DataFileSystem.GetDatafile("TrueWelcomer");
#endregion

#region Hooks

#region // Hooks \\
private void Init()
{
private void Init() {
permission.RegisterPermission("truewelcomer.admin", this);
permission.RegisterPermission("truewelcomer.canSetPreference", this);
permission.RegisterPermission("truewelcomer.hideUser", this);

foreach(BasePlayer player in BasePlayer.activePlayerList)
{
foreach (BasePlayer player in BasePlayer.activePlayerList) {
OnlinePlayers.Add(player.UserIDString);
}
}

private void OnNewSave() {
if (!_config.clearOnWipe) return;
dataFile.Clear();
dataFile.Save();
if (!_config.clearOnWipe) return;

cachedPlayers.Clear();
cachedPlayers.Save();
}

private void OnPlayerDisconnected(BasePlayer player)
{
private bool ShouldAnnounce(BasePlayer player) {
if (_config.hidePlayersWithPermission && permission.UserHasPermission(player.UserIDString, "truewelcomer.hideUser")) return false;
if (!_config.hidePlayersWithAuthlevel) return true;
ServerUsers.User user = ServerUsers.Get(player.userID);

switch(_config.authLevelToHide) {
case AuthLevelEnum.Both when user.group is ServerUsers.UserGroup.Moderator or ServerUsers.UserGroup.Owner:
case AuthLevelEnum.AuthLevel1 when user.group is ServerUsers.UserGroup.Moderator:
case AuthLevelEnum.AuthLevel2 when user.group is ServerUsers.UserGroup.Owner: return false;
default: return true;
}
}

private void OnPlayerDisconnected(BasePlayer player) {
string uid = player.UserIDString;
OnlinePlayers.Remove(uid);

Broadcast(player, "OnLeave", player.displayName);
switch(ShouldAnnounce(player)) {
case true:
Broadcast(player, "OnLeave", player.displayName);
break;
default: return;
}
}

private void OnPlayerSleepEnded(BasePlayer player)
{
private void OnPlayerSleepEnded(BasePlayer player) {
bool isOnline = false;
foreach (BasePlayer p in BasePlayer.activePlayerList) {
if (p.UserIDString == player.UserIDString) isOnline = true;
}

if (!OnlinePlayers.Contains(player.UserIDString) && isOnline)
{
OnlinePlayers.Add(player.UserIDString);
if (dataFile[player.UserIDString] != null)
{
Broadcast(player, "OnJoin", player.displayName);
}
else
{
Broadcast(player, "OnWelcome", player.displayName);
dataFile[player.UserIDString] = player.displayName;
dataFile.Save();
}
}
string keyToUse = "OnJoin";

if (!OnlinePlayers.Contains(player.UserIDString) && isOnline) {
OnlinePlayers.Add(player.UserIDString);
if (cachedPlayers[player.UserIDString] != null) keyToUse = "OnJoin";
else {
keyToUse = "OnWelcome";
cachedPlayers[player.UserIDString] = player.displayName;
cachedPlayers.Save();
}
}

switch(ShouldAnnounce(player)) {
case true:
Broadcast(player, keyToUse, player.displayName);
break;
default: return;
}
}

[ConsoleCommand("tw.data_refresh")]
private void RefreshDataCommand(ConsoleSystem.Arg arg)
{
[Command("hidewelcome")] private void HideWelcomeCommand(BasePlayer player) {
if (!permission.UserHasPermission(player.UserIDString, "truewelcomer.canSetPreference")) {
player.ChatMessage(GetMessage("NoPermission", player.UserIDString));
return;
}

string finalMsg;

if (permission.UserHasPermission(player.UserIDString, "truewelcomer.hideUser")) {
permission.RevokeUserPermission(player.UserIDString, "truewelcomer.hideUser");
finalMsg = GetMessage("NowShowing", player.UserIDString);
} else {
permission.GrantUserPermission(player.UserIDString, "truewelcomer.hideUser", this);
finalMsg = GetMessage("NowHiding", player.UserIDString);
}


finalMsg += _config.hidePlayersWithPermission ? "" : $"\n{GetMessage("ServerConfigAlert", player.UserIDString)}";
player.ChatMessage(finalMsg);
}

[ConsoleCommand("tw.data_refresh")]
private void RefreshDataCommand(ConsoleSystem.Arg arg) {
BasePlayer player2 = arg.Player();
if (player2 != null && !permission.UserHasPermission(player2.UserIDString, "truewelcomer.admin"))
{
if (player2 != null && !permission.UserHasPermission(player2.UserIDString, "truewelcomer.admin")) {
arg.ReplyWith("You are lacking permission, to run this command.");
return;
}

string msg = "";
int num = 0;
foreach(BasePlayer player in BasePlayer.activePlayerList)
{
if (dataFile[player.UserIDString] != null)
{
num += 0;
}
else
{
dataFile[player.UserIDString] = player.displayName;
dataFile.Save();
num += 1;
msg += $"Player {player.displayName} has been added.\n";
}

foreach (BasePlayer player in BasePlayer.activePlayerList) {
if (cachedPlayers[player.UserIDString] != null) num += 0;
else {
cachedPlayers[player.UserIDString] = player.displayName;
cachedPlayers.Save();
num += 1;
msg += $"Player {player.displayName} has been added.\n";
}
}

foreach (BasePlayer player in BasePlayer.sleepingPlayerList)
{
if (dataFile[player.UserIDString] != null)
{
num += 0;
}
else
{
dataFile[player.UserIDString] = player.displayName;
dataFile.Save();
num += 1;
msg += $"Player {player.displayName} has been added.\n";
}

foreach (BasePlayer player in BasePlayer.sleepingPlayerList) {
if (cachedPlayers[player.UserIDString] != null) num += 0;
else {
cachedPlayers[player.UserIDString] = player.displayName;
cachedPlayers.Save();
num += 1;
msg += $"Player {player.displayName} has been added.\n";
}
}

if (num == 0)
{
arg.ReplyWith("No players have been added.");
return;
}
else
{
if (num == 0) arg.ReplyWith("No players have been added.");
else {
string message = $"{num} players have been added to the Data List.\n{msg}";
arg.ReplyWith(message);
}
}

[ConsoleCommand("tw.data_reset")]
private void ResetDataCommand(ConsoleSystem.Arg arg)
{
[ConsoleCommand("tw.data_reset")]
private void ResetDataCommand(ConsoleSystem.Arg arg) {
BasePlayer player2 = arg.Player();
if (player2 != null && !permission.UserHasPermission(player2.UserIDString, "truewelcomer.admin"))
{
if (player2 != null && !permission.UserHasPermission(player2.UserIDString, "truewelcomer.admin")) {
arg.ReplyWith("You are lacking permission, to run this command.");
return;
}

dataFile.Clear();
dataFile.Save();
cachedPlayers.Clear();
cachedPlayers.Save();
arg.ReplyWith("The Data File for True Welcomer has been reset!");
}

#endregion

}
}

0 comments on commit 5399dfb

Please sign in to comment.