From c280f8b9d608ca98698818925921645532ab61ba Mon Sep 17 00:00:00 2001 From: Robert Jelic <36573031+NoryiE@users.noreply.github.com> Date: Fri, 24 Jun 2022 22:11:27 +0200 Subject: [PATCH] Create progressBarEnergyExample.lua --- examples/progressBarEnergyExample.lua | 52 +++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 examples/progressBarEnergyExample.lua diff --git a/examples/progressBarEnergyExample.lua b/examples/progressBarEnergyExample.lua new file mode 100644 index 0000000..5740e5c --- /dev/null +++ b/examples/progressBarEnergyExample.lua @@ -0,0 +1,52 @@ +-- This is a example on how to use progressbars for energy. I used the Mekanism Ultimate Energy Cube. + + +local filePath = "basalt.lua" --here you can change the file path default: basalt.lua +if not(fs.exists(filePath))then + shell.run("pastebin run ESs1mg7P "..filePath) -- this is an alternative to the wget command +end +local basalt = dofile(filePath) -- here you can change the variablename in any variablename you want default: basalt + +local energyCube = peripheral.find("ultimateEnergyCube") + +local main = basalt.createFrame("main"):show() + +local progressText = main:addLabel("currentEnergyValue") + :setText(0) + :setForeground(colors.gray) + :setBackground(false) + :setPosition(10, 3) + :setZIndex(6) + :show() + +local energyProgress = main:addProgressbar("mainEnergyCube") + :setSize(20,3) + :setPosition(2,2) + :setBackground(colors.black) + :setProgressBar(colors.green) + :show() + +energyProgress:onChange(function() + local energy = tostring(energyCube.getEnergy()) + progressText:setText(energy) + progressText:setPosition(energyProgress:getWidth()/2+1 - math.floor(energy:len()/2), 3) +end) + + +local function checkCurrentEnergy() + while true do + energyCube = peripheral.find("ultimateEnergyCube") + if(energyCube~=nil)then + local energyCalculation = energyCube.getEnergy() / energyCube.getMaxEnergy() * 100 + energyProgress:setProgress(energyCalculation) + else + energyProgress:setProgress(0) + os.sleep(3) + end + os.sleep(1) + end +end + +main:addThread("energyThread"):start(checkCurrentEnergy) + +basalt.autoUpdate()