bobmarley12345

ComputerCraftTouchScreenClient

Sep 21st, 2020
71
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local id = "???"
  2. local state = false
  3. local w,h = term.getSize()
  4.  
  5. function save()
  6.     local file = fs.open("redstone","w")
  7.         file.writeLine(id)
  8.         file.writeLine(tostring(state))
  9.     file.close()
  10. end
  11.  
  12. function load()
  13.     if fs.exists("redstone") then
  14.         local file = fs.open("redstone","r")
  15.             id = file.readLine()
  16.             if file.readLine() == "true" then
  17.                 state = true
  18.             end
  19.         file.close()
  20.     end
  21. end
  22.  
  23.  
  24.  
  25. function openRednet()
  26.     for _, side in pairs(redstone.getSides()) do
  27.       if peripheral.getType(side) == "modem" then
  28.         rednet.open(side)
  29.       end
  30.     end
  31. end
  32.  
  33. function setRedstone()
  34.     for _, side in pairs(redstone.getSides()) do
  35.         redstone.setOutput(side, state)
  36.     end
  37. end
  38.  
  39.  
  40. function set()
  41.     sleep(0)
  42.     term.setCursorPos(1, 5)
  43.     term.clearLine()
  44.     write("Text: ", 14, 5)
  45.     id = betterRead(20,5)
  46. end
  47.  
  48. function betterRead(x,y) -- DONE 1
  49.     term.setCursorBlink(true)
  50.     local s = id
  51.  
  52.     while true do
  53.         term.setCursorPos(x,y)
  54.         term.write( string.rep(' ', w - x + 1) )
  55.         term.setCursorPos(x,y)
  56.         if s:len()+x < w then
  57.             term.write(s)
  58.         else
  59.             term.write(s:sub( s:len() - (w-x-2)))
  60.         end
  61.         local e = { os.pullEvent() }
  62.         if e[1] == "char" then
  63.             s = s .. e[2]
  64.         elseif e[1] == "key" then
  65.             if e[2] == keys.enter then
  66.                 break
  67.             elseif e[2] == keys.backspace then
  68.                 s = s:sub( 1, s:len() - 1 )
  69.             end
  70.         end
  71.     end
  72.  
  73.     term.setCursorBlink(false)
  74.     return s
  75. end
  76.  
  77. function draw()
  78.     term.clear()
  79.     write("Text: " .. id, 14,5)
  80.     write("Current State: " .. tostring(state), 5,6)
  81.     write("Hit a button to change the text!",8,8)
  82. end
  83.  
  84. function write(text, x, y)
  85.     term.setCursorPos(x,y)
  86.     term.write(tostring(text))
  87. end
  88.  
  89.  
  90. function main()
  91.     while true do
  92.         draw()
  93.         openRednet()
  94.         local event = {os.pullEvent()}
  95.         if event[1] == "rednet_message" then
  96.             if event[3] == ("" .. id .. tostring(not state)) then
  97.                 state = not state
  98.                 setRedstone()
  99.             end
  100.         elseif event[1] == "key" then
  101.             set()
  102.         end
  103.         save()
  104.     end
  105. end
  106.  
  107.  
  108. load()
  109. setRedstone()
  110. main()
RAW Paste Data