t8rt0t

refuel.lua

Mar 14th, 2024 (edited)
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.99 KB | None | 0 0
  1. if not stats then os.loadAPI("apis/stats.lua") end
  2. --Finds an item in the turtle's inventory
  3. --@Arg string item: The registry name of the item to search for
  4. --@Returns number slot: The slot the item was found in
  5. --@Returns None nil: If the item was not found
  6. function findItem(item)
  7.     for i=1,16 do
  8.         if turtle.getItemDetail(i) and turtle.getItemDetail(i).name == item then
  9.             return i
  10.         end
  11.     end
  12. end
  13. --Reads the list of valid fuels from valid_fuels.json
  14. --@Returns table fuels: A list of the currently known valid fuels
  15. function getValidFuels()
  16.     if not fs.exists("valid_fuels.json") then return {} end
  17.     local file = fs.open("valid_fuels.json","r")
  18.     local data = textutils.unserialiseJSON(file.readAll())
  19.     file.close()
  20.     return data or {}
  21. end
  22. --Checks if a fuel is within a table
  23. --@Arg string fuel: The registry name of the item to be checked
  24. --@OptionalArg table fuelTable: The list of valid fuels, either specified or retrieved from valid_fuels.json
  25. --@Returns boolean isValid: Whether the fuel was found to be valid or not
  26. function isValidFuel(fuel,fuelTable)
  27.     if type(fuel) ~= "string" then return false end
  28.     fuelTable = fuelTable or getValidFuels()
  29.     for k,v in pairs(fuelTable) do
  30.         if v == fuel then return true end
  31.     end
  32.     return false
  33. end
  34. --Adds a known valid fuel to the valid_fuels.json list
  35. --@Arg string fuel: The registry name of the valid fuel
  36. function addValidFuel(fuel)
  37.     if type(fuel) ~= "string" then return end
  38.     local fuelTable = getValidFuels()
  39.     if isValidFuel(fuel,fuelTable) then return end
  40.     table.insert(fuelTable,fuel)
  41.     local file = fs.open("valid_fuels.json","w")
  42.     file.write(textutils.serialiseJSON(fuelTable))
  43.     file.close()
  44. end
  45. --Item = specific item to refuel, optional
  46. --Startlevel = for recursive purposes and recording stats, also optional
  47. --Typically, if the function does not find a valid fuel, the function will also search for unrecognized fuels
  48. --However, this is rather expensive, so if you set isSoft to true, the function will just do nothing if no valid fuels are found
  49. --@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
  50. --@OptionalArg number startLevel: Used for statistics, the starting fuel level of the turtle
  51. --@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
  52. --@Returns string msg: Information about whether the turtle refuelled or not
  53. function refuel(item,startLevel,isSoft)
  54.     startLevel = tonumber(startLevel) or turtle.getFuelLevel()
  55.     --if not startLevel then startLevel = turtle.getFuelLevel() end
  56.     if not item then
  57.         local fuels = getValidFuels()
  58.         for i=1,#fuels do
  59.             local slot = findItem(fuels[i])
  60.             if slot then return refuel(fuels[i],startLevel,true) end
  61.         end
  62.     end
  63.     local slot
  64.     if tonumber(item) then slot = item
  65.     elseif type(item) == "string" then slot = findItem(item) end
  66.     if not tonumber(slot) or turtle.getItemCount(slot) <= 0 then
  67.         if not isSoft then
  68.             for i=1,16 do
  69.                 turtle.select(i)
  70.                 if turtle.refuel(0) then
  71.                     local itemAtSlot = turtle.getItemDetail(i)
  72.                     if itemAtSlot then addValidFuel(itemAtSlot.name) end
  73.                     refuel(i,startLevel,true)
  74.                 end
  75.             end
  76.         end
  77.         if turtle.getFuelLevel() > startLevel then
  78.             stats.add("Times Refueled",1)
  79.             stats.add("Amount Refueled",turtle.getFuelLevel()-startLevel)
  80.             return "refueled to " .. turtle.getFuelLevel()
  81.         end
  82.         return "Did not refuel, no fuel was found"
  83.     end
  84.     turtle.select(slot)
  85.     while turtle.getFuelLevel() + 2000 < turtle.getFuelLimit() do
  86.         turtle.refuel(1)
  87.         if turtle.getItemCount() <= 0 then return refuel(item,startLevel,isSoft) end
  88.     end
  89. end
Advertisement
Add Comment
Please, Sign In to add comment