Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Modem
- os.loadAPI("json")
- local modem = peripheral.wrap('front')
- local glass = peripheral.wrap('bottom')
- local power = peripheral.wrap('left')
- -- Glasses
- local text = 'POWER: 0.00'
- local startX = 20
- local startY = 5
- local statusID = 'AWWayofTime:bloodMagicBaseItems'
- local powerID = 'IC2:fluidUuMatter'
- local c = 0
- local spawnerChannel = 3101
- local thisChannel = 3000
- local coreState
- local viewerCount = 0
- -- Accepted Values for Commands
- local booleanValues = {}
- booleanValues['true'] = true
- booleanValues['yes'] = true
- booleanValues['y'] = true
- booleanValues['on'] = true
- booleanValues['false'] = false
- booleanValues['no'] = false
- booleanValues['n'] = false
- booleanValues['off'] = false
- local spawnerStates = {}
- spawnerStates.cow = false
- spawnerStates.spider = false
- spawnerStates.basalz = false
- spawnerStates.blitz = false
- function init()
- if ( checkErrors() ) then
- error('error found')
- end
- modem.transmit(spawnerChannel, thisChannel, {'setState', 'all', false})
- parallel.waitForAll(glassesLoop, eventLoop, updateTwitchViewerCount)
- end
- -- Loops
- function spawnerLoop()
- while true do
- local items = peripheral.call('right', 'getAvailableItems')
- if ( #items > 0 ) then
- for slot, item in pairs(items) do
- local itemId = item.fingerprint.id
- if ( itemId == 'minecraft:leather' ) then
- modem.transmit(spawnerChannel, thisChannel, {'setState', 'cow', (item.size <= 640)})
- end
- end
- end
- sleep(10)
- end
- end
- function glassesLoop()
- while true do
- glass.clear()
- -- Power
- local powerUsed = power.getEnergyStored()
- local powerMax = power.getMaxEnergyStored()
- local powerPercentage = ( ( powerUsed / powerMax ) * 100 )
- local text = 'POWER: ' .. round(powerPercentage, 2)
- addChargeProgress(startX+1, startY+1, powerID, nil, powerUsed, powerMax, 0.75, 0x7f7f7f, 0.5, 0x4B0082, 1, 0.25, 0.5, 3, text)
- -- Core Status
- local coreText = glass.addText(startX+5,startY+25, 'Core Status: ', 0xFFFF00)
- coreText.setScale(0.5)
- glass.addIcon(startX+5+getStringWidth('Core Status: '),startY+21, statusID, ( coreState and 22 or 12 ))
- -- Spawner Status
- local spawnerStatus = 'Cow: ' .. (spawnerStates['cow'] and 'y' or 'n') .. ', Spider: ' .. (spawnerStates['spider'] and 'y' or 'n') .. ', Blitz: ' .. (spawnerStates['blitz'] and 'y' or 'n') .. ', Basalz: ' .. (spawnerStates['basalz'] and 'y' or 'n')
- glass.addText(startX+5,startY+40, spawnerStatus, 0xFF6900)
- glass.addText(startX+5,startY+55, 'Twitch Viewers: ' .. viewerCount, 0x00FF00)
- glass.sync()
- sleep(1)
- end
- end
- function updateTwitchViewerCount()
- local headers = {
- ['Client-ID'] = '2uiwi7gks15yxct7abpic82z66itg1'
- }
- while true do
- local response = http.get("https://api.twitch.tv/helix/streams?user_id=82541781", headers)
- if ( response ~= nil ) then
- local responseContent = response.readAll()
- local obj = json.decode(responseContent)
- if ( obj.data ~= nil and obj.data[1] ~= nil and obj.data[1].viewer_count ~= nil ) then
- viewerCount = obj.data[1].viewer_count
- end
- sleep(30)
- else
- print(textutils.serialise(response), response)
- end
- end
- end
- function eventLoop()
- while true do
- event,action,user,uuid,message = os.pullEvent()
- if event == 'glasses_chat_command' then
- if ( message:len() > 0 ) then
- local cmd = split(message, '%s+')
- if ( cmd[1] == 'shutdown' ) then
- if ( #cmd == 2 ) then
- if ( cmd[2] == 'core' ) then
- setCoreState(true)
- end
- else
- print('Expected 1 arguments... got ', #cmd-1)
- end
- elseif ( cmd[1] == 'start' ) then
- if ( #cmd == 2 ) then
- if ( cmd[2] == 'core' ) then
- setCoreState(false)
- end
- else
- print('Expected 1 arguments... got ', #cmd-1)
- end
- elseif ( cmd[1] == 'spawner' ) then
- if ( #cmd == 3 ) then
- if ( #cmd[2] > 1 and #cmd[3] > 1 ) then
- if ( booleanValues[cmd[3]] ~= nil ) then
- modem.transmit(spawnerChannel, thisChannel, {'setState', cmd[2], booleanValues[cmd[3]]})
- if ( cmd[2] == 'all' ) then
- for spawner, state in pairs(spawnerStates) do
- spawnerStates[spawner] = booleanValues[cmd[3]]
- end
- else
- spawnerStates[cmd[2]] = booleanValues[cmd[3]]
- end
- else
- print('2nd argument not recognised got,', cmd[3])
- end
- else
- print('Values need to be over 1 character long, got ', cmd[2], ' and ', cmd[3])
- end
- else
- print('Expected 2 arguments... got', #cmd-1)
- end
- elseif ( cmd[1] == 'update' ) then
- shell.run('update')
- end
- end
- elseif event == 'timer' then
- -- Alarm
- if ( alarmTimer and action == alarmTimer ) then
- redstone.setOutput('top', false)
- end
- end
- end
- end
- function checkErrors()
- return false
- end
- function setCoreState(state)
- redstone.setOutput('back', state)
- redstone.setOutput('top', state)
- coreState = state
- alarmTimer = os.startTimer(5)
- end
- -- Utility Functions
- function addLiquidProgress(x, y, id, meta, amt, max_amt, n_a, s_rgb, s_a, name, text)
- local w = getStringWidth(text) + 21
- local h = 18
- glass.addLiquid(x + 1, y + 1, w - 2, h - 2, name)
- glass.addBox(x, y, w, 1, s_rgb, s_a)
- glass.addBox(x, y + h - 1, w, 1, s_rgb, s_a)
- glass.addBox(x, y + 1, 1, h - 2, s_rgb, s_a)
- glass.addBox(x + w - 1, y + 1, 1, h - 2, s_rgb, s_a)
- local amt_prog = math.ceil(amt / max_amt * w - 2)
- glass.addBox(x + 1 + amt_prog, y + 1, w - 2 - amt_prog, h - 2, 0x000000, n_a)
- glass.addIcon(x + 1, y + 1, id, meta)
- glass.addText(x + 18, y + 5, text)
- end
- function addChargeProgress(x, y, id, meta, amt, max_amt, n_a, s_rgb, s_a, rgb, a, tb_a, e_a, e_int, text)
- local w = getStringWidth(text) + 34
- local h = 18
- glass.addBox(x + 1, y + 1, w - 2, h - 2, rgb, a)
- local i = x + 1 + e_a
- while i <= x + w - 2 do
- glass.addBox(i, y + 1, 1, h - 2, 0x000000, e_a)
- i = i + e_int
- end
- glass.addBox(x + 1, y + 1, w - 2, 3, 0x000000, tb_a)
- glass.addBox(x + 1, y + h - 4, w - 2, 3, 0x000000, tb_a)
- glass.addBox(x, y, w, 1, s_rgb, s_a)
- glass.addBox(x, y + h - 1, w, 1, s_rgb, s_a)
- glass.addBox(x, y + 1, 1, h - 2, s_rgb, s_a)
- glass.addBox(x + w - 1, y + 1, 1, h - 2, s_rgb, s_a)
- local amt_prog = math.ceil(amt / max_amt * w - 2)
- glass.addBox(x + 1 + amt_prog, y + 1, w - 2 - amt_prog, h - 2, 0x000000, n_a)
- glass.addIcon(x + 1, y + 1, id, meta)
- glass.addText(x + 22, y + 5, text)
- end
- function gridLiquidProgress(x, y, id, meta, amt, max_amt, n_a, s_rgb, s_a, name, text)
- x = x - 1
- y = y - 1
- addLiquidProgress(x * 19 + 1, y * 19 + 1, id, meta, amt, max_amt, n_a, s_rgb, s_a, name, text)
- return math.ceil((getStringWidth(text) + 20) / 19), 1
- end
- function getStringWidth(text)
- local w = 0
- local def = 5
- local w1 = "i!,.:;|"
- local w2 = "'l"
- local w3 = " t"
- local w4 = "*{}()\"<>f"
- local w6 = "~"
- for i = 1, string.len(text) do
- local c = string.sub(text, i, i)
- if string.find(w1, c) ~= nil then
- w = w + 1
- elseif string.find(w2, c) ~= nil then
- w = w + 2
- elseif string.find(w3, c) ~= nil then
- w = w + 3
- elseif string.find(w4, c) ~= nil then
- w = w + 4
- elseif string.find(w6, c) ~= nil then
- w = w + 6
- else
- w = w + 5
- end
- if i < string.len(text) - 1 then
- w = w + 1
- end
- end
- return w
- end
- function esc(x)
- return (x:gsub('%%', '%%%%')
- :gsub('^%^', '%%^')
- :gsub('%$$', '%%$')
- :gsub('%(', '%%(')
- :gsub('%)', '%%)')
- :gsub('%.', '%%.')
- :gsub('%[', '%%[')
- :gsub('%]', '%%]')
- :gsub('%*', '%%*')
- :gsub('%+', '%%+')
- :gsub('%-', '%%-')
- :gsub('%?', '%%?'))
- end
- function split(str, pat)
- local t = {} -- NOTE: use {n = 0} in Lua-5.0
- local fpat = '(.-)' .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= '' then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- function ReadableNumber(num, places)
- local ret
- local placeValue = ('%%.%df'):format(places or 0)
- if not num then
- return 0
- elseif num >= 1000000000000 then
- ret = placeValue:format(num / 1000000000000) .. 'T' -- trillion
- elseif num >= 1000000000 then
- ret = placeValue:format(num / 1000000000) .. 'B' -- billion
- elseif num >= 1000000 then
- ret = placeValue:format(num / 1000000) .. 'M' -- million
- elseif num >= 1000 then
- ret = placeValue:format(num / 1000) .. 'K' -- thousand
- else
- ret = num -- hundreds
- end
- return ret
- end
- function round(num, numDecimalPlaces)
- local mult = 10^(numDecimalPlaces or 0)
- return math.floor(num * mult + 0.5) / mult
- end
- init()
Advertisement
Add Comment
Please, Sign In to add comment