Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if not stats then os.loadAPI("apis/stats.lua") end
- --Finds an item in the turtle's inventory
- --@Arg string item: The registry name of the item to search for
- --@Returns number slot: The slot the item was found in
- --@Returns None nil: If the item was not found
- function findItem(item)
- for i=1,16 do
- if turtle.getItemDetail(i) and turtle.getItemDetail(i).name == item then
- return i
- end
- end
- end
- --Reads the list of valid fuels from valid_fuels.json
- --@Returns table fuels: A list of the currently known valid fuels
- function getValidFuels()
- if not fs.exists("valid_fuels.json") then return {} end
- local file = fs.open("valid_fuels.json","r")
- local data = textutils.unserialiseJSON(file.readAll())
- file.close()
- return data or {}
- end
- --Checks if a fuel is within a table
- --@Arg string fuel: The registry name of the item to be checked
- --@OptionalArg table fuelTable: The list of valid fuels, either specified or retrieved from valid_fuels.json
- --@Returns boolean isValid: Whether the fuel was found to be valid or not
- function isValidFuel(fuel,fuelTable)
- if type(fuel) ~= "string" then return false end
- fuelTable = fuelTable or getValidFuels()
- for k,v in pairs(fuelTable) do
- if v == fuel then return true end
- end
- return false
- end
- --Adds a known valid fuel to the valid_fuels.json list
- --@Arg string fuel: The registry name of the valid fuel
- function addValidFuel(fuel)
- if type(fuel) ~= "string" then return end
- local fuelTable = getValidFuels()
- if isValidFuel(fuel,fuelTable) then return end
- table.insert(fuelTable,fuel)
- local file = fs.open("valid_fuels.json","w")
- file.write(textutils.serialiseJSON(fuelTable))
- file.close()
- end
- --Item = specific item to refuel, optional
- --Startlevel = for recursive purposes and recording stats, also optional
- --Typically, if the function does not find a valid fuel, the function will also search for unrecognized fuels
- --However, this is rather expensive, so if you set isSoft to true, the function will just do nothing if no valid fuels are found
- --@OptionalArg string item: The registry name of the item to be refueled, or if not found, will instead try to refuel using a valid fuel
- --@OptionalArg number startLevel: Used for statistics, the starting fuel level of the turtle
- --@OptionalArg boolean isSoft: If true, the turtle will not try to search for new fuel types, otherwise, it scans all of its items, which takes a bit of time
- --@Returns string msg: Information about whether the turtle refuelled or not
- function refuel(item,startLevel,isSoft)
- startLevel = tonumber(startLevel) or turtle.getFuelLevel()
- --if not startLevel then startLevel = turtle.getFuelLevel() end
- if not item then
- local fuels = getValidFuels()
- for i=1,#fuels do
- local slot = findItem(fuels[i])
- if slot then return refuel(fuels[i],startLevel,true) end
- end
- end
- local slot
- if tonumber(item) then slot = item
- elseif type(item) == "string" then slot = findItem(item) end
- if not tonumber(slot) or turtle.getItemCount(slot) <= 0 then
- if not isSoft then
- for i=1,16 do
- turtle.select(i)
- if turtle.refuel(0) then
- local itemAtSlot = turtle.getItemDetail(i)
- if itemAtSlot then addValidFuel(itemAtSlot.name) end
- refuel(i,startLevel,true)
- end
- end
- end
- if turtle.getFuelLevel() > startLevel then
- stats.add("Times Refueled",1)
- stats.add("Amount Refueled",turtle.getFuelLevel()-startLevel)
- return "refueled to " .. turtle.getFuelLevel()
- end
- return "Did not refuel, no fuel was found"
- end
- turtle.select(slot)
- while turtle.getFuelLevel() + 2000 < turtle.getFuelLimit() do
- turtle.refuel(1)
- if turtle.getItemCount() <= 0 then return refuel(item,startLevel,isSoft) end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment