Advertisement
Corbinhol

Generic API Code

Sep 28th, 2022 (edited)
622
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.98 KB | None | 0 0
  1. --Generic API Code
  2. --Set this at the beginning
  3. local hostname = "";
  4.  
  5. --Basic Setup Code
  6. local filePath = "basalt.lua" --here you can change the file path that it installs to. default: /basalt.lua
  7. if not(fs.exists(filePath))then
  8.     shell.run("pastebin run ESs1mg7P packed true "..filePath:gsub(".lua", "")) -- this is an alternative to the wget command
  9. end
  10. local basalt = require(filePath:gsub(".lua", "")) -- here you can change the variablename in any variablename you want default: basalt
  11.  
  12.  
  13. local modemSide;
  14. --checks every side to identify which side has the ender modem on.
  15. if peripheral.getType("top") == "modem" then modemSide = "top";
  16. elseif peripheral.getType("right") == "modem" then modemSide = "right";
  17. elseif peripheral.getType("left") == "modem" then modemSide = "left";
  18. elseif peripheral.getType("front") == "modem" then modemSide = "front";
  19. elseif peripheral.getType("back") == "modem" then modemSide = "back";
  20. elseif peripheral.getType("bottom") == "modem" then modemSide = "bottom";
  21. else
  22.     print("Error: Modem not found. Please add modem onto machine");
  23.     os.exit();
  24. end
  25.  
  26. if hostname == "" then
  27.     print("Hostname not set, please set the host name at the top of the script");
  28.     os.exit();
  29. end
  30.  
  31. local protocol = "API";
  32. rednet.unhost(protocol, hostname);
  33.  
  34. if rednet.lookup(protocol, hostname) ~= nil then
  35.     print("Hostname is already in use. Please pick a different hostname");
  36.     os.exit();
  37. end
  38.  
  39. rednet.host(protocol,hostname);
  40. print("Set hostname to " .. hostname);
  41. local apiData = {};
  42. local systemStatus = "online";
  43.  
  44. function api() --Specifically For Communicating Data, Do not have commands in here
  45.     while true do
  46.         local from, message = rednet.receive(protocol);
  47.         if message == "check" then
  48.             rednet.send(from, systemStatus, protocol);
  49.         elseif message == "getData" then
  50.             modem.send(from, "online", protocol);
  51.         end
  52.     end
  53. end
  54.  
  55. function logic()
  56.    
  57. end
  58.  
  59. parallel.waitForAll(api, logic);
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement