Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- AE2 Crafting Interface System
- -- All-in-one file version
- -- Configuration
- local MONITOR_WIDTH = 5 -- 5 monitors wide
- local MONITOR_HEIGHT = 3 -- 3 monitors high
- -- Global variables
- local monitors = {}
- local bridge = nil
- local totalWidth = 0
- local totalHeight = 0
- local currentScreen = "main" -- main, category, quantity
- local selectedCategory = nil
- local selectedItem = nil
- local buttons = {}
- -- Categories and items (focused on popular items)
- local categories = {
- {
- id = "building",
- name = "Building Blocks",
- items = {
- {id = "minecraft:stone", name = "Stone"},
- {id = "minecraft:oak_planks", name = "Oak Planks"},
- {id = "minecraft:glass", name = "Glass"},
- {id = "minecraft:cobblestone", name = "Cobblestone"},
- {id = "minecraft:smooth_stone", name = "Smooth Stone"},
- {id = "minecraft:bricks", name = "Bricks"},
- {id = "minecraft:stone_bricks", name = "Stone Bricks"},
- {id = "minecraft:quartz_block", name = "Quartz Block"}
- }
- },
- {
- id = "tools",
- name = "Tools",
- items = {
- {id = "minecraft:diamond_pickaxe", name = "Diamond Pickaxe"},
- {id = "minecraft:diamond_axe", name = "Diamond Axe"},
- {id = "minecraft:diamond_shovel", name = "Diamond Shovel"},
- {id = "minecraft:diamond_hoe", name = "Diamond Hoe"},
- {id = "minecraft:iron_pickaxe", name = "Iron Pickaxe"},
- {id = "minecraft:iron_axe", name = "Iron Axe"},
- {id = "minecraft:shears", name = "Shears"},
- {id = "minecraft:fishing_rod", name = "Fishing Rod"}
- }
- },
- {
- id = "redstone",
- name = "Redstone",
- items = {
- {id = "minecraft:redstone", name = "Redstone Dust"},
- {id = "minecraft:repeater", name = "Repeater"},
- {id = "minecraft:piston", name = "Piston"},
- {id = "minecraft:sticky_piston", name = "Sticky Piston"},
- {id = "minecraft:hopper", name = "Hopper"},
- {id = "minecraft:observer", name = "Observer"},
- {id = "minecraft:dispenser", name = "Dispenser"},
- {id = "minecraft:dropper", name = "Dropper"}
- }
- },
- {
- id = "combat",
- name = "Combat",
- items = {
- {id = "minecraft:diamond_sword", name = "Diamond Sword"},
- {id = "minecraft:bow", name = "Bow"},
- {id = "minecraft:arrow", name = "Arrow"},
- {id = "minecraft:shield", name = "Shield"},
- {id = "minecraft:diamond_helmet", name = "Diamond Helmet"},
- {id = "minecraft:diamond_chestplate", name = "Diamond Chestplate"},
- {id = "minecraft:diamond_leggings", name = "Diamond Leggings"},
- {id = "minecraft:diamond_boots", name = "Diamond Boots"}
- }
- },
- {
- id = "food",
- name = "Food",
- items = {
- {id = "minecraft:bread", name = "Bread"},
- {id = "minecraft:cooked_beef", name = "Steak"},
- {id = "minecraft:golden_apple", name = "Golden Apple"},
- {id = "minecraft:cooked_chicken", name = "Cooked Chicken"},
- {id = "minecraft:cake", name = "Cake"},
- {id = "minecraft:cookie", name = "Cookie"},
- {id = "minecraft:pumpkin_pie", name = "Pumpkin Pie"},
- {id = "minecraft:golden_carrot", name = "Golden Carrot"}
- }
- },
- {
- id = "transportation",
- name = "Transportation",
- items = {
- {id = "minecraft:rail", name = "Rail"},
- {id = "minecraft:powered_rail", name = "Powered Rail"},
- {id = "minecraft:detector_rail", name = "Detector Rail"},
- {id = "minecraft:minecart", name = "Minecart"},
- {id = "minecraft:chest_minecart", name = "Storage Minecart"},
- {id = "minecraft:furnace_minecart", name = "Furnace Minecart"},
- {id = "minecraft:hopper_minecart", name = "Hopper Minecart"},
- {id = "minecraft:boat", name = "Boat"}
- }
- }
- }
- -- Initialize the system
- function init()
- -- Find monitors - improved detection
- local allPeripherals = peripheral.getNames()
- local monitorCount = 0
- -- Print all available peripherals for debugging
- print("Available peripherals:")
- for _, name in ipairs(allPeripherals) do
- print("- " .. name .. " (" .. peripheral.getType(name) .. ")")
- end
- -- First try the standard naming convention with a wider range
- for i = 0, 15 do -- Try a wider range of indices
- local name = "monitor_" .. i
- if peripheral.isPresent(name) then
- print("Found monitor: " .. name)
- monitors[monitorCount+1] = peripheral.wrap(name)
- monitors[monitorCount+1].setTextScale(0.5)
- monitors[monitorCount+1].clear()
- monitorCount = monitorCount + 1
- end
- end
- -- If we didn't find enough monitors, look for any monitor peripherals
- if monitorCount < (MONITOR_WIDTH * MONITOR_HEIGHT) then
- for _, name in ipairs(allPeripherals) do
- -- Check if it's a monitor and not already in our list
- if peripheral.getType(name) == "monitor" then
- local alreadyAdded = false
- for _, existingMonitor in pairs(monitors) do
- if peripheral.getName(existingMonitor) == name then
- alreadyAdded = true
- break
- end
- end
- if not alreadyAdded and monitorCount < (MONITOR_WIDTH * MONITOR_HEIGHT) then
- monitorCount = monitorCount + 1
- monitors[monitorCount] = peripheral.wrap(name)
- monitors[monitorCount].setTextScale(0.5)
- monitors[monitorCount].clear()
- print("Added monitor: " .. name)
- end
- end
- end
- end
- if monitorCount < (MONITOR_WIDTH * MONITOR_HEIGHT) then
- print("Warning: Not all monitors detected. Found " .. monitorCount .. "/" .. (MONITOR_WIDTH * MONITOR_HEIGHT))
- -- If we found at least one monitor, let's continue anyway
- if monitorCount > 0 then
- print("Continuing with available monitors...")
- -- Adjust the expected dimensions based on what we have
- MONITOR_WIDTH = 1
- MONITOR_HEIGHT = monitorCount
- else
- return false
- end
- else
- print("Successfully connected to " .. monitorCount .. " monitors")
- end
- -- Find ME Bridge
- bridge = peripheral.find("meBridge")
- if not bridge then
- print("Error: ME Bridge not found!")
- return false
- end
- -- Get monitor dimensions
- if monitorCount > 0 then
- local w, h = monitors[1].getSize()
- totalWidth = w * MONITOR_WIDTH
- totalHeight = h * MONITOR_HEIGHT
- print("Total display size: " .. totalWidth .. "x" .. totalHeight)
- end
- return true
- end
- -- Convert global coordinates to monitor-specific coordinates
- function getMonitorAt(x, y)
- if x < 1 or y < 1 or x > totalWidth or y > totalHeight then
- return nil
- end
- local monitorWidth = totalWidth / MONITOR_WIDTH
- local monitorHeight = totalHeight / MONITOR_HEIGHT
- local monX = math.ceil(x / monitorWidth)
- local monY = math.ceil(y / monitorHeight)
- local index = (monY - 1) * MONITOR_WIDTH + monX
- -- Check if the monitor exists at this index
- if not monitors[index] then
- print("Warning: No monitor found at index " .. index .. " (position " .. monX .. "," .. monY .. ")")
- return nil
- end
- local relX = (x - 1) % monitorWidth + 1
- local relY = (y - 1) % monitorHeight + 1
- return monitors[index], relX, relY
- end
- -- Clear all monitors
- function clearScreen()
- for _, monitor in pairs(monitors) do
- monitor.clear()
- end
- end
- -- Write text at global coordinates
- function writeText(x, y, text, textColor, bgColor)
- local monitor, relX, relY = getMonitorAt(x, y)
- if monitor then
- if textColor then monitor.setTextColor(textColor) end
- if bgColor then monitor.setBackgroundColor(bgColor) end
- monitor.setCursorPos(relX, relY)
- monitor.write(text)
- end
- end
- -- Draw a filled rectangle
- function fillRect(startX, startY, endX, endY, color)
- for y = startY, endY do
- for x = startX, endX do
- local monitor, relX, relY = getMonitorAt(x, y)
- if monitor then
- monitor.setBackgroundColor(color)
- monitor.setCursorPos(relX, relY)
- monitor.write(" ")
- end
- end
- end
- end
- -- Draw a button with text
- function drawButton(x, y, width, height, text, bgColor, textColor)
- fillRect(x, y, x + width - 1, y + height - 1, bgColor or colors.blue)
- -- Center the text
- local textX = x + math.floor((width - #text) / 2)
- local textY = y + math.floor(height / 2)
- writeText(textX, textY, text, textColor or colors.white, bgColor or colors.blue)
- return {
- x = x,
- y = y,
- width = width,
- height = height,
- text = text
- }
- end
- -- Check if coordinates are within a button
- function isInButton(button, x, y)
- return x >= button.x and x < button.x + button.width and
- y >= button.y and y < button.y + button.height
- end
- -- Draw a centered title
- function drawTitle(y, text, color)
- local textX = math.floor((totalWidth - #text) / 2) + 1
- writeText(textX, y, text, color or colors.yellow, colors.black)
- end
- -- Get AE2 system status
- function getAE2Status()
- if not bridge then return {items = "Unknown", crafting = "Unknown"} end
- local status = {
- items = "0",
- crafting = "None"
- }
- -- Get item count
- local items = bridge.listItems()
- status.items = tostring(#items)
- -- Get crafting status
- local crafting = bridge.getCraftingCPUs()
- local activeCPUs = 0
- for _, cpu in pairs(crafting) do
- if cpu.busy then activeCPUs = activeCPUs + 1 end
- end
- if activeCPUs > 0 then
- status.crafting = activeCPUs .. " Active"
- else
- status.crafting = "Idle"
- end
- return status
- end
- -- Craft an item using AE2
- function craftItem(itemId, amount)
- if not bridge then return false, "ME Bridge not connected" end
- local success = false
- local message = "Unknown error"
- -- Print debug info
- print("Attempting to craft: " .. itemId .. " x" .. amount)
- -- Check if the item exists in the system
- local items = bridge.listItems()
- local itemExists = false
- for _, item in pairs(items) do
- if item.name == itemId or item.fingerprint == itemId then
- itemExists = true
- print("Item found in system: " .. item.name)
- break
- end
- end
- if not itemExists then
- print("Warning: Item not found in AE2 system")
- end
- -- Try to craft the item
- local status, result = pcall(function()
- return bridge.craftItem({name = itemId, count = amount})
- end)
- if status then
- if result then
- success = true
- message = "Crafting started"
- print("Crafting successful")
- else
- message = "Failed to craft item"
- print("Crafting failed - item may not have a pattern")
- end
- else
- message = "Error: " .. tostring(result)
- print("Crafting error: " .. tostring(result))
- end
- -- Try alternative method if first one failed
- if not success then
- print("Trying alternative crafting method...")
- status, result = pcall(function()
- return bridge.requestCrafting({name = itemId, count = amount})
- end)
- if status and result then
- success = true
- message = "Crafting started (alt method)"
- print("Alternative crafting successful")
- end
- end
- return success, message
- end
- -- Draw the main category selection screen
- function drawMainScreen()
- clearScreen()
- buttons = {}
- -- Draw title
- drawTitle(2, "AE2 Crafting System", colors.yellow)
- -- Draw categories
- local cols = 3
- local rows = math.ceil(#categories / cols)
- local buttonWidth = 20
- local buttonHeight = 3
- local startX = math.floor((totalWidth - (cols * buttonWidth)) / 2) + 1
- local startY = 5
- for i, category in ipairs(categories) do
- local col = (i - 1) % cols
- local row = math.floor((i - 1) / cols)
- local x = startX + (col * (buttonWidth + 2))
- local y = startY + (row * (buttonHeight + 1))
- local button = drawButton(x, y, buttonWidth, buttonHeight, category.name, colors.blue)
- button.action = "category"
- button.category = category
- table.insert(buttons, button)
- end
- -- Draw status
- local status = getAE2Status()
- writeText(2, totalHeight - 1, "Items: " .. status.items, colors.white)
- writeText(totalWidth - 20, totalHeight - 1, "Crafting: " .. status.crafting, colors.white)
- end
- -- Draw the items in a category
- function drawCategoryScreen(category)
- clearScreen()
- buttons = {}
- -- Draw title
- drawTitle(2, category.name .. " Items", colors.yellow)
- -- Draw items
- local itemList = category.items
- local cols = 4
- local rows = math.ceil(#itemList / cols)
- local buttonWidth = 16
- local buttonHeight = 3
- local startX = math.floor((totalWidth - (cols * buttonWidth)) / 2) + 1
- local startY = 5
- for i, item in ipairs(itemList) do
- local col = (i - 1) % cols
- local row = math.floor((i - 1) / cols)
- local x = startX + (col * (buttonWidth + 2))
- local y = startY + (row * (buttonHeight + 1))
- local button = drawButton(x, y, buttonWidth, buttonHeight, item.name, colors.cyan)
- button.action = "item"
- button.item = item
- table.insert(buttons, button)
- end
- -- Back button
- local backButton = drawButton(2, totalHeight - 3, 10, 3, "Back", colors.red)
- backButton.action = "back"
- table.insert(buttons, backButton)
- end
- -- Draw quantity selection screen
- function drawQuantityScreen(item)
- clearScreen()
- buttons = {}
- -- Draw title
- drawTitle(2, "Craft: " .. item.name, colors.yellow)
- -- Draw quantity options
- local quantities = {1, 4, 8, 16, 32, 64}
- local buttonWidth = 10
- local buttonHeight = 3
- local startX = math.floor((totalWidth - (#quantities * buttonWidth)) / 2) + 1
- local startY = totalHeight / 2 - buttonHeight
- for i, amount in ipairs(quantities) do
- local x = startX + ((i-1) * (buttonWidth + 2))
- local button = drawButton(x, startY, buttonWidth, buttonHeight, tostring(amount), colors.green)
- button.action = "quantity"
- button.amount = amount
- table.insert(buttons, button)
- end
- -- Back button
- local backButton = drawButton(2, totalHeight - 3, 10, 3, "Back", colors.red)
- backButton.action = "back"
- table.insert(buttons, backButton)
- end
- -- Handle touch events
- function handleTouch(x, y)
- for _, button in ipairs(buttons) do
- if isInButton(button, x, y) then
- if button.action == "category" then
- selectedCategory = button.category
- currentScreen = "category"
- drawCategoryScreen(selectedCategory)
- elseif button.action == "item" then
- selectedItem = button.item
- currentScreen = "quantity"
- drawQuantityScreen(selectedItem)
- elseif button.action == "quantity" then
- local success, message = craftItem(selectedItem.id, button.amount)
- -- Show feedback
- if success then
- fillRect(totalWidth/4, totalHeight/2, totalWidth*3/4, totalHeight/2 + 2, colors.green)
- writeText(totalWidth/2 - 10, totalHeight/2 + 1, "Crafting " .. button.amount .. " " .. selectedItem.name, colors.white, colors.green)
- else
- fillRect(totalWidth/4, totalHeight/2, totalWidth*3/4, totalHeight/2 + 2, colors.red)
- writeText(totalWidth/2 - 10, totalHeight/2 + 1, "Error: " .. message, colors.white, colors.red)
- end
- -- Wait briefly to show feedback
- sleep(1)
- drawCategoryScreen(selectedCategory)
- elseif button.action == "back" then
- if currentScreen == "category" then
- currentScreen = "main"
- drawMainScreen()
- elseif currentScreen == "quantity" then
- currentScreen = "category"
- drawCategoryScreen(selectedCategory)
- end
- end
- break
- end
- end
- end
- -- Main program
- function run()
- if not init() then
- print("Failed to initialize the system")
- return
- end
- -- Draw initial screen
- drawMainScreen()
- -- Main event loop
- while true do
- local event, side, x, y = os.pullEvent("monitor_touch")
- handleTouch(x, y)
- end
- end
- -- Start the program
- run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement