C#:
// Все права принадлежат дискорд сообществу https://discord.gg/VgNHPpNrz6
using System;
using Oxide.Core;
using System.Collections.Generic;
using Oxide.Core.Plugins;
using Oxide.Game.Rust.Cui;
using Oxide.Core.Configuration;
using Oxide.Core.Libraries.Covalence;
using UnityEngine;
using System.Collections;
using System.Reflection;
using System.IO;
using System.Linq;
namespace Oxide.Plugins {
[Info("ServerRewards", "k1lly0u", "0.3.6", ResourceId = 1751)]
public class ServerRewards : RustPlugin {
#region Fields
[PluginReference]
Plugin Kits;
[PluginReference]
Plugin Economics;
[PluginReference]
Plugin HumanNPC;
[PluginReference]
Plugin LustyMap;
[PluginReference]
Plugin PlaytimeTracker;
#region Datafiles
PointData playerData;
private DynamicConfigFile PlayerData;
RewardDataStorage rewardData;
private DynamicConfigFile RewardData;
SaleDataStorage saleData;
private DynamicConfigFile SaleData;
NPCDealers npcDealers;
private DynamicConfigFile NPC_Dealers;
#endregion
static GameObject webObject;
static UnityWeb uWeb;
static MethodInfo getFileData = typeof(FileStorage).GetMethod("StorageGet", (BindingFlags.Instance | BindingFlags.NonPublic));
static ServerRewards instance;
ConfigData configData;
private Timer saveTimer;
private Dictionary<ulong, OUIData> OpenUI;
private Dictionary<string, string> ItemNames;
private Dictionary<ulong, int> PointCache;
private Dictionary<ulong, NPCInfos> NPCCreator;
#endregion
#region Classes
#region Player data
class PointData {
public Dictionary<ulong, int> Players = new Dictionary<ulong, int>();
}
#endregion
#region Reward data
class RewardDataStorage {
public Dictionary<string, KitInfo> RewardKits = new Dictionary<string, KitInfo>();
public Dictionary<int, ItemInfo> RewardItems = new Dictionary<int, ItemInfo>();
public Dictionary<string, CommandInfo> RewardCommands = new Dictionary<string, CommandInfo>();
public Dictionary<string, Dictionary<ulong, uint>> storedImages = new Dictionary<string, Dictionary<ulong, uint>>();
}
class SaleDataStorage {
public Dictionary<int, Dictionary<ulong, SaleInfo>> Prices = new Dictionary<int, Dictionary<ulong, SaleInfo>>();
}
class SaleInfo {
public float SalePrice;
public string Name;
public bool Enabled;
}
class KitInfo {
public string KitName;
public string Description = "";
public string URL = "";
public int Cost;
}
class ItemInfo {
public string DisplayName;
public string URL;
public int ID;
public int Amount;
public ulong Skin;
public int Cost;
public int TargetID;
}
class CommandInfo {
public List<string> Command;
public string Description;
public int Cost;
}
#endregion
#region Other data
class NPCDealers {
public Dictionary<string, NPCInfos> NPCIDs = new Dictionary<string, NPCInfos>();
}
class NPCInfos {
public float X;
public float Z;
public float ID;
public string NPCID;
public bool isCustom;
public List<int> itemList = new List<int>();
public List<string> kitList = new List<string>();
public List<string> commandList = new List<string>();
public bool allowExchange;
public bool allowTransfer;
public bool allowSales;
}
class KitItemEntry {
public int ItemAmount;
public List<string> ItemMods;
}
public class OUIData {
public ElementType type;
public int page;
public string npcid;
}
#endregion
#region UI
static string UIMain = "SR_Store";
static string UISelect = "SR_Select";
static string UIRP = "SR_RPPanel";
static string UIPopup = "SR_Popup";
class SR_UI {
static public CuiElementContainer CreateElementContainer(string panelName, string color, string aMin, string aMax) {
var NewElement = new CuiElementContainer() {
{
new CuiPanel {
Image = {
Color = color
}
, RectTransform = {
AnchorMin = aMin, AnchorMax = aMax
}
, CursorEnabled = true
}
, new CuiElement().Parent = "Overlay", panelName
}
}
;
return NewElement;
}
static public void CreatePanel(ref CuiElementContainer container, string panel, string color, string aMin, string aMax, bool cursor = false) {
container.Add(new CuiPanel {
Image = {
Color = color
}
, RectTransform = {
AnchorMin = aMin, AnchorMax = aMax
}
, CursorEnabled = cursor
}
, panel, CuiHelper.GetGuid());
}
static public void CreateLabel(ref CuiElementContainer container, string panel, string color, string text, int size, string aMin, string aMax, TextAnchor align = TextAnchor.MiddleCenter, float fadein = 1.0f) {
if (instance.configData.UI_Options.DisableUI_FadeIn) fadein = 0;
container.Add(new CuiLabel {
Text = {
Color = color, FontSize = size, Align = align, FadeIn = fadein, Text = text
}
, RectTransform = {
AnchorMin = aMin, AnchorMax = aMax
}
}
, panel, CuiHelper.GetGuid());
}
static public void CreateButton(ref CuiElementContainer container, string panel, string color, string text, int size, string aMin, string aMax, string command, TextAnchor align = TextAnchor.MiddleCenter, float fadein = 1.0f) {
if (instance.configData.UI_Options.DisableUI_FadeIn) fadein = 0;
container.Add(new CuiButton {
Button = {
Color = color, Command = command, FadeIn = fadein
}
, RectTransform = {
AnchorMin = aMin, AnchorMax = aMax
}
, Text = {
Text = text, FontSize = size, Align = align
}
}
, panel, CuiHelper.GetGuid());
}
static public void LoadImage(ref CuiElementContainer container, string panel, string png, string aMin, string aMax) {
container.Add(new CuiElement {
Name = CuiHelper.GetGuid(), Parent = panel, Components = {
new CuiRawImageComponent {
Png = png
}
, new CuiRectTransformComponent {
AnchorMin = aMin, AnchorMax = aMax
}
}
}
);
}
static public void CreateTextOverlay(ref CuiElementContainer container, string panel, string text, string color, int size, string aMin, string aMax, TextAnchor align = TextAnchor.MiddleCenter, float fadein = 1.0f) {
if (instance.configData.UI_Options.DisableUI_FadeIn) fadein = 0;
container.Add(new CuiLabel {
Text = {
Color = color, FontSize = size, Align = align, FadeIn = fadein, Text = text
}
, RectTransform = {
AnchorMin = aMin, AnchorMax = aMax
}
}
, panel, CuiHelper.GetGuid());
}
}
private Dictionary<string, string> UIColors = new Dictionary<string, string> {
{
"dark", "0.1 0.1 0.1 0.98"
}
, {
"light", "0.9 0.9 0.9 0.1"
}
, {
"grey1", "0.6 0.6 0.6 1.0"
}
, {
"buttonbg", "0.2 0.2 0.2 0.7"
}
, {
"buttoncom", "0 0.5 0.1 0.9"
}
, {
"grey8", "0.8 0.8 0.8 1.0"
}
}
;
#endregion
#endregion
#region Player UI
private void DisplayPoints(BasePlayer player) {
if (player == null) return;
CuiHelper.DestroyUi(player, UIRP);
if (!OpenUI.ContainsKey(player.userID)) return;
int playerPoints = 0;
if (PointCache.ContainsKey(player.userID)) playerPoints = PointCache[player.userID];
var element = SR_UI.CreateElementContainer(UIRP, "0 0 0 0", "0.3 0", "0.7 0.1");
string message = $"{configData.Messaging.MSG_MainColor}{msg("storeRP", player.UserIDString)}: {playerPoints}</color>";
if (Economics && !configData.Categories.Disable_CurrencyExchange) {
var amount = Economics?.Call("GetPlayerMoney", player.userID);
message = message + $" || {configData.Messaging.MSG_MainColor}Economics: {amount}</color>";
}
if (configData.Options.Use_PTT && PlaytimeTracker) {
var time = PlaytimeTracker?.Call("GetPlayTime", player.UserIDString);
if (time is double) {
var playTime = GetPlaytimeClock((double)time);
if (!string.IsNullOrEmpty(playTime)) message = $"{configData.Messaging.MSG_MainColor}{msg("storePlaytime", player.UserIDString)}: {playTime}</color> || " + message;
}
}
SR_UI.CreateLabel(ref element, UIRP, "0 0 0 0", message, 20, "0 0", "1 1", TextAnchor.MiddleCenter, 0f);
CuiHelper.AddUi(player, element);
timer.Once(1, () => DisplayPoints(player));
}
private void PopupMessage(BasePlayer player, string msg) {
CuiHelper.DestroyUi(player, UIPopup);
var element = SR_UI.CreateElementContainer(UIPopup, UIColors["dark"], "0.33 0.45", "0.67 0.6");
SR_UI.CreatePanel(ref element, UIPopup, UIColors["grey1"], "0.01 0.04", "0.99 0.96");
SR_UI.CreateLabel(ref element, UIPopup, "", $"{configData.Messaging.MSG_MainColor}{msg}</color>", 22, "0 0", "1 1");
CuiHelper.AddUi(player, element);
timer.Once(3.5f, () => CuiHelper.DestroyUi(player, UIPopup));
}
private void OpenNavMenu(BasePlayer player, string npcid = null) {
CuiElementContainer element = GetElement(ElementType.Navigation, 0, npcid);
CuiHelper.AddUi(player, element);
DisplayPoints(player);
}
private void SwitchElement(BasePlayer player, ElementType type, int page = 0, string npcid = null) {
if (type == ElementType.Transfer) {
UIElements.DestroyUI(player, OpenUI[player.userID]);
CreateTransferElement(player, page);
OpenUI[player.userID].type = ElementType.Transfer;
OpenUI[player.userID].page = 0;
}
else if (type == ElementType.Sell) {
UIElements.DestroyUI(player, OpenUI[player.userID]);
CreateSaleElement(player);
OpenUI[player.userID].type = ElementType.Sell;
OpenUI[player.userID].page = 0;
}
else if (type == ElementType.Exchange) {
UIElements.DestroyUI(player, OpenUI[player.userID]);
CuiElementContainer element = GetElement(type, page, null);
OpenUI[player.userID].type = ElementType.Exchange;
OpenUI[player.userID].page = 0;
CuiHelper.AddUi(player, element);
return;
}
else {
CuiElementContainer element = GetElement(type, page, npcid);
UIElements.DestroyUI(player, OpenUI[player.userID]);
CuiHelper.DestroyUi(player, UIMain);
OpenUI[player.userID].page = page;
OpenUI[player.userID].type = type;
CuiHelper.AddUi(player, element);
}
}
private CuiElementContainer GetElement(ElementType type, int page, string npcid = null) {
if (!string.IsNullOrEmpty(npcid)) {
if (UIElements.npcElements.ContainsKey(npcid)) return UIElements.npcElements[npcid][type][page];
}
return UIElements.standardElements[type][page];
}
private void DestroyUI(BasePlayer player) {
CuiHelper.DestroyUi(player, UIMain);
CuiHelper.DestroyUi(player, UISelect);
CuiHelper.DestroyUi(player, UIRP);
if (OpenUI.ContainsKey(player.userID)) {
UIElements.DestroyUI(player, OpenUI[player.userID]);
UIElements.DestroyNav(player, OpenUI[player.userID]);
OpenUI.Remove(player.userID);
}
OpenMap(player);
}
public enum ElementType {
None, Navigation, Kits, Items, Commands, Exchange, Transfer, Sell
}
#endregion
#region UI Creation
#region Static UI cache
public static class UIElements {
public static Dictionary<ElementType, CuiElementContainer[]> standardElements;
public static Dictionary<string, Dictionary<ElementType, CuiElementContainer[]>> npcElements;
public static List<string> elementIDs;
public static void RenameComponents(CuiElementContainer[] container) {
foreach(var element in container) {
foreach(var e in element) {
if (e.Name == "AddUI CreatedPanel") e.Name = CuiHelper.GetGuid();
elementIDs.Add(e.Name);
}
}
}
public static void DestroyUI(BasePlayer player, OUIData data) {
if (data.type == ElementType.None) return;
if (data.type == ElementType.Transfer || data.type == ElementType.Sell) {
CuiHelper.DestroyUi(player, UIMain);
return;
}
CuiElementContainer element = null;
if (data.type == ElementType.Exchange) {
if (standardElements[ElementType.Exchange].Length >= data.page) element = standardElements[ElementType.Exchange][data.page];
}
else if (!string.IsNullOrEmpty(data.npcid) && npcElements.ContainsKey(data.npcid)) {
if (npcElements[data.npcid].ContainsKey(data.type)) {
if (npcElements[data.npcid][data.type].Length >= data.page) element = npcElements[data.npcid][data.type][data.page];
}
}
else {
if (standardElements.ContainsKey(data.type)) {
if (standardElements[data.type].Length >= data.page) element = standardElements[data.type][data.page];
}
}
if (element == null) {
DestroyWholeList(player);
return;
}
for (int i = 0; i < element.ToArray().Length;
i++) CuiHelper.DestroyUi(player, element.ToArray()[i].Name);
}
public static void DestroyNav(BasePlayer player, OUIData data) {
CuiElementContainer element = null;
if (!string.IsNullOrEmpty(data.npcid) && npcElements.ContainsKey(data.npcid)) element = npcElements[data.npcid][ElementType.Navigation][0];
else element = standardElements[ElementType.Navigation][0];
for (int i = 0; i < element.ToArray().Length;
i++) CuiHelper.DestroyUi(player, element.ToArray()[i].Name);
}
static void DestroyWholeList(BasePlayer player) {
foreach(var element in elementIDs) CuiHelper.DestroyUi(player, element);
}
}
void InitializeAllElements() {
PrintWarning("Creating and storing all UI elements to cache");
CreateNavUI();
CreateKitsUI();
CreateItemsUI();
CreateCommandsUI();
CreateExchangeUI();
CreateAllNPCs();
}
#region Standard Elements
private void CreateNavUI() {
var Selector = SR_UI.CreateElementContainer(UISelect, UIColors["dark"], "0 0.92", "1 1");
SR_UI.CreatePanel(ref Selector, UISelect, UIColors["light"], "0.01 0.05", "0.99 0.95", true);
SR_UI.CreateLabel(ref Selector, UISelect, "", $"{configData.Messaging.MSG_MainColor}{msg("storeTitle")}</color>", 30, "0.01 0", "0.2 1");
int number = 0;
if (!configData.Categories.Disable_Kits) {
CreateMenuButton(ref Selector, UISelect, msg("storeKits"), $"SRUI_ChangeElement Kits 0", number);
number++;
}
if (!configData.Categories.Disable_Items) {
CreateMenuButton(ref Selector, UISelect, msg("storeItems"), $"SRUI_ChangeElement Items 0", number);
number++;
}
if (!configData.Categories.Disable_Commands) {
CreateMenuButton(ref Selector, UISelect, msg("storeCommands"), $"SRUI_ChangeElement Commands 0", number);
number++;
}
if (Economics) if (!configData.Categories.Disable_CurrencyExchange) {
CreateMenuButton(ref Selector, UISelect, msg("storeExchange"), "SRUI_ChangeElement Exchange 0", number);
number++;
}
if (!configData.Categories.Disable_CurrencyTransfer) {
CreateMenuButton(ref Selector, UISelect, msg("storeTransfer"), "SRUI_ChangeElement Transfer 0", number);
number++;
}
if (!configData.Categories.Disable_SellersScreen) {
CreateMenuButton(ref Selector, UISelect, msg("sellItems"), "SRUI_ChangeElement Sell 0", number);
number++;
}
CreateMenuButton(ref Selector, UISelect, msg("storeClose"), "SRUI_DestroyAll", number);
UIElements.standardElements.Add(ElementType.Navigation, new CuiElementContainer[] {
Selector
}
);
UIElements.RenameComponents(UIElements.standardElements[ElementType.Navigation]);
}
private void CreateKitsUI() {
int maxPages = 0;
var count = rewardData.RewardKits;
if (count.Count > 10) maxPages = (count.Count - 1) / 10 + 1;
List<CuiElementContainer> kitList = new List<CuiElementContainer>();
for (int i = 0; i <= maxPages; i++) kitList.Add(CreateKitsElement(i));
UIElements.standardElements.Add(ElementType.Kits, kitList.ToArray());
UIElements.RenameComponents(UIElements.standardElements[ElementType.Kits]);
}
private void CreateItemsUI() {
int maxPages = 0;
var count = rewardData.RewardItems;
if (count.Count > 21) maxPages = (count.Count - 1) / 21 + 1;
List<CuiElementContainer> itemList = new List<CuiElementContainer>();
for (int i = 0; i <= maxPages; i++) itemList.Add(CreateItemsElement(i));
UIElements.standardElements.Add(ElementType.Items, itemList.ToArray());
UIElements.RenameComponents(UIElements.standardElements[ElementType.Items]);
}
private void CreateCommandsUI() {
int maxPages = 0;
var count = rewardData.RewardCommands;
if (count.Count > 10) maxPages = (count.Count - 1) / 10 + 1;
List<CuiElementContainer> commandList = new List<CuiElementContainer>();
for (int i = 0; i <= maxPages; i++) commandList.Add(CreateCommandsElement(i));
UIElements.standardElements.Add(ElementType.Commands, commandList.ToArray());
UIElements.RenameComponents(UIElements.standardElements[ElementType.Commands]);
}
private void CreateExchangeUI() {
var Main = SR_UI.CreateElementContainer(UIMain, UIColors["dark"], "0 0", "1 0.92");
SR_UI.CreatePanel(ref Main, UIMain, UIColors["light"], "0.01 0.02", "0.99 0.98", true);
SR_UI.CreateLabel(ref Main, UIMain, "", $"{configData.Messaging.MSG_MainColor}{msg("exchange1")}</color>", 24, "0 0.82", "1 0.9");
SR_UI.CreateLabel(ref Main, UIMain, "", $"{configData.Messaging.MSG_Color}{msg("exchange2")}</color>{configData.Messaging.MSG_MainColor}{configData.CurrencyExchange.RP_ExchangeRate} {msg("storeRP")}</color> -> {configData.Messaging.MSG_MainColor}{configData.CurrencyExchange.Econ_ExchangeRate} {msg("storeCoins")}</color>", 20, "0 0.6", "1 0.7");
SR_UI.CreateLabel(ref Main, UIMain, "", $"{configData.Messaging.MSG_MainColor}{msg("storeRP")} => {msg("storeEcon")}</color>", 20, "0.25 0.4", "0.4 0.55");
SR_UI.CreateLabel(ref Main, UIMain, "", $"{configData.Messaging.MSG_MainColor}{msg("storeEcon")} => {msg("storeRP")}</color>", 20, "0.6 0.4", "0.75 0.55");
SR_UI.CreateButton(ref Main, UIMain, UIColors["buttonbg"], msg("storeExchange"), 20, "0.25 0.3", "0.4 0.38", "SRUI_Exchange 1");
SR_UI.CreateButton(ref Main, UIMain, UIColors["buttonbg"], msg("storeExchange"), 20, "0.6 0.3", "0.75 0.38", "SRUI_Exchange 2");
UIElements.standardElements.Add(ElementType.Exchange, new CuiElementContainer[] {
Main
}
);
UIElements.RenameComponents(UIElements.standardElements[ElementType.Exchange]);
}
private CuiElementContainer CreateKitsElement(int page = 0) {
var Main = SR_UI.CreateElementContainer(UIMain, UIColors["dark"], "0 0", "1 0.92");
SR_UI.CreatePanel(ref Main, UIMain, UIColors["light"], "0.01 0.02", "0.99 0.98", true);
var rew = rewardData.RewardKits;
if (rew.Count > 10) {
var maxpages = (rew.Count - 1) / 10 + 1;
if (page < maxpages - 1) SR_UI.CreateButton(ref Main, UIMain, UIColors["buttonbg"], msg("storeNext"), 18, "0.84 0.05", "0.97 0.1", $"SRUI_ChangeElement Kits {page + 1}");
if (page > 0) SR_UI.CreateButton(ref Main, UIMain, UIColors["buttonbg"], msg("storeBack"), 18, "0.03 0.05", "0.16 0.1", $"SRUI_ChangeElement Kits {page - 1}");
}
int maxentries = (10 * (page + 1));
if (maxentries > rew.Count) maxentries = rew.Count;
int rewardcount = 10 * page;
List<string> kitNames = rewardData.RewardKits.Keys.ToList();
int i = 0;
for (int n = rewardcount; n < maxentries; n++) {
var contents = GetKitContents(rew[kitNames[n]].KitName);
if (string.IsNullOrEmpty(contents) || !configData.UI_Options.ShowKitContents) contents = rew[kitNames[n]].Description;
CreateKitCommandEntry(ref Main, UIMain, kitNames[n], contents, rew[kitNames[n]].Cost, i, true);
i++;
}
if (i == 0) SR_UI.CreateLabel(ref Main, UIMain, "", $"{configData.Messaging.MSG_MainColor}{msg("noKits")}</color>", 24, "0 0.82", "1 0.9");
return Main;
}
private CuiElementContainer CreateItemsElement(int page = 0) {
var Main = SR_UI.CreateElementContainer(UIMain, UIColors["dark"], "0 0", "1 0.92");
SR_UI.CreatePanel(ref Main, UIMain, UIColors["light"], "0.01 0.02", "0.99 0.98", true);
var rew = rewardData.RewardItems;
if (rew.Count > 21) {
var maxpages = (rew.Count - 1) / 21 + 1;
if (page < maxpages - 1) SR_UI.CreateButton(ref Main, UIMain, UIColors["buttonbg"], msg("storeNext"), 18, "0.84 0.05", "0.97 0.1", $"SRUI_ChangeElement Items {page + 1}");
if (page > 0) SR_UI.CreateButton(ref Main, UIMain, UIColors["buttonbg"], msg("storeBack"), 18, "0.03 0.05", "0.16 0.1", $"SRUI_ChangeElement Items {page - 1}");
}
int maxentries = (21 * (page + 1));
if (maxentries > rew.Count) maxentries = rew.Count;
int i = 0;
int rewardcount = 21 * page;
for (int n = rewardcount; n < maxentries; n++) {
CreateItemEntry(ref Main, UIMain, n, i);
i++;
}
if (i == 0) SR_UI.CreateLabel(ref Main, UIMain, "", $"{configData.Messaging.MSG_MainColor}{msg("noItems")}</color>", 24, "0 0.82", "1 0.9");
return Main;
}
private CuiElementContainer CreateCommandsElement(int page = 0) {
var Main = SR_UI.CreateElementContainer(UIMain, UIColors["dark"], "0 0", "1 0.92");
SR_UI.CreatePanel(ref Main, UIMain, UIColors["light"], "0.01 0.02", "0.99 0.98", true);
var rew = rewardData.RewardCommands;
if (rew.Count > 10) {
var maxpages = (rew.Count - 1) / 10 + 1;
if (page < maxpages - 1) SR_UI.CreateButton(ref Main, UIMain, UIColors["buttonbg"], msg("storeNext"), 18, "0.84 0.05", "0.97 0.1", $"SRUI_ChangeElement Commands {page + 1}");
if (page > 0) SR_UI.CreateButton(ref Main, UIMain, UIColors["buttonbg"], msg("storeBack"), 18, "0.03 0.05", "0.16 0.1", $"SRUI_ChangeElement Commands {page - 1}");
}
int maxentries = (10 * (page + 1));
if (maxentries > rew.Count) maxentries = rew.Count;
int rewardcount = 10 * page;
List<string> commNames = rewardData.RewardCommands.Keys.ToList();
int i = 0;
for (int n = rewardcount; n < maxentries; n++) {
CreateKitCommandEntry(ref Main, UIMain, commNames[n], rew[commNames[n]].Description, rew[commNames[n]].Cost, i, false);
i++;
}
if (i == 0) SR_UI.CreateLabel(ref Main, UIMain, "", $"{configData.Messaging.MSG_MainColor}{msg("noCommands")}</color>", 24, "0 0.82", "1 0.9");
return Main;
}
#endregion
#region NPC Elements
private void CreateAllNPCs() {
foreach(var npc in npcDealers.NPCIDs) {
if (npc.Value.isCustom) {
CreateNPCMenu(npc.Key);
}
}
PrintWarning("All UI elements created successfully!");
}
private void CreateNPCMenu(string npcid) {
if (UIElements.npcElements.ContainsKey(npcid)) UIElements.npcElements.Remove(npcid);
CreateNPCNavUI(npcid);
CreateNPCCommandsUI(npcid);
CreateNPCItemsUI(npcid);
CreateNPCKitsUI(npcid);
}
private void CreateNPCNavUI(string npcid) {
var Selector = SR_UI.CreateElementContainer(UISelect, UIColors["dark"], "0 0.92", "1 1");
SR_UI.CreatePanel(ref Selector, UISelect, UIColors["light"], "0.01 0.05", "0.99 0.95", true);
SR_UI.CreateLabel(ref Selector, UISelect, "", $"{configData.Messaging.MSG_MainColor}{msg("storeTitle")}</color>", 30, "0.01 0", "0.2 1");
NPCInfos npcInfo = null;
if (!string.IsNullOrEmpty(npcid)) npcInfo = npcDealers.NPCIDs[npcid];
if (npcInfo == null) return;
int number = 0;
if (!configData.Categories.Disable_Kits && npcInfo.kitList.Count > 0) {
CreateMenuButton(ref Selector, UISelect, msg("storeKits"), $"SRUI_ChangeElement Kits 0 {npcid}", number);
number++;
}
if (!configData.Categories.Disable_Items && npcInfo.itemList.Count > 0) {
CreateMenuButton(ref Selector, UISelect, msg("storeItems"), $"SRUI_ChangeElement Items 0 {npcid}", number);
number++;
}
if (!configData.Categories.Disable_Commands && npcInfo.commandList.Count > 0) {
CreateMenuButton(ref Selector, UISelect, msg("storeCommands"), $"SRUI_ChangeElement Commands 0 {npcid}", number);
number++;
}
if (Economics) if (!configData.Categories.Disable_CurrencyExchange && npcInfo.allowExchange) {
CreateMenuButton(ref Selector, UISelect, msg("storeExchange"), "SRUI_ChangeElement Exchange 0", number);
number++;
}
if (!configData.Categories.Disable_CurrencyTransfer && npcInfo.allowTransfer) {
CreateMenuButton(ref Selector, UISelect, msg("storeTransfer"), "SRUI_ChangeElement Transfer 0", number);
number++;
}
if (!configData.Categories.Disable_SellersScreen && npcInfo.allowSales) {
CreateMenuButton(ref Selector, UISelect, msg("sellItems"), "SRUI_ChangeElement Sell 0", number);
number++;
}
CreateMenuButton(ref Selector, UISelect, msg("storeClose"), "SRUI_DestroyAll", number);
UIElements.npcElements.Add(npcid, new Dictionary<ElementType, CuiElementContainer[]> {
{
ElementType.Navigation, new CuiElementContainer[] {
Selector
}
}
}
);
UIElements.RenameComponents(UIElements.npcElements[npcid][ElementType.Navigation]);
}
private void CreateNPCKitsUI(string npcid) {
int maxPages = 0;
var count = npcDealers.NPCIDs[npcid].kitList;
if (count.Count > 10) maxPages = (count.Count - 1) / 10 + 1;
List<CuiElementContainer> kitList = new List<CuiElementContainer>();
for (int i = 0; i <= maxPages; i++) kitList.Add(CreateNPCKitsElement(npcid, i));
UIElements.npcElements[npcid].Add(ElementType.Kits, kitList.ToArray());
UIElements.RenameComponents(UIElements.npcElements[npcid][ElementType.Kits]);
}
private void CreateNPCItemsUI(string npcid) {
int maxPages = 0;
var count = npcDealers.NPCIDs[npcid].itemList;
if (count.Count > 21) maxPages = (count.Count - 1) / 21 + 1;
List<CuiElementContainer> itemList = new List<CuiElementContainer>();
for (int i = 0; i <= maxPages; i++) itemList.Add(CreateNPCItemsElement(npcid, i));
UIElements.npcElements[npcid].Add(ElementType.Items, itemList.ToArray());
UIElements.RenameComponents(UIElements.npcElements[npcid][ElementType.Items]);
}
private void CreateNPCCommandsUI(string npcid) {
int maxPages = 0;
var count = npcDealers.NPCIDs[npcid].commandList;
if (count.Count > 10) maxPages = (count.Count - 1) / 10 + 1;
List<CuiElementContainer> commandList = new List<CuiElementContainer>();
for (int i = 0; i <= maxPages; i++) commandList.Add(CreateNPCCommandsElement(npcid, i));
UIElements.npcElements[npcid].Add(ElementType.Commands, commandList.ToArray());
UIElements.RenameComponents(UIElements.npcElements[npcid][ElementType.Commands]);
}
private CuiElementContainer CreateNPCKitsElement(string npcid, int page = 0) {
var Main = SR_UI.CreateElementContainer(UIMain, UIColors["dark"], "0 0", "1 0.92");
SR_UI.CreatePanel(ref Main, UIMain, UIColors["light"], "0.01 0.02", "0.99 0.98", true);
List<string> kitNames = npcDealers.NPCIDs[npcid].kitList;
if (kitNames.Count > 10) {
var maxpages = (kitNames.Count - 1) / 10 + 1;
if (page < maxpages - 1) SR_UI.CreateButton(ref Main, UIMain, UIColors["buttonbg"], msg("storeNext"), 18, "0.84 0.05", "0.97 0.1", $"SRUI_ChangeElement Kits {page + 1} {npcid}");
if (page > 0) SR_UI.CreateButton(ref Main, UIMain, UIColors["buttonbg"], msg("storeBack"), 18, "0.03 0.05", "0.16 0.1", $"SRUI_ChangeElement Kits {page - 1} {npcid}");
}
int maxentries = (10 * (page + 1));
if (maxentries > kitNames.Count) maxentries = kitNames.Count;
int rewardcount = 10 * page;
int i = 0;
for (int n = rewardcount; n < maxentries; n++) {
var contents = GetKitContents(rewardData.RewardKits[kitNames[n]].KitName);
if (string.IsNullOrEmpty(contents) || !configData.UI_Options.ShowKitContents) contents = rewardData.RewardKits[kitNames[n]].Description;
CreateKitCommandEntry(ref Main, UIMain, kitNames[n], contents, rewardData.RewardKits[kitNames[n]].Cost, i, true);
i++;
}
if (i == 0) SR_UI.CreateLabel(ref Main, UIMain, "", $"{configData.Messaging.MSG_MainColor}{msg("noKits")}</color>", 24, "0 0.82", "1 0.9");
return Main;
}
private CuiElementContainer CreateNPCItemsElement(string npcid, int page = 0) {
var Main = SR_UI.CreateElementContainer(UIMain, UIColors["dark"], "0 0", "1 0.92");
SR_UI.CreatePanel(ref Main, UIMain, UIColors["light"], "0.01 0.02", "0.99 0.98", true);
var rew = npcDealers.NPCIDs[npcid].itemList;
if (rew.Count > 21) {
var maxpages = (rew.Count - 1) / 21 + 1;
if (page < maxpages - 1) SR_UI.CreateButton(ref Main, UIMain, UIColors["buttonbg"], msg("storeNext"), 18, "0.84 0.05", "0.97 0.1", $"SRUI_ChangeElement Items {page + 1} {npcid}");
if (page > 0) SR_UI.CreateButton(ref Main, UIMain, UIColors["buttonbg"], msg("storeBack"), 18, "0.03 0.05", "0.16 0.1", $"SRUI_ChangeElement Items {page - 1} {npcid}");
}
int maxentries = (21 * (page + 1));
if (maxentries > rew.Count) maxentries = rew.Count;
int i = 0;
int rewardcount = 21 * page;
for (int n = rewardcount; n < maxentries; n++) {
CreateItemEntry(ref Main, UIMain, rew[n], i);
i++;
}
if (i == 0) SR_UI.CreateLabel(ref Main, UIMain, "", $"{configData.Messaging.MSG_MainColor}{msg("noItems")}</color>", 24, "0 0.82", "1 0.9");
return Main;
}
private CuiElementContainer CreateNPCCommandsElement(string npcid, int page = 0) {
var Main = SR_UI.CreateElementContainer(UIMain, UIColors["dark"], "0 0", "1 0.92");
SR_UI.CreatePanel(ref Main, UIMain, UIColors["light"], "0.01 0.02", "0.99 0.98", true);
List<string> commNames = npcDealers.NPCIDs[npcid].commandList;
if (commNames.Count > 10) {
var maxpages = (commNames.Count - 1) / 10 + 1;
if (page < maxpages - 1) SR_UI.CreateButton(ref Main, UIMain, UIColors["buttonbg"], msg("storeNext"), 18, "0.84 0.05", "0.97 0.1", $"SRUI_ChangeElement Commands {page + 1} {npcid}");
if (page > 0) SR_UI.CreateButton(ref Main, UIMain, UIColors["buttonbg"], msg("storeBack"), 18, "0.03 0.05", "0.16 0.1", $"SRUI_ChangeElement Commands {page - 1} {npcid}");
}
int maxentries = (10 * (page + 1));
if (maxentries > commNames.Count) maxentries = commNames.Count;
int rewardcount = 10 * page;
int i = 0;
for (int n = rewardcount; n < maxentries; n++) {
if (rewardData.RewardCommands.ContainsKey(commNames[n])) {
CreateKitCommandEntry(ref Main, UIMain, commNames[n], rewardData.RewardCommands[commNames[n]].Description, rewardData.RewardCommands[commNames[n]].Cost, i, false);
i++;
}
}
if (i == 0) SR_UI.CreateLabel(ref Main, UIMain, "", $"{configData.Messaging.MSG_MainColor}{msg("noCommands")}</color>", 24, "0 0.82", "1 0.9");
return Main;
}
#endregion
#endregion
#region Sale System
private void CreateSaleElement(BasePlayer player) {
var HelpMain = SR_UI.CreateElementContainer(UIMain, UIColors["dark"], "0 0", "1 0.92");
SR_UI.CreatePanel(ref HelpMain, UIMain, UIColors["light"], "0.01 0.02", "0.99 0.98", true);
SR_UI.CreateLabel(ref HelpMain, UIMain, "", $"{configData.Messaging.MSG_MainColor}{msg("selectSell")}</color>", 22, "0 0.9", "1 1");
int i = 0;
foreach(var item in player.inventory.containerMain.itemList) {
if (saleData.Prices.ContainsKey(item.info.itemid)) {
if (!saleData.Prices[item.info.itemid].ContainsKey(item.skin)) {
saleData.Prices[item.info.itemid].Add(item.skin, new SaleInfo {
Enabled = false, Name = item?.info?.steamItem?.displayName?.english ?? $"{item.info.displayName.english} {item.skin}", SalePrice = 1
}
);
SavePrices();
}
if (saleData.Prices[item.info.itemid][item.skin].Enabled) {
var name = item.info.displayName.english;
if (ItemNames.ContainsKey(item.info.itemid.ToString())) name = ItemNames[item.info.itemid.ToString()];
CreateInventoryEntry(ref HelpMain, UIMain, item.info.itemid, item.skin, name, item.amount, i);
i++;
}
}
}
CuiHelper.DestroyUi(player, UIMain);
CuiHelper.AddUi(player, HelpMain);
}
private void CreateInventoryEntry(ref CuiElementContainer container, string panelName, int itemId, ulong skinId, string name, int amount, int number) {
var pos = CalcPosInv(number);
SR_UI.CreateLabel(ref container, panelName, "", $"{msg("Name")}: {configData.Messaging.MSG_MainColor}{name}</color>", 14, $"{pos[0]} {pos[1]}", $"{pos[0] + 0.22f} {pos[3]}", TextAnchor.MiddleLeft);
SR_UI.CreateLabel(ref container, panelName, "", $"{msg("Amount")}: {configData.Messaging.MSG_MainColor}{amount}</color>", 14, $"{pos[0] + 0.22f} {pos[1]}", $"{pos[0] + 0.32f} {pos[3]}", TextAnchor.MiddleLeft);
SR_UI.CreateButton(ref container, panelName, UIColors["buttonbg"], msg("Sell"), 14, $"{pos[0] + 0.35f} {pos[1]}", $"{pos[2]} {pos[3]}", $"SRUI_SellItem {itemId} {skinId} {amount} {name}");
}
private void SellItem(BasePlayer player, int itemId, ulong skinId, string name, int amount) {
var HelpMain = SR_UI.CreateElementContainer(UIMain, UIColors["dark"], "0 0", "1 0.92");
SR_UI.CreatePanel(ref HelpMain, UIMain, UIColors["light"], "0.01 0.02", "0.99 0.98", true);
SR_UI.CreateLabel(ref HelpMain, UIMain, "", $"{configData.Messaging.MSG_MainColor}{msg("selectToSell")}</color>", 22, "0 0.9", "1 1");
var price = saleData.Prices[itemId][skinId].SalePrice;
int salePrice = (int)Math.Floor(price * amount);
SR_UI.CreateLabel(ref HelpMain, UIMain, "", string.Format(msg("sellItemF"), configData.Messaging.MSG_MainColor, name), 18, "0.1 0.8", "0.3 0.84", TextAnchor.MiddleLeft);
SR_UI.CreateLabel(ref HelpMain, UIMain, "", string.Format(msg("sellPriceF"), configData.Messaging.MSG_MainColor, price, msg("storeRP")), 18, "0.1 0.76", "0.3 0.8", TextAnchor.MiddleLeft);
SR_UI.CreateLabel(ref HelpMain, UIMain, "", string.Format(msg("sellUnitF"), configData.Messaging.MSG_MainColor, amount), 18, "0.1 0.72", "0.3 0.76", TextAnchor.MiddleLeft);
SR_UI.CreateLabel(ref HelpMain, UIMain, "", string.Format(msg("sellTotalF"), configData.Messaging.MSG_MainColor, salePrice, msg("storeRP")), 18, "0.1 0.68", "0.3 0.72", TextAnchor.MiddleLeft);
SR_UI.CreateButton(ref HelpMain, UIMain, UIColors["buttonbg"], "+ 10000", 16, "0.84 0.72", "0.89 0.76", $"SRUI_SellItem {itemId} {skinId} {amount + 10000} {name}");
SR_UI.CreateButton(ref HelpMain, UIMain, UIColors["buttonbg"], "+ 1000", 16, "0.78 0.72", "0.83 0.76", $"SRUI_SellItem {itemId} {skinId} {amount + 1000} {name}");
SR_UI.CreateButton(ref HelpMain, UIMain, UIColors["buttonbg"], "+ 100", 16, "0.72 0.72", "0.77 0.76", $"SRUI_SellItem {itemId} {skinId} {amount + 100} {name}");
SR_UI.CreateButton(ref HelpMain, UIMain, UIColors["buttonbg"], "+ 10", 16, "0.66 0.72", "0.71 0.76", $"SRUI_SellItem {itemId} {skinId} {amount + 10} {name}");
SR_UI.CreateButton(ref HelpMain, UIMain, UIColors["buttonbg"], "+ 1", 16, "0.6 0.72", "0.65 0.76", $"SRUI_SellItem {itemId} {skinId} {amount + 1} {name}");
SR_UI.CreateButton(ref HelpMain, UIMain, UIColors["buttonbg"], "-1", 16, "0.54 0.72", "0.59 0.76", $"SRUI_SellItem {itemId} {skinId} {amount - 1} {name}");
SR_UI.CreateButton(ref HelpMain, UIMain, UIColors["buttonbg"], "-10", 16, "0.48 0.72", "0.53 0.76", $"SRUI_SellItem {itemId} {skinId} {amount - 10} {name}");
SR_UI.CreateButton(ref HelpMain, UIMain, UIColors["buttonbg"], "-100", 16, "0.42 0.72", "0.47 0.76", $"SRUI_SellItem {itemId} {skinId} {amount - 100} {name}");
SR_UI.CreateButton(ref HelpMain, UIMain, UIColors["buttonbg"], "-1000", 16, "0.36 0.72", "0.41 0.76", $"SRUI_SellItem {itemId} {skinId} {amount - 1000} {name}");
SR_UI.CreateButton(ref HelpMain, UIMain, UIColors["buttonbg"], "-10000", 16, "0.3 0.72", "0.35 0.76", $"SRUI_SellItem {itemId} {skinId} {amount - 10000} {name}");
SR_UI.CreateButton(ref HelpMain, UIMain, UIColors["buttonbg"], msg("cancelSale"), 16, "0.75 0.34", "0.9 0.39", "SRUI_CancelSale");
SR_UI.CreateButton(ref HelpMain, UIMain, UIColors["buttonbg"], msg("confirmSale"), 16, "0.55 0.34", "0.7 0.39", $"SRUI_Sell {itemId} {skinId} {amount} {name}");
CuiHelper.DestroyUi(player, UIMain);
CuiHelper.AddUi(player, HelpMain);
}
#region Commands
[ConsoleCommand("SRUI_CancelSale")]
private void cmdCancelSale(ConsoleSystem.Arg arg) {
var player = arg.connection.player as BasePlayer;
if (player == null) return;
CreateSaleElement(player);
}
[ConsoleCommand("SRUI_SellItem")]
private void cmdSellItem(ConsoleSystem.Arg arg) {
var player = arg.connection.player as BasePlayer;
if (player == null) return;
int itemId = arg.GetInt(0);
ulong skinId = arg.GetUInt64(1);
int amount = arg.GetInt(2);
string name = arg.GetString(3);
var max = GetAmount(player, itemId, skinId);
if (amount <= 0) amount = 1;
if (amount > max) amount = max;
SellItem(player, itemId, skinId, name, amount);
}
[ConsoleCommand("SRUI_Sell")]
private void cmdSell(ConsoleSystem.Arg arg) {
var player = arg.connection.player as BasePlayer;
if (player == null) return;
int itemId = arg.GetInt(0);
ulong skinId = arg.GetUInt64(1);
int amount = arg.GetInt(2);
string name = arg.GetString(3);
float price = saleData.Prices[itemId][skinId].SalePrice;
int salePrice = (int)Math.Floor(price * amount);
TakeResources(player, itemId, skinId, amount);
AddPoints(player.userID, salePrice);
CreateSaleElement(player);
PopupMessage(player, string.Format(msg("saleSuccess"), amount, name, salePrice, msg("storeRP")));
}
#endregion
#region Functions
private float[] CalcPosInv(int number) {
Vector2 dimensions = new Vector2(0.45f, 0.04f);
Vector2 origin = new Vector2(0.015f, 0.86f);
float offsetY = 0.005f;
float offsetX = 0.033f;
float posX = 0;
float posY = 0;
if (number < 18) {
posX = origin.x;
posY = (offsetY + dimensions.y) * number;
}
else {
number -= 18;
posX = offsetX + dimensions.x;
posY = (offsetY + dimensions.y) * number;
}
Vector2 offset = new Vector2(posX, -posY);
Vector2 posMin = origin + offset;
Vector2 posMax = posMin + dimensions;
return new float[] {
posMin.x, posMin.y, posMax.x, posMax.y
}
;
}
private int GetAmount(BasePlayer player, int itemid, ulong skinid) {
List<Item> items = player.inventory.AllItems().ToList().FindAll((Item x) => x.info.itemid == itemid);
int num = 0;
foreach (Item item in items) {
if (!item.IsBusy()) {
if (item.skin == skinid) num = num + item.amount;
}
}
return num;
}
private void TakeResources(BasePlayer player, int itemid, ulong skinId, int iAmount) {
int num = TakeResourcesFrom(player, player.inventory.containerMain.itemList, itemid, skinId, iAmount);
if (num < iAmount) num += TakeResourcesFrom(player, player.inventory.containerBelt.itemList, itemid, skinId, iAmount);
if (num < iAmount) num += TakeResourcesFrom(player, player.inventory.containerWear.itemList, itemid, skinId, iAmount);
}
private int TakeResourcesFrom(BasePlayer player, List<Item> container, int itemid, ulong skinId, int iAmount) {
List<Item> collect = new List<Item>();
List<Item> items = new List<Item>();
int num = 0;
foreach (Item item in container) {
if (item.info.itemid == itemid && item.skin == skinId) {
int num1 = iAmount - num;
if (num1 > 0) {
if (item.amount <= num1) {
if (item.amount <= num1) {
num = num + item.amount;
items.Add(item);
if (collect != null) collect.Add(item);
}
if (num != iAmount) continue;
break;
}
else {
item.MarkDirty();
Item item1 = item;
item1.amount = item1.amount - num1;
num = num + num1;
Item item2 = ItemManager.CreateByItemID(itemid, 1, skinId);
item2.AddOwners(item.owners, 1f);
// ... [Code preview truncated due to length. Full source in attached file] ...