Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Print anerror message and a stack trace.
- function err(msg)
- if msg then textutils.pagedPrint(msg) end
- textutils.pagedPrint(debug.traceback())
- error()
- end
- -- Check if item is at the selected slot, returns the detail of the item if found, else nil.
- function checkSelected(item)
- local data = turtle.getItemDetail()
- if data then
- if data.name == item then
- return data
- end
- end
- return nil
- end
- -- Count number of occurenses of type item in the inventory and return it.
- function getItemCount(item)
- local count = 0
- for i=1, 16, 1 do
- turtle.select(i)
- local data = checkSelected(item)
- if data then
- count = count + data.count
- end
- end
- return count
- end
- -- Count number of occurenses of type item in the inventory and check if it is the same or more than count. If it is less than count, pause and ask for refill.
- function hasNumItems(item, count)
- local c = getItemCount(item)
- print("Has only", c, "items of type", count, "need", count,", add items and press 'enter' when done.")
- while c < count do
- local action, key = os.pullEventRaw()
- if action == "key" then
- if key == keys.enter then
- c = getItemCount(item)
- if c >= count then
- print("Refilled!")
- return
- else
- print("That was not of type", item, ", try again and press 'enter' when done!")
- end
- end
- end
- end
- end
- -- Check if an item exists in the inventory.
- function findItem(item)
- for i=1, 16, 1 do
- turtle.select(i)
- local data = checkSelected(item)
- if data then
- return data
- end
- end
- return nil
- end
- -- Refule with fuelType if fuel level is less then minFuelLevel. If no fuel was found, pause and ask for refill.
- function refuel(fuelType, minFuelLevel, refuelAmount)
- local level = turtle.getFuelLevel()
- if level < minFuelLevel then
- local fuel = findItem(fuelType)
- local hasFuel = -1
- if turtle.refuel(refuelAmount) then hasFuel = 1 end
- while hasFuel <= 0 do
- if hasFuel == -1 then
- print("No fuel (coal) in inventory! Add fuel and press 'enter' key to continue.")
- hasFuel = 0
- end
- action, key = os.pullEventRaw()
- if action == "key" then
- if key == keys.enter then
- fuel = findItem(fuelType)
- if turtle.refuel(refuelAmount) then
- print("Refilled fuel!")
- hasFuel = 1
- else
- print("That was not fuel, try again and press 'enter' when done!")
- end
- end
- end
- end
- end
- end
- -- Get the selected slot at the specified item. If no item was found, pause and ask for refill.
- function selectItem(item)
- -- Check if item is in inventory
- local data = checkSelected(item)
- if data then
- return data
- else
- data = findItem(item)
- if data then
- return data
- end
- end
- -- Item was not found, ask for it.
- print(item, "was found, add", item, "and press 'enter' when done!")
- local hasItem = false
- while hasItem == false do
- local action, key = os.pullEventRaw()
- if action == "key" then
- if key == keys.enter then
- data = findItem(item)
- if data then
- hasItem = true
- else
- print("That was not ", item, ", try again and press 'enter' when done!")
- end
- end
- end
- end
- return selectItem(item)
- end
Add Comment
Please, Sign In to add comment