Advertisement
Corbinhol

Trade Station Controller

Sep 20th, 2022 (edited)
981
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.33 KB | None | 0 0
  1. --Trade Station Controller
  2. local protocol = "AUTOBUYER"
  3. local gui = require("SmallGUI");
  4. rednet.open("top");
  5.  
  6. file = fs.open("data.dat", "r");
  7. data = textutils.unserialize(file.readAll());
  8.  
  9. local hostname = data["itemID"];
  10. local side = "back";
  11.  
  12. rednet.host(hostname, protocol);
  13.  
  14. local uptime = 0;
  15. local currentLog = {};
  16.  
  17. function log(input)
  18.     local tempOut = "[" .. gui.parseTime(uptime) .. "] " .. input;
  19.     table.insert(currentLog, tempOut);
  20. end
  21.  
  22. function enableMachine()
  23.     if redstone.getOutput(side) == false then
  24.         redstone.setOutput(side, true);
  25.         log("Enabling Trading Station");
  26.     end
  27. end
  28.  
  29. function disableMachine()
  30.     if redstone.getOutput(side) == true then
  31.         redstone.setOutput(side, false);
  32.         log("Disabling Trading Station");
  33.     end
  34. end
  35.  
  36. disableMachine();
  37. function waitForMessage()
  38.     while true do
  39.         address, message = rednet.receive(protocol);
  40.         if message == "enable" then
  41.             enableMachine();
  42.         elseif message == "disable" then
  43.             disableMachine();
  44.         elseif message == "getData" then
  45.             log("Sending Data...");
  46.             rednet.send(address, textutils.serialize(data), protocol);
  47.         end
  48.     end
  49. end
  50.  
  51.  
  52. function setUptime()
  53.     while true do uptime = uptime + 1; sleep(1) end
  54. end
  55.  
  56. function tradeStationStatus()
  57.     if redstone.getOutput(side) == false then return "Disabled" else return "Enabled" end
  58. end
  59.  
  60. function updateScreen()
  61.     term.clear()
  62.     while true do
  63.         gui.printf(string.rep("=", term.getSize()),1,1);
  64.         gui.printf("Trade Station Controller [Ver 2.0]|Uptime: " .. gui.parseTime(uptime),1,2);
  65.         gui.printf(string.rep("=", term.getSize()),1,3);
  66.         gui.printf("Item: " .. data["displayName"],1,4);
  67.         gui.printf("Trade Station Status: " .. tradeStationStatus() .. "            ",1,5);
  68.         gui.printf(string.rep("=", term.getSize()),1,6);
  69.         for i = 1, 12, 1 do
  70.             tableLength = table.getn(currentLog);
  71.             if currentLog[tableLength - i] ~= nil then
  72.                 gui.printf(currentLog[tableLength - i] .. string.rep(" ", term.getSize()), 1, 6+i);
  73.             end
  74.         end
  75.         gui.printf(string.rep("=", term.getSize()),1,19);
  76.         sleep(0);
  77.     end
  78. end
  79.  
  80. parallel.waitForAny(waitForMessage, setUptime, updateScreen);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement