Consolidated Inventory on an LCD Panel 0/5 (5)

Shows all the inventory for the containers specified at the beginning of the script

  1. const string PANEL_NAME = "LCD Panel";  
  2. const string CONTAINER_NAME = "Raw Ore";
  3. const int PANEL_LINES = 22;
  4. int lineOffset = 0;
  5.  
  6. void Main()
  7. {
  8.     try {
  9.     List<IMyTerminalBlock> work = new List<IMyTerminalBlock>();
  10.     Dictionary<String,float> consolidated = new Dictionary<String,float>();
  11.     GridTerminalSystem.SearchBlocksOfName(PANEL_NAME, work);      
  12.     IMyTextPanel panel = null;
  13.     for (int i = 0; i < work.Count; i++)  
  14.     {  
  15.         if (work&#91;i] is IMyTextPanel) {
  16.            panel = (IMyTextPanel)work&#91;i];  
  17.            break;
  18.         }
  19.     }
  20.     List<IMyTerminalBlock> containerList = new List<IMyTerminalBlock>();
  21.     GridTerminalSystem.SearchBlocksOfName(CONTAINER_NAME, containerList);      
  22.  
  23.     for (int i = 0; i < containerList.Count; i++)  
  24.     {  
  25.         if (containerList&#91;i] is IMyCargoContainer) {
  26.             var containerInvOwner = containerList&#91;i] as IMyInventoryOwner;
  27.             var containerItems = containerInvOwner.GetInventory(0).GetItems();  
  28.             for(int j = containerItems.Count - 1; j >= 0; j--)    
  29.             {    
  30.                 String itemName = decodeItemName(containerItems[j].Content.SubtypeName,
  31.                                   containerItems[j].Content.TypeId.ToString()) + "|" +
  32.                                   containerItems[j].Content.TypeId.ToString();
  33.                 float amount = (float)containerItems[j].Amount;
  34.                 if (!consolidated.ContainsKey(itemName)) {
  35.                    consolidated.Add(itemName, amount);
  36.                 } else {
  37.                    consolidated[itemName] += amount;
  38.                 }
  39.             }  
  40.         }
  41.     }
  42.     List<String> list = new List<String>();
  43.     var enumerator = consolidated.GetEnumerator();
  44.     while (enumerator.MoveNext())
  45.     {      
  46.         var pair = enumerator.Current;
  47.         String itemKey = pair.Key;
  48.         float itemValue = pair.Value;
  49.        
  50.         String txt = itemKey.Split('|')[0] + "  -  ";
  51.         String amt = amountFormatter(itemValue,itemKey.Split('|')[1]);
  52.         txt += amt;
  53.         list.Add(txt);
  54.     }    
  55.     list.Sort();
  56.     list.Insert(0,"------------------------------------------------------");
  57.     list.Insert(0,CONTAINER_NAME + " Inventory");
  58.     for (int o=0; o < lineOffset; o++) {
  59.         String shiftedItem = list&#91;0];
  60.         list.RemoveAt(0);
  61.         list.Add(shiftedItem);
  62.     }
  63.     panel.WritePublicText(String.Join("\n",list.ToArray()), false);
  64.  
  65.     panel.ShowTextureOnScreen();  
  66.     panel.ShowPublicTextOnScreen();
  67.     if (list.Count > PANEL_LINES) {
  68.         lineOffset++;
  69.         if (list.Count - lineOffset < PANEL_LINES) {
  70.            lineOffset = 0;
  71.         }
  72.     }
  73.     } catch (Exception ex) {
  74.         throw new Exception(ex.StackTrace);
  75.     }
  76. }
  77.  
  78. String amountFormatter(float amt, String typeId) {
  79.     if (typeId.EndsWith("_Ore") || typeId.EndsWith("_Ingot")) {
  80.         if (amt > 1000.0f) {
  81.           return "" + Math.Round((float)amt/1000,2).ToString("###,###,##0.00") + "K";
  82.         } else {
  83.           return "" + Math.Round((float)amt,2).ToString("###,###,##0.00");
  84.         }
  85.     }
  86.     return "" + Math.Round((float)amt,0).ToString("###,###,##0");
  87. }
  88.  
  89. String decodeItemName(String name, String typeId)  
  90. {
  91.     if (name.Equals("Construction")) { return "Construction Component"; }
  92.     if (name.Equals("MetalGrid")) { return "Metal Grid"; }
  93.     if (name.Equals("InteriorPlate")) { return "Interior Plate"; }
  94.     if (name.Equals("SteelPlate")) { return "Steel Plate"; }
  95.     if (name.Equals("SmallTube")) { return "Small Steel Tube"; }
  96.     if (name.Equals("LargeTube")) { return "Large Steel Tube"; }
  97.     if (name.Equals("BulletproofGlass")) { return "Bulletproof Glass"; }
  98.     if (name.Equals("Reactor")) { return "Reactor Component"; }
  99.     if (name.Equals("Thrust")) { return "Thruster Component"; }
  100.     if (name.Equals("GravityGenerator")) { return "GravGen Component"; }
  101.     if (name.Equals("Medical")) { return "Medical Component"; }
  102.     if (name.Equals("RadioCommunication")) { return "Radio Component"; }
  103.     if (name.Equals("Detector")) { return "Detector Component"; }
  104.     if (name.Equals("SolarCell")) { return "Solar Cell"; }
  105.     if (name.Equals("PowerCell")) { return "Power Cell"; }
  106.     if (name.Equals("AutomaticRifleItem")) { return "Rifle"; }
  107.     if (name.Equals("AutomaticRocketLauncher")) { return "Rocket Launcher"; }
  108.     if (name.Equals("WelderItem")) { return "Welder"; }
  109.     if (name.Equals("AngleGrinderItem")) { return "Grinder"; }
  110.     if (name.Equals("HandDrillItem")) { return "Hand Drill"; }
  111.     if (typeId.EndsWith("_Ore")) {
  112.         if (name.Equals("Stone")) {
  113.             return name;
  114.         }
  115.         return name + " Ore";
  116.     }
  117.     if (typeId.EndsWith("_Ingot")) {
  118.         if (name.Equals("Stone")) {
  119.             return "Gravel";
  120.         }
  121.         if (name.Equals("Magnesium")) {
  122.             return name + " Powder";
  123.         }
  124.         if (name.Equals("Silicon")) {
  125.             return name + " Wafer";
  126.         }
  127.        return name + " Ingot";
  128.     }
  129.     return name;
  130. }

Please rate this post

3 Responses

  1. […] script is similar to the Consolidated Inventory on an LCD Panel. It also shows the percentage that your cargo containers are full (at the […]

  2. […] my containers, one that shows me refined ore inventory in all my containers, etc. They all use the Consolidated Inventory script from this […]

  3. Thank you very much for these scripts. I tried to submit a 5/5 rating but it entered as 0/5. Really sorry..

Leave a Reply