Advertisement
enderpro100

MarquitoLuaUtils

Oct 9th, 2022 (edited)
2,163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.62 KB | Gaming | 0 0
  1. --- Program : Computercraft Library (name MarquitoLuaUtils)
  2. --- Author : LightKnight51
  3. --- last modification : 18/04/2023
  4.  
  5.  
  6. --- Variables
  7.  
  8. -- Log level
  9. LogLevel = {INFO = "INFO", ERROR = "ERROR"}
  10. -- Device type
  11. DeviceType = {COMPUTER = "COMPUTER", COMMAND_COMPUTER="COMMAND_COMPUTER", PAD = "PAD", TURTLE="TURTLE"}
  12. -- Turtle type
  13. TurtleType = {NONE="NONE", MINING="MINING", MELEE = "MELEE", FARM="FARM", FELLING="FELLING", CRAFT="CRAFT", DIG="DIG", NOISY="NOISY"}
  14. -- Turtle type
  15.  
  16. --- Functions
  17.  
  18. ---- Main functions
  19.  
  20. -- Program exist in this machine ?
  21. function ProgramExistInLocal(programName)
  22.     return fs.exists(programName)
  23. end
  24.  
  25. -- Download program
  26. function DownloadProgram(pastebinCode, programName)
  27.     if ProgramExistInLocal(programName) then
  28.         fs.delete(programName)
  29.     end
  30.     shell.run("pastebin get " .. pastebinCode .. " " .. programName)
  31. end
  32.  
  33. -- Get content code of a program
  34. function GetProgramContent(programName)
  35.     local programContent = ""
  36.  
  37.     io.input(programName)
  38.  
  39.     programContent = io.read("a")
  40.  
  41.     return programContent
  42. end
  43.  
  44. -- Check if a list of values contain a specific value
  45. function ListContain(searchValue, ...)
  46.     local arg = { ... }
  47.  
  48.     local contain = false
  49.  
  50.     local argValue = arg[1]
  51.  
  52.     local i = 0
  53.  
  54.     while argValue ~= nil do
  55.         if argValue == searchValue then
  56.             contain = true
  57.             break
  58.         end
  59.  
  60.         i = i + 1
  61.         argValue = arg[i]
  62.     end
  63.  
  64.     return contain
  65. end
  66.  
  67. -- Return true if a variable is an array
  68. function IsAnArray(varToCheck)
  69.     return varToCheck ~= nil and type(varToCheck) == "table"
  70. end
  71.  
  72. -- Split a string into an array, with a specific separator
  73. function Split(stringToSplit, separator)
  74.     if separator == nil then
  75.         separator = "%s"
  76.     end
  77.     local splitArray = {}
  78.     for str in string.gmatch(stringToSplit, "([^" .. separator .. "]+)") do
  79.             table.insert(splitArray, str)
  80.     end
  81.     return splitArray
  82. end
  83.  
  84. -- Get size of the array
  85. function GetArraySize(array)
  86.     local arraySize = 0
  87.  
  88.     if IsAnArray(array) then
  89.         arraySize = table.getn(array)
  90.     end
  91.    
  92.     return arraySize
  93. end
  94.  
  95. -- Replace a string inside the first string, by another (or empty string if stringToReplace equal nil)
  96. function Replace(stringWhereReplace, stringToRemove, stringToReplace)
  97.     if stringToReplace == nil then
  98.         stringToReplace = ""
  99.     end
  100.     --return stringWhereReplace:gsub("%"..stringToRemove, stringToReplace)
  101.     local newString = string.gsub(stringWhereReplace, stringToRemove, stringToReplace)
  102.     return newString
  103. end
  104.  
  105. -- Print and log an error occur with specific item
  106. function Error(itemID)
  107.     print("Error occurs with : " .. itemID)
  108.     Log("Error occurs with : " .. itemID, LogLevel.ERROR)
  109. end
  110.  
  111. -- Get the device type
  112. function GetDeviceType()
  113.     local deviceType
  114.     if turtle then
  115.         deviceType = DeviceType.TURTLE
  116.     elseif pocket then
  117.         deviceType = DeviceType.PAD
  118.     elseif commands then
  119.         deviceType = DeviceType.COMMAND_COMPUTER
  120.     else
  121.         deviceType = DeviceType.COMPUTER
  122.     end
  123.     return deviceType
  124. end
  125.  
  126.  
  127. ---- File functions
  128.  
  129. -- Read a file
  130. function ReadFileContent(filePath, fileName, fileExtension)
  131.     local fileContent = ""
  132.  
  133.     if fs.exists(filePath .. "/" .. fileName .. "." .. "txt") then
  134.         fileContent = fs.open(filePath .. "/" .. fileName .. "." .. "txt", "r").readAll()
  135.     end
  136.  
  137.     return fileContent
  138. end
  139.  
  140. -- Move a file
  141. function MoveFile(currentFilePath, currentFileName, fileExtension, newFilePath, newFileName)
  142.     local fileContent = fs.open(currentFilePath .. "/" .. currentFileName .. "." .. fileExtension, "r").readAll()
  143.  
  144.     fs.delete(currentFilePath .. "/" .. currentFileName .. "." .. fileExtension)
  145.  
  146.     local newFile = fs.open(newFilePath .. "/" .. newFileName .. "." .. fileExtension, "w")
  147.     newFile.write(fileContent)
  148. end
  149.  
  150. -- Get or create a file with his file path
  151. function GetOrCreateFile(filePath, fileName, fileExtension)
  152.     local file = fs.open(filePath .. "/" .. fileName .. "." .. fileExtension, "w")
  153.  
  154.     return file
  155. end
  156.  
  157. ---- Logs functions
  158.  
  159. -- Get or create a log file with his file path
  160. function GetOrCreateLogFile(filePath, fileName)
  161.     local isNewLog = fs.exists(filePath .. "/" .. fileName .. "." .. "txt") == false
  162.  
  163.     local logFile = fs.open(filePath .. "/" .. fileName .. "." .. "txt", "a")
  164.  
  165.     if isNewLog then
  166.         logFile.writeLine("--     LogFile      --")
  167.         logFile.writeLine("-- MarquitoLuaUtils --")
  168.         logFile.writeLine("-- Log created on : ".. GetLocalDateTime())
  169.         logFile.writeLine("-- Computer ID : ".. os.getComputerID())
  170.         logFile.writeLine("----------------------")
  171.     end
  172.  
  173.     logFile.close()
  174. end
  175.  
  176. -- Log data, with a log level (info or error)
  177. function Log(logLevel, dataToLog)
  178.     GetOrCreateLogFile("/logs", "logFile")
  179.     -- Open the file
  180.     local logFile = fs.open("/logs" .. "/" .. "logFile" .. "." .. "txt", "a")
  181.  
  182.     logFile.writeLine("[" .. GetLocalDateTime() .. "] - " .. logLevel .. " " .. dataToLog)
  183.  
  184.     logFile.close()
  185. end
  186.  
  187. -- End the current file log
  188. function EndLog()
  189.     local endDateTime = GetLocalDateTime()
  190.     local logFile = fs.open("/logs" .. "/" .. "logFile" .. "." .. "txt", "a")
  191.     logFile.writeLine("[" .. endDateTime .. "] - End of this log file")
  192.     logFile.close()
  193.  
  194.     local logName = os.getComputerID() .. "_" .. string.gsub(endDateTime, " ", "_")
  195.  
  196.     MoveFile("/logs", "logFile", "txt", "/logs/archive", logName)
  197. end
  198.  
  199. ---- Configuration functions
  200.  
  201. -- Get or create a log file with his file path
  202. function GetOrCreateConfFile()
  203.     local isNewConf = fs.exists("/conf" .. "/" .. "confFile" .. "." .. "conf") == false
  204.  
  205.     if isNewConf then
  206.         local confFile = fs.open("/conf" .. "/" .. "confFile" .. "." .. "conf", "w")
  207.         confFile.writeLine("--     ConfFile     --")
  208.         confFile.writeLine("-- MarquitoLuaUtils --")
  209.         confFile.writeLine("-- Conf created on : ".. GetLocalDateTime())
  210.         confFile.writeLine("-- Computer ID : ".. os.getComputerID())
  211.         confFile.writeLine("----------------------")
  212.         confFile.close()
  213.     end
  214. end
  215.  
  216. -- Get conf value
  217. function GetConfValue(confName)
  218.     local confValue = ""
  219.  
  220.     GetOrCreateConfFile()
  221.     -- Conf file
  222.     local confFile = fs.open("/conf" .. "/" .. "confFile" .. "." .. "conf", "r")
  223.     local confFileLines = Split(confFile.readAll(),"\n")
  224.     confFile.close()
  225.  
  226.     if GetArraySize(confFileLines) > 0 then
  227.         for lineNumber, line in ipairs(confFileLines) do
  228.             if lineNumber > 5 then
  229.                 if string.find(line, confName .. "=") then
  230.                     confValue = Replace(line, confName .. "=", "")
  231.                     break
  232.                 end
  233.             end
  234.         end
  235.     end
  236.  
  237.     return confValue
  238. end
  239.  
  240. -- Set conf value
  241. function SetConfValue(confName, confValue)
  242.     GetOrCreateConfFile()
  243.     -- Conf file
  244.     local confFile = fs.open("/conf" .. "/" .. "confFile" .. "." .. "conf", "r")
  245.     local confFileLines = Split(confFile.readAll(),"\n")
  246.     confFile.close()
  247.  
  248.     confFile = fs.open("/conf" .. "/" .. "confFile" .. "." .. "conf", "w")
  249.  
  250.     local confFound = false
  251.  
  252.     if GetArraySize(confFileLines) > 0 then
  253.         for _, line in ipairs(confFileLines) do
  254.             if string.find(line, confName .. "=") then
  255.                 confFile.writeLine(confName .. "=" .. confValue)
  256.                 confFound = true
  257.             else
  258.                 confFile.writeLine(line)
  259.             end
  260.         end
  261.         if not confFound then
  262.             confFile.writeLine(confName .. "=" .. tostring(confValue))
  263.         end
  264.         confFile.close()
  265.     end
  266. end
  267.  
  268. ---- Date time functions
  269.  
  270. -- Get the current local date time (not minecraft but system)
  271. function GetLocalDateTime()
  272.     return os.date('%d-%m-%Y %H:%M:%S', GetCurrentLocalTimeStamp() / 1000)
  273. end
  274.  
  275. -- Get the current local date (not minecraft but system)
  276. function GetLocalDate()
  277.     return os.date('%d-%m-%Y', GetCurrentLocalTimeStamp() / 1000)
  278. end
  279.  
  280. -- Get the current local time (not minecraft but system)
  281. function GetLocalTime()
  282.     return os.date('%H:%M:%S', GetCurrentLocalTimeStamp() / 1000)
  283. end
  284.  
  285. -- Get the current local timestamp (not minecraft but system)
  286. function GetCurrentLocalTimeStamp()
  287.     return os.epoch("utc")
  288. end
  289.  
  290. ---- Http functions
  291.  
  292. -- Get the request for this url
  293. function GetDownloadRequest(url, isBinary)
  294.     return http.get(url, nil, isBinary)
  295. end
  296.  
  297. -- Get and save the file from the request
  298. function SaveFileFromRequest(request, filePath, fileName, fileExtension)
  299.     local file = fs.open(filePath .. "/" .. fileName .. "." .. fileExtension, "w")
  300.     -- Write data to the file
  301.     file.write(request.readAll())
  302.     -- Close the request
  303.     request.close()
  304.     -- Close the file
  305.     file.close()
  306. end
  307.  
  308. -- Download a file from http url, and return the file path if exist
  309. function DownloadAndGetFile(url, filePath, fileName, fileExtension)
  310.     local request = GetDownloadRequest(url, false)
  311.     SaveFileFromRequest(request, filePath, fileName, fileExtension)
  312.     return filePath .. "/" .. fileName .. "." .. fileExtension
  313. end
  314.  
  315. -- Download a finary file from http url, and return the file path if exist
  316. function DownloadAndGetBinaryFile(url, filePath, fileName, fileExtension)
  317.     local request = GetDownloadRequest(url, true)
  318.     SaveFileFromRequest(request, filePath, fileName, fileExtension)
  319.     return filePath .. "/" .. fileName .. "." .. fileExtension
  320. end
  321.  
  322. -- Download an audio file from http url, and return the file path if exist
  323. function DownloadAndGetAudioFile(url, fileName)
  324.     return DownloadAndGetBinaryFile(url, "/data/audio", fileName, "dfpwm")
  325. end
  326.  
  327. ---- Rednet functions
  328.  
  329. -- Find rednet wireless modem
  330. function FindWirelessRednetModem()
  331.     local wirelessRednetModem = peripheral.find("modem")
  332.  
  333.     if wirelessRednetModem ~= nil then
  334.         if wirelessRednetModem.isWireless() ~= true then
  335.             wirelessRednetModem = nil
  336.         end
  337.     end
  338.  
  339.     return wirelessRednetModem
  340. end
  341.  
  342. -- Find the side of the wireless modem
  343. function GetWirelessRednetModemSide()
  344.     local modemSide = nil
  345.  
  346.     local wirelessRednetModem = FindWirelessRednetModem()
  347.  
  348.     if wirelessRednetModem ~= nil then
  349.         modemSide = peripheral.getName(wirelessRednetModem)
  350.     end
  351.  
  352.     return modemSide
  353. end
  354.  
  355. -- Open rednet if wireless modem has found, and return if operation is successfull
  356. function OpenWirelessRednetModem()
  357.     local modemIsReady = false
  358.  
  359.     local wirelessRednetModem = FindWirelessRednetModem()
  360.  
  361.     if wirelessRednetModem ~= nil then
  362.         local wirelessModemSide = peripheral.getName(wirelessRednetModem)
  363.         if rednet.isOpen(wirelessModemSide) ~= true then
  364.             rednet.open(wirelessModemSide)
  365.         end
  366.         modemIsReady = true
  367.     end
  368.  
  369.     return modemIsReady
  370. end
  371.  
  372. -- Open rednet if wireless modem has found
  373. function CloseWirelessRednetModem()
  374.     local wirelessRednetModem = FindWirelessRednetModem()
  375.  
  376.     if wirelessRednetModem ~= nil then
  377.         local wirelessModemSide = peripheral.getName(wirelessRednetModem)
  378.         if rednet.isOpen(wirelessModemSide) then
  379.             rednet.close(wirelessModemSide)
  380.         end
  381.     end
  382. end
  383.  
  384. -- Receive data from rednet
  385. function ReceiveDataFromRednet(optionalProtocol)
  386.     local dataResult = nil
  387.  
  388.     local senderId, message, protocol = rednet.receive()
  389.     if optionalProtocol == nil or protocol == optionalProtocol then
  390.         dataResult = message
  391.     end
  392.  
  393.     return dataResult
  394. end
  395.  
  396. -- Send data to one device with rednet
  397. function SendDataWithRednetForOneDevice(deviceId, data, optionalProtocol)
  398.     if optionalProtocol == nil then
  399.         rednet.send(tonumber(deviceId), data)
  400.     else
  401.         rednet.send(tonumber(deviceId), data, optionalProtocol)
  402.     end
  403. end
  404.  
  405. -- Send data to multiples devices with rednet
  406. function SendDataWithRednetForMultiplesDevices(deviceIds, data, optionalProtocol)
  407.     if IsAnArray(deviceIds) then
  408.         for _, deviceId in ipairs(deviceIds) do
  409.             SendDataWithRednetForOneDevice(deviceId, data, optionalProtocol)
  410.         end
  411.     end
  412. end
  413.  
  414. -- Send data to multiples devices with rednet
  415. function BroadcastDataWithRednet(data, optionalProtocol)
  416.     if optionalProtocol == nil then
  417.         rednet.broadcast(data)
  418.     else
  419.         rednet.broadcast(data, optionalProtocol)
  420.     end
  421. end
  422.  
  423. ---- Turtle functions
  424.  
  425. -- Get the turtle type
  426. function GetTurtleType()
  427.     local turtleType = TurtleType.NONE
  428.  
  429.     -- The current slot
  430.     local currentSlot = turtle.getSelectedSlot()
  431.     -- The empty slot for find current equipment
  432.     local emptySlot = FindEmptySlot()
  433.  
  434.     if emptySlot then
  435.         turtle.select(emptySlot)
  436.         local modemSide = GetWirelessRednetModemSide()
  437.         local item = ""
  438.  
  439.         if modemSide then
  440.             if modemSide == "left" then
  441.                 -- Check right side
  442.                 turtle.equipRight()
  443.                 item = turtle.getItemDetail(emptySlot)
  444.                 turtle.equipRight()
  445.                 turtleType = CheckTurtleType(item)
  446.             else
  447.                 -- Check left side
  448.                 turtle.equipLeft()
  449.                 item = turtle.getItemDetail(emptySlot)
  450.                 turtle.equipLeft()
  451.                 turtleType = CheckTurtleType(item)
  452.             end
  453.         else
  454.             -- Check right side
  455.             turtle.equipRight()
  456.             item = turtle.getItemDetail(emptySlot)
  457.             turtle.equipRight()
  458.             if item then
  459.                 turtleType = CheckTurtleType(item)
  460.             else
  461.                 -- Check left side
  462.                 turtle.equipLeft()
  463.                 item = turtle.getItemDetail(emptySlot)
  464.                 turtle.equipLeft()
  465.                 turtleType = CheckTurtleType(item)
  466.             end
  467.         end
  468.     else
  469.         Log(LogLevel.ERROR, "No empty slot was found for detect turtle type")
  470.         -- TODO
  471.     end
  472.  
  473.     turtle.select(currentSlot)
  474.  
  475.     return turtleType
  476. end
  477.  
  478. -- Check the turtle type
  479. function CheckTurtleType(item)
  480.     local turtleType = TurtleType.NONE
  481.  
  482.     if item then
  483.         if string.find(item.name, "_sword") then
  484.             turtleType = TurtleType.MELEE
  485.         elseif string.find(item.name, "_pickaxe") then
  486.             turtleType = TurtleType.MINING
  487.         elseif string.find(item.name, "_axe") then
  488.             turtleType = TurtleType.FELLING
  489.         elseif string.find(item.name, "_hoe") then
  490.             turtleType = TurtleType.FARM
  491.         elseif string.find(item.name, "_shovel") then
  492.             turtleType = TurtleType.DIG
  493.         elseif string.find(item.name, "speaker") then
  494.             turtleType = TurtleType.NOISY
  495.         elseif string.find(item.name, "crafting_table") then
  496.             turtleType = TurtleType.CRAFT
  497.         end
  498.     end
  499.  
  500.     return turtleType
  501. end
  502.  
  503. -- Return the first empty slot, nil if no slot found
  504. function FindEmptySlot()
  505.     local emptySlot = nil
  506.  
  507.     for i = 1, 16 do
  508.         local data = turtle.getItemDetail(i)
  509.         if data == nil then
  510.             emptySlot = i
  511.             break
  512.         end
  513.     end
  514.  
  515.     return emptySlot
  516. end
  517.  
  518. -- Select an item inside the turtle with is minecraft ID
  519. function SelectionID(itemID)
  520.     return FindIDContains(itemID, true)
  521. end
  522.  
  523. -- Find an item inside the turtle with is minecraft ID, or a part of a minecraft ID
  524. function FindIDContains(searchItemID, isEntireId, logError)
  525.     j = nil
  526.     for i = 1, 16 do
  527.  
  528.         data = turtle.getItemDetail(i)
  529.         if data then
  530.             bCondition = false
  531.             if isEntireId then
  532.                 bCondition = (data.name == searchItemID)
  533.             else
  534.                 bCondition = (string.find(data.name,searchItemID) ~= nil)
  535.             end
  536.             if bCondition then
  537.                 j = i
  538.                 exit = false
  539.                 break
  540.             elseif i == 16 and data.name ~= searchItemID then
  541.                 if logError == true or logError == nil then
  542.                     Error(searchItemID)
  543.                 end
  544.                 j = nil
  545.             end
  546.         end
  547.     end
  548.     return j
  549. end
  550.  
  551. -- Refuel the turtle
  552. function Refuel()
  553.     while not exit do
  554.         fuel = turtle.getFuelLevel()
  555.         if fuel < 25 then
  556.             blazeRodID = SelectionID("minecraft:blaze_rod")
  557.             coalID = SelectionID("minecraft:coal")
  558.             fuelID = nil
  559.             if blazeRodID ~= nil then
  560.                 fuelID = blazeRodID  
  561.             elseif coalID ~= nil then
  562.                 fuelID = coalID
  563.             end
  564.             if fuelID ~= nil then
  565.                 turtle.select(fuelID)
  566.                 turtle.refuel(1)
  567.             else
  568.                 Error("fuelError")
  569.                 os.shutdown()
  570.             end
  571.         end
  572.         sleep(0.5)
  573.     end
  574. end
  575.  
  576. -- Detect if we have a block on a specific side, and return the bloc name, or a boolean is just detection
  577. -- (true if the turtle don't need to dig this block)
  578. function BlockDetection(position, justDetection)
  579.     success, detectionBlock = nil
  580.     block_name = ""
  581.     if position == "right" then
  582.         turtle.turnRight()
  583.         success, detectionBlock = turtle.inspect()
  584.         turtle.turnLeft()
  585.         if success then
  586.             block_name = RecognizeLiquidAirBlocks(detectionBlock, justDetection)
  587.         end
  588.     elseif position == "left" then
  589.         turtle.turnLeft()
  590.         turtle.turnRight()
  591.         success, detectionBlock = turtle.inspect()
  592.         if success then
  593.             block_name = RecognizeLiquidAirBlocks(detectionBlock, justDetection)
  594.         end
  595.     elseif position == "bottom" then
  596.         turtle.turnRight()
  597.         turtle.turnRight()
  598.         success, detectionBlock = turtle.inspect()
  599.         turtle.turnRight()
  600.         turtle.turnRight()
  601.         if success then
  602.             block_name = RecognizeLiquidAirBlocks(detectionBlock, justDetection)
  603.         end
  604.     elseif position == "front" then
  605.         success, detectionBlock = turtle.inspect()
  606.         if success then
  607.             block_name = RecognizeLiquidAirBlocks(detectionBlock, justDetection)
  608.         end
  609.     elseif position == "down" then
  610.         success, detectionBlock = turtle.inspectDown()
  611.         if success then
  612.             block_name = RecognizeLiquidAirBlocks(detectionBlock, justDetection)
  613.         end
  614.     elseif position == "up" then
  615.         success, detectionBlock = turtle.inspectUp()
  616.         if success then
  617.             block_name = RecognizeLiquidAirBlocks(detectionBlock, justDetection)
  618.         end
  619.     else
  620.         block_name = "minecraft:?"
  621.     end
  622.  
  623.     return block_name
  624. end
  625.  
  626. -- Recognize a block and return his name
  627. -- (true if this block is air, water or lava)
  628. function RecognizeLiquidAirBlocks(detectionBlock, justDetection)
  629.     block_name = ""
  630.     if justDetection == true then
  631.         if  detectionBlock.name == "minecraft:air" or detectionBlock.name == "minecraft:flowing_water" or detectionBlock.name == "minecraft:water" or detectionBlock.name == "minecraft:flowing_lava" or detectionBlock.name == "minecraft:lava" then
  632.             block_name = true
  633.         else
  634.             block_name = false
  635.         end
  636.     elseif justDetection == false then
  637.         block_name = detectionBlock.name
  638.     end
  639.     return block_name
  640. end
  641.  
  642. -- Drop every items down or up
  643. function DropAllItems(dropDown, excludingItems)
  644.     -- Array size
  645.     local arraySize = table.getn(excludingItems)
  646.    
  647.     -- Loop each items inside the turtle
  648.     for m = 1, 16  do
  649.         local canBeDrop = true
  650.         -- Get item name in current slot
  651.         local turtleCurrentItemName = nil
  652.         if turtle.getItemDetail(m) ~= nil then
  653.             turtleCurrentItemName = turtle.getItemDetail(m).name
  654.         end
  655.        
  656.         if turtleCurrentItemName ~= nil and arraySize ~= nil and arraySize > 0 then
  657.             for s = 1, arraySize do
  658.                 local current
  659.                 if string.find(turtleCurrentItemName, excludingItems[s]) then
  660.                     canBeDrop = false
  661.                     break
  662.                 end
  663.             end
  664.         end
  665.  
  666.         if canBeDrop then
  667.             turtle.select(m)
  668.             if dropDown then
  669.                 turtle.dropDown()
  670.             else
  671.                 turtle.dropUp()
  672.             end
  673.         end
  674.     end
  675.     local direction = "up"
  676.     if dropDown then
  677.         direction = "down"
  678.     end
  679.     Log(LogLevel.INFO, "Items has moved " .. direction)
  680. end
  681.  
  682. -- Drop every items down or up in chest
  683. function DropAllItemsInChest(dropDown, excludingItems)
  684.     local direction = "up"
  685.     if dropDown then
  686.         direction = "down"
  687.     end
  688.  
  689.     if string.find(BlockDetection(direction, false), "chest") then
  690.         DropAllItems(dropDown, excludingItems)
  691.         Log(LogLevel.INFO, "Items has moved to chest " .. direction)
  692.     else
  693.         Log(LogLevel.ERROR, "Chest not found ")
  694.     end
  695. end
  696.  
  697. ---- Monitor screen functions
  698.  
  699. -- Find screen
  700. function FindScreen()
  701.     return peripheral.find("monitor")
  702. end
  703.  
  704. -- Get screen size
  705. function GetScreenSize(screen)
  706.     return screen.getSize()
  707. end
  708.  
  709. -- Get center of screen
  710. function GetCenterOfScreen(screen)
  711.     local screenWidth, screenHeight = GetScreenSize(screen)
  712.  
  713.     return screenWidth / 2, screenHeight / 2
  714. end
  715.  
  716. ---- Speaker functions
  717.  
  718. -- Find speaker
  719. function FindSpeaker()
  720.     return peripheral.find("speaker")
  721. end
  722.  
  723. -- Launch new music
  724. function LaunchMusic(dfpwmLib, speaker, musicFileName)
  725.     local dfpwm = require("cc.audio.dfpwm")
  726.  
  727.     local decoder = dfpwm.make_decoder()
  728.     for chunk in io.lines("/data/audio/" .. musicFileName .. ".dfpwm", 16 * 1024) do
  729.         local buffer = decoder(chunk)
  730.        
  731.         while not speaker.playAudio(buffer) do
  732.             os.pullEvent("speaker_audio_empty")
  733.         end
  734.     end
  735.  
  736. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement