Advertisement
HydrantHunter

gateBuddy 1.5

Jun 4th, 2014
1,035
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.72 KB | None | 0 0
  1. --[[  LanteaCraft      ]]--
  2. --[[      and SGCraft  ]]--
  3. --[[    Gate Buddy     ]]--
  4. --[[      by Dog       ]]--
  5. --[[ aka HydrantHunter ]]--
  6. --[[ pastebin 1U2XPPCw ]]--
  7. local gbVer = "1.5.02"
  8. --[[
  9. Tested with/requires:
  10.   - Minecraft 1.7.10
  11.   - LanteaCraft-1.7.10-70 || SGCraft 1.95-mc1.7.10
  12.   - ComputerCraft 1.73, 1.74, 1.75
  13.     - A computer (standard or advanced) with a wireless modem, or a wireless turtle (standard or advanced)
  14.     - A stargate with a CC adapter
  15. ]]--
  16. local client, gate, thisGate
  17. local modemSide = "none"
  18. local lcGate = false
  19.  
  20. local function netReceive()
  21.   local commands = {
  22.     pPull = function() end, --# do nothing, just return
  23.     pPush = function() end, --# do nothing, just return
  24.     QRY = function() sleep(0.1) rednet.send(client, thisGate) end,
  25.     endCall = function() pcall(gate.disconnect) end,
  26.   }
  27.   while true do
  28.     local id, message = rednet.receive()
  29.     if type(message) == "table" and message.program == "pSync" then
  30.       if message.password then
  31.         if not lcGate then
  32.           gate.sendMessage(message.password)
  33.         end
  34.         return
  35.       end
  36.       client = id
  37.       for command, action in pairs(commands) do
  38.         if (message.command and tostring(command) == message.command) or (message.password and tostring(command) == message.password) then
  39.           action()
  40.           return
  41.         end
  42.       end
  43.       if #tostring(message.command) == 7 or #tostring(message.command) == 9 then
  44.         if message.command ~= thisGate then
  45.           if lcGate then
  46.             if gate.isConnected() or gate.isDialing() then
  47.               return
  48.             end
  49.           else
  50.             if gate.stargateState() ~= "Idle" then
  51.               return
  52.             end
  53.           end
  54.           gate.dial(message.command)
  55.           return
  56.         end
  57.       end
  58.     end
  59.   end
  60. end
  61.  
  62. local function charInput()
  63.   while true do
  64.     local _, char = os.pullEvent("char")
  65.     if string.lower(char) == "q" then
  66.       rednet.unhost("pSync")
  67.       rednet.close(modemSide)
  68.       term.clear()
  69.       term.setCursorPos(1, 1)
  70.       term.write("gateBuddy is OFFLINE")
  71.       term.setCursorPos(1, 3)
  72.       return
  73.     end
  74.   end
  75. end
  76.  
  77. local function initError(device)
  78.   term.clear()
  79.   term.setCursorPos(1, 1)
  80.   term.write("No " .. device .. " detected!")
  81.   term.setCursorPos(1, 3)
  82.   term.write("gateBuddy is OFFLINE")
  83.   term.setCursorPos(1, 5)
  84.   return false
  85. end
  86.  
  87. local function initModem()
  88.   for _,side in ipairs(rs.getSides()) do
  89.     if peripheral.getType(side) == "modem" and peripheral.call(side, "isWireless") then
  90.       modemSide = side
  91.       rednet.open(side)
  92.       return true
  93.     end
  94.   end
  95.   return initError("Wireless Modem")
  96. end
  97.  
  98. local function initGate()
  99.   gate = peripheral.find("stargate")
  100.   if type(gate) == "table" then
  101.     lcGate = pcall(gate.getAddress)
  102.     thisGate = lcGate and gate.getAddress() or gate.localAddress()
  103.     return true
  104.   else
  105.     return initError("Stargate")
  106.   end
  107. end
  108.  
  109. local function initMe()
  110.   term.setBackgroundColor(colors.black)
  111.   term.setTextColor(colors.white)
  112.   term.clear()
  113.   term.setCursorPos(2, 2)
  114.   term.write("gateBuddy initializing...")
  115.   if not initGate() then return false end
  116.   local ccLabel = os.getComputerLabel()
  117.   if ccLabel == nil or tostring(ccLabel) == "" then
  118.     os.setComputerLabel(thisGate .. " Gate")
  119.   end
  120.   if not initModem() then return false end
  121.   rednet.host("pSync", thisGate)
  122.   term.clear()
  123.   term.setCursorPos(2, 2)
  124.   term.write("gateBuddy " .. gbVer .. " is ONLINE")
  125.   term.setCursorPos(2, 5)
  126.   term.write("This Gate: " .. thisGate)
  127.   term.setCursorPos(2, 8)
  128.   term.write("press 'q' to quit")
  129.   return true
  130. end
  131.  
  132. if not initMe() then return end
  133.  
  134. parallel.waitForAny(netReceive, charInput)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement