i have setup a system to get items from computer via rednet and redpower. However it works. But there is an issue i would like to find a better solution.
I use tables to print a options menu. But if i use more options then the terminal can hold the menu mess up and the program don't work anymore. I want a "NEXT PAGE" Button to print more options. I can do it with shell.run page2 but i asking if there is a better way to putt all my options in one code.
I'm new to lua and i use and change the code from another forum user here. Thank for that!
Regards
Here is my code for the host:
rednet.open ("left")
local client1_id = 83
local options = {
"Marble",
"Cobblestone",
"Basalt Cobblestone",
"Sand",
"Sandstone",
"Glass",
"Wheat",
"Dirt",
"Iron Ore",
"Copper Ore",
"Tin Ore",
"Gold Ore",
"Diamonds",
"Coal",
"NEXT PAGE",
--you can add more in here
}
local function opt(m,mY)
n=1
l=#m
while true do
for i=1, l, 1 do
if i==n then
local x, y = term.getSize()
local b = string.len(">"..m[i].."<")/2
local x = (x/2)-b
term.setCursorPos(x,i+mY)
term.clearLine()
print(">"..m[i].."<")
else
local x, y = term.getSize()
b = string.len(m[i])/2
x = (x/2)-b
term.setCursorPos(x,i+mY)
term.clearLine()
print(m[i]) end
end
a, b= os.pullEventRaw()
if a == "key" then
if b==200 and n>1 then n=n-1 end
if b==208 and n<l then n=n+1 end
if b==28 then break end
end
end
term.clear() term.setCursorPos(1,1)
return n
end
while true do
term.clear()
term.setCursorPos(1,1)
print("What item do you want?")
local item = opt(options,2) --the second parameter is the y position start point
if item == 1 then
term.clear()
term.setCursorPos(1,1)
print("How many Marble do you want?")
write("Number: ")
local number = tonumber(read())
for i = 1,number do
rednet.send(client1_id, "white") -- MARBLE
sleep(.1)
rednet.send(client1_id, "white")
sleep(.6)
end
elseif item == 2 then
term.clear()
term.setCursorPos(1,1)
print("How many Cobblestone do you want?")
write("Number: ")
local number = tonumber(read())
for i = 1,number do
rednet.send(client1_id, "black") -- COBBLESTONE
sleep(.1)
rednet.send(client1_id, "black")
sleep(.6)
end
elseif item == 3 then
term.clear()
term.setCursorPos(1,1)
print("How many Basalt Cobblestone do you want?")
write("Number: ")
local number = tonumber(read())
for i = 1,number do
rednet.send(client1_id, "orange") -- BASALT COBBLESTONE
sleep(.1)
rednet.send(client1_id, "orange")
sleep(.6)
end
elseif item == 4 then
term.clear()
term.setCursorPos(1,1)
print("How many Sand do you want?")
write("Number: ")
local number = tonumber(read())
for i = 1,number do
rednet.send(client1_id, "magenta") -- SAND
sleep(.1)
rednet.send(client1_id, "magenta")
sleep(.6)
end
elseif item == 5 then
term.clear()
term.setCursorPos(1,1)
print("How many Sandstone do you want?")
write("Number: ")
local number = tonumber(read())
for i = 1,number do
rednet.send(client1_id, "lightBlue") -- SANDSTONE
sleep(.1)
rednet.send(client1_id, "lightBlue")
sleep(.6)
end
elseif item == 6 then
term.clear()
term.setCursorPos(1,1)
print("How many Glass do you want?")
write("Number: ")
local number = tonumber(read())
for i = 1,number do
rednet.send(client1_id, "yellow") -- GLASS
sleep(.1)
rednet.send(client1_id, "yellow")
sleep(.6)
end
elseif item == 7 then
term.clear()
term.setCursorPos(1,1)
print("How many Wheat do you want?")
write("Number: ")
local number = tonumber(read())
for i = 1,number do
rednet.send(client1_id, "lime") -- WHEAT
sleep(.1)
rednet.send(client1_id, "lime")
sleep(.6)
end
elseif item == 8 then
term.clear()
term.setCursorPos(1,1)
print("How many Dirt do you want?")
write("Number: ")
local number = tonumber(read())
for i = 1,number do
rednet.send(client1_id, "gray") -- DIRT
sleep(.1)
rednet.send(client1_id, "gray")
sleep(.6)
end
elseif item == 9 then
term.clear()
term.setCursorPos(1,1)
print("How many Iron Ore do you want?")
write("Number: ")
local number = tonumber(read())
for i = 1,number do
rednet.send(client1_id, "lightGray") -- IRON ORE
sleep(.1)
rednet.send(client1_id, "lightGray")
sleep(.6)
end
elseif item == 10 then
term.clear()
term.setCursorPos(1,1)
print("How many Copper Ore do you want?")
write("Number: ")
local number = tonumber(read())
for i = 1,number do
rednet.send(client1_id, "cyan") -- COPPER ORE
sleep(.1)
rednet.send(client1_id, "cyan")
sleep(.6)
end
elseif item == 11 then
term.clear()
term.setCursorPos(1,1)
print("How many Tin Ore do you want?")
write("Number: ")
local number = tonumber(read())
for i = 1,number do
rednet.send(client1_id, "purple") -- TIN ORE
sleep(.1)
rednet.send(client1_id, "purple")
sleep(.6)
end
elseif item == 12 then
term.clear()
term.setCursorPos(1,1)
print("How many Gold Ore do you want?")
write("Number: ")
local number = tonumber(read())
for i = 1,number do
rednet.send(client1_id, "blue") -- GOLD ORE
sleep(.1)
rednet.send(client1_id, "blue")
sleep(.6)
end
elseif item == 13 then
term.clear()
term.setCursorPos(1,1)
print("How many Diamonds do you want?")
write("Number: ")
local number = tonumber(read())
for i = 1,number do
rednet.send(client1_id, "brown") -- DIAMONDS
sleep(.1)
rednet.send(client1_id, "brown")
sleep(.6)
end
elseif item == 14 then
term.clear()
term.setCursorPos(1,1)
print("How many Coal do you want?")
write("Number: ")
local number = tonumber(read())
for i = 1,number do
rednet.send(client1_id, "green") -- COAL
sleep(.1)
rednet.send(client1_id, "green")
sleep(.6)
end
---
elseif item == 99 then
term.clear()
term.setCursorPos(1,1)
sleep(.2)
shell.run("item2") -- Next Page
close()
end
end