NiallDoherty

MobSwitcher

May 30th, 2021 (edited)
720
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.38 KB | None | 0 0
  1. local ButtonAPI = require "ButtonAPI"
  2.  
  3. local drawerController = peripheral.find("storagedrawers:controller")
  4. local chests = {peripheral.find("minecraft:chest")}
  5. local inputChest = peripheral.getName(chests[2])
  6. local outputChest = peripheral.getName(chests[1])
  7. local windowSize = {term.getSize()}
  8. local windowWidth = windowSize[1]
  9. local windowHeight = windowSize[2]
  10. local currentScreen = 1
  11. -- Assume a mob is in the machine
  12. local mobInMachine = true
  13. local spawnerOn = false
  14.  
  15. local function formatName(string)
  16.     local actualName = string:sub(string:find(":") + 1, #string - 1)
  17.     return actualName:sub(1, 1):upper()..actualName:sub(2)
  18. end
  19.  
  20. local function getMobNames()
  21.     local mobNames = {}
  22.     for a = 2, drawerController.size() do
  23.         local item = drawerController.getItemDetail(a)
  24.         if item then
  25.             mobNames[a] = formatName(item.displayName)
  26.         end
  27.     end
  28.     return mobNames
  29. end
  30.  
  31. local function switch(self)
  32.     if spawnerOn then
  33.         spawnerOn = false
  34.         self:update(colours.red, "Off")
  35.         rs.setAnalogOutput("back", 0)
  36.         return
  37.     end
  38.     spawnerOn = true
  39.     self:update(colours.green, "On")
  40.     rs.setAnalogOutput("back", 15)
  41. end
  42.  
  43. local currentButton = {}
  44. local function setCurrentButton(theButton)
  45.     currentButton:update(colours.red, nil)
  46.     theButton:update(colours.green, nil)
  47.     currentButton = theButton
  48. end
  49.  
  50. local function retrieveMob(self)
  51.     if spawnerOn then return end
  52.     if mobInMachine then
  53.         if self then setCurrentButton(self) end
  54.         rs.setAnalogOutput("bottom", 0)
  55.         local counter = 0
  56.         while not (drawerController.pullItems(outputChest, 1) > 0) do
  57.             sleep(0.1)
  58.             counter = counter + 1
  59.             if counter == 15 then break end
  60.         end
  61.         rs.setAnalogOutput("bottom", 15)
  62.     end
  63.     mobInMachine = false
  64. end
  65.  
  66. local function swapMob(self, currentSlot)
  67.     if spawnerOn then return end
  68.     setCurrentButton(self)
  69.     retrieveMob(self)
  70.     while not (drawerController.pushItems(inputChest, currentSlot) > 0) do sleep(0.1) end
  71.     mobInMachine = true
  72. end
  73.  
  74. local function createScreens(mobNames)
  75.     local screens = {}
  76.     local screen = 1
  77.     screens[screen] = {}
  78.     local xValue = 2
  79.     local yValue = 2
  80.  
  81.     for slot, name in pairs(mobNames) do
  82.         if xValue + #name + 3 > windowWidth then
  83.             xValue = 2
  84.             yValue = yValue + 3
  85.             if yValue > windowHeight - 5 then
  86.                 yValue = 2
  87.                 screen = screen + 1
  88.                 screens[screen] = {}
  89.             end
  90.         end
  91.         screens[screen][slot] =  ButtonAPI.createButton(xValue, yValue, nil, nil, mobNames[slot], colours.red, swapMob)
  92.         xValue = xValue + #name + 3
  93.     end
  94.  
  95.     local i = 0
  96.     while screens[currentScreen][i] == nil do i = i + 1 end
  97.     currentButton = screens[currentScreen][i]
  98.     return screens
  99. end
  100.  
  101. local function drawNewScreen(self, screens, bottom)
  102.     if self then setCurrentButton(self) end
  103.     term.clear()
  104.     ButtonAPI.drawButtons(screens[currentScreen])
  105.     ButtonAPI.drawButtons(bottom)
  106. end
  107.  
  108. local function previousScreen(self, screens, bottom)
  109.     if currentScreen > 1 then
  110.         currentScreen = currentScreen - 1
  111.         drawNewScreen(self, screens, bottom)
  112.         return
  113.     end
  114.     self:update(colours.red, nil)
  115. end
  116.  
  117. local function nextScreen(self, screens, bottom)
  118.     if currentScreen < #screens then
  119.         currentScreen = currentScreen + 1
  120.         drawNewScreen(self, screens, bottom)
  121.         return
  122.     end
  123.     self:update(colours.red, nil)
  124. end
  125.  
  126. -- Make sure spawner is off at startup
  127. rs.setAnalogOutput("back", 0)
  128.  
  129. -- Make sure spawner is empty at startup
  130. retrieveMob()
  131.  
  132. -- Generate list of mobs in drawers
  133. local mobNames = getMobNames()
  134.  
  135. -- Create buttons for each mob and divide them into screens
  136. local screens = createScreens(mobNames)
  137.  
  138. -- Create bottom row buttons
  139. local prevButton = ButtonAPI.createButton(2, windowHeight - 1, nil, nil, "Previous", colours.red, previousScreen)
  140. local retrieveButton = ButtonAPI.createButton((windowWidth / 2) - 5, windowHeight - 1, nil, nil, "Retrieve", colours.red, retrieveMob)
  141. local onOffButton = ButtonAPI.createButton((windowWidth / 2) + 7, windowHeight - 1, nil, nil, "Off", colours.red, switch)
  142. local nextButton = ButtonAPI.createButton(windowWidth - 6, windowHeight-1, nil, nil, "Next", colours.red, nextScreen)
  143. local bottomRow = {prevButton, retrieveButton, onOffButton, nextButton}
  144.  
  145. -- Draw the first screen
  146. drawNewScreen(nil, screens, bottomRow)
  147.  
  148. -- Begin event loop
  149. while true do
  150.     local eventData = {os.pullEvent()}
  151.     if eventData[1] == "mouse_click" then
  152.         -- Check if the click occured on one of the bottom row buttons
  153.         for a = 1, #bottomRow do
  154.             local theButton = bottomRow[a]
  155.             if theButton:clicked(eventData[3], eventData[4]) then
  156.                 -- Parameters will be ignore for retrieve and switch functions
  157.                 theButton:onClickMethod(screens, bottomRow)
  158.                 break
  159.             end
  160.         end
  161.  
  162.         -- Check if the click occured on one of the mob buttons
  163.         for slot, button in pairs(screens[currentScreen]) do
  164.             local theButton = button
  165.             if theButton:clicked(eventData[3], eventData[4]) then
  166.                 theButton:onClickMethod(slot)
  167.                 break
  168.             end
  169.         end
  170.     end
  171. end
  172.  
Add Comment
Please, Sign In to add comment