Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local filename = "block_data.txt"
- local communicator = 0 -- Initially set to 0
- -- Function to read data from file
- local function readDataFromFile()
- local file = fs.open(filename, "r") -- Open file in read mode
- if file then
- local data = textutils.unserialize(file.readAll()) -- Read all content and deserialize
- file.close() -- Close the file
- return data
- else
- return nil
- end
- end
- -- Function to display menu and get user choice
- local function getMenuChoice()
- print("Modify Block Data Menu:")
- print("1. Add block")
- print("2. Remove block")
- print("3. Modify block value")
- print("4. Save and send data")
- print("5. Exit without saving")
- write("Enter your choice: ")
- return tonumber(read())
- end
- -- Function to add a block
- local function addBlock(data)
- write("Enter block name: ")
- local blockName = read()
- write("Enter block value: ")
- local blockValue = tonumber(read())
- table.insert(data.blockList, blockName)
- table.insert(data.valueList, blockValue)
- print("Block added successfully.")
- end
- -- Function to remove a block
- local function removeBlock(data)
- print("Current block list:")
- for i, blockName in ipairs(data.blockList) do
- print(i .. ". " .. blockName)
- end
- write("Enter the index of the block to remove: ")
- local index = tonumber(read())
- if index and index >= 1 and index <= #data.blockList then
- table.remove(data.blockList, index)
- table.remove(data.valueList, index)
- print("Block removed successfully.")
- else
- print("Invalid index.")
- end
- end
- -- Function to modify block value
- local function modifyBlockValue(data)
- print("Current block list:")
- for i, blockName in ipairs(data.blockList) do
- print(i .. ". " .. blockName .. " (Value: " .. data.valueList[i] .. ")")
- end
- write("Enter the index of the block to modify: ")
- local index = tonumber(read())
- if index and index >= 1 and index <= #data.blockList then
- write("Enter the new value for the block: ")
- local newValue = tonumber(read())
- if newValue then
- data.valueList[index] = newValue
- print("Block value modified successfully.")
- else
- print("Invalid value.")
- end
- else
- print("Invalid index.")
- end
- end
- -- Function to write data to file and send it to a computer
- local function writeDataToFileAndSend(data)
- local file = fs.open(filename, "w") -- Open file in write mode
- if file then
- file.write(textutils.serialize(data)) -- Serialize data and write to file
- file.close() -- Close the file
- -- Send the file to the computer named "communicator"
- if communicator ~= 0 then
- local fileContent = fs.open(filename, "r")
- if fileContent then
- local content = fileContent.readAll()
- fileContent.close()
- rednet.send(communicator, content)
- print("Data saved and sent to computer 'communicator' successfully.")
- else
- print("Failed to read data from file.")
- end
- else
- print("Computer 'communicator' not set.")
- end
- return true
- else
- return false
- end
- end
- -- Main function
- local function main()
- -- Check if file exists, if not, create it with empty data
- if not fs.exists(filename) then
- local emptyData = {blockList = {}, valueList = {}}
- if not writeDataToFileAndSend(emptyData) then
- print("Failed to create file.")
- return
- end
- end
- -- Read data from file
- local data = readDataFromFile()
- if not data then
- print("Failed to read data from file.")
- return
- end
- while true do
- local choice = getMenuChoice()
- if choice == 1 then
- addBlock(data)
- elseif choice == 2 then
- removeBlock(data)
- elseif choice == 3 then
- modifyBlockValue(data)
- elseif choice == 4 then
- if writeDataToFileAndSend(data) then
- print("Data saved successfully.")
- else
- print("Failed to save data.")
- end
- elseif choice == 5 then
- print("Exiting without saving.")
- break
- else
- print("Invalid choice. Please enter a number between 1 and 5.")
- end
- end
- end
- -- Run the main function
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement