table and files

Started by draddy, Jan 08, 2020, 09:31 pm

Previous topic - Next topic

draddy

Jan 08, 2020, 09:31 pm Last Edit: Jan 08, 2020, 09:37 pm by draddy
hi out there,
im new in this forum - and, at some points my english may isn't the best - hope its ok ;)

just make a program to emit some items in my ME system and send out a redstone signal to produce more (most basic thinks for energy generation ... )

the tool reads in a file with the names and emit values, in the next step i want to use a 2nd Computer terminal, to add items to the list ...

lets have a look to the list file
Code Select
{"Nether Wart","minecraft:nether_wart","0",500*64, 600*64, "low", "red", false}
{"Rotten Flesh","minecraft:rotten_flesh","0",1000*64, 1100*64, "low", "green", false}

lets translate:
[1] = display name, [2]mc name, [3]damage, [4]min count, [5]max count, [6] emit signal below "low) oder above "high" the count, [7] color of redstone signal, [8] bool to set signal on or off on init

the emit program will read it via
Code Select
function readFile ()
 runs = true
 meItems = {}
    while runs do
 f = fs.open("disk/emitList", "r")
        if f then runs = false else err1="no file@ ".. getTime() end
    end
    if err1 then print(err1) end
    err1 = nil
 while running do

 t = f.readLine()
 if t == nil then
 running = false

 else
 tT = textutils.unserialise(t)
 table.insert(meItems, tT)
 end
 
 end
 running = true
end

works fine so far ...

now i want to setup a 2nd computer attached to a minichest, put a item in chest and can add this item to the list.

Code Select
--check for item in chest
function checkChest()
 if chest then cItem = chest.list() end
end

--find item in me system
function checkMe()
 if cItem then sItem = me.findItem(cItem[1].name .."@" ..cItem[1].damage).getMetadata() end
end

--ask User and return answer
function getUserInput(ask)
 print(ask)
 answer = read()
 return answer
end
--{"Nether Wart","minecraft:nether_wart","0",500*64, 600*64, "low", "red", false}
function main()
 monitor.clear()
 monitor.setCursorPos(1,2)
 monitor.write("Put item in chest,")
 monitor.setCursorPos(1,3)
 monitor.write("and follow terminal")
 while true do
    userRead = {}
 cItem ={}
 checkChest()
 term.clear()
 term.setCursorPos(1,1)
 if #cItem == 0 then
 print("please put the Item you want to add in the Chest")
 else
 checkMe()
 ask = "found item ".. sItem.displayName .. " in chest. Add to emitList?"
 ans = getUserInput(ask)
 if string.upper(ans) == "YES" or string.upper(ans) == "Y" or string.upper(ans) == "JA" or string.upper(ans) == "J" then
 userRead[1] = sItem.displayName
 userRead[2] = sItem.name
 userRead[3] = sItem.damage
 ask = "please enter the lower item limit"
 ans = getUserInput(ask)
 if tonumber(ans) ~= nil then userRead[4] = tonumber(ans) else print(ans .." is not a number, set to 0") userRead[4] = 0 end
 ask = "please enter the upper item limit"
 ans = getUserInput(ask)
 if tonumber(ans) ~= nil then userRead[5] = tonumber(ans) else print(ans .." is not a number, set to 1000") userRead[5] = 1000 end
 ask = "emit signal if (a)bove oder (b)elow limit?"
 ans = getUserInput(ask)
 if string.upper(ans) == "B" then userRead[6] = "low" elseif string.upper(ans) == "A" then userRead[6] = "high" else userRead[6] = "low" end
 ask = "what signal color?"
 ans = getUserInput(ask)
 userRead[7] = ans
 userRead[8] = false
 end
 rs.setOutput("bottom", true)
 end
        if #userRead == 8 then
 print(userRead[1] ..", "..userRead[2] ..", "..userRead[3] ..", "..userRead[4] ..", "..userRead[5] ..", "..userRead[6] ..", "..userRead[7])
 ask = "write to file ?"
 ans = getUserInput(ask)
 if string.upper(ans) == "YES" or string.upper(ans) == "Y" or string.upper(ans) == "JA" or string.upper(ans) == "J" then
 fw = fs.open(file, "a")
 fw.writeLine(userRead)
 fw.close()
 end
 end
 sleep(10)
        rs.setOutput("bottom", false)
 end
