jille_Jr icon

CC: Turtle label that shows fuel level

jille_Jr | PRO | 11/15/20 04:19:38 PM UTC (Edited) | 0 ⭐ | 1143 👁️ | Never ⏰ | []
Lua |

1.03 KB

|

None

|

0 👍

/

0 👎

--- Save this to some file (ex: /utils/turtle2.lua)
--- then run this ONCE in startup.lua:
---   shell.run("turtle2.lua")
---
--- If your turtle is named "Bob"
--- Then with this script, your turtle will be
--- renamed to "Bob (123)", if it has 123 in fuel level
---
--- The label is automatically updated again when
--- the turtle consumes fuel.
 
local _turtle = {}
local name = os.getComputerLabel() or ""
name = name:match("(.+) %(%d+%)") or name
 
for key,value in pairs(turtle) do
    _turtle[key] = value
end
 
local function updateLabel()
    os.setComputerLabel(
        table.concat {
            name,
            " (",
            _turtle.getFuelLevel(),
            ")"
        }
    )
end
 
local function wrapFuelUse(f)
    return function(...)
        local result = {f(...)}
        updateLabel()
        return unpack(result)
    end
end
 
for _, fname in ipairs {
    "forward",
    "back",
    "up",
    "down",
    "refuel"
} do
    turtle[fname] = wrapFuelUse(_turtle[fname])
end
 
updateLabel()

Comments