Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2015
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 46.24 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4.  
  5. using UnityEngine;
  6. using Rust;
  7. using Oxide.Game.Rust.Cui;
  8.  
  9. namespace Oxide.Plugins
  10. {
  11.     [Info("Player Trade Tweaked", "emu/Visagalis", "0.1.2", ResourceId = 1242)]
  12.     class PlayerTrade : RustPlugin
  13.     {
  14.         #region Strings //this is now properly formatted, only the chat commands are missing (line 913)
  15.         public const string s_TradeRequestPlayer = "Trade request sent to {0}";
  16.         public const string s_TradeRequestOther = "{0} wants to trade with you! Type /tra to accept";
  17.         public const string s_TradeRequestDeclined = "{0} declined your trade request!";
  18.         public const string s_TradeRequestNotFound = "Cannot find player named {0}";
  19.         public const string s_TradeRequestPlayerBusy = "You already have a trade request pending! Type /trd to decline";
  20.         public const string s_TradeRequestOtherBusy = "{0} is already trading!";
  21.         public const string s_WrongTradeRequest = "Usage: /trr \"partial or full player name\"";
  22.         public const string s_WrongOffer = "Usage: /tro \"partial or full item name\" amount";
  23.         public const string s_WrongOfferBlueprint = "Usage: /trob \"partial or full blueprint name\"";
  24.         public const string s_TradeOfferFailed = "You don't have {0}";
  25.         public const string s_TradeCanceled = "Trade canceled!";
  26.         public const string s_NoItemFound = "No item found with that name!";
  27.         public const string s_Disconnected = "You disconnected! Trade canceled.";
  28.         public const string s_DisconnectedOther = "Your partner disconnected! Trade canceled.";
  29.         public const string s_Death = "You died! Trade canceled.";
  30.         public const string s_DeathOther = "Your partner died! Trade canceled.";
  31.         public const string s_TookDamage = "You took damage! Trade canceled.";
  32.         public const string s_TookDamageOther = "Your partner took damage! Trade canceled.";
  33.         public const string s_TradeMade = "Trade successful!";
  34.         public const string s_TimeOut = "{0} failed to answer your trade request in time.";
  35.         public const string s_TimeOutOther = "You failed to answer the trade request in time.";
  36.         public const string s_TooFar = "Your partner is too far away!";
  37.         public const string s_RangeGet = "Trading range is {0}";
  38.         public const string s_CooldownGet = "Trade cooldown is {0}";
  39.         public const string s_TradePartner = "You are trading with {0}";
  40.         public const string s_TradeSuccessful = "Transaction complete!";
  41.         public const string s_YouGet = "You get";
  42.         public const string s_YouGive = "You give";
  43.         public const string s_PlayerReady = "You are ready";
  44.         public const string s_OtherReady = "Partner is ready";
  45.         #endregion
  46.  
  47.         #region Config
  48.  
  49.         private const float defaultTradeDistance = 0f;
  50.         private const float defaultTradeCooldown = 0f;
  51.  
  52.         private static float tradeDistance = defaultTradeDistance;
  53.         private static float tradeCooldown = defaultTradeCooldown;
  54.         private static float timeOut = 30f;
  55.  
  56.         protected override void LoadDefaultConfig()
  57.         {
  58.             PrintWarning("Creating a new configuration file.");
  59.             Config.Clear();
  60.             Config["TradeRange"] = defaultTradeDistance;
  61.             Config["TradeCooldown"] = defaultTradeCooldown;
  62.             SaveConfig();
  63.         }
  64.  
  65.         override protected void LoadConfig()
  66.         {
  67.             tradeDistance = GetConfig<float>("TradeRange", defaultTradeDistance);
  68.             tradeCooldown = GetConfig<float>("TradeCooldown", defaultTradeCooldown);
  69.         }
  70.  
  71.         T GetConfig<T>(string key, T defaultValue) {
  72.             try {
  73.                 var val = Config[key];
  74.                 if (val == null)
  75.                     return defaultValue;
  76.                 if (val is List<object>) {
  77.                     var t = typeof(T).GetGenericArguments()[0];
  78.                     if (t == typeof(String)) {
  79.                         var cval = new List<string>();
  80.                         foreach (var v in val as List<object>)
  81.                             cval.Add((string)v);
  82.                         val = cval;
  83.                     } else if (t == typeof(int)) {
  84.                         var cval = new List<int>();
  85.                         foreach (var v in val as List<object>)
  86.                             cval.Add(Convert.ToInt32(v));
  87.                         val = cval;
  88.                     }
  89.                 } else if (val is Dictionary<string, object>) {
  90.                     var t = typeof(T).GetGenericArguments()[1];
  91.                     if (t == typeof(int)) {
  92.                         var cval = new Dictionary<string,int>();
  93.                         foreach (var v in val as Dictionary<string, object>)
  94.                             cval.Add(Convert.ToString(v.Key), Convert.ToInt32(v.Value));
  95.                         val = cval;
  96.                     }
  97.                 }
  98.                 return (T)Convert.ChangeType(val, typeof(T));
  99.             } catch (Exception ex) {
  100.                 return defaultValue;
  101.             }
  102.         }
  103.  
  104.         [ChatCommand("traderange")]
  105.         private void SetRange(BasePlayer player, string command, string[] args)
  106.         {
  107.             if(args.Length == 0)
  108.             {
  109.                 PrintToChat(player, string.Format(s_RangeGet, tradeDistance));
  110.                 return;
  111.             }
  112.  
  113.             if(args.Length != 1 || !player.IsAdmin())
  114.                 return;
  115.  
  116.             float range;
  117.             bool isValid = float.TryParse(args[0], out range);
  118.  
  119.             if(!isValid)
  120.                 return;
  121.  
  122.             Config["TradeRange"] = range;
  123.             SaveConfig();
  124.             LoadConfig();
  125.  
  126.             PrintToChat(player, "Trade range set to " + range);
  127.         }
  128.  
  129.         [ChatCommand("tradecooldown")]
  130.         private void SetLimit(BasePlayer player, string command, string[] args)
  131.         {
  132.             if(args.Length == 0)
  133.             {
  134.                 PrintToChat(player, string.Format(s_CooldownGet, tradeCooldown));
  135.                 return;
  136.             }
  137.  
  138.             if(args.Length != 1 || !player.IsAdmin())
  139.                 return;
  140.  
  141.             float cooldown;
  142.             bool isValid = float.TryParse(args[0], out cooldown);
  143.  
  144.             if(!isValid)
  145.                 return;
  146.  
  147.             Config["TradeCooldown"] = cooldown;
  148.             SaveConfig();
  149.             LoadConfig();
  150.  
  151.             PrintToChat(player, "Trade cooldown set to " + cooldown);
  152.         }
  153.  
  154.         #endregion
  155.  
  156.  
  157.         #region Oxide Hooks
  158.  
  159.         private void OnPlayerInit(BasePlayer player)
  160.         {
  161.             AddTrader(player);
  162.         }
  163.  
  164.         private void OnServerInitialized()
  165.         {
  166.             LoadConfig();
  167.             initGUI();
  168.  
  169.  
  170.             for (int i = 0; i < BasePlayer.activePlayerList.Count; i++)
  171.             {
  172.                 AddTrader(BasePlayer.activePlayerList[i]);
  173.             }
  174.         }
  175.  
  176.         private void AddTrader(BasePlayer player)
  177.         {
  178.             if(player.GetComponent<Trader>() == null)
  179.                 player.gameObject.AddComponent<Trader>();
  180.         }
  181.  
  182.         private void OnItemRemovedFromContainer(ItemContainer container, Item item)
  183.         {
  184.             Trader trader;
  185.             if(container.playerOwner != null)
  186.             {
  187.                 trader = Trader.GetTrader(container.playerOwner);
  188.                 if(trader != null)
  189.                 {
  190.                     trader.CheckOfferedItems();
  191.                 }
  192.             }
  193.         }
  194.  
  195.         private void OnPlayerDisconnected(BasePlayer player)
  196.         {
  197.             CancelTrade(player, s_Disconnected, s_DisconnectedOther);
  198.         }
  199.  
  200.  
  201.         private void OnEntityDeath(BaseCombatEntity entity, HitInfo hitInfo)
  202.         {
  203.             CancelTrade(entity as BasePlayer, s_Death, s_DeathOther);
  204.         }
  205.  
  206.  
  207.         private void OnEntityTakeDamage(BaseCombatEntity entity, HitInfo hitInfo)
  208.         {
  209.             CancelTrade(entity as BasePlayer, s_TookDamage, s_TookDamageOther);
  210.         }
  211.  
  212.         #endregion
  213.  
  214.         private void CancelTrade(BasePlayer player, string message, string partnerMessage)
  215.         {
  216.             if(player == null)
  217.                 return;
  218.  
  219.             Trader trader = player.GetComponent<Trader>();
  220.             TradeSession session;
  221.  
  222.             if(trader != null)
  223.             {
  224.                 session = trader.GetTradeSession();
  225.                 if(session != null)
  226.                 {
  227.                     PrintToChat(player, message);
  228.                     PrintToChat(trader.GetOther().GetPlayer(), partnerMessage);
  229.                     session.CloseSession();
  230.                 }
  231.             }
  232.         }
  233.  
  234.         #region Commands
  235.  
  236.         [ChatCommand("tra")]
  237.         private void AcceptTradeRequest(BasePlayer player, string command, string[] args)
  238.         {
  239.             Trader trader = player.GetComponent<Trader>();
  240.  
  241.             if (trader == null)
  242.                 return;
  243.  
  244.             TradeSession session = trader.GetTradeSession();
  245.  
  246.             if(session != null)
  247.                 session.AcceptRequest();
  248.         }
  249.  
  250.         [ChatCommand("tro")]
  251.         private void OfferItem(BasePlayer player, string command, string[] args)
  252.         {
  253.             Trader trader = player.GetComponent<Trader>();
  254.  
  255.             if(trader == null)
  256.                 return;
  257.  
  258.             TradeSession session = trader.GetTradeSession();
  259.  
  260.             if(session == null || !session.IsAccepted())
  261.                 return;
  262.  
  263.             TradeItem tradeItem;
  264.             int amount;
  265.             bool isAmountValid;
  266.             ItemDefinition itemDef;
  267.  
  268.             if(args.Length == 1 || args.Length == 2)
  269.             {
  270.                 itemDef = FindItemByDisplayName(args[0]);
  271.  
  272.                 if(args.Length == 2)
  273.                     isAmountValid = int.TryParse(args[1], out amount);
  274.                 else
  275.                 {
  276.                     isAmountValid = true;
  277.                     amount = 1;
  278.                 }
  279.  
  280.                 if(itemDef != null && isAmountValid)
  281.                 {
  282.                     tradeItem = new TradeItem(itemDef, amount);
  283.                     trader.OfferItem(tradeItem);
  284.                     return;
  285.                 }
  286.                 else if(itemDef == null)
  287.                 {
  288.                     PrintToChat(player, s_NoItemFound);
  289.                     return;
  290.                 }
  291.             }
  292.  
  293.             PrintToChat(player, s_WrongOffer);
  294.         }
  295.  
  296.  
  297.         [ChatCommand("tror")]
  298.         private void RemoveOffer(BasePlayer player, string command, string[] args)
  299.         {
  300.             Trader trader = Trader.GetTrader(player);
  301.  
  302.             if(trader == null)
  303.                 return;
  304.  
  305.             TradeSession session = trader.GetTradeSession();
  306.  
  307.             if(session == null)
  308.                 return;
  309.  
  310.             trader.RemoveLastOffer();
  311.         }
  312.  
  313.         [ChatCommand("trob")]
  314.         private void OfferBlueprint(BasePlayer player, string command, string[] args)
  315.         {
  316.             Trader trader = player.GetComponent<Trader>();
  317.  
  318.             if(trader == null)
  319.                 return;
  320.  
  321.             TradeSession session = trader.GetTradeSession();
  322.  
  323.             if(session == null || !session.IsAccepted())
  324.                 return;
  325.  
  326.             TradeItem tradeItem;
  327.             ItemDefinition itemDef;
  328.  
  329.             if(args.Length == 1)
  330.             {
  331.                 itemDef = FindItemByDisplayName(args[0]);
  332.  
  333.                 if(itemDef != null)
  334.                 {
  335.                     tradeItem = new TradeItem(itemDef, 1, true);
  336.                     trader.OfferItem(tradeItem);
  337.                 }
  338.                 else
  339.                 {
  340.                     PrintToChat(player, s_NoItemFound);
  341.                 }
  342.  
  343.                 return;
  344.             }
  345.  
  346.             PrintToChat(player, s_WrongOfferBlueprint);
  347.         }
  348.  
  349.         [ChatCommand("trd")]
  350.         private void DeclineTrade(BasePlayer player, string command, string[] args)
  351.         {
  352.             Trader trader = player.GetComponent<Trader>();
  353.             Trader other;
  354.  
  355.             if(trader == null)
  356.                 return;
  357.  
  358.             TradeSession session = trader.GetTradeSession();
  359.  
  360.             if(session != null)
  361.             {
  362.                 other = trader.GetOther();
  363.                 session.CloseSession();
  364.  
  365.                 if(other != null)
  366.                     PrintToChat(other.GetPlayer(), string.Format(s_TradeRequestDeclined, player.displayName));
  367.             }
  368.         }
  369.  
  370.         [ChatCommand("trl")]
  371.         private void LockTradeOffer(BasePlayer player, string command, string[] args)
  372.         {
  373.             Trader trader = player.GetComponent<Trader>();
  374.  
  375.             if(trader != null && trader.GetTradeSession() != null)
  376.                 trader.AcceptTrade();
  377.         }
  378.  
  379.         [ChatCommand("trr")]
  380.         private void SendTradeRequest(BasePlayer player, string command, string[] args)
  381.         {
  382.             Trader trader = player.GetComponent<Trader>();
  383.             BasePlayer partner = null;
  384.  
  385.             if(args.Length == 1)
  386.                 partner = GetPlayerByName(args[0]);
  387.             else
  388.             {
  389.                 PrintToChat(player, s_WrongTradeRequest);
  390.                 return;
  391.             }
  392.  
  393.             if(trader != null)
  394.             {
  395.                 if(partner == null || partner == player)
  396.                     PrintToChat(player, string.Format(s_TradeRequestNotFound, args[0]));
  397.                 else
  398.                 {
  399.                     if (player.HasPlayerFlag(BasePlayer.PlayerFlags.InBuildingPrivilege)
  400.                         || player.HasPlayerFlag(BasePlayer.PlayerFlags.HasBuildingPrivilege))
  401.                     {
  402.                         PrintToChat(player, "You can't trade because, you are in range of [Tool Cupboard]!");
  403.                         return;
  404.                     }
  405.  
  406.                     if (partner.HasPlayerFlag(BasePlayer.PlayerFlags.InBuildingPrivilege)
  407.                         || partner.HasPlayerFlag(BasePlayer.PlayerFlags.HasBuildingPrivilege))
  408.                     {
  409.                         PrintToChat(player, "You can't trade because, your partner is in range of [Tool Cupboard]!");
  410.                         PrintToChat(partner, player.displayName + " tried to trade with you, but couldn't because you are in range of [Tool Cupboard]!");
  411.                         return;
  412.                     }
  413.                     trader.RequestTrade(partner);
  414.                 }
  415.             }
  416.         }
  417.  
  418.         #endregion
  419.  
  420.         class Trader : MonoBehaviour
  421.         {
  422.             private BasePlayer player;
  423.             private TradeSession currentTradeSession;
  424.  
  425.             private float timeOfRequest;
  426.             private float timeOfLastTrade;
  427.  
  428.             private List<TradeItem> offeredItems = new List<TradeItem>();
  429.             private bool tradeAccepted;
  430.  
  431.             void Awake()
  432.             {
  433.                 player = GetComponent<BasePlayer>();
  434.  
  435.                 if(player == null)
  436.                 {
  437.                     Debug.LogError("Trader is not a BasePlayer");
  438.                     GameObject.Destroy(this);
  439.                 }
  440.             }
  441.  
  442.             void Update()
  443.             {
  444.                 if(currentTradeSession != null)
  445.                 {
  446.                     if(!IsCloseEnoughTo(GetOther()))
  447.                     {
  448.                         player.ChatMessage(s_TooFar);
  449.                         GetOther().GetPlayer().ChatMessage(s_TooFar);
  450.                         currentTradeSession.CloseSession();
  451.                     }
  452.  
  453.                     if(!currentTradeSession.IsAccepted() && Time.time - timeOfRequest > timeOut)
  454.                     {
  455.                         currentTradeSession.TimeOut();
  456.                     }
  457.                 }
  458.             }
  459.  
  460.             public BasePlayer GetPlayer()
  461.             {
  462.                 return player;
  463.             }
  464.  
  465.             public TradeSession GetTradeSession()
  466.             {
  467.                 return currentTradeSession;
  468.             }
  469.  
  470.             public List<TradeItem> GetOfferedItems()
  471.             {
  472.                 return offeredItems;
  473.             }
  474.  
  475.             public void SetTradeSession(TradeSession session)
  476.             {
  477.                 currentTradeSession = session;
  478.             }
  479.  
  480.             public bool IsCloseEnoughTo(Trader other)
  481.             {
  482.                 if(tradeDistance <= 0)
  483.                     return true;
  484.  
  485.                 Vector3 playerPos = player.transform.position;
  486.                 Vector3 otherPos = other.transform.position;
  487.  
  488.                 if(Vector3.Distance(playerPos, otherPos) <= tradeDistance)
  489.                     return true;
  490.                 else
  491.                     return false;
  492.             }
  493.  
  494.             public void RequestTrade(Trader partner)
  495.             {
  496.                 TradeSession newTrade;
  497.                 float lastTradeTimePassed = Time.time - partner.GetTimeOfLastTrade();
  498.                 bool abort = false;
  499.  
  500.                 if(lastTradeTimePassed < tradeCooldown)
  501.                 {
  502.                     player.ChatMessage(partner.GetPlayer().displayName + " cannot trade for " + (int)(tradeCooldown - lastTradeTimePassed) + " seconds");
  503.                     abort = true;
  504.                 }
  505.  
  506.                 lastTradeTimePassed = Time.time - timeOfLastTrade;
  507.                 if(lastTradeTimePassed < tradeCooldown)
  508.                 {
  509.                     player.ChatMessage("You cannot trade for " + (int)(tradeCooldown - lastTradeTimePassed) + " seconds");
  510.                     abort = true;
  511.                 }
  512.  
  513.                 if(abort)
  514.                     return;
  515.  
  516.                 if(this.GetTradeSession() != null)
  517.                 {
  518.                     player.ChatMessage(s_TradeRequestPlayerBusy);
  519.                     return;
  520.                 }
  521.  
  522.                 if(partner.GetTradeSession() != null)
  523.                 {
  524.                     player.ChatMessage(string.Format(s_TradeRequestOtherBusy, partner.GetPlayer().displayName));
  525.                     return;
  526.                 }
  527.  
  528.                 partner.GetPlayer().ChatMessage(string.Format(s_TradeRequestOther, player.displayName));
  529.                 player.ChatMessage(string.Format(s_TradeRequestPlayer, partner.GetPlayer().displayName));
  530.  
  531.                 timeOfRequest = Time.time;
  532.                 partner.SetTimeOfRequest(Time.time);
  533.                 newTrade = new TradeSession(this, partner);
  534.                 this.SetTradeSession(newTrade);
  535.                 partner.SetTradeSession(newTrade);
  536.             }
  537.  
  538.             public void SetTimeOfRequest(float time)
  539.             {
  540.                 timeOfRequest = time;
  541.             }
  542.  
  543.             public void SetTimeOfLastTrade(float time)
  544.             {
  545.                 timeOfLastTrade = time;
  546.             }
  547.  
  548.             public float GetTimeOfLastTrade()
  549.             {
  550.                 return timeOfLastTrade;
  551.             }
  552.  
  553.             public void RequestTrade(BasePlayer partner)
  554.             {
  555.                 Trader partnerTrader = partner.GetComponent<Trader>();
  556.  
  557.                 if(partnerTrader != null)
  558.                     this.RequestTrade(partnerTrader);
  559.             }
  560.  
  561.             public void ClearTradeSession()
  562.             {
  563.                 tradeAccepted = false;
  564.                 offeredItems.Clear();
  565.                 currentTradeSession = null;
  566.                 DestroyTradeGUI();
  567.             }
  568.  
  569.             public static Trader GetTrader(BasePlayer player)
  570.             {
  571.                 if(player == null)
  572.                     return null;
  573.  
  574.                 return player.GetComponent<Trader>();
  575.             }
  576.  
  577.             public bool GetTradeAccepted()
  578.             {
  579.                 return tradeAccepted;
  580.             }
  581.  
  582.             private void OnItemOffered()
  583.             {
  584.                 tradeAccepted = false;
  585.                 CuiHelper.DestroyUi(player, "PlayerAccepted");
  586.                 CuiHelper.DestroyUi(player, "OtherAccepted");
  587.             }
  588.  
  589.             public void AcceptTrade()
  590.             {
  591.                 if(tradeAccepted)
  592.                     return;
  593.  
  594.                 Trader other = GetOther();
  595.  
  596.                 tradeAccepted = true;
  597.                 CuiElementContainer playerAcceptIndicatorContainer = new CuiElementContainer();
  598.                 playerAcceptIndicatorContainer.Add(playerAcceptIndicatorGUI);
  599.                 CuiHelper.AddUi(player, playerAcceptIndicatorContainer);
  600.                 CuiElementContainer otherAcceptIndicatorContainer = new CuiElementContainer();
  601.                 otherAcceptIndicatorContainer.Add(otherAcceptIndicatorGUI);
  602.                 CuiHelper.AddUi(other.GetPlayer(), otherAcceptIndicatorContainer);
  603.  
  604.                 if (tradeAccepted && other.GetTradeAccepted())
  605.                     currentTradeSession.MakeTrade();
  606.             }
  607.  
  608.             public void OfferItem(TradeItem item)
  609.             {
  610.                 if(this.HasItem(item))
  611.                     offeredItems.Add(item);
  612.                 else
  613.                 {
  614.                     player.ChatMessage(string.Format(s_TradeOfferFailed, item.GetItemDef().displayName.english + " " + item.GetAmount() + "x"));
  615.                     return;
  616.                 }
  617.  
  618.                 currentTradeSession.GetPartner().OnItemOffered();
  619.                 currentTradeSession.GetInitiator().OnItemOffered();
  620.  
  621.                 UpdateOfferGUI();
  622.             }
  623.  
  624.             public void RemoveLastOffer()
  625.             {
  626.                 if(currentTradeSession == null)
  627.                     return;
  628.  
  629.                 if(offeredItems.Count > 0)
  630.                     offeredItems.Remove(offeredItems[offeredItems.Count-1]);
  631.  
  632.                 currentTradeSession.GetPartner().OnItemOffered();
  633.                 currentTradeSession.GetInitiator().OnItemOffered();
  634.                 UpdateOfferGUI();
  635.             }
  636.  
  637.             private bool HasItem(TradeItem item)
  638.             {
  639.                 if(item.IsBlueprint())
  640.                 {
  641.                     Item[] items = player.inventory.AllItems();
  642.  
  643.                     for(int i = 0; i < items.Length; i++)
  644.                     {
  645.                         if(items[i].info.itemid == item.GetItemDef().itemid && items[i].IsBlueprint())
  646.                             return true;
  647.                     }
  648.                 }
  649.                 else
  650.                 {
  651.                     int hasAmount;
  652.                     int tradedAmount = 0;
  653.                     hasAmount = player.inventory.GetAmount(item.GetItemDef().itemid);
  654.  
  655.                     foreach(TradeItem offered in offeredItems)
  656.                     {
  657.                         if(offered.GetItemDef().itemid == item.GetItemDef().itemid)
  658.                             tradedAmount += offered.GetAmount();
  659.                     }
  660.  
  661.                     if(hasAmount - tradedAmount >= item.GetAmount())
  662.                         return true;
  663.                 }
  664.                 return false;
  665.             }
  666.  
  667.             public void CheckOfferedItems()
  668.             {
  669.                 if(currentTradeSession == null)
  670.                     return;
  671.  
  672.                 if(currentTradeSession.IsMakingTrade())
  673.                     return;
  674.  
  675.                 List<TradeItem> offersToRemove = new List<TradeItem>();
  676.  
  677.                 foreach(TradeItem item in offeredItems)
  678.                 {
  679.                     if(!HasItem(item))
  680.                         offersToRemove.Add(item);
  681.                 }
  682.  
  683.                 foreach(TradeItem item in offersToRemove)
  684.                 {
  685.                     offeredItems.Remove(item);
  686.                 }
  687.  
  688.                 UpdateOfferGUI();
  689.             }
  690.  
  691.             private void UpdateOfferGUI()
  692.             {
  693.                 BasePlayer other = GetOther().GetPlayer();
  694.                 string s_OfferedItems = TradeItem.ListToString(offeredItems);
  695.  
  696.                 playerOfferIndicatorText.Text = otherOfferIndicatorText.Text = s_OfferedItems;
  697.  
  698.                 CuiHelper.DestroyUi(player, "PlayerOfferList");
  699.                 CuiHelper.DestroyUi(other, "OtherOfferList");
  700.  
  701.                 CuiElementContainer playerOfferIndicatorContainer = new CuiElementContainer();
  702.                 playerOfferIndicatorContainer.Add(playerOfferIndicatorGUI);
  703.                 CuiElementContainer otherOfferIndicatorContainer = new CuiElementContainer();
  704.                 otherOfferIndicatorContainer.Add(otherOfferIndicatorGUI);
  705.                 CuiHelper.AddUi(player, playerOfferIndicatorContainer);
  706.                 CuiHelper.AddUi(other, otherOfferIndicatorContainer);
  707.             }
  708.  
  709.             public Trader GetOther()
  710.             {
  711.                 if(currentTradeSession != null)
  712.                 {
  713.                     if(this.IsInitiator())
  714.                         return currentTradeSession.GetPartner();
  715.                     else
  716.                         return currentTradeSession.GetInitiator();
  717.                 }
  718.  
  719.                 return null;
  720.             }
  721.  
  722.             public bool IsInitiator()
  723.             {
  724.                 if(currentTradeSession != null)
  725.                 {
  726.                     if(currentTradeSession.GetInitiator() == this)
  727.                         return true;
  728.                 }
  729.  
  730.                 return false;
  731.             }
  732.  
  733.             public void CreateTradeGUI()
  734.             {
  735.                 CuiElementContainer baseTradeGUI = new CuiElementContainer();
  736.                 baseTradeGUI.Add(partnerLabelPanelGUI);
  737.                 baseTradeGUI.Add(infoPanelGUI);
  738.                 baseTradeGUI.Add(infoLabelGUI);
  739.                 baseTradeGUI.Add(tradePanelGUI);
  740.                 baseTradeGUI.Add(playerPanelGUI);
  741.                 baseTradeGUI.Add(otherPanelGUI);
  742.                 baseTradeGUI.Add(playerOfferLabelGUI);
  743.                 baseTradeGUI.Add(otherOfferLabelGUI);
  744.                 CuiPanel buttonsPanel = new CuiPanel { Image = { Color = "0 0 0 0" }, RectTransform = { AnchorMin = "0.75 0.8", AnchorMax = "0.85 0.88" } };
  745.                 CuiButton cancel = new CuiButton
  746.                 {
  747.                     Button =
  748.                     {
  749.                         Command = "tradeCancel",
  750.                         Close = "InfoPanel",
  751.                         Color = "1.0 0.0 0.0 1.0"
  752.                     },
  753.                     RectTransform =
  754.                     {
  755.                         AnchorMin = "0 0",
  756.                         AnchorMax = "1 0.4",
  757.                     },
  758.                     Text =
  759.                     {
  760.                         Text = "Decline",
  761.                         FontSize = 16,
  762.                         Align = TextAnchor.MiddleCenter
  763.                     }
  764.                 };
  765.                 CuiButton accept = new CuiButton
  766.                 {
  767.                     Button =
  768.                     {
  769.                         Command = "tradeAccept",
  770.                         Close = "InfoPanel",
  771.                         Color = "0.2 0.8 0.2 1.0"
  772.                     },
  773.                     RectTransform =
  774.                     {
  775.                         AnchorMin = "0 0.6",
  776.                         AnchorMax = "1 1",
  777.                     },
  778.                     Text =
  779.                     {
  780.                         Text = "Accept",
  781.                         FontSize = 16,
  782.                         Align = TextAnchor.MiddleCenter
  783.                     }
  784.                 };
  785.                 baseTradeGUI.Add(buttonsPanel, "HUD/Overlay", "buttonPanel");
  786.                 baseTradeGUI.Add(accept, "buttonPanel", "buttonAccept");
  787.                 baseTradeGUI.Add(cancel, "buttonPanel", "buttonDecline");
  788.                 CuiElementContainer partnerIndicatorContainer = new CuiElementContainer();
  789.                 partnerIndicatorText.Text = string.Format(s_TradePartner, GetOther().GetPlayer().displayName);
  790.                 partnerIndicatorContainer.Add(partnerIndicatorGUI);
  791.                 CuiHelper.AddUi(player, baseTradeGUI);
  792.                 CuiHelper.AddUi(player, partnerIndicatorContainer);
  793.             }
  794.  
  795.             public void DestroyTradeGUI()
  796.             {
  797.                 CuiHelper.DestroyUi(player, "TradePanel");
  798.                 CuiHelper.DestroyUi(player, "PlayerPanel");
  799.                 CuiHelper.DestroyUi(player, "OtherPanel");
  800.                 CuiHelper.DestroyUi(player, "InfoPanel");
  801.                 CuiHelper.DestroyUi(player, "InfoLabel");
  802.                 CuiHelper.DestroyUi(player, "PlayerAccepted");
  803.                 CuiHelper.DestroyUi(player, "OtherAccepted");
  804.                 CuiHelper.DestroyUi(player, "PlayerOfferLabel");
  805.                 CuiHelper.DestroyUi(player, "OtherOfferLabel");
  806.                 CuiHelper.DestroyUi(player, "PlayerOfferList");
  807.                 CuiHelper.DestroyUi(player, "OtherOfferList");
  808.                 CuiHelper.DestroyUi(player, "PartnerLabelPanel");
  809.                 CuiHelper.DestroyUi(player, "PartnerLabel");
  810.                 CuiHelper.DestroyUi(player, "buttonPanel");
  811.                 CuiHelper.DestroyUi(player, "buttonAccept");
  812.                 CuiHelper.DestroyUi(player, "buttonDecline");
  813.             }
  814.         }
  815.  
  816.         #region JSON
  817.  
  818.         public static void initGUI()
  819.         {
  820.             otherOfferIndicatorGUI.Components.Add(otherOfferIndicatorText);
  821.             otherOfferIndicatorGUI.Components.Add(otherOfferIndicatorRect);
  822.             partnerIndicatorGUI.Components.Add(partnerIndicatorText);
  823.             partnerIndicatorGUI.Components.Add(partnerIndicatorRect);
  824.             playerAcceptIndicatorText.Text = s_PlayerReady;
  825.             playerAcceptIndicatorGUI.Components.Add(playerAcceptIndicatorText);
  826.             playerAcceptIndicatorGUI.Components.Add(playerAcceptIndicatorRect);
  827.             otherAcceptIndicatorText.Text = s_OtherReady;
  828.             otherAcceptIndicatorGUI.Components.Add(otherAcceptIndicatorText);
  829.             otherAcceptIndicatorGUI.Components.Add(otherAcceptIndicatorRect);
  830.             playerOfferIndicatorGUI.Components.Add(playerOfferIndicatorText);
  831.             playerOfferIndicatorGUI.Components.Add(playerOfferIndicatorRect);
  832.             partnerLabelPanelGUI.Components.Add(partnerLabelPanelImage);
  833.             partnerLabelPanelGUI.Components.Add(partnerLabelPanelRect);
  834.             infoPanelGUI.Components.Add(infoPanelImage);
  835.             infoPanelGUI.Components.Add(infoPanelRect);
  836.             infoLabelGUI.Components.Add(infoLabelText);
  837.             infoLabelGUI.Components.Add(infoLabelRect);
  838.             tradePanelGUI.Components.Add(tradePanelImage);
  839.             tradePanelGUI.Components.Add(tradePanelRect);
  840.             playerPanelGUI.Components.Add(playerPanelImage);
  841.             playerPanelGUI.Components.Add(playerPanelRect);
  842.             otherPanelGUI.Components.Add(otherPanelImage);
  843.             otherPanelGUI.Components.Add(otherPanelRect);
  844.             playerOfferLabelText.Text = s_YouGive;
  845.             playerOfferLabelGUI.Components.Add(playerOfferLabelText);
  846.             playerOfferLabelGUI.Components.Add(playerOfferLabelRect);
  847.             otherOfferLabelText.Text = s_YouGet;
  848.             otherOfferLabelGUI.Components.Add(otherOfferLabelText);
  849.             otherOfferLabelGUI.Components.Add(otherOfferLabelRect);
  850.         }
  851.  
  852.  
  853.         public static CuiElement partnerIndicatorGUI = new CuiElement { Parent = "PartnerLabelPanel", Name = "PartnerLabel", FadeOut = 0.5f };
  854.         public static CuiTextComponent partnerIndicatorText = new CuiTextComponent { Align = TextAnchor.MiddleCenter, FadeIn = 0.5f, FontSize = 22 };
  855.         public static CuiRectTransformComponent partnerIndicatorRect = new CuiRectTransformComponent { AnchorMin = "0 0", AnchorMax = "1 1" };
  856.  
  857.         public static CuiElement otherOfferIndicatorGUI = new CuiElement { Name = "OtherOfferList", FadeOut = 0.5f, Parent = "OtherPanel" };
  858.         public static CuiTextComponent otherOfferIndicatorText = new CuiTextComponent { Align = TextAnchor.UpperCenter, Color = "1.0 0.4 0.0 1.0", FadeIn = 0.5f, FontSize = 20 };
  859.         public static CuiRectTransformComponent otherOfferIndicatorRect = new CuiRectTransformComponent { AnchorMin = "0 0.1", AnchorMax = "1 0.9" };
  860.  
  861.         public static CuiElement playerAcceptIndicatorGUI = new CuiElement { Name = "PlayerAccepted", FadeOut = 0.5f, Parent = "PlayerPanel" };
  862.         public static CuiTextComponent playerAcceptIndicatorText = new CuiTextComponent { Align = TextAnchor.LowerCenter, Color = "0.1 1.0 0.1 1.0", FadeIn = 0.5f, FontSize = 24 };
  863.         public static CuiRectTransformComponent playerAcceptIndicatorRect = new CuiRectTransformComponent { AnchorMin = "0 0.02", AnchorMax = "1 0.2" };
  864.        
  865.         public static CuiElement otherAcceptIndicatorGUI = new CuiElement { Name = "OtherAccepted", FadeOut = 0.5f, Parent = "OtherPanel" };
  866.         public static CuiTextComponent otherAcceptIndicatorText = new CuiTextComponent { Align = TextAnchor.LowerCenter, Color = "0.1 1.0 0.1 1.0", FadeIn = 0.5f, FontSize = 24 };
  867.         public static CuiRectTransformComponent otherAcceptIndicatorRect = new CuiRectTransformComponent { AnchorMin = "0 0.02", AnchorMax = "1 0.2" };
  868.  
  869.         public static CuiElement playerOfferIndicatorGUI = new CuiElement { Name = "PlayerOfferList", FadeOut = 0.5f, Parent = "PlayerPanel" };
  870.         public static CuiTextComponent playerOfferIndicatorText = new CuiTextComponent { Align = TextAnchor.UpperCenter, Color = "1.0 0.4 0.0 1.0", FadeIn = 0.5f, FontSize = 20 };
  871.         public static CuiRectTransformComponent playerOfferIndicatorRect = new CuiRectTransformComponent { AnchorMin = "0 0.1", AnchorMax = "1 0.9" };
  872.  
  873.         private static CuiElement partnerLabelPanelGUI = new CuiElement { Name = "PartnerLabelPanel", Parent = "HUD/Overlay", FadeOut = 0.5f };
  874.         private static CuiImageComponent partnerLabelPanelImage = new CuiImageComponent { FadeIn = 0.5f, Color = "0.5 0.5 0.5 0.5" };
  875.         private static CuiRectTransformComponent partnerLabelPanelRect = new CuiRectTransformComponent { AnchorMin = "0.35 0.92", AnchorMax = "0.75 0.98" };
  876.  
  877.         private static CuiElement infoPanelGUI = new CuiElement { Name = "InfoPanel", Parent = "HUD/Overlay", FadeOut = 0.5f };
  878.         private static CuiImageComponent infoPanelImage = new CuiImageComponent { FadeIn = 0.5f, Color = "0.5 0.5 0.5 0.5" };
  879.         private static CuiRectTransformComponent infoPanelRect = new CuiRectTransformComponent { AnchorMin = "0.78 0.3", AnchorMax = "0.98 0.5" };
  880.  
  881.         /* TODO
  882.             Remove last offer: /tror
  883.             Lock/Accept trade offer: /trl
  884.             Cancel trade: /trd"",
  885.         */
  886.  
  887.         private static CuiElement infoLabelGUI = new CuiElement { Name = "InfoLabel", FadeOut = 0.5f, Parent = "InfoPanel" };
  888.         private static CuiTextComponent infoLabelText = new CuiTextComponent { Align = TextAnchor.MiddleCenter, FadeIn = 0.5f, FontSize = 16, Text = "Chat commands:\nOffer items: /tro ''item name'' amount\nOffer blueprint: /trob ''item name''" };
  889.         private static CuiRectTransformComponent infoLabelRect = new CuiRectTransformComponent { AnchorMin = "0 0", AnchorMax = "1 1" };
  890.  
  891.         private static CuiElement tradePanelGUI = new CuiElement { Name = "TradePanel", Parent = "HUD/Overlay", FadeOut = 0.5f };
  892.         private static CuiImageComponent tradePanelImage = new CuiImageComponent { FadeIn = 0.5f, Color = "0.5 0.5 0.5 0.5" };
  893.         private static CuiRectTransformComponent tradePanelRect = new CuiRectTransformComponent { AnchorMin = "0.35 0.15", AnchorMax = "0.75 0.9" };
  894.  
  895.         private static CuiElement playerPanelGUI = new CuiElement { Name = "PlayerPanel", Parent = "TradePanel", FadeOut = 0.5f };
  896.         private static CuiImageComponent playerPanelImage = new CuiImageComponent { FadeIn = 0.5f, Color = "0.5 0.5 0.5 0.5" };
  897.         private static CuiRectTransformComponent playerPanelRect = new CuiRectTransformComponent { AnchorMin = "0.05 0.05", AnchorMax = "0.475 0.95" };
  898.  
  899.         private static CuiElement otherPanelGUI = new CuiElement { Name = "OtherPanel", Parent = "TradePanel", FadeOut = 0.5f };
  900.         private static CuiImageComponent otherPanelImage = new CuiImageComponent { FadeIn = 0.5f, Color = "0.5 0.5 0.5 0.5" };
  901.         private static CuiRectTransformComponent otherPanelRect = new CuiRectTransformComponent { AnchorMin = "0.525 0.05", AnchorMax = "0.95 0.95" };
  902.  
  903.         private static CuiElement playerOfferLabelGUI = new CuiElement { Name = "PlayerOfferLabel", FadeOut = 0.5f, Parent = "PlayerPanel" };
  904.         private static CuiTextComponent playerOfferLabelText = new CuiTextComponent { Align = TextAnchor.UpperCenter, FadeIn = 0.5f, FontSize = 24 };
  905.         private static CuiRectTransformComponent playerOfferLabelRect = new CuiRectTransformComponent { AnchorMin = "0 0.9", AnchorMax = "1 1" };
  906.  
  907.         private static CuiElement otherOfferLabelGUI = new CuiElement { Name = "OtherOfferLabel", FadeOut = 0.5f, Parent = "OtherPanel" };
  908.         private static CuiTextComponent otherOfferLabelText = new CuiTextComponent { Align = TextAnchor.UpperCenter, FadeIn = 0.5f, FontSize = 24 };
  909.         private static CuiRectTransformComponent otherOfferLabelRect = new CuiRectTransformComponent { AnchorMin = "0 0.9", AnchorMax = "1 1" };
  910.         #endregion
  911.  
  912.         class TradeSession
  913.         {
  914.             private Trader initiator;
  915.             private Trader partner;
  916.  
  917.             private bool requestAccepted;
  918.             private bool makingTrade;
  919.  
  920.             public TradeSession(Trader initiator, Trader partner)
  921.             {
  922.                 this.initiator = initiator;
  923.                 this.partner = partner;
  924.             }
  925.  
  926.             public void TimeOut()
  927.             {
  928.                 initiator.GetPlayer().ChatMessage(string.Format(s_TimeOut, partner.GetPlayer().displayName));
  929.                 partner.GetPlayer().ChatMessage(s_TimeOutOther);
  930.                 CloseSession();
  931.             }
  932.  
  933.  
  934.  
  935.             public void AcceptRequest()
  936.             {
  937.                 if(requestAccepted)
  938.                     return;
  939.  
  940.                 requestAccepted = true;
  941.                 initiator.CreateTradeGUI();
  942.                 partner.CreateTradeGUI();
  943.             }
  944.  
  945.             public void CloseSession()
  946.             {
  947.                 initiator.ClearTradeSession();
  948.                 partner.ClearTradeSession();
  949.             }
  950.  
  951.             public bool IsMakingTrade()
  952.             {
  953.                 return makingTrade;
  954.             }
  955.  
  956.             public bool IsAccepted()
  957.             {
  958.                 return requestAccepted;
  959.             }
  960.  
  961.             public void MakeTrade()
  962.             {
  963.                 makingTrade = true;
  964.  
  965.                 List<TradeItem> initiatorOffered = initiator.GetOfferedItems();
  966.                 List<TradeItem> partnerOffered = partner.GetOfferedItems();
  967.  
  968.                 TransferOffered(initiator, partner);
  969.                 TransferOffered(partner, initiator);
  970.  
  971.                 if(initiatorOffered.Count > 0 || partnerOffered.Count > 0)
  972.                 {
  973.                     initiator.SetTimeOfLastTrade(Time.time);
  974.                     partner.SetTimeOfLastTrade(Time.time);
  975.  
  976.                     initiator.GetPlayer().ChatMessage(s_TradeSuccessful);
  977.                     partner.GetPlayer().ChatMessage(s_TradeSuccessful);
  978.                 }
  979.  
  980.                 CloseSession();
  981.                 makingTrade = false;
  982.             }
  983.  
  984.             public static void TransferOffered(Trader from, Trader to)
  985.             {
  986.                 int amountLeft;
  987.                 Item itemToGive;
  988.                 PlayerInventory fromInventory = from.GetPlayer().inventory;
  989.                 PlayerInventory toInventory = to.GetPlayer().inventory;
  990.  
  991.                 List<TradeItem> offered = from.GetOfferedItems();
  992.  
  993.                 Item[] offeredItems;
  994.  
  995.                 foreach(TradeItem item in offered)
  996.                 {
  997.                     itemToGive = null;
  998.                     amountLeft = item.GetAmount();
  999.                     offeredItems = fromInventory.AllItems();
  1000.  
  1001.                     for(int i = 0; i < offeredItems.Length; i++)
  1002.                     {
  1003.                         if(amountLeft <= 0)
  1004.                             break;
  1005.  
  1006.                         if(offeredItems[i].info.itemid == item.GetItemDef().itemid && item.IsBlueprint() == offeredItems[i].IsBlueprint())
  1007.                         {
  1008.                             itemToGive = offeredItems[i];
  1009.  
  1010.                             if(!item.IsBlueprint())
  1011.                             {
  1012.                                 if(offeredItems[i].amount > amountLeft)
  1013.                                     itemToGive = itemToGive.SplitItem(amountLeft);
  1014.  
  1015.                                 amountLeft -= itemToGive.amount;
  1016.                             }
  1017.  
  1018.                             if(!toInventory.GiveItem(itemToGive))
  1019.                             {
  1020.                                 itemToGive.Drop(to.GetPlayer().transform.position, Vector3.zero);
  1021.                             }
  1022.                         }
  1023.                     }
  1024.                 }
  1025.             }
  1026.  
  1027.             public Trader GetInitiator()
  1028.             {
  1029.                 return initiator;
  1030.             }
  1031.  
  1032.             public Trader GetPartner()
  1033.             {
  1034.                 return partner;
  1035.             }
  1036.         }
  1037.  
  1038.         class TradeItem
  1039.         {
  1040.             private ItemDefinition itemDef;
  1041.             private int amount;
  1042.             private bool isBlueprint;
  1043.             private bool isMoney;
  1044.  
  1045.             public TradeItem(ItemDefinition itemDef, int amount, bool isBlueprint = false, bool isMoney = false)
  1046.             {
  1047.                 this.itemDef = itemDef;
  1048.                 this.amount = amount;
  1049.                 this.isBlueprint = isBlueprint;
  1050.                 this.isMoney = isMoney;
  1051.             }
  1052.  
  1053.             public ItemDefinition GetItemDef()
  1054.             {
  1055.                 return itemDef;
  1056.             }
  1057.  
  1058.             public int GetAmount()
  1059.             {
  1060.                 return amount;
  1061.             }
  1062.  
  1063.             public bool IsBlueprint()
  1064.             {
  1065.                 return isBlueprint;
  1066.             }
  1067.  
  1068.             public bool IsMoney()
  1069.             {
  1070.                 return isMoney;
  1071.             }
  1072.  
  1073.             public static string ListToString(List<TradeItem> itemList)
  1074.             {
  1075.                 string str = "";
  1076.  
  1077.                 foreach(TradeItem item in itemList)
  1078.                 {
  1079.                     if(!item.IsMoney())
  1080.                         str = str + item.GetItemDef().displayName.english;
  1081.                     else
  1082.                         str = str + "Money";
  1083.  
  1084.                     if(item.IsBlueprint())
  1085.                     {
  1086.                         str = str + "(Blueprint)\n";
  1087.                     }
  1088.                     else
  1089.                     {
  1090.                         str = str + "("+item.GetAmount()+"x)\n";
  1091.                     }
  1092.                 }
  1093.  
  1094.                 return str;
  1095.             }
  1096.         }
  1097.  
  1098.         private ItemDefinition FindItemByDisplayName(string name)
  1099.         {
  1100.             List<ItemDefinition> itemList = ItemManager.GetItemDefinitions();
  1101.             ItemDefinition foundItem = null;
  1102.             name = name.ToLower();
  1103.             string current;
  1104.             string last;
  1105.  
  1106.             foreach(ItemDefinition item in itemList)
  1107.             {
  1108.                 current = item.displayName.english.ToLower();
  1109.                 if(current.Contains(name))
  1110.                 {
  1111.                     if(foundItem != null)
  1112.                     {
  1113.                         last = foundItem.displayName.english.ToLower();
  1114.                         if(last.Replace(name, "").Length > current.Replace(name, "").Length)
  1115.                         {
  1116.                             foundItem = item;
  1117.                         }
  1118.                     }
  1119.                     else
  1120.                         foundItem = item;
  1121.                 }
  1122.             }
  1123.  
  1124.             return foundItem;
  1125.         }
  1126.  
  1127.         private BasePlayer GetPlayerByName(string name)
  1128.         {
  1129.             string currentName;
  1130.             string lastName;
  1131.             BasePlayer foundPlayer = null;
  1132.             name = name.ToLower();
  1133.  
  1134.             foreach(BasePlayer player in BasePlayer.activePlayerList)
  1135.             {
  1136.                 currentName = player.displayName.ToLower();
  1137.  
  1138.                 if(currentName.Contains(name))
  1139.                 {
  1140.                     if(foundPlayer != null)
  1141.                     {
  1142.                         lastName = foundPlayer.displayName;
  1143.                         if(currentName.Replace(name, "").Length < lastName.Replace(name, "").Length)
  1144.                         {
  1145.                             foundPlayer = player;
  1146.                         }
  1147.                     }
  1148.  
  1149.                     foundPlayer = player;
  1150.                 }
  1151.             }
  1152.  
  1153.             return foundPlayer;
  1154.         }
  1155.  
  1156.         [ConsoleCommand("tradeAccept")]
  1157.         private void tradeAccept(ConsoleSystem.Arg arg)
  1158.         {
  1159.             var player = arg.connection.player as BasePlayer;
  1160.             if (player == null)
  1161.                 return;
  1162.             Trader trader = player.GetComponent<Trader>();
  1163.  
  1164.             if (trader != null && trader.GetTradeSession() != null)
  1165.             {
  1166.                 if (player.HasPlayerFlag(BasePlayer.PlayerFlags.InBuildingPrivilege)
  1167.                     || player.HasPlayerFlag(BasePlayer.PlayerFlags.HasBuildingPrivilege))
  1168.                 {
  1169.                     CancelTrade(player, "You are too close to [Tool Cupboard]. Trade canceled.", "Your partner is too close to [Tool Cupboard]. Trace canceled.");
  1170.                     return;
  1171.                 }
  1172.                 var partner = trader.GetTradeSession().GetPartner().GetPlayer();
  1173.                 if (partner.HasPlayerFlag(BasePlayer.PlayerFlags.InBuildingPrivilege)
  1174.                     || partner.HasPlayerFlag(BasePlayer.PlayerFlags.HasBuildingPrivilege))
  1175.                 {
  1176.                     CancelTrade(partner, "You are too close to [Tool Cupboard]. Trade canceled.", "Your partner is too close to [Tool Cupboard]. Trace canceled.");
  1177.                     return;
  1178.                 }
  1179.                 trader.AcceptTrade();
  1180.             }
  1181.         }
  1182.         [ConsoleCommand("tradeCancel")]
  1183.         private void tradeCancel(ConsoleSystem.Arg arg)
  1184.         {
  1185.             var player = arg.connection.player as BasePlayer;
  1186.             if (player == null)
  1187.                 return;
  1188.             Trader trader = player.GetComponent<Trader>();
  1189.             Trader other;
  1190.  
  1191.             if (trader == null)
  1192.                 return;
  1193.  
  1194.             TradeSession session = trader.GetTradeSession();
  1195.  
  1196.             if (session != null)
  1197.             {
  1198.                 other = trader.GetOther();
  1199.                 session.CloseSession();
  1200.  
  1201.                 if (other != null)
  1202.                     PrintToChat(other.GetPlayer(), string.Format(s_TradeRequestDeclined, player.displayName));
  1203.             }
  1204.         }
  1205.     }
  1206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement