Чтобы добавить функциональность принятия трейда в ваш плагин, вам нужно создать команду для игроков, которая позволит им принимать трейды. Ниже приведен обновленный код плагина с добавленной функцией для принятия трейда:Сделай функцид для принятия трейда
C#:
using System;
using System.Collections.Generic;
using Oxide.Core;
using Oxide.Core.Plugins;
using Oxide.Game.Rust.Cui;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("ItemTrading", "YourName", "1.0.0")]
class ItemTrading : RustPlugin
{
private Dictionary<ulong, TradeData> tradeRequests = new Dictionary<ulong, TradeData>();
private Dictionary<ulong, TradeData> tradeOffers = new Dictionary<ulong, TradeData>();
class TradeData
{
public BasePlayer Initiator;
public BasePlayer Target;
public List<Item> OfferedItems = new List<Item>();
public List<Item> RequestedItems = new List<Item>();
public DateTime ExpirationTime;
public bool IsAccepted;
public string PanelName;
}
void OpenTradeRequestGUI(BasePlayer initiator, BasePlayer target)
{
CuiElementContainer container = new CuiElementContainer();
string panelName = $"tradeRequestPanel_{initiator.userID}";
container.Add(new CuiPanel
{
Image = { Color = "0.1 0.1 0.1 0.9" },
RectTransform = { AnchorMin = "0.3 0.3", AnchorMax = "0.7 0.7" },
CursorEnabled = true
}, "Hud.Menu", panelName);
// Add elements to the container for trade request GUI
CuiHelper.AddUi(initiator, container);
tradeRequests[initiator.userID] = new TradeData
{
Initiator = initiator,
Target = target,
ExpirationTime = DateTime.Now.AddMinutes(5),
PanelName = panelName
};
// Notify the target player about the trade request
target.ChatMessage("You have received a trade request from " + initiator.displayName + ". Type /trade accept to accept.");
initiator.ChatMessage("Trade request sent successfully to " + target.displayName + ".");
}
void OpenTradeConfirmationGUI(BasePlayer initiator, BasePlayer target)
{
CuiElementContainer container = new CuiElementContainer();
string panelName = $"tradeConfirmationPanel_{target.userID}";
container.Add(new CuiPanel
{
Image = { Color = "0.1 0.1 0.1 0.9" },
RectTransform = { AnchorMin = "0.3 0.3", AnchorMax = "0.7 0.7" },
CursorEnabled = true
}, "Hud.Menu", panelName);
// Add elements to the container for trade confirmation GUI, allowing item transfer
CuiHelper.AddUi(target, container);
tradeOffers[target.userID] = new TradeData
{
Initiator = initiator,
Target = target,
ExpirationTime = DateTime.Now.AddMinutes(5),
PanelName = panelName
};
}
void PerformTrade(BasePlayer player1, BasePlayer player2)
{
TradeData data1 = tradeOffers[player2.userID];
TradeData data2 = tradeRequests[player1.userID];
// Perform trade logic
// Close UI after trade completion
CuiHelper.DestroyUi(player1, data2.PanelName);
CuiHelper.DestroyUi(player2, data1.PanelName);
}
void OnTradeAccept(ulong userID)
{
if (tradeRequests.ContainsKey(userID))
{
BasePlayer initiator = tradeRequests[userID].Initiator;
BasePlayer target = tradeRequests[userID].Target;
OpenTradeConfirmationGUI(initiator, target);
PerformTrade(initiator, target);
tradeRequests.Remove(userID);
}
}
void OnTradeDecline(ulong userID)
{
if (tradeRequests.ContainsKey(userID))
{
BasePlayer target = tradeRequests[userID].Target;
CuiHelper.DestroyUi(target, tradeRequests[userID].PanelName);
tradeRequests.Remove(userID);
}
}
void OnTradeComplete(ulong userID)
{
if (tradeOffers.ContainsKey(userID))
{
CuiHelper.DestroyUi(tradeOffers[userID].Initiator, tradeOffers[userID].PanelName);
tradeOffers.Remove(userID);
}
}
void OnTradeCancel(ulong userID)
{
if (tradeRequests.ContainsKey(userID))
{
BasePlayer initiator = tradeRequests[userID].Initiator;
BasePlayer target = tradeRequests[userID].Target;
CuiHelper.DestroyUi(initiator, tradeRequests[userID].PanelName);
CuiHelper.DestroyUi(target, tradeRequests[userID].PanelName);
tradeRequests.Remove(userID);
}
}
[ChatCommand("sendtrade")]
void SendTradeCommand(BasePlayer player, string command, string[] args)
{
if (args.Length != 1)
{
PrintToChat(player, "Usage: /sendtrade [target]");
return;
}
BasePlayer targetPlayer = FindPlayer(args[0]);
if (targetPlayer == null)
{
PrintToChat(player, "Player not found.");
return;
}
OpenTradeRequestGUI(player, targetPlayer);
}
[ChatCommand("canceltrade")]
void CancelTradeCommand(BasePlayer player, string command, string[] args)
{
if (tradeRequests.ContainsKey(player.userID))
{
OnTradeCancel(player.userID);
PrintToChat(player, "Trade canceled.");
}
else
{
PrintToChat(player, "You don't have an active trade to cancel.");
}
}
[ChatCommand("trade")]
void TradeCommand(BasePlayer player, string command, string[] args)
{
if (args.Length == 1 && args[0] == "accept")
{
OnTradeAccept(player.userID);
PrintToChat(player, "Trade accepted.");
}
}
BasePlayer FindPlayer(string targetName)
{
foreach (BasePlayer player in BasePlayer.activePlayerList)
{
if (player.displayName.ToLower().Contains(targetName.ToLower()))
{
return player;
}
}
return null;
}
// Other methods and functions
}
}
Этот обновленный код включает новую команду
/trade accept
, которую игроки могут использовать для принятия трейда. При вводе этой команды игрок примет предложенный трейд. Пожалуйста, протестируйте данную функциональность и убедитесь, что она работает корректно в вашем плагине.