C#:
using Oxide.Core.Plugins;
using Oxide.Game.Rust.Cui;
using UnityEngine;
using System.Collections.Generic;
using Newtonsoft.Json;
using Oxide.Core.Plugins;
using Oxide.Game.Rust.Cui;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Craft", "Я и Я", "1.0.0")]
class Craft : RustPlugin
{
#region Вар
private string Layer = "Craft_UI";
[PluginReference] Plugin ImageLibrary;
#endregion
#region Класс
public class CraftSettings
{
[JsonProperty("Название категории")] public string CategoryName;
[JsonProperty("Предметы")] public List<ItemList> item;
}
public class ItemList
{
[JsonProperty("ID (Не должен совпадать с другими предметами)")] public string ID;
[JsonProperty("Название")] public string DisplayName;
[JsonProperty("Описание предмета")] public string Description;
[JsonProperty("Уровень верстака")] public int Workbench;
[JsonProperty("Короткое название предмета")] public string ShortName;
[JsonProperty("Команда")] public string Command;
[JsonProperty("SkinID")] public ulong SkinID;
[JsonProperty("Изображение")] public string Url;
[JsonProperty("Предметы для крафта")] public List<ItemCraft> items;
}
public class ItemCraft
{
[JsonProperty("Короткое название предмета")] public string ShortName;
[JsonProperty("SkinID")] public ulong SkinID;
[JsonProperty("Кол-во")] public int Amount;
}
#endregion
#region Конфиг
Configuration config;
class Configuration
{
[JsonProperty("Настройки крафта")] public List<CraftSettings> settings;
public static Configuration GetNewConfig()
{
return new Configuration
{
settings = new List<CraftSettings>()
{
new CraftSettings
{
CategoryName = "Конструкции",
item = new List<ItemList>()
{
new ItemList
{
ID = "1",
DisplayName = "Забор",
Description = "Забор — украсьте эту территорию этим забором.\nПросто ахуенный забор!",
Workbench = 1,
ShortName = "wall.graveyard.fence",
Command = null,
SkinID = 0,
Url = null,
items = new List<ItemCraft>()
{
new ItemCraft
{
ShortName = "wood",
SkinID = 0,
Amount = 1000
}
}
},
new ItemList
{
ID = "2",
DisplayName = "Хуй знает что это",
Description = "Какая то услуга, хуй знает зачем она нужна!\nЯ так и не понял ничего(тест)",
Workbench = 1,
ShortName = null,
Command = "xyita",
SkinID = 0,
Url = "https://imgur.com/zpXoO6a.png",
items = new List<ItemCraft>()
{
new ItemCraft
{
ShortName = "charcoal",
SkinID = 0,
Amount = 5000
}
}
}
}
},
new CraftSettings
{
CategoryName = "Оружие",
item = new List<ItemList>()
{
new ItemList
{
ID = "222",
DisplayName = "Ак",
Description = "Ахуенная винтовка, прям раз выстрелил и нет пидораса!",
Workbench = 1,
ShortName = "rifle.ak",
Command = null,
SkinID = 0,
Url = null,
items = new List<ItemCraft>()
{
new ItemCraft
{
ShortName = "sulfur",
SkinID = 0,
Amount = 2000
}
}
}
}
},
}
};
}
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
config = Config.ReadObject<Configuration>();
if (config?.settings == null) LoadDefaultConfig();
}
catch
{
PrintWarning($"Ошибка чтения конфигурации 'oxide/config/{Name}', создаём новую конфигурацию!!");
LoadDefaultConfig();
}
NextTick(SaveConfig);
}
protected override void LoadDefaultConfig() => config = Configuration.GetNewConfig();
protected override void SaveConfig() => Config.WriteObject(config);
#endregion
#region Хуки
void OnServerInitialized()
{
foreach (var check in config.settings)
{
foreach (var item in check.item)
{
ImageLibrary.Call("AddImage", item.Url != null ? item.Url : $"https://rustlabs.com/img/items180/{item.ShortName}.png", item.Url != null ? item.Url : item.ShortName);
foreach (var items in item.items)
ImageLibrary.Call("AddImage", $"https://rustlabs.com/img/items180/{items.ShortName}.png", items.ShortName);
}
}
}
void Unload()
{
foreach (var check in BasePlayer.activePlayerList)
CuiHelper.DestroyUi(check, "Crafts");
}
#endregion
#region Команды
[ConsoleCommand("craft")]
void ConsoleCraft(ConsoleSystem.Arg args)
{
var player = args.Player();
if (player != null && args.HasArgs(1))
{
if (args.Args[0] == "category")
{
ItemUI(player, int.Parse(args.Args[1]));
}
if (args.Args[0] == "item")
{
ItemCraftUI(player, int.Parse(args.Args[1]), args.Args[2]);
}
if (args.Args[0] == "crafts")
{
bool enable = true;
var items = config.settings[int.Parse(args.Args[1])].item.FirstOrDefault(z => z.ID == args.Args[2]);
if (player.currentCraftLevel < items.Workbench)
{
SendReply(player, $"Нужен верстак {items.Workbench} уровня");
return;
}
foreach (var check in items.items)
{
var haveCount = player.inventory.GetAmount(ItemManager.FindItemDefinition(check.ShortName).itemid);
if (haveCount >= check.Amount) continue;
enable = false;
}
if (!enable)
{
SendReply(player, "У вас не хватает ресурсов для крафта");
return;
}
foreach (var check in items.items)
{
player.inventory.Take(null, ItemManager.FindItemDefinition(check.ShortName).itemid, check.Amount);
}
if (items.Command != null)
{
Server.Command(items.Command.Replace("%STEAMID%", player.UserIDString));
}
if (items.ShortName != null)
{
var item = ItemManager.CreateByItemID(ItemManager.FindItemDefinition(items.ShortName).itemid, 1, items.SkinID);
if (!player.inventory.GiveItem(item))
{
item.Drop(player.inventory.containerMain.dropPosition, player.inventory.containerMain.dropVelocity, new Quaternion());
return;
}
}
SendReply(player, $"Вы успешно скрафтили: {items.DisplayName}");
CuiHelper.DestroyUi(player, Layer);
return;
}
}
}
#endregion
#region Интерфейс
void CraftUI(BasePlayer player)
{
CuiHelper.DestroyUi(player, "Crafts");
var container = new CuiElementContainer();
container.Add(new CuiPanel
{
RectTransform = { AnchorMin = "0.464 0", AnchorMax = "0.966 1", OffsetMax = "0 0" },
Image = { Color = "0 0 0 0.9" },
}, "Menu_UI", "Crafts");
container.Add(new CuiPanel
{
RectTransform = { AnchorMin = "0 0.2", AnchorMax = "1 0.8", OffsetMax = "0 0" },
Image = { Color = "0 0 0 0" },
}, "Crafts", Layer);
float width = 0.2f, height = 0.0978f, startxBox = 0f, startyBox = 0.992f - height, xmin = startxBox, ymin = startyBox;
for (int z = 0; z < config.settings.Count(); z++)
{
container.Add(new CuiButton
{
RectTransform = { AnchorMin = $"{xmin} {ymin}", AnchorMax = $"{xmin + width} {ymin + height * 1}", OffsetMin = "0 1", OffsetMax = "0 -1" },
Button = { Color = "0 0 0 0", Command = $"craft category {z}" },
Text = { Text = $" {config.settings.ElementAt(z).CategoryName.ToUpper()}", Color = "1 1 1 0.5", Align = TextAnchor.MiddleLeft, FontSize = 18, Font = "robotocondensed-regular.ttf" }
}, Layer);
xmin += width;
if (xmin + width >= 0)
{
xmin = startxBox;
ymin -= height;
}
}
CuiHelper.AddUi(player, container);
ItemUI(player, 0);
}
void ItemUI(BasePlayer player, int category)
{
CuiHelper.DestroyUi(player, "Items");
var container = new CuiElementContainer();
container.Add(new CuiPanel
{
RectTransform = { AnchorMin = "0.22 0", AnchorMax = "1 1", OffsetMax = "0 0" },
Image = { Color = "0 0 0 0" },
}, Layer, "Items");
float width = 0.195f, height = 0.21f, startxBox = 0f, startyBox = 0.92f - height, xmin = startxBox, ymin = startyBox;
foreach (var check in config.settings[category].item)
{
container.Add(new CuiButton
{
RectTransform = { AnchorMin = $"{xmin} {ymin}", AnchorMax = $"{xmin + width} {ymin + height * 1}", OffsetMin = "3 3", OffsetMax = "-3 -3" },
Button = { Color = "0 0 0 0.5", Command = $"craft item {category} {check.ID}" },
Text = { Text = $"", Color = "1 1 1 0.5", Align = TextAnchor.MiddleLeft, FontSize = 18, Font = "robotocondensed-regular.ttf" }
}, "Items", "Image");
container.Add(new CuiPanel
{
RectTransform = { AnchorMin = $"0 0", AnchorMax = $"1 1", OffsetMin = "10 10", OffsetMax = "-10 -10" },
Image = { Color = "1 1 1 0.15" }
}, "Image");
var image = check.Url != null ? check.Url : check.ShortName;
container.Add(new CuiElement
{
Parent = "Image",
Components =
{
new CuiRawImageComponent { Png = (string) ImageLibrary.Call("GetImage", image) },
new CuiRectTransformComponent { AnchorMin = "0 0", AnchorMax = "1 1", OffsetMin = "20 20", OffsetMax = "-20 -20" }
}
});
xmin += width;
if (xmin + width >= 1)
{
xmin = startxBox;
ymin -= height;
}
}
CuiHelper.AddUi(player, container);
}
void ItemCraftUI(BasePlayer player, int category, string name)
{
CuiHelper.DestroyUi(player, "1");
var container = new CuiElementContainer();
var check = config.settings[category].item.FirstOrDefault(z => z.ID == name);
int ItemCount = check.items.Count(), Count = 8;
float Position = 0.2f, Width = 0.1f, Height = 0.14f, Margin = 0.005f, MinHeight = 0.21f;
Position = 0.5f - ItemCount / 2f * Width - (ItemCount - 1) / 2f * Margin;
int current = 1;
container.Add(new CuiPanel
{
RectTransform = { AnchorMin = "0 0", AnchorMax = "1 1", OffsetMax = "0 0" },
Image = { Color = "0.117 0.121 0.109 0.8", Material = "", },
}, "Crafts", "1");
container.Add(new CuiPanel
{
RectTransform = { AnchorMin = "0 0.2", AnchorMax = "1 0.8", OffsetMax = "0 0" },
Image = { Color = "0 0 0 0" },
}, "1", "Craft");
container.Add(new CuiButton
{
RectTransform = { AnchorMin = $"0 0", AnchorMax = $"1 1", OffsetMax = "0 0" },
Button = { Color = "1 1 1 0", Close = "1" },
Text = { Text = $"", Color = "1 1 1 0.5", Align = TextAnchor.LowerCenter, FontSize = 18, Font = "robotocondensed-regular.ttf" }
}, "Craft");
container.Add(new CuiLabel
{
RectTransform = { AnchorMin = $"0.42 0.6", AnchorMax = $"0.58 0.85", OffsetMax = "0 0" },
Text = { Text = $"", Color = "1 1 1 0.5", Align = TextAnchor.LowerCenter, FontSize = 18, Font = "robotocondensed-regular.ttf" }
}, "Craft", "Image");
var image = check.Url != null ? check.Url : check.ShortName;
container.Add(new CuiElement
{
Parent = "Image",
Components =
{
new CuiRawImageComponent { Png = (string) ImageLibrary.Call("GetImage", image) },
new CuiRectTransformComponent { AnchorMin = "0 0", AnchorMax = "1 1", OffsetMin = "5 5", OffsetMax = "-5 -5" }
}
});
container.Add(new CuiLabel
{
RectTransform = { AnchorMin = $"0.2 0.4", AnchorMax = $"0.8 0.59", OffsetMax = "0 0" },
Text = { Text = $"<b><size=25>{check.DisplayName.ToUpper()}</size></b>\n{check.Description}", Color = "1 1 1 0.5", Align = TextAnchor.UpperCenter, FontSize = 12, Font = "robotocondensed-regular.ttf" }
}, "Craft");
container.Add(new CuiLabel
{
RectTransform = { AnchorMin = $"0.2 0.35", AnchorMax = $"0.8 0.4", OffsetMax = "0 0" },
Text = { Text = $"СПИСОК ПРЕДМЕТОВ ДЛЯ КРАФТА", Color = "1 1 1 0.5", Align = TextAnchor.UpperCenter, FontSize = 12, Font = "robotocondensed-regular.ttf" }
}, "Craft");
foreach (var item in check.items)
{
int haveCount = player.inventory.GetAmount(ItemManager.FindItemDefinition(item.ShortName).itemid);
var colors = haveCount >= item.Amount ? "0.39 0.73 0.49 0.3" : "0.76 0.24 0.24 0.3";
container.Add(new CuiPanel
{
RectTransform = { AnchorMin = $"{Position} {MinHeight}", AnchorMax = $"{Position + Width} {MinHeight + Height}", OffsetMax = "0 0" },
Image = { Color = colors }
}, "Craft", "Images");
Position += (Width + Margin);
container.Add(new CuiElement
{
Parent = "Images",
Components =
{
new CuiRawImageComponent { Png = (string) ImageLibrary.Call("GetImage", item.ShortName) },
new CuiRectTransformComponent { AnchorMin = "0 0", AnchorMax = "1 1", OffsetMin = "5 5", OffsetMax = "-5 -5" }
}
});
var text = haveCount >= item.Amount ? "СОБРАНО " : $"X{item.Amount} ";
container.Add(new CuiLabel
{
RectTransform = { AnchorMin = $"0 0", AnchorMax = $"1 1", OffsetMax = "0 0" },
Text = { Text = text, Color = "1 1 1 0.5", Align = TextAnchor.LowerRight, FontSize = 12, Font = "robotocondensed-regular.ttf" }
}, "Images");
}
container.Add(new CuiButton
{
RectTransform = { AnchorMin = "0.4 0.12", AnchorMax = "0.6 0.18", OffsetMax = "0 0" },
Button = { Color = "1 1 1 0.1", Command = $"craft crafts {category} {check.ID}" },
Text = { Text = "СКРАФТИТЬ", Color = "1 1 1 0.5", Align = TextAnchor.MiddleCenter, FontSize = 20, Font = "robotocondensed-bold.ttf" }
}, "Craft");
var textWork = 0 < check.Workbench ? $"Нужен верстак {check.Workbench} уровня" : "Верстак не нужен";
container.Add(new CuiLabel
{
RectTransform = { AnchorMin = "0 0.02", AnchorMax = "1 0.12", OffsetMax = "0 0" },
Text = { Text = textWork, Color = "1 1 1 0.5", Align = TextAnchor.MiddleCenter, FontSize = 12, Font = "robotocondensed-regular.ttf" }
}, "Craft");
CuiHelper.AddUi(player, container);
}
#endregion
}
}