C#:
using Oxide.Core;
using Oxide.Core.Plugins;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using Facepunch;
using Newtonsoft.Json;
using Oxide.Game.Rust.Cui;
using Rust;
using Rust.Ai;
using UnityEngine;
using VLB;
using Color = UnityEngine.Color;
using Debug = UnityEngine.Debug;
using System.Text;
namespace Oxide.Plugins
{
[Info("Покупка чинука в магазине","Допилил Viking", "1.0.0")]
class Ch47Buy : RustPlugin
{
static Ch47Buy _plugin;
Dictionary<ulong, BaseEntity> chinok = new Dictionary<ulong, BaseEntity>();
void Loaded() => _plugin = this;
private Dictionary<uint, uint> PRM = new Dictionary<uint, uint>();
void OnEntityBuilt(Planner planner, GameObject gameobject)
{
BaseEntity entity = gameobject.ToBaseEntity();
if(entity == null) return;
if (entity.skinID != 1771792500) return;
NextTick(() => entity.Kill());
var ePos = entity.transform.position;
Vector3 position = new Vector3(ePos.x,ePos.y+1, ePos.z);
var hitted = false;
RaycastHit Hit;
if (Physics.Raycast(position, Vector3.down, out Hit, LayerMask.GetMask(new string[] {"Construction"})))
{
var rhEntity = Hit.GetEntity();
if (rhEntity != null)
{
hitted = true;
if(!PRM.ContainsKey(rhEntity.net.ID))
PRM.Add(rhEntity.net.ID, 0);
}
}
BaseEntity rEntity = GameManager.server.CreateEntity("assets/prefabs/npc/ch47/ch47.entity.prefab", entity.transform.position, entity.GetNetworkRotation(), true);
chinok.Add(rEntity.net.ID, entity);
rEntity.Spawn();
rEntity.skinID = 1771792500;
if (!hitted) return;
PRM[Hit.GetEntity().net.ID] = rEntity.net.ID;
}
bool GiveRecycler(ItemContainer container)
{
var item = ItemManager.CreateByItemID(833533164, 1, 1771792500);
item.name = "Чинук";
return item.MoveToContainer(container, -1, false);
}
bool GiveRecycler(BasePlayer player)
{
var item = ItemManager.CreateByItemID(833533164, 1, 1771792500);
item.name = "Чинук";
if (!player.inventory.GiveItem(item))
{
item.Drop(player.inventory.containerMain.dropPosition, player.inventory.containerMain.dropVelocity, new Quaternion());
return false;
}
return true;
}
[ConsoleCommand("ch47.add")]
private void CmdAddch47(ConsoleSystem.Arg arg)
{
if (!arg.IsAdmin)
{
SendError(arg, "[Ошибка] У вас нет доступа к этой команде!");
return;
}
if (!arg.HasArgs())
{
PrintError(
":\n[Ошибка] Введите ch47.add steamid/nickname\n[Пример] ch47.add Hellig\n");
return;
}
var player = BasePlayer.Find(arg.Args[0]);
if (player == null)
{
PrintError($"[Ошибка] Не удается найти игрока {arg.Args[0]}");
return;
}
GiveRecycler(player);
}
[ConsoleCommand("UI_Chinok")]
private void CmdAddch471(ConsoleSystem.Arg arg)
{
if (arg.Player() == null) return;
uint chinokIndex = 0;
if (uint.TryParse(arg.Args[0], out chinokIndex) == false)
return;
BaseEntity chinokEntity;
if (chinok.TryGetValue(chinokIndex, out chinokEntity))
{
chinokEntity.AdminKill();
GiveRecycler(arg.Player());
}
}
private class InterfaceManager
{
#region Vars
public static InterfaceManager Instance;
public const string UI_Layer = "UI_Chinok";
public const string UI_Instruction = "UI_Instruction_Chinok";
public Dictionary<string, string> Interfaces;
#endregion
#region Main
public InterfaceManager()
{
Instance = this;
Interfaces = new Dictionary<string, string>();
BuildInterface();
}
public static void AddInterface(string name, string json)
{
if (Instance.Interfaces.ContainsKey(name))
{
_plugin.PrintError($"Error! Tried to add existing cui elements! -> {name}");
return;
}
Instance.Interfaces.Add(name, json);
}
public static string GetInterface(string name)
{
string json = string.Empty;
if (Instance.Interfaces.TryGetValue(name, out json) == false)
{
_plugin.PrintWarning($"Warning! UI elements not found by name! -> {name}");
}
return json;
}
public static void DestroyAll()
{
for (var i = 0; i < BasePlayer.activePlayerList.Count; i++)
{
var player = BasePlayer.activePlayerList[i];
CuiHelper.DestroyUi(player, UI_Layer);
CuiHelper.DestroyUi(player, UI_Instruction);
}
}
#endregion
private void BuildInterface()
{
CuiElementContainer container = new CuiElementContainer()
{
{
new CuiElement
{
Parent = UI_Layer,
Name = $"{UI_Layer}.Take",
Components =
{
new CuiImageComponent
{
Color = GetColor("#CA2539", 0.8f)
},
new CuiRectTransformComponent
{
AnchorMin = $"0 1",
AnchorMax = $"0 1",
OffsetMin = "225 -100",
OffsetMax = "275 -50"
},
}
}
},
{
new CuiElement
{
Parent = $"{UI_Layer}.Take",
Components =
{
new CuiRawImageComponent
{
Png = _plugin.GetImage(GetNameByURL("https://i.imgur.com/973xdCt.png"))
},
new CuiRectTransformComponent
{
AnchorMin = $"0 0",
AnchorMax = $"1 1"
}
}
}
},
{
new CuiButton
{
RectTransform =
{
AnchorMin = $"0 0",
AnchorMax = $"1 1",
},
Button =
{
Color = "0 0 0 0", Command = "UI_Chinok {Chinok}", Close = UI_Layer
},
Text =
{
Text = ""
}
},
$"{UI_Layer}.Take"
},
};
AddInterface("ChinokMain", container.ToJson());
}
}
[PluginReference] private Plugin ImageLibrary;
private string GetImage(string name)
{
string ID = (string)ImageLibrary?.Call("GetImage", name);
if (ID == "")
ID = (string)ImageLibrary?.Call("GetImage", name) ?? ID;
return ID;
}
public static string GetColor(string hex, float alpha = 1f)
{
if (hex.Length != 7) hex = "#FFFFFF";
if (alpha < 0 || alpha > 1f) alpha = 1f;
var color = ColorTranslator.FromHtml(hex);
var r = Convert.ToInt16(color.R) / 255f;
var g = Convert.ToInt16(color.G) / 255f;
var b = Convert.ToInt16(color.B) / 255f;
return $"{r} {g} {b} {alpha}";
}
private static string GetNameByURL(string url)
{
var splitted = url.Split('/');
var endUrl = splitted[splitted.Length - 1];
var name = endUrl.Split('.')[0];
return name;
}
}
}