SHARE
TWEET

Ejection Manager

NickNDS Dec 14th, 2019 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public string monitoringKeyword = "monitor";
  2.  
  3. public List<IMyConveyorSorter> stoneEjectionSorterList = new List<IMyConveyorSorter>();
  4. public List<IMyShipConnector> monitoredConnectorList = new List<IMyShipConnector>();
  5.  
  6. public bool firstRun = true, connected = true;
  7.  
  8. public TimeSpan recheckSpan = TimeSpan.FromSeconds(0);
  9.  
  10. public string lastMessage = "Booting";
  11.  
  12. public Program()
  13. {
  14.     Runtime.UpdateFrequency = UpdateFrequency.Update10;
  15.     monitoringKeyword = monitoringKeyword.ToLower();
  16.     GridTerminalSystem.GetBlocksOfType<IMyConveyorSorter>(stoneEjectionSorterList, b => b.CustomData.ToLower().Contains(monitoringKeyword));
  17.     GridTerminalSystem.GetBlocksOfType<IMyShipConnector>(monitoredConnectorList, b => b.CustomData.ToLower().Contains(monitoringKeyword));
  18.     lastMessage = "Booted With: " + monitoredConnectorList.Count + ": Connectors and: " + stoneEjectionSorterList.Count + " Sorters";
  19. }
  20.  
  21. public void Save() { }
  22.  
  23. public void Main(string argument, UpdateType updateSource)
  24. {
  25.     try {
  26.         if (monitoredConnectorList.Count > 0 || stoneEjectionSorterList.Count > 0)
  27.         {
  28.             if (recheckSpan.TotalSeconds <= 0.0)
  29.             {
  30.                 bool tempConnected = Connected();
  31.                 if (tempConnected != connected || firstRun)
  32.                 {
  33.                     connected = tempConnected;
  34.                     Update();
  35.                 }
  36.             } else recheckSpan -= Runtime.TimeSinceLastRun;
  37.         } else lastMessage = "No connectors or conveyors";
  38.         if (argument != "") Commands(argument);
  39.     } catch { lastMessage = "Error caught in main"; }
  40.     Echo(lastMessage);
  41. }
  42.  
  43. public void Commands(string argument)
  44. {
  45.     string arg = argument.ToLower();
  46.     switch (arg)
  47.     {
  48.         case "disconnect":
  49.             Connect(true);
  50.         break;
  51.         case "connect":
  52.             Connect();
  53.         break;
  54.         case "unlock":
  55.             Connect(true);
  56.         break;
  57.         case "lock":
  58.             Connect();
  59.         break;
  60.         case "scan":
  61.             GridTerminalSystem.GetBlocksOfType<IMyConveyorSorter>(stoneEjectionSorterList, b => b.CustomData.ToLower().Contains(monitoringKeyword));
  62.             GridTerminalSystem.GetBlocksOfType<IMyShipConnector>(monitoredConnectorList, b => b.CustomData.ToLower().Contains(monitoringKeyword));
  63.             lastMessage = "Booted With: " + monitoredConnectorList.Count + ": Connectors and: " + stoneEjectionSorterList.Count + " Sorters";
  64.         break;
  65.     }
  66. }
  67.  
  68. public void Connect(bool disconnect = false)
  69. {
  70.     bool tempConnected = disconnect;
  71.     for (int i = 0; i < monitoredConnectorList.Count; i++)
  72.     {
  73.         if (!disconnect && monitoredConnectorList[i].Status == MyShipConnectorStatus.Connectable)
  74.         {
  75.             tempConnected = true;
  76.             monitoredConnectorList[i].Connect();
  77.         }
  78.         else if (monitoredConnectorList[i].Status == MyShipConnectorStatus.Connected)
  79.         {
  80.             if (!disconnect) tempConnected = true;
  81.             else
  82.             {
  83.                 tempConnected = false;
  84.                 monitoredConnectorList[i].Disconnect();
  85.             }
  86.         }
  87.     }
  88.     if (tempConnected)
  89.     {
  90.         lastMessage = "Connected";
  91.         connected = true;
  92.         SetFilter();
  93.         SetConnector();
  94.     }
  95.     else
  96.     {
  97.         lastMessage = "Disconnected";
  98.         connected = false;
  99.         SetFilter();
  100.         SetConnector();
  101.     }
  102. }
  103.  
  104. public void SetFilter()
  105. {
  106.     List<MyInventoryItemFilter> filters = new List<MyInventoryItemFilter>();
  107.     for (int i = 0; i < stoneEjectionSorterList.Count; i++)
  108.     {
  109.         if (connected && stoneEjectionSorterList[i].Mode != MyConveyorSorterMode.Blacklist)
  110.         {
  111.             stoneEjectionSorterList[i].GetFilterList(filters);
  112.             stoneEjectionSorterList[i].SetFilter(MyConveyorSorterMode.Blacklist, filters);
  113.         }
  114.         else if (!connected && stoneEjectionSorterList[i].Mode != MyConveyorSorterMode.Whitelist)
  115.         {
  116.             stoneEjectionSorterList[i].GetFilterList(filters);
  117.             stoneEjectionSorterList[i].SetFilter(MyConveyorSorterMode.Whitelist, filters);
  118.         }
  119.     }
  120. }
  121.  
  122. public void SetConnector()
  123. {
  124.     for (int i = 0; i < monitoredConnectorList.Count; i++)
  125.     {
  126.         monitoredConnectorList[i].ThrowOut = !connected;
  127.     }
  128. }
  129.  
  130. public void Update()
  131. {
  132.     recheckSpan = TimeSpan.FromSeconds(5);
  133.     firstRun = false;
  134.     SetFilter();
  135.     SetConnector();
  136.     if (connected) lastMessage = "Connected";
  137.     else lastMessage = "Disconnected";
  138. }
  139.  
  140. public bool Connected()
  141. {
  142.     for (int i = 0; i < monitoredConnectorList.Count; i++)
  143.     {
  144.         if (monitoredConnectorList[i].Status == MyShipConnectorStatus.Connected) return true;
  145.     }
  146.     return false;
  147. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top