Advertisement
lucasmontec

tankDisplay

Jul 5th, 2025 (edited)
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.09 KB | None | 0 0
  1. -- Full script updated to strip mod names from fluid labels
  2.  
  3. local tanks = {
  4.     {
  5.         amount = 5000,
  6.         capacity = 10000,
  7.         fluid = "mod:water"
  8.     },
  9.     {
  10.         amount = 2500,
  11.         capacity = 5000,
  12.         fluid = "mod:submod:lava",
  13.         color = "red"
  14.     },
  15. }
  16.  
  17. local function renderTank(x, y, widthPercent, heightPercent, tank)
  18.     local w, h = term.getSize()
  19.     local width = math.max(1, math.floor(w * widthPercent))
  20.     local height = math.max(1, math.floor(h * heightPercent))
  21.  
  22.     local fillRatio = tank.amount / tank.capacity
  23.     local filledHeight = math.floor(height * fillRatio)
  24.  
  25.     local fluidColorMap = {
  26.         ["blue"] = colors.blue,
  27.         ["red"] = colors.red,
  28.         default = colors.green
  29.     }
  30.     local fillColor = fluidColorMap[tank.color] or fluidColorMap.default
  31.  
  32.     -- Extract only the last part of the fluid name after the last colon
  33.     local label = tank.fluid:match("([^:]+)$")
  34.     local labelWidth = #label
  35.     local centeredX = x + math.floor((labelWidth - width) / 2)
  36.  
  37.     for i = 1, height do
  38.         term.setCursorPos(centeredX, y + height - i)
  39.         if i <= filledHeight then
  40.             term.setBackgroundColor(fillColor)
  41.         else
  42.             term.setBackgroundColor(colors.gray)
  43.         end
  44.         term.write(string.rep(" ", width))
  45.     end
  46.  
  47.     -- Draw fill percent inside bar at top of filled section, centered
  48.     if filledHeight > 0 then
  49.         local percentText = string.format("%d%%", fillRatio * 100)
  50.         local textX = centeredX + math.floor((width - #percentText) / 2)
  51.         term.setCursorPos(textX, y + height - filledHeight)
  52.         term.setBackgroundColor(fillColor)
  53.         term.setTextColor(colors.white)
  54.         term.write(percentText)
  55.         term.setTextColor(colors.white)
  56.     end
  57.  
  58.     term.setBackgroundColor(colors.black)
  59.  
  60.     -- Print fluid name below
  61.     term.setCursorPos(x, y + height + 1)
  62.     term.write(label)
  63. end
  64.  
  65. term.clear()
  66. renderTank(4, 2, 0.1, 0.8, tanks[1])
  67. renderTank(25, 2, 0.1, 0.8, tanks[2])
  68.  
  69. -- Let me know next enhancements or auto-refresh loop requirements.
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement