FireDemonite icon

getItem (computer, ChatBox)

FireDemonite | PRO | 10/07/18 02:42:28 PM UTC | 0 ⭐ | 149 👁️ | Never ⏰ | []
Lua |

3.12 KB

|

None

|

0 👍

/

0 👎

local cmd = ""
local turtleID = 0
 
local CurrItems = {
  ["Sugar Cane"] = {
   name = "minecraft:reeds",
   count = 1,
   location = {0,0},
   map = {}
  },
  ["Iron Ingot"] = {
   name = "minecraft:iron_ingot",
   count = 1,
   location = {0,1},
   map = {}
  },
}
 
 
if (fs.exists("Config.txt")) then
    local file = fs.open("Config.txt","r")
    file.readLine()
    turtleID = file.readLine()
else
    while not (tonumber(turtleID) and tonumber(turtleID) > 0) do
        term.clear()
        term.setCursorPos(1,1)
        term.write("Turtle ID: ")
        turtleID = read()
        if not (tonumber(turtleID)) then
            print("Not a number!")
            sleep(1)
        end
    end
    local file = fs.open("Config.txt","a")
    file.close()
    file = fs.open("Config.txt","w")
 
    file.writeLine("TurtleID: ")
    file.write(turtleID)
    file.close()
end
 
function fetchItem(item, itemAmount)
    if (checkItem(item)) then
        getItem = CurrItems[item]
        rednet.open("left")
        rednet.send(tonumber(turtleID), {getItem, tonumber(itemAmount)}, "FireCrafting")
        rednet.close()
        return true
    else
        return false
    end
end
 
function checkItem(item)
    if (CurrItems[item] ~= nil) then
        print(CurrItems[item])
        return true
    else
        return false
    end
end
    
chatbox = peripheral.wrap("bottom")
chatbox.setName("ItemRouter")
 
while true do
getitem = false
cmd = ""
item = ""
itemAmount = ""
term.clear()
term.setCursorPos(1,1)
print("Type 'item' to get an item, type 'list' to get a list of all the items")
--term.write("->")
--cmd = read()
event, side, name, cmd = os.pullEvent("chat_message")
for w in string.gmatch(cmd, "%d+") do
    itemAmount = w
end
for w in string.gmatch(cmd, "%a+") do
    if ( w == "getItem") then
        getitem = true
    end
    if (getitem and w ~= "getItem") then
        item = item.." "..w
    end
end
item = string.sub(item,2)
 
print("Item: "..item.." Amount: "..itemAmount)
 
if (getitem) then
    
    --if (cmd == "getItem") then
        term.clear()
        term.setCursorPos(1,1)
        --term.write("Item: ")
        sleep(1)
        --item = ""
        chatbox.say("Item: "..item)
        --item = read()
        --while n2 ~= name do
        --    e,s,n2,item = os.pullEvent("chat_message")
        --end
        term.write("Item: "..item)
        sleep(1)
        --itemAmount = ""
        chatbox.say("Amount: "..itemAmount)
        --itemAmount = read()
        --while n3 ~= name do
        --    e,s,n3,itemAmount = os.pullEvent("chat_message")
        --end
        print("ItemAmount: "..itemAmount)
        if (fetchItem(item, itemAmount)) then
            print("Getting items...")
            sleep(5)
        else
            print("Not an exisiting item")
            chatbox.say("Not a valid item")
            sleep(1)
        end
    --elseif (cmd == "list") then
      --  listItems()
    --elseif (cmd == "exit") then
      --  break
    --else
      --  print("Type 'item' or 'list'")
      --  sleep(1)
    --end
end
 
end

Comments