Advertisement
CelticCoder

serverBlockValues

Feb 17th, 2024 (edited)
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.53 KB | None | 0 0
  1. local filename = "block_data.txt"
  2. local communicator = 0  -- Initially set to 0
  3.  
  4. -- Function to read data from file
  5. local function readDataFromFile()
  6.     local file = fs.open(filename, "r") -- Open file in read mode
  7.     if file then
  8.         local data = textutils.unserialize(file.readAll()) -- Read all content and deserialize
  9.         file.close() -- Close the file
  10.         return data
  11.     else
  12.         return nil
  13.     end
  14. end
  15.  
  16. -- Function to display menu and get user choice
  17. local function getMenuChoice()
  18.     print("Modify Block Data Menu:")
  19.     print("1. Add block")
  20.     print("2. Remove block")
  21.     print("3. Modify block value")
  22.     print("4. Save and send data")
  23.     print("5. Exit without saving")
  24.     write("Enter your choice: ")
  25.     return tonumber(read())
  26. end
  27.  
  28. -- Function to add a block
  29. local function addBlock(data)
  30.     write("Enter block name: ")
  31.     local blockName = read()
  32.     write("Enter block value: ")
  33.     local blockValue = tonumber(read())
  34.     table.insert(data.blockList, blockName)
  35.     table.insert(data.valueList, blockValue)
  36.     print("Block added successfully.")
  37. end
  38.  
  39. -- Function to remove a block
  40. local function removeBlock(data)
  41.     print("Current block list:")
  42.     for i, blockName in ipairs(data.blockList) do
  43.         print(i .. ". " .. blockName)
  44.     end
  45.     write("Enter the index of the block to remove: ")
  46.     local index = tonumber(read())
  47.     if index and index >= 1 and index <= #data.blockList then
  48.         table.remove(data.blockList, index)
  49.         table.remove(data.valueList, index)
  50.         print("Block removed successfully.")
  51.     else
  52.         print("Invalid index.")
  53.     end
  54. end
  55.  
  56. -- Function to modify block value
  57. local function modifyBlockValue(data)
  58.     print("Current block list:")
  59.     for i, blockName in ipairs(data.blockList) do
  60.         print(i .. ". " .. blockName .. " (Value: " .. data.valueList[i] .. ")")
  61.     end
  62.     write("Enter the index of the block to modify: ")
  63.     local index = tonumber(read())
  64.     if index and index >= 1 and index <= #data.blockList then
  65.         write("Enter the new value for the block: ")
  66.         local newValue = tonumber(read())
  67.         if newValue then
  68.             data.valueList[index] = newValue
  69.             print("Block value modified successfully.")
  70.         else
  71.             print("Invalid value.")
  72.         end
  73.     else
  74.         print("Invalid index.")
  75.     end
  76. end
  77.  
  78. -- Function to write data to file and send it to a computer
  79. local function writeDataToFileAndSend(data)
  80.     local file = fs.open(filename, "w") -- Open file in write mode
  81.     if file then
  82.         file.write(textutils.serialize(data)) -- Serialize data and write to file
  83.         file.close() -- Close the file
  84.  
  85.         -- Send the file to the computer named "communicator"
  86.         if communicator ~= 0 then
  87.             local fileContent = fs.open(filename, "r")
  88.             if fileContent then
  89.                 local content = fileContent.readAll()
  90.                 fileContent.close()
  91.                 rednet.send(communicator, content)
  92.                 print("Data saved and sent to computer 'communicator' successfully.")
  93.             else
  94.                 print("Failed to read data from file.")
  95.             end
  96.         else
  97.             print("Computer 'communicator' not set.")
  98.         end
  99.  
  100.         return true
  101.     else
  102.         return false
  103.     end
  104. end
  105.  
  106. -- Main function
  107. local function main()
  108.     -- Check if file exists, if not, create it with empty data
  109.     if not fs.exists(filename) then
  110.         local emptyData = {blockList = {}, valueList = {}}
  111.         if not writeDataToFileAndSend(emptyData) then
  112.             print("Failed to create file.")
  113.             return
  114.         end
  115.     end
  116.  
  117.     -- Read data from file
  118.     local data = readDataFromFile()
  119.  
  120.     if not data then
  121.         print("Failed to read data from file.")
  122.         return
  123.     end
  124.  
  125.     while true do
  126.         local choice = getMenuChoice()
  127.  
  128.         if choice == 1 then
  129.             addBlock(data)
  130.         elseif choice == 2 then
  131.             removeBlock(data)
  132.         elseif choice == 3 then
  133.             modifyBlockValue(data)
  134.         elseif choice == 4 then
  135.             if writeDataToFileAndSend(data) then
  136.                 print("Data saved successfully.")
  137.             else
  138.                 print("Failed to save data.")
  139.             end
  140.         elseif choice == 5 then
  141.             print("Exiting without saving.")
  142.             break
  143.         else
  144.             print("Invalid choice. Please enter a number between 1 and 5.")
  145.         end
  146.     end
  147. end
  148.  
  149. -- Run the main function
  150. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement