Advertisement
MCFunRide

MineChat Updated

Jun 28th, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. --Setup monitor
  2. local monitor = peripheral.wrap( "left" )
  3. monitor.clear()
  4. monitor.setCursorPos( 1, 1 )
  5. term.clear()
  6. term.setCursorPos( 1, 1 )
  7.  
  8. local intro = "Welcome to the Android MineChat Host Program. This program allows allows a server's player's to communicate with the server's host at any time. Type '$$$' at any time to quit the program. Press 'enter' to begin."
  9.  
  10. --Instructions
  11. term.redirect( peripheral.wrap( "left" ) )
  12. term.setTextColor( colors.green )
  13. print( intro )
  14. print( "" )
  15. term.setTextColor( colors.blue )
  16. term.setCursorBlink(true)
  17. term.redirect( term.native() )
  18. term.setTextColor( colors.green )
  19. print( intro )
  20. print( "" )
  21. term.setTextColor( colors.blue )
  22. term.setCursorBlink(true)
  23. while true do
  24. local event, key = os.pullEvent( "key" )
  25. if key == keys.enter then
  26. break
  27. end
  28. end
  29.  
  30. --Setup server
  31. cond = true
  32. logFile = fs.open( "log", fs.exists( "log" ) and "a" or "w")
  33. outFile = fs.open( "outputServer", "w" )
  34. outFile.write( "Server is Connected" )
  35. outFile.write( "" )
  36. outFile.close()
  37.  
  38. --Function for printing the server input
  39. function output()
  40. while true do
  41. sleep(0)
  42. if fs.exists( "inputServer" ) == true and fs.getSize( "inputServer" ) > 0 then
  43. sleep(0)
  44. inFile = fs.open( "inputServer", "r" )
  45. while fs.exists( "inputServer" ) == true do
  46. local serverText = inFile.readAll()
  47. if serverText ~= nil then
  48. term.redirect( peripheral.wrap( "left" ) )
  49. term.setTextColor( colors.red )
  50. print( serverText )
  51. print( "" )
  52. term.redirect( term.native() )
  53. term.setTextColor( colors.red )
  54. print( serverText )
  55. print("")
  56. logFile.write( serverText )
  57. logFile.writeLine()
  58. term.setTextColor( colors.blue )
  59. fs.delete( "inputServer" )
  60. end
  61. end
  62. inFile.close()
  63. end
  64. end
  65. end
  66.  
  67. --Function for server output
  68. function input()
  69. while true do
  70. local text = read()
  71. if ( text ~= nil ) and ( text ~= "$$$" ) and ( text ~= "" )then
  72. term.redirect( peripheral.wrap( "left" ) )
  73. term.setTextColor( colors.blue )
  74. print( "User: "..text )
  75. print( "" )
  76. term.redirect( term.native() )
  77. if fs.exists( "outputServer" ) == false then
  78. outFile = fs.open( "outputServer", "w" )
  79. else
  80. outFile = fs.open( "outputServer", "a" )
  81. end
  82. outFile.write( "Server: "..text )
  83. outFile.writeLine()
  84. outFile.close()
  85. logFile.write( "User: "..text )
  86. logFile.writeLine()
  87. text = nil
  88. end
  89. if text == "$$$" then
  90. cond = false
  91. return
  92. end
  93. end
  94. end
  95.  
  96. --Function for scrolling (unused)
  97. function scroll()
  98. while true do
  99. local event, key = os.pullEvent( "key" )
  100. if key == keys.up then
  101. term.redirect( peripheral.wrap( "left" ) )
  102. term.scroll( 1 )
  103. term.redirect( term.native() )
  104. end
  105. if key == keys.down then
  106. term.redirect( peripheral.wrap( "left" ) )
  107. term.scroll( -1 )
  108. term.redirect( term.native() )
  109. end
  110. end
  111. end
  112.  
  113. --Main body
  114. while cond do
  115.  
  116. parallel.waitForAny( output, input )
  117.  
  118. end
  119.  
  120. --Clean up
  121. fs.delete( "inputServer" )
  122. logFile.write("------------------------------------\n")
  123. inFile = fs.open( "inputServer", "w" )
  124. outFile = fs.open( "outputServer", "w" )
  125. outFile.write( "Server is not Connected" )
  126. outFile.close()
  127. inFile.close()
  128. logFile.close()
  129. term.clear()
  130. term.setCursorPos( 0, 0 )
  131. monitor.clear()
  132. monitor.setCursorPos( 1, 1 )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement