RJ-Bradach

Fuel Checker API

Jun 24th, 2021 (edited)
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.41 KB | Gaming | 0 0
  1. -- Fuel Check API --------------------------------------------------------------------------------------------------------------------
  2. -- API to check a turtles fuel level and refuel itself
  3. -- This API contains one function: refuel()
  4. -- The refuel function takes has one parameter named fuelMargin.
  5. -- The user can input a margin for fuel in units of moves. This margin is compared against the current fuel level of the turtle.
  6. -- If the fuel margin (number of moves) is less than the fuel level of the turtle,
  7. -- or no fuel margin is provided, the turtle will attempt to refuel itself
  8. -- If the turtle finds combustable items in its inventory it will refuel with them, checking all inventory slots for combustable items.
  9. -- After refueling, if the turtle's fuel level is still below the fuel margin (if provided), a message will be printed with fuel information
  10.  
  11. function refuel(fuelMargin)
  12.     local isFueled = true
  13.     if (fuelMargin and turtle.getFuelLevel() < fuelMargin or fuelMargin == nil) then
  14.         isFueled = false
  15.         for i = 1, 16 do
  16.             turtle.select(i)
  17.             local is_fuel = turtle.refuel(0)
  18.             if is_fuel then
  19.                 turtle.refuel(64)
  20.             end
  21.         end
  22.         local fuelLevel = turtle.getFuelLevel()
  23.         if (fuelMargin and fuelLevel < fuelMargin ) then
  24.             print("Fuel is still too low\nFuel Margin: " .. fuelMargin .. "\nFuel Level: " .. fuelLevel .. "\nFuel Needed: " .. fuelMargin-fuelLevel)
  25.             return isFueled
  26.         end
  27.     end
  28. end
  29.  
Tags: minecraft
Add Comment
Please, Sign In to add comment