ketchu13

7dtd-server-fixes - Recipes Tools Console Command

Oct 2nd, 2014
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.45 KB | None | 0 0
  1. /*7 Days To Die - Recipes Tools: ex: load a custom recipes.xml
  2. @Author: Alloc => 7dtd-server-fixes => https://7dtd.illy.bz/browser/binary-improvements/7dtd-server-fixes
  3. @Author: ketchu13 => Recipes console command => [email protected]
  4.  
  5. Usage:
  6. > rpc arg0 arg1* arg2*
  7.  
  8. arg0    = list      => List recipe(s) name(s).
  9.         = list+     => List recipe(s) name(s) and properties.  
  10.    
  11.         arg1    = All       => List All recipes names.
  12.                 = null      => List All recipes names.
  13.                 = bullet    => List recipe(s) name(s) which contains string "bullet".
  14.  
  15.         arg2    = say       => Server give the answer in chat.
  16.                 = null      => Server give the answer in chat.
  17.                 = sayk      => ketchu13 give the answer in chat.
  18.                 = zsay      => ZBot give the answer in chat.
  19.                 /TODO/= zpm     => ZBot give the answer in the target chat./TODO/
  20.         Example:
  21.         > rpc list All say  
  22.         OR
  23.         > rpc list All
  24.         OR
  25.         > rpc list
  26.         Server displays a list of all the recipes names, in chat's window of all connected users.
  27.  
  28. arg0    = reload    => Reload recipes list from xml.
  29.  
  30.         arg1    = Recipes.xml.
  31.  
  32. */
  33. using System;
  34. using System.IO;
  35.  
  36. namespace AllocsFixes.CustomCommands
  37. {
  38.     public class Recipes : ConsoleCommand
  39.     {
  40.         private readonly string recipesPath = UnityEngine.Application.dataPath + "/../Data/Config";
  41.         private UnityEngine.NetworkPlayer K = default(UnityEngine.NetworkPlayer);
  42.  
  43.         public Recipes (ConsoleSdtd cons) : base(cons)
  44.         {
  45.             K = cons.issuerOfCurrentClientCommand;
  46.         }
  47.  
  48.         public override string Description()
  49.         {
  50.             return "Suit of recipes tools (reload, list, list+).";
  51.         }
  52.  
  53.         public override string[] Names()
  54.         {
  55.             return new string[] { "recipes", "rcp" };
  56.         }
  57.  
  58.         public override void Run(string[] _params)
  59.         {
  60.             try {
  61.                 if (_params[0].Equals("reload")) {
  62.                     LoadRecipes(_params);
  63.                 }
  64.                 int mode = -1;
  65.                 if (_params[0].Equals("list")){ mode = 1; }
  66.                 if (_params[0].Equals("list+")){ mode = 3; }
  67.                 if (mode != -1) {
  68.                     if (_params.Length == 1 || _params[1].Equals("All")) {                     
  69.                         ListRecipes(mode, "All", null);
  70.                         return;
  71.                     }
  72.                     if (_params.Length == 3) {
  73.                         if (!_params[1].Equals("All")) {//ex: rpc list bullet say
  74.                             mode--;
  75.                         }
  76.                         ListRecipes(mode, _params[1], _params[2]);
  77.                         return;
  78.                     }
  79.                 }
  80.                 Usage(mode);
  81.             }
  82.             catch (Exception e) {
  83.                 Log.Out("Error in Recipes.Run: " + e);
  84.             }
  85.         }
  86.  
  87.         private void LoadRecipes(string[] _params) {
  88.             string filePath= null;
  89.             string msg = null;
  90.             if (_params.Length == 1) {//Reload TFP recipes list
  91.                 filePath = "recipes.xml";
  92.                 msg = "TFP recipes list is reloaded.";
  93.             }
  94.             if (_params.Length == 2) {//Reload custom recipes list
  95.                 filePath = _params[1] + ".xml";
  96.                 msg = "Custom recipes list " + filePath + " is loaded.";
  97.             }
  98.             if ((filePath != null) && File.Exists(string.Concat(recipesPath, "/" , filePath))) {
  99.                 CraftingManager.Cleanup();
  100.             //  RecipesFromXml.LoadRecipies(new C72AA(recipesPath, filePath));
  101.                 m_Console.SendResult(msg);
  102.                 return;
  103.             }
  104.             Usage(4);
  105.         }
  106.  
  107.         private void ListRecipes (int mode, string arg1, string arg2) {//arg1 "All" or "query string"  - arg2 output type
  108.             int num1 = 0;
  109.             int num2 = 0;
  110.             string msg1, msg2 = "";
  111.             Recipe[] allRecipes = CraftingManager.GetAllRecipes ();
  112.             SendResult (arg2, "## Recipes List ##");
  113.             for (int i = 0; i < allRecipes.Length; i++)
  114.             {
  115.                 Recipe recipe = allRecipes [i];
  116.                 if ((mode == 0 && recipe.GetName ().ToLower ().Contains (arg1)) || (mode == 1)) {//list recipes names
  117.                     num1++;
  118.                     SendResult (arg2, string.Concat(new object[] {
  119.                         num1,
  120.                         ". ",
  121.                         recipe.GetName ()}));
  122.                 }
  123.                 if ((mode == 2 && recipe.GetName ().ToLower ().Contains (arg1)) || (mode == 3)) {//list recipes Names And properties
  124.                     num2++;
  125.                     SendResult(arg2, "**" + recipe.GetName() + "**");
  126.                     msg1 = string.Concat (
  127.                         new object[] {
  128.                             num2, ". ", recipe.GetName (),
  129.                             ", count= ", recipe.count,
  130.                             ", scrapable= ", recipe.scrapable, 
  131.                             (recipe.tooltip == null) ? string.Empty : (", tooltip= " + recipe.tooltip),
  132.                             recipe.craftingTool.Equals (ItemValue.None) ? string.Empty : (", craftTool= " + ItemBase.list [recipe.craftingTool.type].GetItemName (recipe.craftingTool)),
  133.                             (recipe.craftingArea == null || recipe.craftingArea.Length <= 0) ? string.Empty : (", craftingArea= " + recipe.craftingArea),
  134.                             (recipe.craftingTime <= 0f) ? string.Empty : (", craftingTime= " + recipe.craftingTime.ToString ())
  135.                         });
  136.                     SendResult (arg2, msg1);
  137.                     SendResult (arg2,"**Ingredients**");
  138.                     foreach (Vector2i current in recipe.ingredients.Keys) {//ingredients
  139.                         InventoryField inventoryField = recipe.ingredients[current];
  140.                         msg2 = string.Concat(
  141.                             new object[] {
  142.                                 "Name= ", ItemBase.list[inventoryField.itemValue.type].GetItemName(inventoryField.itemValue),
  143.                                 ", count= ", inventoryField.count,
  144.                                 (recipe.craftingArea == null || !recipe.craftingArea.Equals ("campfire")) ? (", gridPosition= " + current) : string.Empty
  145.                             });
  146.                         SendResult (arg2, msg2);
  147.                     }
  148.                     SendResult(arg2, "****");
  149.                 }
  150.             }
  151.             SendResult (arg2, string.Concat("Listed ", (num1 >= num2) ? num1 : num2 , " matching recipes."));
  152.             return;
  153.         }
  154.  
  155.         private void SendResult (string arg1, string arg2) {
  156.             string msg = string.Concat (new object[] { "\t", arg2 });
  157.             string name = null;
  158.             string outPut = arg1.ToLower ();
  159.             switch (outPut) {
  160.             case "say":
  161.                 name = "[FF00FF]Server";//Server
  162.                 break;
  163.             case "zsay":
  164.                 name = "[00FF00]ZBot";//ZBot
  165.                 break;
  166.             case "sayk":
  167.                 name = "[FF0000]ketchu13";//ketchu13
  168.                 break;
  169.             case "zpm":
  170.                 //ZBot private
  171.                 CommonMappingFunctions.GetConnectionManager ().networkView.RPC ("RPC_ChatMessage", K,
  172.                     new object[] {
  173.                         msg,
  174.                         -1,
  175.                         "[00FF00]ZBot(Chuchote)",
  176.                         true
  177.                     }
  178.                 );
  179.                 return;
  180.             case "sayA":
  181.                 CommonMappingFunctions.GetGameManager().GetRPCNetworkView().RPC("RPC_ChatMessage", UnityEngine.RPCMode.AllBuffered,
  182.                     new object[] {
  183.                         msg,
  184.                         -1,
  185.                         string.Empty,
  186.                         false
  187.                     }
  188.                 );
  189.                 return;
  190.             }
  191.             if (name != null) {
  192.                 CommonMappingFunctions.GetGameManager ().SendChatMessage (msg, -1, name);
  193.                 return;
  194.             }
  195.             m_Console.SendResult (msg);        
  196.         }
  197.  
  198.         public void Usage(int mode) {
  199.             switch (mode) {
  200.             case -1:
  201.                 m_Console.SendResult ("Usage: recipes list <searchString> <outPutType*>");
  202.                 m_Console.SendResult ("   or: recipes list All <outPutType*>");
  203.                 m_Console.SendResult ("   or: recipes list+ <searchString> <outPutType*>");
  204.                 m_Console.SendResult ("   or: recipes list+ All <outPutType*>");
  205.                 m_Console.SendResult ("   or: recipes reload <customRecipes.xml>");
  206.                 m_Console.SendResult ("   or: recipes reload");
  207.                 goto case 13;
  208.             case 0:
  209.                 m_Console.SendResult("Usage: recipes list <searchString> <outPutType>");
  210.                 goto case 13;
  211.             case 1:
  212.                 m_Console.SendResult("Usage: recipes list All <outPutType>");
  213.                 m_Console.SendResult("   or: recipes list");
  214.                 goto case 13;          
  215.             case 2:
  216.                 m_Console.SendResult("Usage: recipes list+ <searchString> <outPutType>");
  217.                 goto case 13;
  218.             case 3:
  219.                 m_Console.SendResult("Usage: recipes list+ All <outPutType>");
  220.                 m_Console.SendResult("   or: recipes list+");
  221.                 goto case 13;
  222.             case 4:
  223.                 m_Console.SendResult("Usage: recipes reload <customRecipes.xml>");
  224.                 m_Console.SendResult("   or: recipes reload");
  225.                 break;
  226.             case 13:
  227.                 m_Console.SendResult ("*<outPutType>: say, sayk, zsay, sayA, cons");
  228.                 break;
  229.             }
  230.         }
  231.     }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment