Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- === CONFIGURATION ===
- local monitor = peripheral.wrap("right")
- local inventoryManager = peripheral.wrap("left")
- -- Item lists
- local blockedItems = {}
- local allowedItems = {}
- local transferredItems = {}
- local transferStatus = false
- -- Transfer status
- local transferStatus = false
- -- Set the default text size
- monitor.setTextScale(0.5)
- -- Function to check if the monitor supports colors
- local monitorSupportsColor = monitor.isColor() or monitor.isColour()
- -- Function to display text on the monitor and computer with a background
- function writeToAll(y, text, textColor, bgColor)
- textColor = textColor or colors.white
- bgColor = bgColor or colors.black
- monitor.setBackgroundColor(bgColor)
- monitor.setTextColor(textColor)
- monitor.setCursorPos(1, y) -- Set cursor to the first position
- monitor.clearLine()
- monitor.write(text)
- term.setBackgroundColor(bgColor)
- term.setTextColor(textColor)
- term.setCursorPos(1, y) -- Set cursor to the first position
- term.clearLine()
- term.write(text)
- end
- -- Function to draw buttons on the monitor and terminal with text length adjustment
- function drawLeftAlignedButton(y, text, color)
- color = color or colors.gray
- monitor.setBackgroundColor(color)
- term.setBackgroundColor(color)
- monitor.setTextColor(colors.white)
- term.setTextColor(colors.white)
- local textLength = #text
- monitor.setCursorPos(2, y)
- monitor.clearLine()
- monitor.write(text)
- term.setCursorPos(1, y)
- term.clearLine()
- term.write(text)
- end
- -- Function to create a menu frame with a title background extending the entire width
- function drawMenuFrame(title)
- monitor.setBackgroundColor(colors.blue)
- term.setBackgroundColor(colors.blue)
- local monitorWidth, _ = monitor.getSize()
- local termWidth, _ = term.getSize()
- -- Create title bar from edge to edge
- monitor.clearLine()
- term.clearLine()
- for x = 1, monitorWidth do
- monitor.setCursorPos(x, 1)
- monitor.write(" ")
- end
- for x = 1, termWidth do
- term.setCursorPos(x, 1)
- term.write(" ")
- end
- -- Center the title
- local centerPos = math.floor((math.min(monitorWidth, termWidth) - #title) / 2) + 1
- monitor.setCursorPos(centerPos, 1)
- monitor.write(title)
- term.setCursorPos(centerPos, 1)
- term.write(title)
- monitor.setBackgroundColor(colors.black)
- term.setBackgroundColor(colors.black)
- end
- -- Function to clear the screen
- function clearScreen()
- monitor.setBackgroundColor(colors.black)
- term.setBackgroundColor(colors.black)
- monitor.clear()
- term.clear()
- monitor.setCursorPos(1, 1)
- term.setCursorPos(1, 1)
- end
- -- Function to get the mod name from the item ID
- function getModNameFromID(itemID)
- if itemID then
- return itemID:match("^(.-):") or "unknown"
- else
- return "unknown"
- end
- end
- -- Function to check if an item is in the list
- function isItemInList(newItem, list)
- for _, listItem in ipairs(list) do
- if listItem.id == newItem.id and listItem.method == newItem.method then
- return true
- end
- end
- return false
- end
- -- Function to add an item to the list
- function addItemToList(listType, method, line)
- line = line or 3 -- Set default value for `line` if not passed
- local item = inventoryManager.getItemInHand()
- if not item then
- writeToAll(line, "Error: No item in hand!", colors.white, colors.red)
- os.sleep(2)
- return
- end
- -- Create a new item depending on the selected method
- local newItem
- if method == "ID" then
- newItem = { id = item.name or "unknown", method = "ID" }
- elseif method == "TAG" then
- newItem = { id = (item.tags and item.tags[1]) or "No tag available", method = "TAG" }
- elseif method == "MOD" then
- newItem = { id = getModNameFromID(item.name), method = "MOD" }
- end
- -- Check if `newItem` has correct data
- if not newItem or newItem.id == "unknown" or newItem.id == "No tag available" then
- writeToAll(line, "Error: Item data not found!", colors.white, colors.red)
- os.sleep(2)
- return
- end
- -- Check if the item already exists in the list
- local targetList = (listType == "blocked") and blockedItems or allowedItems
- if isItemInList(newItem, targetList) then
- writeToAll(line, "Error: Item already on list!", colors.white, colors.red)
- os.sleep(2)
- return
- end
- -- Confirm addition of the new item
- local confirm = confirmAddItem(newItem.id, newItem.method)
- if confirm then
- table.insert(targetList, newItem)
- writeToAll(5, "Item added successfully!", colors.white, colors.green)
- else
- writeToAll(7, "Action cancelled.", colors.white, colors.red)
- end
- os.sleep(2)
- end
- -- Function for manual entry of items
- function manualEntry(listType)
- clearScreen()
- writeToAll(3, "Enter ID, TAG, or MOD:")
- local input = ""
- while true do
- local event, param = os.pullEvent()
- if event == "char" then
- input = input .. param
- writeToAll(5, input)
- elseif event == "key" then
- if param == keys.enter then
- break
- elseif param == keys.backspace and #input > 0 then
- input = input:sub(1, #input - 1)
- writeToAll(5, input)
- end
- end
- end
- if #input > 0 then
- local newItem = { id = input, method = "Manual" }
- local targetList = listType == "blocked" and blockedItems or allowedItems
- if isItemInList(newItem, targetList) then
- writeToAll(7, "Item already on list!", colors.white, colors.red)
- else
- table.insert(targetList, newItem)
- writeToAll(5, "Item added successfully!", colors.white, colors.green)
- end
- else
- writeToAll(7, "No input provided!", colors.white, colors.red)
- end
- os.sleep(2)
- end
- -- Function to display item addition confirmation screen with colored buttons
- function confirmAddItem(itemName, method)
- clearScreen()
- writeToAll(2, "Add " .. itemName .. " (" .. method .. ")?")
- drawLeftAlignedButton(5, "Yes", colors.green)
- drawLeftAlignedButton(7, "No", colors.red)
- while true do
- local event, side, x, y = os.pullEvent()
- if event == "monitor_touch" or (event == "mouse_click" and side == 1) then
- if y == 5 then
- monitor.setBackgroundColor(colors.green)
- term.setBackgroundColor(colors.green)
- return true
- elseif y == 7 then
- monitor.setBackgroundColor(colors.red)
- term.setBackgroundColor(colors.red)
- return false
- end
- end
- end
- end
- -- Update transferred item list
- local function startTransfer(type)
- transferStatus = true -- Set transfer status to active
- local maxVisibleItems = 14 -- Maximum number of visible items on the screen
- while transferStatus do
- clearScreen()
- drawMenuFrame("Transfer In Progress")
- -- List of transferred items
- local items = inventoryManager.list() -- Retrieve item list
- for slot, item in pairs(items) do
- local shouldTransfer = false
- -- Decide if item should be transferred
- if type == "all_except_blocked" then
- -- Transfer if item is NOT on the blockedItems list
- shouldTransfer = not isItemInList({id = item.name, method = "ID"}, blockedItems)
- elseif type == "only_added" then
- -- Transfer if item IS on the allowedItems list
- shouldTransfer = isItemInList({id = item.name, method = "ID"}, allowedItems)
- end
- -- Transfer item if it meets the criteria
- if shouldTransfer then
- inventoryManager.removeItemFromPlayer("up", item)
- -- Add item with quantity to transferred items list
- table.insert(transferredItems, {name = item.name, count = item.count})
- end
- end
- -- Display list of transferred items with quantity
- local startIndex = math.max(1, #transferredItems - maxVisibleItems + 1)
- for i = startIndex, #transferredItems do
- local transferredItem = transferredItems[i]
- writeToAll(i - startIndex + 3, transferredItem.name .. " (" .. transferredItem.count .. "x)") -- Wyświetlanie z ilością
- end
- -- `Stop` button on line 18
- drawLeftAlignedButton(18, "Stop", colors.red)
- -- Wait for `monitor_touch` events or a 5-second timer
- local timer = os.startTimer(5)
- while true do
- local event, side, x, y = os.pullEvent()
- if (event == "monitor_touch" or (event == "mouse_click" and side == 1)) and y == 18 then
- -- Clicking `Stop` stops the transfer
- transferStatus = false
- transferredItems = {} -- Reset transferred items list
- clearScreen()
- drawMenuFrame("Transfer Stopped")
- writeToAll(5, "Transfer Stopped", colors.white, colors.red)
- os.sleep(2)
- return
- elseif event == "timer" and side == timer then
- -- Continue transfer after 5 seconds
- break
- end
- end
- end
- end
- -- Function to draw the main menu
- function drawMainMenu()
- clearScreen()
- drawMenuFrame("Main Menu")
- drawLeftAlignedButton(5, "Transfer All Except Blocked", colors.gray)
- drawLeftAlignedButton(7, "Transfer Only Added Items", colors.gray)
- end
- -- Function to handle selection in the main menu
- function handleMainMenuTouch(y)
- if y >= 5 and y <= 6 then
- transferAllExceptBlockedMenu()
- waitForSubMenu("all_except_blocked")
- elseif y >= 7 and y <= 8 then
- transferOnlyAddedItemsMenu()
- waitForSubMenu("only_added")
- end
- end
- -- Function to display submenu for the "Transfer All Except Blocked" option
- function transferAllExceptBlockedMenu()
- clearScreen()
- drawMenuFrame("Transfer All Except Blocked")
- drawLeftAlignedButton(5, "Add Item to Block List", colors.gray)
- drawLeftAlignedButton(7, "Show Blocked List", colors.gray) -- Position Y = 7
- drawLeftAlignedButton(10, "Start Transfer", colors.green)
- drawLeftAlignedButton(18, "Reset All", colors.red)
- end
- -- Function to display submenu for the "Transfer Only Added Items" option
- function transferOnlyAddedItemsMenu()
- clearScreen()
- drawMenuFrame("Transfer Only Added Items")
- drawLeftAlignedButton(5, "Add Item to Transfer List", colors.gray)
- drawLeftAlignedButton(7, "Show Transfer List", colors.gray) -- Position Y = 7
- drawLeftAlignedButton(10, "Start Transfer", colors.green)
- drawLeftAlignedButton(18, "Reset All", colors.red)
- end
- -- Function to select item addition method, displayed on both devices
- function selectItemAddMethod(listType)
- clearScreen()
- drawMenuFrame("Select Add Method")
- drawLeftAlignedButton(5, "Add by ID", colors.gray)
- drawLeftAlignedButton(7, "Add by TAG", colors.gray)
- drawLeftAlignedButton(9, "Add by MOD", colors.gray)
- drawLeftAlignedButton(11, "Manual Entry", colors.lightgray)
- if listType == "blocked" then
- drawLeftAlignedButton(13, "Add All from Inventory", colors.lightgray)
- end
- drawLeftAlignedButton(16, "Back", colors.gray)
- while true do
- local event, side, x, y = os.pullEvent()
- if event == "monitor_touch" or (event == "mouse_click" and side == 1) then
- if y == 5 then
- addItemToList(listType, "ID")
- break
- elseif y == 7 then
- addItemToList(listType, "TAG")
- break
- elseif y == 9 then
- addItemToList(listType, "MOD")
- break
- elseif y == 11 then
- manualEntry(listType)
- break
- elseif y == 13 and listType == "blocked" then
- confirmAddAllFromInventory()
- break
- elseif y == 16 then
- drawMainMenu()
- break
- end
- end
- end
- end
- -- Add all items from inventory to blocked
- function confirmAddAllFromInventory()
- clearScreen()
- writeToAll(2, "Add all items from inventory?", colors.white, colors.gray)
- drawLeftAlignedButton(5, "Yes", colors.green)
- drawLeftAlignedButton(7, "No", colors.red)
- while true do
- local event, side, x, y = os.pullEvent()
- if event == "monitor_touch" or (event == "mouse_click" and side == 1) then
- if y == 5 then
- addAllItemsToBlockedList()
- writeToAll(5, "All items added to blocked list!", colors.white, colors.green)
- os.sleep(2)
- return
- elseif y == 7 then
- writeToAll(7, "Action cancelled.", colors.white, colors.red)
- os.sleep(2)
- return
- end
- end
- end
- end
- -- Load all items form inventory to blocked items
- function addAllItemsToBlockedList()
- local items = inventoryManager.list() -- List all items in inventory
- for slot, item in pairs(items) do
- local newItem = { id = item.name or "unknown", method = "ID" }
- -- Check if item is already on the list
- if not isItemInList(newItem, blockedItems) then
- table.insert(blockedItems, newItem)
- end
- end
- end
- function toggleTransfer(type)
- transferStatus = not transferStatus
- if transferStatus then
- startTransfer(type)
- end
- end
- -- Function waiting for an action in the submenu with support for monitor_touch and mouse_click
- function waitForSubMenu(type)
- while true do
- local event, side, x, y = os.pullEvent()
- if event == "monitor_touch" or (event == "mouse_click" and side == 1) then
- if y == 5 then
- selectItemAddMethod(type == "all_except_blocked" and "blocked" or "allowed")
- elseif y == 7 then
- -- Correct call to showList for the appropriate list
- if type == "all_except_blocked" then
- showList(blockedItems, "Blocked List")
- else
- showList(allowedItems, "Transfer List")
- end
- elseif y == 10 then
- toggleTransfer(type)
- elseif y == 18 then
- blockedItems, allowedItems, transferredItems = {}, {}, {}
- drawMainMenu()
- break
- end
- end
- clearScreen()
- if type == "all_except_blocked" then
- transferAllExceptBlockedMenu()
- else
- transferOnlyAddedItemsMenu()
- end
- end
- end
- -- Function to display the list of items with click and removal handling
- function showList(list, title)
- local page = 1
- local itemsPerPage = 11 -- Number of visible items on the screen
- while true do
- local maxPage = math.ceil(#list / itemsPerPage) -- Update the maximum number of pages after item removal
- clearScreen()
- drawMenuFrame("" .. title .. " (Page " .. page .. "/" .. maxPage .. ")")
- local offset = (page - 1) * itemsPerPage
- for i = 1, itemsPerPage do
- local index = offset + i
- if list[index] then
- writeToAll(i + 3, list[index].id .. " (" .. list[index].method .. ")")
- end
- end
- drawLeftAlignedButton(16, "Next Page", colors.gray)
- drawLeftAlignedButton(17, "Previous Page", colors.gray)
- drawLeftAlignedButton(18, "Back", colors.gray)
- local event, side, x, y, button = os.pullEvent()
- -- Handling page navigation and return
- if (event == "monitor_touch" or (event == "mouse_click" and side == 1)) then
- if y == 16 and page < maxPage then
- page = page + 1
- elseif y == 17 and page > 1 then
- page = page - 1
- elseif y == 18 then
- break
- else
- -- Handling item removal with the left mouse button
- local itemIndex = y - 3 + offset
- if list[itemIndex] then
- local item = list[itemIndex]
- local confirm = confirmRemoveItem(item)
- if confirm then
- table.remove(list, itemIndex)
- writeToAll(5, "Item removed successfully!", colors.white, colors.green)
- os.sleep(2)
- else
- writeToAll(7, "Action cancelled.", colors.white, colors.red)
- os.sleep(2)
- end
- end
- end
- end
- end
- end
- -- Function to display the item removal confirmation screen
- function confirmRemoveItem(item)
- clearScreen()
- writeToAll(2, "Remove " .. item.id .. " (" .. item.method .. ")?", colors.white, colors.red)
- drawLeftAlignedButton(5, "Yes", colors.green)
- drawLeftAlignedButton(7, "No", colors.red)
- while true do
- local event, side, x, y = os.pullEvent()
- if event == "monitor_touch" or (event == "mouse_click" and side == 1) then
- if y == 5 then
- return true -- Confirm item removal
- elseif y == 7 then
- return false -- Cancel item removal
- end
- end
- end
- end
- -- Main program loop
- while true do
- drawMainMenu()
- local event, side, x, y = os.pullEvent()
- if event == "monitor_touch" or (event == "mouse_click" and side == 1) then
- handleMainMenuTouch(y)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement