Guest User

Minecraft CC rednet basic Counication.

a guest
Jun 21st, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.22 KB | None | 0 0
  1. function readADV( _sReplaceChar, _tHistory ) -- slightly modified read function credit to dan200 for original
  2.     term.setCursorBlink( true )
  3.  
  4.     local sLine = ""
  5.     local nHistoryPos = nil
  6.     local nPos = 0
  7.     if _sReplaceChar then
  8.         _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  9.     end
  10.    
  11.     local w, h = term.getSize()
  12.     local sx, sy = term.getCursorPos() 
  13.     local function redraw()
  14.         local nScroll = 0
  15.         if sx + nPos >= w then
  16.             nScroll = (sx + nPos) - w
  17.         end
  18.            
  19.         term.setCursorPos( sx, sy )
  20.         term.write( string.rep(" ", w - sx + 1) )
  21.         term.setCursorPos( sx, sy )
  22.         if _sReplaceChar then
  23.             term.write( string.rep(_sReplaceChar, string.len(sLine) - nScroll) )
  24.         else
  25.             term.write( string.sub( sLine, nScroll + 1 ) )
  26.         end
  27.         term.setCursorPos( sx + nPos - nScroll, sy )
  28.     end
  29.    
  30.     while true do
  31.         local sEvent, param = os.pullEvent()
  32.         if sEvent == "char" then
  33.             sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  34.             nPos = nPos + 1
  35.             redraw()
  36.            
  37.         elseif sEvent == "key" then
  38.             if param == 28 then
  39.                 -- Enter
  40.                 break
  41.                
  42.             elseif param == 203 then
  43.                 -- Left
  44.                 if nPos > 0 then
  45.                     nPos = nPos - 1
  46.                     redraw()
  47.                 end
  48.                
  49.             elseif param == 205 then
  50.                 -- Right               
  51.                 if nPos < string.len(sLine) then
  52.                     nPos = nPos + 1
  53.                     redraw()
  54.                 end
  55.            
  56.             elseif param == 200 or param == 208 then
  57.                 -- Up or down
  58.                 if _tHistory then
  59.                     if param == 200 then
  60.                         -- Up
  61.                         if nHistoryPos == nil then
  62.                             if #_tHistory > 0 then
  63.                                 nHistoryPos = #_tHistory
  64.                             end
  65.                         elseif nHistoryPos > 1 then
  66.                             nHistoryPos = nHistoryPos - 1
  67.                         end
  68.                     else
  69.                         -- Down
  70.                         if nHistoryPos == #_tHistory then
  71.                             nHistoryPos = nil
  72.                         elseif nHistoryPos ~= nil then
  73.                             nHistoryPos = nHistoryPos + 1
  74.                         end                    
  75.                     end
  76.                    
  77.                     if nHistoryPos then
  78.                         sLine = _tHistory[nHistoryPos]
  79.                         nPos = string.len( sLine )
  80.                     else
  81.                         sLine = ""
  82.                         nPos = 0
  83.                     end
  84.                     redraw()
  85.                 end
  86.             elseif param == 14 then
  87.                 -- Backspace
  88.                 if nPos > 0 then
  89.                     sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  90.                     nPos = nPos - 1                
  91.                     redraw()
  92.                 end
  93.             end
  94.         else
  95.             redraw()
  96.         end
  97.     end
  98.     term.setCursorBlink( false )
  99.     term.setCursorPos( w + 1, sy )
  100.     return sLine
  101. end
  102.  
  103. local function writer()
  104.     while true do
  105.         coroutine.yield()
  106.         term.setCursorPos(1,1)
  107.         term.clearLine()
  108.         if stat == "to" then
  109.             write("To  :")
  110.         elseif stat == "mes" then
  111.             write("Mes :")
  112.         end
  113.     end
  114. end
  115.  
  116. function writer2()
  117.     term.setCursorPos(1,1)
  118.     term.clearLine()
  119.     if stat == "to" then
  120.         write("To  :")
  121.     elseif stat == "mes" then
  122.         write("Mes :")
  123.     end
  124. end
  125.  
  126. local function send()
  127.     while true do
  128.         local sizX,sizY = term.getSize()
  129.         term.setCursorPos(6,1)
  130.         stat = "to"
  131.         writer2()
  132.         local id = tonumber(readADV())
  133.         term.setCursorPos(6,1)
  134.         stat = "mes"
  135.         writer2()
  136.         local message = readADV()
  137.         rednet.send(id,message)
  138.     end
  139. end
  140.  
  141. local function recive()
  142.     local lastX,lastY = 1,2
  143.     while true do
  144.         term.setCursorBlink( true )
  145.         local event = {coroutine.yield()}
  146.         term.setCursorBlink( false )
  147.         if event[1] == "rednet_message" then
  148.             local sizX,sizY = term.getSize()
  149.             term.setCursorPos(1,lastY)
  150.             print("Frm: "..event[2].." Dist: "..event[4].."M Mes: "..event[3])
  151.             lastX,lastY = term.getCursorPos()
  152.         end
  153.     end
  154. end
  155.  
  156. local function openRednet()
  157.     local listOfSides = rs.getSides()
  158.     for i = 1,6 do
  159.         if peripheral.isPresent(listOfSides[i]) and peripheral.getType(listOfSides[i]) == "modem" then
  160.             rednet.open(listOfSides[i])
  161.             return listOfSides[i]
  162.         end
  163.     end
  164. end
  165. modemOn = openRednet()
  166. if modemOn == nil then
  167.     print("No WIFI Modem")
  168.     error()
  169. else
  170.     print("Opened wifi on "..modemOn.." side")
  171. end
  172.  
  173. term.clear()
  174. term.setCursorPos(1,1)
  175. local stat = nil
  176.  
  177. local reciveHandel = coroutine.create(recive)
  178. local writerHandel = coroutine.create(writer)
  179. local sendHandel = coroutine.create(send)
  180. while true do -- start a loop
  181.     local e,e1,e2,e3,e4,e5 = os.pullEvent()
  182.     coroutine.resume(reciveHandel,e,e1,e2,e3,e4,e5)
  183.     coroutine.resume(writerHandel)
  184.     coroutine.resume(sendHandel,e,e1,e2,e3,e4,e5)
  185. end
Advertisement
Add Comment
Please, Sign In to add comment