jille_Jr

CC: Furnace monitor (via Quark)

Nov 21st, 2020 (edited)
1,717
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.71 KB | None | 0 0
  1. --furnacemonitor.lua
  2.  
  3. --(( Settings ))--
  4.  
  5. local textScale = 0.5
  6.  
  7. ---@class FurnaceMonitorSides
  8. ---@field topFurn string
  9. ---@field monitor string
  10. ---@field botFurn string
  11. local _
  12.  
  13. ---@type FurnaceMonitorSides[]
  14. local furnaceMonitorSides = {
  15.   {
  16.     topFurn = "minecraft:furnace_11817",
  17.     monitor = "monitor_4475",
  18.     botFurn = "minecraft:furnace_11810",
  19.   },
  20.   {
  21.     topFurn = "minecraft:furnace_11815",
  22.     monitor = "monitor_4473",
  23.     botFurn = "minecraft:furnace_11812",
  24.   },
  25.   {
  26.     topFurn = "minecraft:furnace_11813",
  27.     monitor = "monitor_4474",
  28.     botFurn = "minecraft:furnace_11809",
  29.   },
  30.   {
  31.     topFurn = "minecraft:furnace_11818",
  32.     monitor = "monitor_4476",
  33.     botFurn = "minecraft:furnace_11819",
  34.   },
  35.   {
  36.     topFurn = "minecraft:furnace_11820",
  37.     monitor = "monitor_4477",
  38.     botFurn = "minecraft:furnace_11821",
  39.   },
  40.   {
  41.     topFurn = "minecraft:furnace_11822",
  42.     monitor = "monitor_4478",
  43.     botFurn = "minecraft:furnace_11823",
  44.   },
  45. }
  46.  
  47. --(( Types & Static variables ))--
  48.  
  49. local furnaceBurnableSlot = 1
  50. local furnaceFuelSlot = 2
  51. local furnaceResultSlot = 3
  52.  
  53. local ticksPerSecond = 20
  54. local ticksPerCookedItem = 10 * ticksPerSecond
  55.  
  56. local orientation = {
  57.   top = 0,
  58.   bottom = 1,
  59. }
  60.  
  61. ---@class FurnaceMonitor
  62. ---@field furn quarkFurnace
  63. ---@field redirect Redirect
  64. ---@field orientation integer
  65. local _
  66.  
  67. --(( Functions ))--
  68.  
  69. ---@param periphType string
  70. ---@param side string
  71. ---@return Peripheral
  72. local function getPeripheralOfType(periphType, side)
  73.   local actualType = peripheral.getType(side)
  74.   local periph = peripheral.wrap(side)
  75.  
  76.   if not actualType or not periph then
  77.     error(string.format("Expected %s at '%s', got nil.", periphType, side))
  78.   end
  79.   if actualType ~= periphType then
  80.     error(string.format("Expected %s at '%s', got %s.", periphType, side, actualType))
  81.   end
  82.  
  83.   return periph
  84. end
  85.  
  86. ---@param side string
  87. ---@return Monitor
  88. local function getMonitor(side)
  89.   return getPeripheralOfType("monitor", side)
  90. end
  91.  
  92. ---@param side string
  93. ---@return quarkFurnace
  94. local function getFurnace(side)
  95.   return getPeripheralOfType("minecraft:furnace", side)
  96. end
  97.  
  98. ---@param furn quarkFurnace
  99. ---@param redirect Redirect
  100. ---@param orientation integer
  101. ---@return FurnaceMonitor
  102. local function createFurnMon(furn, redirect, orientation)
  103.   ---@type FurnaceMonitor
  104.   local furnMon = {}
  105.   furnMon.furn = furn
  106.   furnMon.redirect = redirect
  107.   furnMon.orientation = orientation
  108.   return furnMon
  109. end
  110.  
  111. ---@param source Redirect
  112. ---@param windowHeight integer
  113. ---@return Window,Window
  114. local function createWindows(source, windowHeight)
  115.   local maxW,maxH = source.getSize()
  116.   local botY = maxH - windowHeight + 1
  117.  
  118.   local topWindow = window.create(
  119.     source, 1, 1, maxW, windowHeight
  120.   )
  121.   local botWindow = window.create(
  122.     source, 1, botY, maxW, windowHeight
  123.   )
  124.  
  125.   return topWindow, botWindow
  126. end
  127.  
  128. ---@param col1 string
  129. ---@param col2 string
  130. ---@param barWidth integer
  131. ---@param fullWidth integer
  132. local function getBlittableBarColors(col1, col2, barWidth, fullWidth)
  133.   return col1:rep(barWidth) .. col2:rep(fullWidth - barWidth)
  134. end
  135.  
  136. ---@param r Redirect
  137. ---@param title string
  138. ---@param x integer
  139. ---@param y integer
  140. ---@param width integer
  141. ---@param ticksPassed integer
  142. ---@param ticksTotal integer
  143. ---@param rounding fun(ticks:number):integer
  144. local function drawTicksProgress(r, title, x, y, width, ticksPassed, ticksTotal, rounding)
  145.   local perc = ticksPassed / ticksTotal
  146.   if ticksTotal == 0 then
  147.     perc = 0
  148.   end
  149.   local barWidth = (rounding or math.ceil)(width * perc)
  150.   local percX = width - #"100%"
  151.  
  152.   -- constructs string like:
  153.   -- " 12 Coal        56% "
  154.   local str =
  155.     table.concat {
  156.       title or "",
  157.       string.rep(" ", width),
  158.     }:sub(1, percX)
  159.     .. string.format("%3.f%%", 100*perc)
  160.  
  161.   local bg = getBlittableBarColors("e", "7", barWidth, width)
  162.   local fg
  163.   if not title then
  164.     fg = getBlittableBarColors("8", "1", barWidth, width)
  165.   else
  166.     fg = getBlittableBarColors("0", "1", barWidth, width)
  167.   end
  168.  
  169.   -- draw bar
  170.   r.setCursorPos(x, y)
  171.   r.blit(str, fg, bg)
  172. end
  173.  
  174. ---@param inv quarkInventory
  175. ---@param slot integer
  176. ---@return string|nil
  177. local function getItemDisplayName(inv, slot)
  178.   ---@type quarkItemMeta
  179.   local item = inv.getItemMeta(slot)
  180.   if item then
  181.     return string.format("%-2i %s", item.count, item. displayName)
  182.   end
  183. end
  184.  
  185. ---@param inv quarkInventory
  186. ---@return integer,integer
  187. local function calcResultItemCounts(inv)
  188.   ---@type quarkItemMeta
  189.   local resultItems = inv.getItemMeta(furnaceResultSlot)
  190.   ---@type quarkItemObj
  191.   local burnableItems = inv.getItemMeta(furnaceBurnableSlot)
  192.  
  193.   local itemCount = resultItems and resultItems.count or 0
  194.   local maxCount = math.min(
  195.     resultItems and resultItems.maxCount or 0,
  196.     itemCount + (burnableItems and burnableItems.count or 0)
  197.   )
  198.  
  199.   return itemCount, maxCount
  200. end
  201.  
  202. ---@param furnMon FurnaceMonitor
  203. local function drawFurnace(furnMon)
  204.   local f = furnMon.furn
  205.   local r = furnMon.redirect
  206.  
  207.   local x = 1
  208.   local w,h = r.getSize()
  209.   local barWidth = w
  210.   local doneY, burnableY, fuelY, resultY = 1, 2, 4, 3
  211.   if furnMon.orientation == orientation.bottom then
  212.     doneY = h
  213.   end
  214.  
  215.   local burnable = getItemDisplayName(f, furnaceBurnableSlot)
  216.   drawTicksProgress(r, burnable, x, burnableY, barWidth, f.getCookTime(), ticksPerCookedItem, math.floor)
  217.  
  218.   local fuel = getItemDisplayName(f, furnaceFuelSlot)
  219.   drawTicksProgress(r, fuel, x, fuelY, barWidth, f.getRemainingBurnTime(), f.getBurnTime(), math.ceil)
  220.  
  221.   local result = getItemDisplayName(f, furnaceResultSlot)
  222.   local resultItemCount, resultMaxItems = calcResultItemCounts(f)
  223.   drawTicksProgress(r, result, x, resultY, barWidth, resultItemCount, resultMaxItems, math.floor)
  224.  
  225.   local bg = colors.black
  226.   if resultItemCount == resultMaxItems and resultItemCount > 0 then
  227.     bg = colors.lime
  228.   end
  229.   r.setBackgroundColor(bg)
  230.   r.setCursorPos(1,doneY)
  231.   r.write(string.rep(" ", w))
  232. end
  233.  
  234. --(( Main program ))--
  235.  
  236. ---@type FurnaceMonitor[]
  237. local furnMons = {}
  238.  
  239. for _,v in pairs(furnaceMonitorSides) do
  240.   ---@type FurnaceMonitorSides
  241.   local sides = v
  242.  
  243.   local m = getMonitor(sides.monitor)
  244.   local topFurn = getFurnace(sides.topFurn)
  245.   local botFurn = getFurnace(sides.botFurn)
  246.  
  247.   m.setBackgroundColor(colors.black)
  248.   m.clear()
  249.  
  250.   m.setTextScale(textScale)
  251.   local _,h = m.getSize()
  252.   local rowHeight = math.floor(h / 2)
  253.  
  254.   local topW,botW = createWindows(m, rowHeight)
  255.   table.insert(furnMons, createFurnMon(topFurn, topW, orientation.top))
  256.   table.insert(furnMons, createFurnMon(botFurn, botW, orientation.bottom))
  257. end
  258.  
  259. while true do
  260.   for _,v in ipairs(furnMons) do
  261.     drawFurnace(v)
  262.   end
  263.   sleep(.5)
  264. end
  265.  
  266. --(( EOF ))--
  267.  
Advertisement
Add Comment
Please, Sign In to add comment