C#:
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Linq;
using Oxide.Core.Libraries.Covalence;
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
namespace Oxide.Plugins
{
[Info("Photographs Owner", "Khan", "1.0.1")]
[Description("Easily Identify Photographs Owner and SteamId + Discord Logging.")]
public class PhotographsOwner : CovalencePlugin
{
#region Fields
private const string Use = "photographsowner.use";
private const string Admin = "photographsowner.admin";
private const int PhotoID = 62577426;
#endregion
#region Config
private PluginConfig _config;
private class PluginConfig
{
[JsonProperty("Display Name")] public bool DisplayName = true;
[JsonProperty("Display SteamID")] public bool SteamID = false;
[JsonProperty("Set Who's Photo Command")] public string Command = "photo";
[JsonProperty("Set Admin Command")] public string ACommand = "setphoto";
[JsonProperty("Set Discord Webhook Logging")] public string WebHook = "";
[JsonIgnore]
public int Options => DisplayName && SteamID ? 0 : DisplayName ? 1 : SteamID ? 2 : 3;
private string ToJson() => JsonConvert.SerializeObject(this);
public Dictionary<string, object> ToDictionary() => JsonConvert.DeserializeObject<Dictionary<string, object>>(ToJson());
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_config = Config.ReadObject<PluginConfig>();
if (_config == null)
throw new JsonException();
if (!_config.ToDictionary().Keys.SequenceEqual(Config.ToDictionary(x => x.Key, x => x.Value).Keys))
{
PrintWarning($"Configuration file {Name}.json was Updated");
SaveConfig();
}
}
catch
{
PrintError("Configuration file is corrupt! Loading Default Config");
LoadDefaultConfig();
}
}
protected override void SaveConfig() => Config.WriteObject(_config, true);
protected override void LoadDefaultConfig() => _config = new PluginConfig();
#endregion
#region Oxide
private void Init()
{
permission.RegisterPermission(Use, this);
permission.RegisterPermission(Admin, this);
AddCovalenceCommand(_config.Command, nameof(Photographer));
AddCovalenceCommand(_config.ACommand, nameof(Set));
}
private void OnServerInitialized()
{
if (string.IsNullOrEmpty(_config.WebHook))
Unsubscribe(nameof(OnPhotoCaptured));
}
private void OnPhotoCaptured(PhotoEntity photoEntity, Item item, BasePlayer player, Byte[] array) => SendDiscordEmbed(player, array);
#endregion
#region Discord
private void SendDiscordEmbed(BasePlayer player, byte[] image)
{
string title = Lang("Title", player.UserIDString, player.displayName);
string description = Lang("Description", player.UserIDString, MapHelper.GridToString(MapHelper.PositionToGrid(player.ServerPosition)));
var payload = new
{
embeds = new[]
{
new {
title, description,
color = 2895667,
image = new { url = "attachment://image.jpeg" },
timestamp = DateTime.Now
}
}
};
WWWForm form = new WWWForm();
form.AddBinaryData( "file", image, "image.jpg" );
form.AddField("payload_json", JsonConvert.SerializeObject(payload));
ServerMgr.Instance.StartCoroutine(Upload(_config.WebHook, form));
}
private IEnumerator Upload(string url, WWWForm data)
{
UnityWebRequest www = UnityWebRequest.Post(url, data);
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
PrintError($"Failed to send Discord message: {www.error}");
yield return null;
}
#endregion
#region Command
private bool HasPermission(IPlayer player) => player.IsAdmin || player.HasPermission(Use);
private void Photographer(IPlayer player, string command, string[] args)
{
if (!HasPermission(player))
{
player.Reply(Lang("NoPermPhoto", player.Id, _config.Command));
return;
}
List<Item> photos = FindPhotos(player.Object as BasePlayer);
if (photos == null || photos.Count == 0)
{
player.Reply(Lang("NoneFound", player.Id));
return;
}
TextTable textTable = CreateTable(player);
foreach (Item photo in photos)
{
PhotoEntity photograph = BaseNetworkable.serverEntities.Find(photo.instanceData.subEntity) as PhotoEntity;
if (photograph == null)
continue;
string[] data = new string[2];
switch (_config.Options)
{
case 0:
data[0] = $"{players.FindPlayerById(photograph.PhotographerSteamId.ToString())?.Name ?? "Server Owned"}";
data[1] = $"{photograph.PhotographerSteamId}";
textTable.AddRow(data);
continue;
case 1:
data[0] = $"{players.FindPlayerById(photograph.PhotographerSteamId.ToString())?.Name ?? "Server Owned"}";
textTable.AddRow(data);
continue;
case 2:
data[0] = $"{photograph.PhotographerSteamId}";
textTable.AddRow(data);
continue;
default:
continue;
}
}
player.Reply(Lang("Photographers", player.Id, textTable));
}
private void Set(IPlayer player, string command, string[] args)
{
if (!permission.UserHasPermission(player.Id, Admin))
{
player.Reply(Lang("NoPermPhoto", player.Id, _config.ACommand));
return;
}
if (args.Length < 1)
{
player.Reply(Lang("Syntax", player.Id, _config.ACommand));
return;
}
switch (args[0].ToLower())
{
case "name":
_config.DisplayName = bool.Parse(args[1]);
player.Reply(Lang("CmdResponse", player.Id, "name", _config.DisplayName));
SaveConfig();
break;
case "id":
_config.SteamID = bool.Parse(args[1]);
player.Reply(Lang("CmdResponse", player.Id, "id", _config.SteamID));
SaveConfig();
break;
default:
player.Reply(Lang("Syntax", player.Id, _config.ACommand));
break;
}
}
#endregion
#region Helpers
private List<Item> FindPhotos(BasePlayer player) => player == null ? null : Get(player.inventory);//player.inventory.FindItemsByItemID(PhotoID);
private static List<Item> Get(PlayerInventory inv)
{
List<Item> photos = new List<Item>();
void ProcessContainer(ItemContainer container)
{
if (container == null) return;
foreach (Item item in container.itemList)
{
if (item.info.itemid != PhotoID)
continue;
photos.Add(item);
}
}
ProcessContainer(inv.containerMain);
ProcessContainer(inv.containerBelt);
Item bp = inv.GetAnyBackpack();
if (bp != null)
ProcessContainer(bp.contents);
return photos;
}
private TextTable CreateTable(IPlayer player)
{
TextTable textTable = new TextTable();
if (_config.DisplayName)
textTable.AddColumn(Lang("DisplayName", player.Id));
if (_config.SteamID)
textTable.AddColumn(Lang("SteamID", player.Id));
return textTable;
}
#endregion
#region Lang
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(new Dictionary<string, string>
{
{"NoPermPhoto", "Unkown Command: {0}"},
{"NoneFound", "No Photos Found"},
{"Syntax", "Invalid syntax. Please use {0} option true or false."},
{"CmdResponse", "Successfully set {0}, to {1}"},
{"Photographers", "Photographers:\n{0}"},
{"DisplayName", "Display Name"},
{"SteamID", "Steam ID"},
{"Title", "Photographer: {0}"},
{"Description", "Photographed In Zone: {0}"},
}, this);
}
[JsonIgnore]
public string c = "ZwN3AN== 1789200393 Khan";
private string Lang(string key, string id = null, params object[] args) => string.Format(lang.GetMessage(key, this, id), args);
#endregion
}
}