end

works also - without writing it to my file, tryed this and that but in my file i always get an input like
Code Select
{
 "Bone Meal",
 "minecraft:dye",
 "15",
 100000,
 120000,
 "low",
 "brown",
 false
 }

at this point i hang - would like to have it in one line - like my manuel list is - at least, i can't read that back to my system. i've tried f.readAll() - but i can't wrap it back to an matrix. the "original" 1 line = 1 item i can read with meItems[x][y] so meItems[2][1] will be Rotten Flesh.

and know will the noob ask you pros: how can i get rid of that?

i also got in some lua tests: values are changed, so display name wasn't 1st but maybe 3rd and so on .. ok that could be done by adding 1="Rotten Felsh", 2="minecraft ..." ... but, realy need it?

thanks and sorry for big first post ;)
draddy

€ most print() are for debug only ;)

Lupus590

Jan 09, 2020, 10:52 am #1 Last Edit: Jan 09, 2020, 10:56 am by Lupus590
If you want the two computers to share data then you are better off using rednet than the filesystem.

Since the two computers are reading the same chest you might want to get rid of the second computer altogether and use the parallel API instead, you might be able to rewrite your program to not even need the parallel API.


Also, as a side note, computers don't share files unless they are using a shared disk drive or some file over rednet system. While there is a way to get them to share files this is considered a glitch and requires op commands to do.

draddy

hi,
my setup looks like that:

Comp1 is doing the main job, reads the list, find items in the ME, and emit the redstone signal, and repeat that until some error occure i didn't capture (if that happens i alway look to find a way to capture ^^)
Comp1 is connectet to a Monitor to print the item counts and give a info of the redstone signal (aktiv / inaktiv) - also via cable modem connected to a diskdrive.

comp2 via cable modem to the same diskdrive and to the "input" chest.

if i use edit disk/emitList and add a new item manual its all fine. few seconds after saving the list the new item appear on the monitor and the redstone signal is set (or not, if enough on stock ... )

if it helps i can add all files.

thanks
draddy

draddy

hi,
for some other reasons i do a little change to my list file
Code Select
{displayName = "Nether Wart", name = "minecraft:nether_wart", damage = "0", min = 500*64, max = 600*64,emit = "low", color = "red", initEmit = false}
and changed the way to write the user input into my list file:
Code Select
fw.writeLine("{displayName = "..textutils.serialise(userRead.displayName) .. ", name = " ..textutils.serialise(userRead.name) .. ", damage = " ..textutils.serialise(tostring(userRead.damage)) .. ", min = " ..textutils.serialise(userRead.min) .. ", max = " ..textutils.serialise(userRead.max) .. ", emit = " ..textutils.serialise(userRead.emit) .. ", color = " ..textutils.serialise(userRead.color) .. ", initEmit = " ..textutils.serialise(userRead.initEmit) .."}")
looks horrible - but in this way it is clean and readable in my list file ...

still happy for suggestions

Lupus590

seeing all your code will help. it sounds like your item scanner is having the issues

but really you should not have two computers accessing the same disk and should use something like rednet instead

draddy

hi,

so here is the multiEmitter:
https://pastebin.com/9buRTWSk

and this is the "feeder"
https://pastebin.com/qfgu7CFt

the list just looks like:
Code Select
{displayName = "Nether Wart", name = "minecraft:nether_wart", damage = "0", min = 500*64, max = 600*64,emit = "low", color = "red", initEmit = false}
{displayName = "Rotten Flesh", name = "minecraft:rotten_flesh", damage = "0", min = 1000*64, max = 1100*64,emit = "low", color = "green", initEmit = false}

emitter only read the file, and only if it has changed since last read.
feeder only write to the file, if user add an new Item.

thx
draddy