Advertisement
davedumas0

opencomputers mobSpawner control_v1.2

May 27th, 2023 (edited)
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. -- load components
  2. local component = require("component")
  3. local sides = require("sides")
  4. local event = require("event")
  5. local term = require("term")
  6. local serialization = require("serialization")
  7.  
  8. -- load peripherals
  9. local inventory_controller = component.inventory_controller
  10. local gpu = component.gpu
  11. local modem = component.modem
  12. local adapter_side = sides.east
  13.  
  14. -- initialize variables
  15. local mobs = {}
  16. local selected = 1
  17. local dialogVisible = false
  18.  
  19. -- Load function
  20. local function load()
  21. --scan for mob tool
  22. for slot = 1, inventory_controller.getInventorySize(1) do
  23. local item = inventory_controller.getStackInSlot(1, slot)
  24. if item and item.name == "industrialforegoing:mob_imprisonment_tool" then
  25. local nbt = item.nbt -- NBT data as a serialized string
  26. local mobName = item.label:gsub("Mob Imprisonment Tool ", "") -- Remove "Mob Imprisonment Tool "
  27. table.insert(mobs, mobName)
  28. end
  29. end
  30. end
  31.  
  32. -- Function to draw the UI
  33. local function drawUI()
  34. term.clear()
  35. local width, height = gpu.getResolution()
  36. local boxWidth = 40
  37. local boxHeight = height - 6
  38. local boxX = math.floor((width - boxWidth) / 2)
  39. local boxY = math.floor((height - boxHeight) / 2)
  40.  
  41. -- Draw the box
  42. gpu.setForeground(0xFFFFFF) -- White
  43. gpu.setBackground(0x000000) -- Black
  44. gpu.fill(boxX, boxY, boxWidth, boxHeight, " ")
  45.  
  46. -- Draw the header
  47. term.setCursor(boxX + 2, boxY + 1)
  48. print("================================")
  49. term.setCursor(boxX + 2, boxY + 2)
  50. print(" MOB SPAWNER ")
  51. term.setCursor(boxX + 2, boxY + 3)
  52. print("================================")
  53.  
  54. -- Draw the mobs
  55. for i, mob in ipairs(mobs) do
  56. local mobY = boxY + i + 3
  57. if i == selected then
  58. gpu.setForeground(0xFFFFFF) -- White
  59. gpu.setBackground(0x0000FF) -- Blue
  60. else
  61. gpu.setForeground(0x000000) -- Black
  62. gpu.setBackground(0xFFFFFF) -- White
  63. end
  64. term.setCursor(boxX + 2, mobY)
  65. term.write(mob)
  66. end
  67.  
  68. -- Reset to default colors after drawing
  69. gpu.setForeground(0x000000) -- Black
  70. gpu.setBackground(0xFFFFFF) -- White
  71. end
  72.  
  73. -- Function to draw the confirmation dialog
  74. local function drawDialog(mob)
  75. term.clear()
  76. local width, height = gpu.getResolution()
  77. local dialogWidth = 30
  78. local dialogHeight = 10
  79. local dialogX = math.floor((width - dialogWidth) / 2)
  80. local dialogY = math.floor((height - dialogHeight) / 2)
  81.  
  82. -- Draw the dialog box
  83. gpu.setForeground(0xFFFFFF) -- White
  84. gpu.setBackground(0x000000) -- Black
  85. gpu.fill(dialogX, dialogY, dialogWidth, dialogHeight, " ")
  86.  
  87. -- Draw the dialog content
  88. term.setCursor(dialogX + 2, dialogY + 1)
  89. print("================================")
  90. term.setCursor(dialogX + 2, dialogY + 2)
  91. print(" CONFIRMATION REQUIRED ")
  92. term.setCursor(dialogX + 2, dialogY + 3)
  93. print("================================")
  94. term.setCursor(dialogX + 2, dialogY + 5)
  95. print("Put " .. mob .. " in duplicator?")
  96. term.setCursor(dialogX + 2, dialogY + 7)
  97. print("Press 'Y' for YES")
  98. term.setCursor(dialogX + 2, dialogY + 8)
  99. print("Press 'C' for CANCEL")
  100.  
  101. -- Reset to default colors after drawing
  102. gpu.setForeground(0x000000) -- Black
  103. gpu.setBackground(0xFFFFFF) -- White
  104. end
  105.  
  106. -- Draw function
  107. local function draw()
  108. if not dialogVisible then
  109. drawUI()
  110. else
  111. drawDialog(mobs[selected])
  112. end
  113. end
  114.  
  115. -- Update function
  116. local function update()
  117. local _, _, char, code = event.pull("key_down")
  118. if not dialogVisible then
  119. if code == 200 then -- Up arrow
  120. selected = math.max(selected - 1, 1)
  121. elseif code == 208 then -- Down arrow
  122. selected = math.min(selected + 1, #mobs)
  123. elseif code == 203 then -- Left arrow
  124. selected = selected - 1
  125. if selected < 1 then
  126. selected = #mobs
  127. end
  128. elseif code == 205 then -- Right arrow
  129. selected = selected + 1
  130. if selected > #mobs then
  131. selected = 1
  132. end
  133. elseif code == 28 then -- Enter
  134. if selected <= #mobs then
  135. dialogVisible = true
  136. end
  137. end
  138. else -- Handling events within the dialog
  139. if code == 28 then -- Enter
  140. modem.broadcast(123, mobs[selected])
  141. dialogVisible = false
  142. elseif code == 46 then -- C for CANCEL
  143. dialogVisible = false
  144. end
  145. end
  146. end
  147.  
  148. -- Load the inventory first
  149. load()
  150.  
  151. -- Main game loop
  152. while true do
  153. draw()
  154. update()
  155. end
  156.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement