This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
tfoote's profile picture

Server inside Minecraft

Started by tfoote, 11 June 2012 - 12:47 AM
tfoote #1
Posted 11 June 2012 - 02:47 AM
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
Bossman201 #2
Posted 11 June 2012 - 05: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.
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.
cant_delete_account #3
Posted 11 June 2012 - 05:56 AM
As for sending directories, no, there's no way to send an entire directory with files inside.
Ohreally?:

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
tfoote #4
Posted 11 June 2012 - 04:01 PM
Thank you both but i don't get the directories thing… can you put "–" and explain it
Bossman201 #5
Posted 12 June 2012 - 01:39 AM
Ohreally?:
Wow, didn't know that was possible.
cant_delete_account #6
Posted 12 June 2012 - 03:49 AM
Thank you both but i don't get the directories thing… can you put "–" and explain it
Sure:

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
=)
tfoote #7
Posted 12 June 2012 - 04:40 AM
thanks