Advertisement
Guest User

Untitled

a guest
Nov 1st, 2024
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.88 KB | None | 0 0
  1. -- === CONFIGURATION ===
  2. local monitor = peripheral.wrap("right")
  3. local inventoryManager = peripheral.wrap("left")
  4.  
  5. -- Item lists
  6. local blockedItems = {}
  7. local allowedItems = {}
  8. local transferredItems = {}
  9. local transferStatus = false
  10.  
  11. -- Transfer status
  12. local transferStatus = false
  13.  
  14. -- Set the default text size
  15. monitor.setTextScale(0.5)
  16.  
  17. -- Function to check if the monitor supports colors
  18. local monitorSupportsColor = monitor.isColor() or monitor.isColour()
  19.  
  20. -- Function to display text on the monitor and computer with a background
  21. function writeToAll(y, text, textColor, bgColor)
  22. textColor = textColor or colors.white
  23. bgColor = bgColor or colors.black
  24. monitor.setBackgroundColor(bgColor)
  25. monitor.setTextColor(textColor)
  26. monitor.setCursorPos(1, y) -- Set cursor to the first position
  27. monitor.clearLine()
  28. monitor.write(text)
  29.  
  30. term.setBackgroundColor(bgColor)
  31. term.setTextColor(textColor)
  32. term.setCursorPos(1, y) -- Set cursor to the first position
  33. term.clearLine()
  34. term.write(text)
  35. end
  36.  
  37.  
  38. -- Function to draw buttons on the monitor and terminal with text length adjustment
  39. function drawLeftAlignedButton(y, text, color)
  40. color = color or colors.gray
  41. monitor.setBackgroundColor(color)
  42. term.setBackgroundColor(color)
  43. monitor.setTextColor(colors.white)
  44. term.setTextColor(colors.white)
  45. local textLength = #text
  46. monitor.setCursorPos(2, y)
  47. monitor.clearLine()
  48. monitor.write(text)
  49. term.setCursorPos(1, y)
  50. term.clearLine()
  51. term.write(text)
  52. end
  53.  
  54. -- Function to create a menu frame with a title background extending the entire width
  55. function drawMenuFrame(title)
  56. monitor.setBackgroundColor(colors.blue)
  57. term.setBackgroundColor(colors.blue)
  58. local monitorWidth, _ = monitor.getSize()
  59. local termWidth, _ = term.getSize()
  60.  
  61. -- Create title bar from edge to edge
  62. monitor.clearLine()
  63. term.clearLine()
  64. for x = 1, monitorWidth do
  65. monitor.setCursorPos(x, 1)
  66. monitor.write(" ")
  67. end
  68. for x = 1, termWidth do
  69. term.setCursorPos(x, 1)
  70. term.write(" ")
  71. end
  72.  
  73. -- Center the title
  74. local centerPos = math.floor((math.min(monitorWidth, termWidth) - #title) / 2) + 1
  75. monitor.setCursorPos(centerPos, 1)
  76. monitor.write(title)
  77. term.setCursorPos(centerPos, 1)
  78. term.write(title)
  79. monitor.setBackgroundColor(colors.black)
  80. term.setBackgroundColor(colors.black)
  81. end
  82.  
  83. -- Function to clear the screen
  84. function clearScreen()
  85. monitor.setBackgroundColor(colors.black)
  86. term.setBackgroundColor(colors.black)
  87. monitor.clear()
  88. term.clear()
  89. monitor.setCursorPos(1, 1)
  90. term.setCursorPos(1, 1)
  91. end
  92.  
  93. -- Function to get the mod name from the item ID
  94. function getModNameFromID(itemID)
  95. if itemID then
  96. return itemID:match("^(.-):") or "unknown"
  97. else
  98. return "unknown"
  99. end
  100. end
  101.  
  102. -- Function to check if an item is in the list
  103. function isItemInList(newItem, list)
  104. for _, listItem in ipairs(list) do
  105. if listItem.id == newItem.id and listItem.method == newItem.method then
  106. return true
  107. end
  108. end
  109. return false
  110. end
  111.  
  112. -- Function to add an item to the list
  113. function addItemToList(listType, method, line)
  114. line = line or 3 -- Set default value for `line` if not passed
  115. local item = inventoryManager.getItemInHand()
  116.  
  117. if not item then
  118. writeToAll(line, "Error: No item in hand!", colors.white, colors.red)
  119. os.sleep(2)
  120. return
  121. end
  122.  
  123. -- Create a new item depending on the selected method
  124. local newItem
  125. if method == "ID" then
  126. newItem = { id = item.name or "unknown", method = "ID" }
  127. elseif method == "TAG" then
  128. newItem = { id = (item.tags and item.tags[1]) or "No tag available", method = "TAG" }
  129. elseif method == "MOD" then
  130. newItem = { id = getModNameFromID(item.name), method = "MOD" }
  131. end
  132.  
  133. -- Check if `newItem` has correct data
  134. if not newItem or newItem.id == "unknown" or newItem.id == "No tag available" then
  135. writeToAll(line, "Error: Item data not found!", colors.white, colors.red)
  136. os.sleep(2)
  137. return
  138. end
  139.  
  140. -- Check if the item already exists in the list
  141. local targetList = (listType == "blocked") and blockedItems or allowedItems
  142. if isItemInList(newItem, targetList) then
  143. writeToAll(line, "Error: Item already on list!", colors.white, colors.red)
  144. os.sleep(2)
  145. return
  146. end
  147.  
  148. -- Confirm addition of the new item
  149. local confirm = confirmAddItem(newItem.id, newItem.method)
  150. if confirm then
  151. table.insert(targetList, newItem)
  152. writeToAll(5, "Item added successfully!", colors.white, colors.green)
  153. else
  154. writeToAll(7, "Action cancelled.", colors.white, colors.red)
  155. end
  156.  
  157. os.sleep(2)
  158. end
  159.  
  160. -- Function for manual entry of items
  161. function manualEntry(listType)
  162. clearScreen()
  163. writeToAll(3, "Enter ID, TAG, or MOD:")
  164. local input = ""
  165.  
  166. while true do
  167. local event, param = os.pullEvent()
  168. if event == "char" then
  169. input = input .. param
  170. writeToAll(5, input)
  171. elseif event == "key" then
  172. if param == keys.enter then
  173. break
  174. elseif param == keys.backspace and #input > 0 then
  175. input = input:sub(1, #input - 1)
  176. writeToAll(5, input)
  177. end
  178. end
  179. end
  180.  
  181. if #input > 0 then
  182. local newItem = { id = input, method = "Manual" }
  183. local targetList = listType == "blocked" and blockedItems or allowedItems
  184. if isItemInList(newItem, targetList) then
  185. writeToAll(7, "Item already on list!", colors.white, colors.red)
  186. else
  187. table.insert(targetList, newItem)
  188. writeToAll(5, "Item added successfully!", colors.white, colors.green)
  189. end
  190. else
  191. writeToAll(7, "No input provided!", colors.white, colors.red)
  192. end
  193. os.sleep(2)
  194. end
  195.  
  196. -- Function to display item addition confirmation screen with colored buttons
  197. function confirmAddItem(itemName, method)
  198. clearScreen()
  199. writeToAll(2, "Add " .. itemName .. " (" .. method .. ")?")
  200. drawLeftAlignedButton(5, "Yes", colors.green)
  201. drawLeftAlignedButton(7, "No", colors.red)
  202.  
  203. while true do
  204. local event, side, x, y = os.pullEvent()
  205. if event == "monitor_touch" or (event == "mouse_click" and side == 1) then
  206. if y == 5 then
  207. monitor.setBackgroundColor(colors.green)
  208. term.setBackgroundColor(colors.green)
  209. return true
  210. elseif y == 7 then
  211. monitor.setBackgroundColor(colors.red)
  212. term.setBackgroundColor(colors.red)
  213. return false
  214. end
  215. end
  216. end
  217. end
  218.  
  219. -- Update transferred item list
  220. local function startTransfer(type)
  221. transferStatus = true -- Set transfer status to active
  222. local maxVisibleItems = 14 -- Maximum number of visible items on the screen
  223.  
  224. while transferStatus do
  225. clearScreen()
  226. drawMenuFrame("Transfer In Progress")
  227.  
  228. -- List of transferred items
  229. local items = inventoryManager.list() -- Retrieve item list
  230. for slot, item in pairs(items) do
  231. local shouldTransfer = false
  232.  
  233. -- Decide if item should be transferred
  234. if type == "all_except_blocked" then
  235. -- Transfer if item is NOT on the blockedItems list
  236. shouldTransfer = not isItemInList({id = item.name, method = "ID"}, blockedItems)
  237. elseif type == "only_added" then
  238. -- Transfer if item IS on the allowedItems list
  239. shouldTransfer = isItemInList({id = item.name, method = "ID"}, allowedItems)
  240. end
  241.  
  242. -- Transfer item if it meets the criteria
  243. if shouldTransfer then
  244. inventoryManager.removeItemFromPlayer("up", item)
  245.  
  246. -- Add item with quantity to transferred items list
  247. table.insert(transferredItems, {name = item.name, count = item.count})
  248. end
  249. end
  250.  
  251. -- Display list of transferred items with quantity
  252. local startIndex = math.max(1, #transferredItems - maxVisibleItems + 1)
  253. for i = startIndex, #transferredItems do
  254. local transferredItem = transferredItems[i]
  255. writeToAll(i - startIndex + 3, transferredItem.name .. " (" .. transferredItem.count .. "x)") -- Wyświetlanie z ilością
  256. end
  257.  
  258. -- `Stop` button on line 18
  259. drawLeftAlignedButton(18, "Stop", colors.red)
  260.  
  261. -- Wait for `monitor_touch` events or a 5-second timer
  262. local timer = os.startTimer(5)
  263. while true do
  264. local event, side, x, y = os.pullEvent()
  265. if (event == "monitor_touch" or (event == "mouse_click" and side == 1)) and y == 18 then
  266. -- Clicking `Stop` stops the transfer
  267. transferStatus = false
  268. transferredItems = {} -- Reset transferred items list
  269. clearScreen()
  270. drawMenuFrame("Transfer Stopped")
  271. writeToAll(5, "Transfer Stopped", colors.white, colors.red)
  272. os.sleep(2)
  273. return
  274. elseif event == "timer" and side == timer then
  275. -- Continue transfer after 5 seconds
  276. break
  277. end
  278. end
  279. end
  280. end
  281.  
  282. -- Function to draw the main menu
  283. function drawMainMenu()
  284. clearScreen()
  285. drawMenuFrame("Main Menu")
  286. drawLeftAlignedButton(5, "Transfer All Except Blocked", colors.gray)
  287. drawLeftAlignedButton(7, "Transfer Only Added Items", colors.gray)
  288. end
  289.  
  290. -- Function to handle selection in the main menu
  291. function handleMainMenuTouch(y)
  292. if y >= 5 and y <= 6 then
  293. transferAllExceptBlockedMenu()
  294. waitForSubMenu("all_except_blocked")
  295. elseif y >= 7 and y <= 8 then
  296. transferOnlyAddedItemsMenu()
  297. waitForSubMenu("only_added")
  298. end
  299. end
  300.  
  301. -- Function to display submenu for the "Transfer All Except Blocked" option
  302. function transferAllExceptBlockedMenu()
  303. clearScreen()
  304. drawMenuFrame("Transfer All Except Blocked")
  305. drawLeftAlignedButton(5, "Add Item to Block List", colors.gray)
  306. drawLeftAlignedButton(7, "Show Blocked List", colors.gray) -- Position Y = 7
  307. drawLeftAlignedButton(10, "Start Transfer", colors.green)
  308. drawLeftAlignedButton(18, "Reset All", colors.red)
  309. end
  310.  
  311. -- Function to display submenu for the "Transfer Only Added Items" option
  312. function transferOnlyAddedItemsMenu()
  313. clearScreen()
  314. drawMenuFrame("Transfer Only Added Items")
  315. drawLeftAlignedButton(5, "Add Item to Transfer List", colors.gray)
  316. drawLeftAlignedButton(7, "Show Transfer List", colors.gray) -- Position Y = 7
  317. drawLeftAlignedButton(10, "Start Transfer", colors.green)
  318. drawLeftAlignedButton(18, "Reset All", colors.red)
  319. end
  320.  
  321. -- Function to select item addition method, displayed on both devices
  322. function selectItemAddMethod(listType)
  323. clearScreen()
  324. drawMenuFrame("Select Add Method")
  325. drawLeftAlignedButton(5, "Add by ID", colors.gray)
  326. drawLeftAlignedButton(7, "Add by TAG", colors.gray)
  327. drawLeftAlignedButton(9, "Add by MOD", colors.gray)
  328. drawLeftAlignedButton(11, "Manual Entry", colors.lightgray)
  329. if listType == "blocked" then
  330. drawLeftAlignedButton(13, "Add All from Inventory", colors.lightgray)
  331. end
  332. drawLeftAlignedButton(16, "Back", colors.gray)
  333.  
  334. while true do
  335. local event, side, x, y = os.pullEvent()
  336. if event == "monitor_touch" or (event == "mouse_click" and side == 1) then
  337. if y == 5 then
  338. addItemToList(listType, "ID")
  339. break
  340. elseif y == 7 then
  341. addItemToList(listType, "TAG")
  342. break
  343. elseif y == 9 then
  344. addItemToList(listType, "MOD")
  345. break
  346. elseif y == 11 then
  347. manualEntry(listType)
  348. break
  349. elseif y == 13 and listType == "blocked" then
  350. confirmAddAllFromInventory()
  351. break
  352. elseif y == 16 then
  353. drawMainMenu()
  354. break
  355. end
  356. end
  357. end
  358. end
  359.  
  360. -- Add all items from inventory to blocked
  361. function confirmAddAllFromInventory()
  362. clearScreen()
  363. writeToAll(2, "Add all items from inventory?", colors.white, colors.gray)
  364. drawLeftAlignedButton(5, "Yes", colors.green)
  365. drawLeftAlignedButton(7, "No", colors.red)
  366.  
  367. while true do
  368. local event, side, x, y = os.pullEvent()
  369. if event == "monitor_touch" or (event == "mouse_click" and side == 1) then
  370. if y == 5 then
  371. addAllItemsToBlockedList()
  372. writeToAll(5, "All items added to blocked list!", colors.white, colors.green)
  373. os.sleep(2)
  374. return
  375. elseif y == 7 then
  376. writeToAll(7, "Action cancelled.", colors.white, colors.red)
  377. os.sleep(2)
  378. return
  379. end
  380. end
  381. end
  382. end
  383.  
  384. -- Load all items form inventory to blocked items
  385. function addAllItemsToBlockedList()
  386. local items = inventoryManager.list() -- List all items in inventory
  387.  
  388. for slot, item in pairs(items) do
  389. local newItem = { id = item.name or "unknown", method = "ID" }
  390.  
  391. -- Check if item is already on the list
  392. if not isItemInList(newItem, blockedItems) then
  393. table.insert(blockedItems, newItem)
  394. end
  395. end
  396. end
  397.  
  398. function toggleTransfer(type)
  399. transferStatus = not transferStatus
  400. if transferStatus then
  401. startTransfer(type)
  402. end
  403. end
  404.  
  405. -- Function waiting for an action in the submenu with support for monitor_touch and mouse_click
  406. function waitForSubMenu(type)
  407. while true do
  408. local event, side, x, y = os.pullEvent()
  409.  
  410. if event == "monitor_touch" or (event == "mouse_click" and side == 1) then
  411. if y == 5 then
  412. selectItemAddMethod(type == "all_except_blocked" and "blocked" or "allowed")
  413. elseif y == 7 then
  414. -- Correct call to showList for the appropriate list
  415. if type == "all_except_blocked" then
  416. showList(blockedItems, "Blocked List")
  417. else
  418. showList(allowedItems, "Transfer List")
  419. end
  420. elseif y == 10 then
  421. toggleTransfer(type)
  422. elseif y == 18 then
  423. blockedItems, allowedItems, transferredItems = {}, {}, {}
  424. drawMainMenu()
  425. break
  426. end
  427. end
  428.  
  429. clearScreen()
  430. if type == "all_except_blocked" then
  431. transferAllExceptBlockedMenu()
  432. else
  433. transferOnlyAddedItemsMenu()
  434. end
  435. end
  436. end
  437.  
  438. -- Function to display the list of items with click and removal handling
  439. function showList(list, title)
  440. local page = 1
  441. local itemsPerPage = 11 -- Number of visible items on the screen
  442.  
  443. while true do
  444. local maxPage = math.ceil(#list / itemsPerPage) -- Update the maximum number of pages after item removal
  445.  
  446. clearScreen()
  447. drawMenuFrame("" .. title .. " (Page " .. page .. "/" .. maxPage .. ")")
  448. local offset = (page - 1) * itemsPerPage
  449. for i = 1, itemsPerPage do
  450. local index = offset + i
  451. if list[index] then
  452. writeToAll(i + 3, list[index].id .. " (" .. list[index].method .. ")")
  453. end
  454. end
  455. drawLeftAlignedButton(16, "Next Page", colors.gray)
  456. drawLeftAlignedButton(17, "Previous Page", colors.gray)
  457. drawLeftAlignedButton(18, "Back", colors.gray)
  458.  
  459. local event, side, x, y, button = os.pullEvent()
  460.  
  461. -- Handling page navigation and return
  462. if (event == "monitor_touch" or (event == "mouse_click" and side == 1)) then
  463. if y == 16 and page < maxPage then
  464. page = page + 1
  465. elseif y == 17 and page > 1 then
  466. page = page - 1
  467. elseif y == 18 then
  468. break
  469. else
  470. -- Handling item removal with the left mouse button
  471. local itemIndex = y - 3 + offset
  472. if list[itemIndex] then
  473. local item = list[itemIndex]
  474. local confirm = confirmRemoveItem(item)
  475. if confirm then
  476. table.remove(list, itemIndex)
  477. writeToAll(5, "Item removed successfully!", colors.white, colors.green)
  478. os.sleep(2)
  479. else
  480. writeToAll(7, "Action cancelled.", colors.white, colors.red)
  481. os.sleep(2)
  482. end
  483. end
  484. end
  485. end
  486. end
  487. end
  488.  
  489. -- Function to display the item removal confirmation screen
  490. function confirmRemoveItem(item)
  491. clearScreen()
  492. writeToAll(2, "Remove " .. item.id .. " (" .. item.method .. ")?", colors.white, colors.red)
  493. drawLeftAlignedButton(5, "Yes", colors.green)
  494. drawLeftAlignedButton(7, "No", colors.red)
  495.  
  496. while true do
  497. local event, side, x, y = os.pullEvent()
  498. if event == "monitor_touch" or (event == "mouse_click" and side == 1) then
  499. if y == 5 then
  500. return true -- Confirm item removal
  501. elseif y == 7 then
  502. return false -- Cancel item removal
  503. end
  504. end
  505. end
  506. end
  507.  
  508. -- Main program loop
  509. while true do
  510. drawMainMenu()
  511. local event, side, x, y = os.pullEvent()
  512. if event == "monitor_touch" or (event == "mouse_click" and side == 1) then
  513. handleMainMenuTouch(y)
  514. end
  515. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement