Advertisement
Corbinhol

furnaceServer

Sep 12th, 2022 (edited)
1,072
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.11 KB | None | 0 0
  1. sleep(2)
  2. rednet.open("back");
  3. print("Starting Up...");
  4. local furnacesConnected = 0;
  5. local furnaces = {};
  6. local currentlySmelting = {};
  7.  
  8. print("Attempting Connection With Clients")
  9. for i=1, 32, 1 do
  10.     furnaces[i] = {};
  11.     local furnace = rednet.lookup("furnace", "Furnace" .. i);
  12.     if furnace ~= nil then
  13.         print("Found Server");
  14.         rednet.send(furnace, "iam Server");
  15.         furnacesConnected = furnacesConnected + 1;
  16.         furnaces[i]["connectionStatus"] = "connected";
  17.     else
  18.         furnaces[i]["connectedStatus"] = "disconnected";
  19.     end
  20. end
  21.  
  22. term.clear();
  23.  
  24. local isBurning = "Idle"
  25. local totalFuel = 0;
  26. local fuel = {};
  27. local totalItems = 0;
  28. local items = {};
  29.  
  30. function updateScreen()
  31.     term.setCursorPos(1,2);
  32.     term.write("Furnaces Connected: ");
  33.     term.setCursorPos(1,3);
  34.     term.write("Furnace Status: ");
  35.     term.setCursorPos(1,4);
  36.     term.write("Total Fuel Amount In System: ");
  37.     term.setCursorPos(1,5);
  38.     term.write("Fuel In System: ");
  39.     term.setCursorPos(1,9);
  40.     term.write("Total Items in system: ");
  41.     term.setCursorPos(1, 10);
  42.     term.write("Items In System: ");
  43.     term.setCursorPos(1, 14);
  44.  
  45.     term.setCursorPos(string.len("Furnaces Connected: ") + 1,2)
  46.     term.setTextColor(colors.green);
  47.     term.write(furnacesConnected);
  48.     term.setTextColor(colors.white);
  49.     term.setCursorPos(string.len("Furnace Status: ") + 1, 3)
  50.     if isBurning == "Idle" then term.setTextColor(colors.orange);
  51.     elseif isBurning == "Burning" then term.setTextColor(colors.green);
  52.     elseif isBurning == "Needs Fuel" then term.setTextColor(colors.red);
  53.     end
  54.     term.write(isBurning);
  55.     term.setTextColor(colors.white);
  56.     for i=1, 32, 1 do
  57.         local tempColor;
  58.         if furnaces[i]["status"] == "idle" then tempColor = colors.lightGray;
  59.         elseif furnaces[i]["status"] == "burning" then tempColor = colors.green;
  60.         elseif furnaces[i]["status"] == "needs fuel" then tempColor = colors.red;
  61.         end
  62.         paintutils.drawPixel(1+i, 15, tempColor)
  63.         term.setBackgroundColor(colors.black)
  64.         term.setCursorPos(string.len("Total Fuel Amount In System: ") + 1, 4)
  65.         term.write(totalFuel);
  66.         term.setCursorPos(1,6)
  67.         if fuel[1] ~= nil then
  68.             term.write("   -" .. fuel[1]);
  69.             if fuel[2] ~= nil then
  70.                 term.setCursorPos(1,7)
  71.                 term.write("   -" .. fuel[2]);
  72.                 if fuel[3] ~= nil then
  73.                     term.setCursorPos(1,8)
  74.                     term.write("   -" .. fuel[3]);
  75.                 end
  76.             end
  77.         end
  78.        
  79.         if totalItems == 0 then
  80.             term.setTextColor(colors.gray);
  81.             term.setCursorPos(1,9);
  82.             term.write("Total Items in system: ");
  83.             term.setCursorPos(1, 10);
  84.             term.write("Items In System: ");
  85.         else
  86.             term.setTextColor(colors.white);
  87.             term.setCursorPos(1,9);
  88.             term.write("Total Items in system: ");
  89.             term.setCursorPos(1, 10);
  90.             term.write("Items In System: ");
  91.         end
  92.         term.setCursorPos(string.len("Total Items in system: ") + 1, 9)
  93.         term.write(totalItems);
  94.         term.setCursorPos(1, 11)
  95.         if items[1] ~= nil then
  96.             term.write("   -" .. items[1]);
  97.             if items[2] ~= nil then
  98.                 term.setCursorPos(1,12)
  99.                 term.write("   -" .. items[2]);
  100.                 if items[3] ~= nil then
  101.                     term.setCursorPos(1,13)
  102.                     term.write("   -" .. items[3]);
  103.                 end
  104.             end
  105.         end
  106.         term.setTextColor(colors.white);
  107.     end
  108. end
  109.  
  110. function disconnectFurnace(id)
  111.     furnacesConnected = furnacesConnected - 1;
  112.     furnaces[id]["connectionStatus"] = "disconnected";
  113. end
  114. function updateData()
  115.     totalFuel = 0;
  116.     totalItems = 0;
  117.     isBurning = "Idle         "
  118.     for t=1,32,1 do
  119.         furnaces[t]["status"] = "idle";
  120.         if furnaces[t]["connectionStatus"] == "connected" then
  121.             if furnaces[t]["itemAmount"] > 0 and furnaces[t]["fuelAmount"] > 0 then isBurning = "Burning           "; furnaces[t]["status"] = "burning";
  122.             elseif furnaces[t]["itemAmount"] > 0 and furnaces[t]["fuelAmount"] == 0 then isBurning = "Needs Fuel        "; furnaces[t]["status"] = "needs fuel";
  123.             end
  124.  
  125.             if furnaces[t]["fuelAmount"] == 0 then furnaces[t]["status"] = "needs fuel"; end
  126.             totalFuel = totalFuel + furnaces[t]["fuelAmount"];
  127.            
  128.             if furnaces[t]["fuelType"] ~= nil and furnaces[t]["fuelType"] ~= fuel[1] and furnaces[t]["fuelType"] ~= fuel[2] and furnaces[t]["fuelType"] ~= fuel[3] then
  129.                 if fuel[1] ~= furnaces[t]["fuelType"] and fuel[1] == nil then fuel[1] = furnaces[t]["fuelType"];
  130.                 elseif fuel[2] ~= furnaces[t]["fuelType"] and fuel[2] == nil then fuel[2] = furnaces[t]["fuelType"];
  131.                 elseif fuel[3] ~= furnaces[t]["fuelType"] and fuel[3] == nil then fuel[3] = furnaces[t]["fuelType"];
  132.                 end
  133.             end
  134.  
  135.             totalItems = totalItems + furnaces[t]["itemAmount"];
  136.  
  137.             if furnaces[t]["itemType"] ~= nil and furnaces[t]["itemType"] ~= items[1] and furnaces[t]["itemType"] ~= items[2] and furnaces[t]["itemType"] ~= items[3] then
  138.                 if items[1] ~= furnaces[t]["itemType"] and items[1] == nil then items[1] = furnaces[t]["itemType"];
  139.                 elseif items[2] ~= furnaces[t]["itemType"] and items[2] == nil then items[2] = furnaces[t]["itemType"];
  140.                 elseif items[3] ~= furnaces[t]["itemType"] and items[3] == nil then items[3] = furnaces[t]["itemType"];
  141.                 end
  142.             end
  143.         end
  144.     end
  145.     updateScreen()
  146. end
  147.  
  148. function getData()
  149.     for i=1,32,1 do
  150.         local furnace = rednet.lookup("furnace", "Furnace" .. i);
  151.         if furnace ~= nil then
  152.             if furnaces[i]["connectionStatus"] == "disconnected" then
  153.                 rednet.send(furnace, "iam Server");
  154.                 furnaces[i]["connectionStatus"] = "connected";
  155.                 furnacesConnected = furnacesConnected + 1;
  156.                 sleep(1);
  157.             end
  158.             rednet.send(furnace, "getFuelType");
  159.             _, furnaces[i]["fuelType"] = rednet.receive(1);
  160.             rednet.send(furnace, "getItem");
  161.             _, furnaces[i]["itemType"] = rednet.receive(1);
  162.             rednet.send(furnace, "getFuel");
  163.             local _, fuelAmountTemp = rednet.receive(1);
  164.             if fuelAmountTemp == nil then furnaces[i]["fuelAmount"]  = 0;
  165.             else furnaces[i]["fuelAmount"] = tonumber(fuelAmountTemp);
  166.             end
  167.             rednet.send(furnace, "getItemAmount");
  168.             _, itemAmountTemp = rednet.receive(1);
  169.             if itemAmountTemp == nil then furnaces[i]["itemAmount"] = 0;
  170.             else furnaces[i]["itemAmount"] = tonumber(itemAmountTemp);
  171.             end
  172.         else
  173.             if furnaces[i]["connectionStatus"] == "connected" then disconnectFurnace(i) end
  174.         end
  175.     end
  176. end
  177.  
  178.  
  179. getData();
  180. while true do
  181.     parallel.waitForAll(getData, updateData)
  182. end
  183.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement