Is it possible to make a server to store data and other files in minecraft? Can you send files and directories and such over rednet? If so HOW
Thank you
Server inside Minecraft
Started by tfoote, Jun 11 2012 01:47 AM
6 replies to this topic
#1
Posted 11 June 2012 - 01:47 AM
#2
Posted 11 June 2012 - 04:23 AM
You can send files over rednet by using the FileSystem and Rednet API's. To send a file you'll have to open the file first with fs.open() in 'read' mode.
Then you must read the file and create a variable to store the file in temporarily.
Then use the rednet API to send the "file".
As for sending directories, no, there's no way to send an entire directory with files inside. What you need to do is check the type of each entry in the computer to determine if it is a directory or file, then send a command to the receiving computer to create the directory, which you can then add files to. I can't remember off of the top of my head what the code for that is, as minecraft isn't opening for me at the moment.
file = fs.open(location, mode)
Example code:
file = fs.open("helloworld", r)
Then you must read the file and create a variable to store the file in temporarily.
local var = file.readAll() file.close() -- IMPORTANT
Then use the rednet API to send the "file".
rednet.send(id, file) Example code: rednet.send(0, passwordData)
As for sending directories, no, there's no way to send an entire directory with files inside. What you need to do is check the type of each entry in the computer to determine if it is a directory or file, then send a command to the receiving computer to create the directory, which you can then add files to. I can't remember off of the top of my head what the code for that is, as minecraft isn't opening for me at the moment.
#3
Posted 11 June 2012 - 04:56 AM
Bossman201, on 11 June 2012 - 04:23 AM, said:
As for sending directories, no, there's no way to send an entire directory with files inside.
local fAllFiles = fs.list("/")
for i, fItem in pairs(fAllFiles) do
if fs.isDir(fItem) then
local fDirFiles = fs.list(fItem)
for ij, fDirItem in pairs(fDirFiles) do
local fDirItemRead = fs.open(fDirItem, "r")
if fDirItemRead then
rednet.broadcast(fDirItemRead.readAll())
fDirItemRead.close()
else
error("Error when handling file in directory '"..fItem.."'.nFile deleted while processing?")
end
end
else
local fItemRead = fs.open(fItem, "r")
if fItemRead then
rednet.broadcast(fItemRead.readAll())
fItemRead.close()
else
error("Error when handling file '"..fItem.."'.nFile deleted while processing?")
end
end
end
#4
Posted 11 June 2012 - 03:01 PM
Thank you both but i don't get the directories thing... can you put "--" and explain it
#6
Posted 12 June 2012 - 02:49 AM
tfoote, on 11 June 2012 - 03:01 PM, said:
Thank you both but i don't get the directories thing... can you put "--" and explain it
local fAllFiles = fs.list("/") -- Now fAllFiles is a table of all files on the computer.
for i, fItem in pairs(fAllFiles) do -- Loop through each file in fAllFiles
if fs.isDir(fItem) then -- If the file is a directory
local fDirFiles = fs.list(fItem) -- Now fDirFiles is a table of all files in the directory
for ij, fDirItem in pairs(fDirFiles) do -- Loop through each file in fDirFiles
local fDirItemRead = fs.open(fDirItem, "r") -- Open the current "selected" file in read mode
if fDirItemRead then -- If the file exists / no errors when opening
rednet.broadcast(fDirItemRead.readAll()) -- Broadcast the file contents
fDirItemRead.close() -- Close the file handler
else -- Else (if the file doesn't exist / there is an error)
error("Error when handling file in directory '"..fItem.."'.nFile deleted while processing?") -- Quit program with error message inside "s
end -- End
end -- same, lol
else -- If the file isn't a directory
local fItemRead = fs.open(fItem, "r") -- Open the current "selected" file in read mode
if fItemRead then -- If the file exists / no errors when opening
rednet.broadcast(fItemRead.readAll())
fItemRead.close()
else -- Else (if the file doesn't exist / there is an error)
error("Error when handling file '"..fItem.."'.nFile deleted while processing?") -- Quit program with error message inside "s
end -- What the End comment said, k? I'm gonna stop these comments now.
end
end
=)
#7
Posted 12 June 2012 - 03:40 AM
thanks
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











