Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---general functions
- local function nextLine(target, lines) ---jumps to the next line on a monitor or terminal
- if target == nil then
- target = term
- end
- if lines == nil then
- lines = 1
- end
- local x, y = target.getCursorPos()
- target.setCursorPos(1, y + lines)
- end
- local function changeColors(bgColor, textColor, target)
- if target == nil then
- target = term
- end
- target.setBackgroundColor(bgColor)
- target.setTextColor(textColor)
- end
- local function clearScreen(monitor, color) ---clears the screen and sets cursor to 1, 1
- if monitor == nil then
- monitor = term
- end
- if color == nil then
- color = colors.black
- end
- monitor.setBackgroundColor(color)
- monitor.setCursorPos(1, 1)
- monitor.clear()
- end
- local function centerWindow(inputWidth, inputHeight, windowColor, textColor, target)
- if target == nil then
- target = term
- end
- if textColor == nil then
- textColor = colors.black
- end
- local width, height = target.getSize()
- local x = math.floor((width - inputWidth) / 2) + 1
- local y = math.floor((height - inputHeight) / 2) + 1
- if target == term then target = term.current() end
- local windowName = window.create(target, x, y, inputWidth, inputHeight)
- windowName.setBackgroundColor(windowColor)
- windowName.setTextColor(textColor)
- windowName.clear()
- return windowName
- end
- local function connectMonitor() -- connects monitor, returns the wrapped monitor, forces you to attach monitor
- -- usage: monitor = shaka.connectMonitor()
- local window = ""
- local sides = {"left", "right", "top", "bottom", "front", "back"}
- for i, side in ipairs(sides) do
- local device = peripheral.getType(side)
- if device == "monitor" then
- monitor = peripheral.wrap(side)
- end
- end
- if monitor then
- return monitor
- else
- term.setBackgroundColor(colors.black)
- term.clear()
- window = centerWindow(39, 5, colors.red)
- window.clear()
- centerText("No monitor attached!", 2, window)
- changeColors(colors.red, colors.gray)
- centerText("Connect one to any side to continue..", 4, window)
- repeat
- event, side = os.pullEvent("peripheral")
- until connectMonitor()
- end
- clearScreen()
- end
- local function connectModem() -- connects modem and opens rednet, returns the wrapped modem, forces you to attach modem
- -- usage: modem = shaka.connectModem() or just shaka.connectModem() to open rednet
- local window = ""
- local modemFound = false
- local sides = {"left", "right", "top", "bottom", "front", "back"}
- for i, side in ipairs(sides) do
- local device = peripheral.getType(side)
- if device == "modem" then
- rednet.open(side)
- modem = peripheral.wrap(side)
- if modem.isWireless() then
- modemFound = true
- break
- end
- end
- end
- if modemFound then
- return modem
- else
- term.setBackgroundColor(colors.black)
- term.clear()
- window = centerWindow(39, 5, colors.red)
- window.clear()
- centerText("No wireless modem connected!", 2, window)
- changeColors(colors.red, colors.gray)
- centerText("Connect one on any side to continue..", 4, window)
- repeat
- event, side = os.pullEvent("peripheral")
- until connectModem()
- end
- clearScreen()
- end
- local function readFile(fileName, serializeOption) ---reads the content of a file and returns them, false if no file. 2nd option true = leave serialized, 2nd option empty or false = unserialize
- if fs.exists(fileName) then
- local file = fs.open(fileName, "r")
- contents = file.readAll()
- file.close()
- content = textutils.unserialize(contents)
- else
- return false
- end
- if serializeOption == false or serializeOption == nil then
- return content
- else
- return contents
- end
- end
- local function writeFile(fileName, content)
- local file = fs.open(fileName, "w")
- local fileContent = textutils.serialize(content)
- file.write(fileContent) ---try if nil
- file.close()
- return true
- end
- ---text manipulation
- local function prettyName(item, cap) -- removes every character in a string before ":" and replaces "_" with a space, if set to true capitalizes first letter
- local displayName = string.gsub(item, ".*:", "") -- remove everything before the colon
- displayName = string.gsub(displayName, "_", " ") -- replace underscores with spaces
- if cap == true then
- displayName = string.gsub(displayName, "^%l", string.upper) -- capitalize the first letter
- end
- return displayName
- end
- local function formatNumber(number) --- makes big numbers nicer, for example 1000 = 1.000, returns the number
- local formatted = tostring(number)
- local k = #formatted % 3
- if k == 0 then k = 3 end
- formatted = formatted:sub(1, k) .. formatted:sub(k+1):gsub("(%d%d%d)", ".%1")
- return formatted
- end
- local function centerText(text, y, monitor) -- centers text on monitor or terminal
- if monitor == nil then
- monitor = term
- end
- local w, h = monitor.getSize()
- local x = math.floor((w - string.len(text) + 2) / 2)
- if y == nil then
- local a, b = monitor.getCursorPos()
- y = b
- end
- monitor.setCursorPos(x , y)
- monitor.write(text)
- end
- local function split(s, delimiter) --- splits a string with a given delimiter, for example " " or "," - access with result[1], result[2], etc
- if delimiter == nil then
- printError("No delimiter specified.")
- end
- result = {};
- for match in (s..delimiter):gmatch("(.-)"..delimiter) do
- table.insert(result, match);
- end
- return result;
- end
- local function stringFind(stringToSearchIn, stringToSearchFor) -- self explanatory
- if string.find(stringToSearchIn, stringToSearchFor) ~= nil then
- return true
- else
- return false
- end
- end
- function shortenString(str, length)
- if string.len(str) > length then
- return string.sub(str, 1, length)
- else
- return str
- end
- end
- ---turtle functions
- local function turtleFindItem(item, selectItem) -- finds an item in a turtle inventory, returns true or false. if selectItem == true it selects the item
- --usage success, slot = shaka.turtleFindItem(item, sel)
- for i = 1, 16 do
- local data = turtle.getItemDetail(i)
- if data then
- if data.name == item then
- if selectItem == true then
- turtle.select(i)
- end
- return true, i
- end
- end
- end
- return false
- end
- local function turtleResetRedstone()
- local sides = {"top", "bottom", "left", "right", "front", "back"}
- for i = 1, #sides do
- redstone.setOutput(sides[i], false)
- end
- end
- local function turtleForward(numberOfMoves)
- local moveCount = 0
- if numberOfMoves == nil then numberOfMoves = 1 end
- repeat
- if turtle.forward() == false then
- if turtle.dig() == false then
- turtle.attack()
- end
- sleep(0.1)
- else
- moveCount = moveCount + 1
- end
- until moveCount == numberOfMoves
- end
- local function turtleBack(numberOfMoves)
- local moveCount = 0
- if numberOfMoves == nil then numberOfMoves = 1 end
- repeat
- if turtle.back() == false then
- turtle.turnLeft()
- turtle.turnLeft()
- if turtle.dig() == false then
- turtle.attack()
- end
- turtle.turnRight()
- turtle.turnRight()
- else
- moveCount = moveCount + 1
- end
- until moveCount == numberOfMoves
- end
- local function turtleUp(numberOfMoves)
- local moveCount = 0
- if numberOfMoves == nil then numberOfMoves = 1 end
- repeat
- if turtle.up() == false then
- if turtle.digUp() == false then
- turtle.attackUp()
- end
- sleep(0.1)
- else
- moveCount = moveCount + 1
- end
- until moveCount == numberOfMoves
- end
- local function turtleDown(numberOfMoves)
- local moveCount = 0
- if numberOfMoves == nil then numberOfMoves = 1 end
- repeat
- if turtle.down() == false then
- if turtle.digDown() == false then
- turtle.attackDown()
- end
- sleep(0.1)
- else
- moveCount = moveCount + 1
- end
- until moveCount == numberOfMoves
- end
- ---computer inventory functions
- local function invSearch(inventory, item, partialSearch) ---search for a given item in a given inventory, stops at first found item and returns the slot, partial matches work too
- for slot, data in pairs(inventory.list()) do
- if partialSearch then -- set 3rd argument to true for this
- if stringFind(data.name, item) then
- return slot
- end
- else
- if data.name == item then
- return slot
- end
- end
- end
- return nil
- end
- function moveAllItems(sourceName, targetName)
- local sourceInv = peripheral.wrap(sourceName)
- local targetInv = peripheral.wrap(targetName)
- local tasks = {}
- local invData = sourceInv.list()
- local taskCount = 0
- local totalMovedItems = 0 -- new variable to track the total moved items
- for k, v in pairs(invData) do
- if taskCount >= 200 then
- break
- end
- taskCount = taskCount + 1
- tasks[#tasks+1] = function()
- local itemData = sourceInv.getItemDetail(k)
- local movedItems = sourceInv.pushItems(targetName, k)
- if movedItems < itemData.count or movedItems == 0 then
- -- return false
- else
- totalMovedItems = totalMovedItems + movedItems -- increment total moved items
- end
- end
- end
- parallel.waitForAll(unpack(tasks))
- return totalMovedItems -- return the total moved items
- end
- return {
- split = split,
- centerText = centerText,
- prettyName = prettyName,
- turtleFindItem = turtleFindItem,
- nextLine = nextLine,
- clearScreen = clearScreen,
- connectMonitor = connectMonitor,
- findAvailableInventory = findAvailableInventory,
- stringFind = stringFind,
- invSearch = invSearch,
- connectModem = connectModem,
- resetColors = resetColors,
- changeColors = changeColors,
- errorColors = errorColors,
- readFile = readFile,
- writeFile = writeFile,
- centerWindow = centerWindow,
- shortenString = shortenString,
- moveAllItems = moveAllItems,
- turtleForward = turtleForward,
- turtleBack = turtleBack,
- turtleUp = turtleUp,
- turtleDown = turtleDown,
- turtleResetRedstone = turtleResetRedstone,
- formatNumber = formatNumber,
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement