Advertisement
Shaka01

Shaka API, should be dependancy download

Mar 2nd, 2023 (edited)
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.78 KB | None | 0 0
  1. ---general functions
  2.  
  3. local function nextLine(target, lines) ---jumps to the next line on a monitor or terminal
  4.     if target == nil then
  5.         target = term
  6.     end
  7.     if lines == nil then
  8.         lines = 1
  9.     end
  10.     local x, y = target.getCursorPos()
  11.     target.setCursorPos(1, y + lines)
  12. end
  13.  
  14. local function changeColors(bgColor, textColor, target)
  15.     if target == nil then
  16.         target = term
  17.     end
  18.     target.setBackgroundColor(bgColor)
  19.     target.setTextColor(textColor)
  20. end
  21.  
  22. local function clearScreen(monitor, color) ---clears the screen and sets cursor to 1, 1
  23.     if monitor == nil then
  24.         monitor = term
  25.     end
  26.     if color == nil then
  27.         color = colors.black
  28.     end
  29.     monitor.setBackgroundColor(color)
  30.     monitor.setCursorPos(1, 1)
  31.     monitor.clear()
  32. end
  33.  
  34. local function centerWindow(inputWidth, inputHeight, windowColor, textColor, target)
  35.     if target == nil then
  36.         target = term
  37.     end
  38.     if textColor == nil then
  39.         textColor = colors.black
  40.     end
  41.     local width, height = target.getSize()
  42.     local x = math.floor((width - inputWidth) / 2) + 1
  43.     local y = math.floor((height - inputHeight) / 2) + 1
  44.     if target == term then target = term.current() end
  45.     local windowName = window.create(target, x, y, inputWidth, inputHeight)
  46.     windowName.setBackgroundColor(windowColor)
  47.     windowName.setTextColor(textColor)
  48.     windowName.clear()
  49.     return windowName
  50. end
  51.  
  52. local function connectMonitor() -- connects monitor, returns the wrapped monitor, forces you to attach monitor
  53. -- usage: monitor = shaka.connectMonitor()
  54.   local window = ""
  55.   local sides = {"left", "right", "top", "bottom", "front", "back"}
  56.   for i, side in ipairs(sides) do
  57.     local device = peripheral.getType(side)
  58.     if device == "monitor" then
  59.       monitor = peripheral.wrap(side)
  60.     end
  61.   end
  62.   if monitor then
  63.     return monitor
  64.   else
  65.     term.setBackgroundColor(colors.black)
  66.     term.clear()
  67.     window = centerWindow(39, 5, colors.red)
  68.     window.clear()
  69.     centerText("No monitor attached!", 2, window)
  70.     changeColors(colors.red, colors.gray)
  71.     centerText("Connect one to any side to continue..", 4, window)
  72.     repeat
  73.         event, side = os.pullEvent("peripheral")
  74.     until connectMonitor()
  75.   end
  76.   clearScreen()
  77. end
  78.  
  79. local function connectModem() -- connects modem and opens rednet, returns the wrapped modem, forces you to attach modem
  80. -- usage: modem = shaka.connectModem() or just shaka.connectModem() to open rednet
  81.   local window = ""
  82.   local modemFound = false
  83.   local sides = {"left", "right", "top", "bottom", "front", "back"}
  84.   for i, side in ipairs(sides) do
  85.     local device = peripheral.getType(side)
  86.     if device == "modem" then
  87.       rednet.open(side)
  88.       modem = peripheral.wrap(side)
  89.       if modem.isWireless() then
  90.         modemFound = true
  91.         break
  92.       end
  93.     end
  94.   end
  95.   if modemFound then
  96.     return modem
  97.   else
  98.     term.setBackgroundColor(colors.black)
  99.     term.clear()
  100.     window = centerWindow(39, 5, colors.red)
  101.     window.clear()
  102.     centerText("No wireless modem connected!", 2, window)
  103.     changeColors(colors.red, colors.gray)
  104.     centerText("Connect one on any side to continue..", 4, window)
  105.     repeat
  106.         event, side = os.pullEvent("peripheral")
  107.     until connectModem()
  108.   end
  109.   clearScreen()
  110. end
  111.  
  112. 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
  113.     if fs.exists(fileName) then
  114.         local file = fs.open(fileName, "r")
  115.         contents = file.readAll()
  116.         file.close()
  117.         content = textutils.unserialize(contents)
  118.     else
  119.         return false
  120.     end
  121.     if serializeOption == false or serializeOption == nil then
  122.         return content
  123.     else
  124.         return contents
  125.     end
  126. end
  127.  
  128. local function writeFile(fileName, content)
  129.     local file = fs.open(fileName, "w")
  130.     local fileContent = textutils.serialize(content)
  131.     file.write(fileContent) ---try if nil
  132.     file.close()
  133.     return true
  134. end
  135.  
  136. ---text manipulation
  137.  
  138. local function prettyName(item, cap) -- removes every character in a string before ":" and replaces "_" with a space, if set to true capitalizes first letter
  139.   local displayName = string.gsub(item, ".*:", "") -- remove everything before the colon
  140.   displayName = string.gsub(displayName, "_", " ") -- replace underscores with spaces
  141.   if cap == true then
  142.     displayName = string.gsub(displayName, "^%l", string.upper) -- capitalize the first letter
  143.   end
  144.   return displayName
  145. end
  146.  
  147. local function formatNumber(number) --- makes big numbers nicer, for example 1000 = 1.000, returns the number
  148.   local formatted = tostring(number)
  149.   local k = #formatted % 3
  150.   if k == 0 then k = 3 end
  151.     formatted = formatted:sub(1, k) .. formatted:sub(k+1):gsub("(%d%d%d)", ".%1")
  152.   return formatted
  153. end
  154.  
  155. local function centerText(text, y, monitor) -- centers text on monitor or terminal
  156.   if monitor == nil then
  157.     monitor = term
  158.   end
  159.   local w, h = monitor.getSize()
  160.   local x = math.floor((w - string.len(text) + 2) / 2)
  161.   if y == nil then
  162.     local a, b = monitor.getCursorPos()
  163.     y = b
  164.   end
  165.   monitor.setCursorPos(x , y)
  166.   monitor.write(text)
  167. end
  168.  
  169. local function split(s, delimiter) --- splits a string with a given delimiter, for example " " or "," - access with result[1], result[2], etc
  170.     if delimiter == nil then
  171.         printError("No delimiter specified.")
  172.     end
  173.     result = {};
  174.     for match in (s..delimiter):gmatch("(.-)"..delimiter) do
  175.         table.insert(result, match);
  176.     end
  177.     return result;
  178. end
  179.  
  180. local function stringFind(stringToSearchIn, stringToSearchFor) -- self explanatory
  181.   if string.find(stringToSearchIn, stringToSearchFor) ~= nil then
  182.     return true
  183.   else
  184.     return false
  185.   end
  186. end
  187.  
  188. function shortenString(str, length)
  189.   if string.len(str) > length then
  190.     return string.sub(str, 1, length)
  191.   else
  192.     return str
  193.   end
  194. end
  195.  
  196. ---turtle functions
  197.  
  198. local function turtleFindItem(item, selectItem) -- finds an item in a turtle inventory, returns true or false. if selectItem == true it selects the item
  199. --usage success, slot = shaka.turtleFindItem(item, sel)
  200.     for i = 1, 16 do
  201.         local data = turtle.getItemDetail(i)
  202.         if data then
  203.             if data.name == item then
  204.                 if selectItem == true then
  205.                     turtle.select(i)
  206.                 end
  207.                 return true, i
  208.             end
  209.         end
  210.     end
  211.     return false
  212. end
  213.  
  214. local function turtleResetRedstone()
  215.     local sides = {"top", "bottom", "left", "right", "front", "back"}
  216.     for i = 1, #sides do
  217.         redstone.setOutput(sides[i], false)
  218.     end
  219. end
  220.  
  221.  
  222. local function turtleForward(numberOfMoves)
  223.     local moveCount = 0
  224.     if numberOfMoves == nil then numberOfMoves = 1 end
  225.     repeat
  226.         if turtle.forward() == false then
  227.             if turtle.dig() == false then
  228.                 turtle.attack()
  229.             end
  230.             sleep(0.1)
  231.         else
  232.             moveCount = moveCount + 1
  233.         end
  234.     until moveCount == numberOfMoves
  235. end
  236.  
  237.  
  238. local function turtleBack(numberOfMoves)
  239.     local moveCount = 0
  240.     if numberOfMoves == nil then numberOfMoves = 1 end
  241.     repeat
  242.         if turtle.back() == false then
  243.             turtle.turnLeft()
  244.             turtle.turnLeft()
  245.             if turtle.dig() == false then
  246.                 turtle.attack()
  247.             end
  248.             turtle.turnRight()
  249.             turtle.turnRight()
  250.         else
  251.             moveCount = moveCount + 1
  252.         end
  253.     until moveCount == numberOfMoves
  254. end
  255.  
  256. local function turtleUp(numberOfMoves)
  257.     local moveCount = 0
  258.     if numberOfMoves == nil then numberOfMoves = 1 end
  259.     repeat
  260.         if turtle.up() == false then
  261.             if turtle.digUp() == false then
  262.                 turtle.attackUp()
  263.             end
  264.             sleep(0.1)
  265.         else
  266.             moveCount = moveCount + 1
  267.         end
  268.     until moveCount == numberOfMoves
  269. end
  270.  
  271. local function turtleDown(numberOfMoves)
  272.     local moveCount = 0
  273.     if numberOfMoves == nil then numberOfMoves = 1 end
  274.     repeat
  275.         if turtle.down() == false then
  276.             if turtle.digDown() == false then
  277.                 turtle.attackDown()
  278.             end
  279.             sleep(0.1)
  280.         else
  281.             moveCount = moveCount + 1
  282.         end
  283.     until moveCount == numberOfMoves
  284. end
  285.  
  286. ---computer inventory functions
  287.  
  288. 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
  289.     for slot, data in pairs(inventory.list()) do
  290.         if partialSearch then -- set 3rd argument to true for this
  291.             if stringFind(data.name, item) then
  292.                 return slot
  293.             end
  294.         else
  295.             if data.name == item then
  296.                 return slot
  297.             end
  298.         end
  299.     end
  300.     return nil
  301. end
  302.  
  303. function moveAllItems(sourceName, targetName)
  304.   local sourceInv = peripheral.wrap(sourceName)
  305.   local targetInv = peripheral.wrap(targetName)
  306.   local tasks = {}
  307.   local invData = sourceInv.list()
  308.   local taskCount = 0
  309.   local totalMovedItems = 0 -- new variable to track the total moved items
  310.   for k, v in pairs(invData) do
  311.     if taskCount >= 200 then
  312.       break
  313.     end
  314.     taskCount = taskCount + 1
  315.     tasks[#tasks+1] = function()
  316.       local itemData = sourceInv.getItemDetail(k)
  317.       local movedItems = sourceInv.pushItems(targetName, k)
  318.       if movedItems < itemData.count or movedItems == 0 then
  319.         -- return false
  320.       else
  321.         totalMovedItems = totalMovedItems + movedItems -- increment total moved items
  322.       end
  323.     end
  324.   end
  325.   parallel.waitForAll(unpack(tasks))
  326.   return totalMovedItems -- return the total moved items
  327. end
  328.  
  329.  
  330.  
  331. return {
  332.  
  333. split = split,
  334. centerText = centerText,
  335. prettyName = prettyName,
  336. turtleFindItem = turtleFindItem,
  337. nextLine = nextLine,
  338. clearScreen = clearScreen,
  339. connectMonitor = connectMonitor,
  340. findAvailableInventory = findAvailableInventory,
  341. stringFind = stringFind,
  342. invSearch = invSearch,
  343. connectModem = connectModem,
  344. resetColors = resetColors,
  345. changeColors = changeColors,
  346. errorColors = errorColors,
  347. readFile = readFile,
  348. writeFile = writeFile,
  349. centerWindow = centerWindow,
  350. shortenString = shortenString,
  351. moveAllItems = moveAllItems,
  352. turtleForward = turtleForward,
  353. turtleBack = turtleBack,
  354. turtleUp = turtleUp,
  355. turtleDown = turtleDown,
  356. turtleResetRedstone = turtleResetRedstone,
  357. formatNumber = formatNumber,
  358. }
  359.  
  360.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement