Add crafting functionality to handle crafting requests and integrate with networked turtle
This commit is contained in:
@@ -2749,6 +2749,25 @@ local function handleSmelterTouch(x, y)
|
||||
smelterPage = smelterPage + 1
|
||||
end
|
||||
smelterNeedsRedraw = true
|
||||
|
||||
elseif action == "craft" then
|
||||
local recipeIdx = data
|
||||
local recipe = CRAFTABLE[recipeIdx]
|
||||
if recipe then
|
||||
local short = recipe.output:gsub("^minecraft:", ""):gsub("_", " ")
|
||||
print(string.format("[CRAFT-UI] Craft request: %s (#%d)", short, recipeIdx))
|
||||
local ok, err = craftItem(recipeIdx)
|
||||
if ok then
|
||||
statusMessage = "Crafted " .. short .. " x" .. recipe.count
|
||||
statusColor = colors.lime
|
||||
else
|
||||
statusMessage = "Craft failed: " .. (err or "unknown")
|
||||
statusColor = colors.red
|
||||
end
|
||||
statusTimer = 5
|
||||
needsRedraw = true
|
||||
smelterNeedsRedraw = true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2778,6 +2797,8 @@ local function broadcastState()
|
||||
smeltingPaused = smeltingPaused,
|
||||
disabledRecipes = disabledRecipes,
|
||||
smeltable = SMELTABLE,
|
||||
craftable = CRAFTABLE,
|
||||
craftTurtleOk = craftTurtleName and peripheral.wrap(craftTurtleName) ~= nil,
|
||||
}
|
||||
networkModem.transmit(BROADCAST_CHANNEL, ORDER_CHANNEL, state)
|
||||
end
|
||||
@@ -2822,6 +2843,7 @@ local function main()
|
||||
networkModem = peripheral.wrap(name)
|
||||
networkModemName = name
|
||||
networkModem.open(ORDER_CHANNEL)
|
||||
networkModem.open(CRAFT_REPLY_CHANNEL)
|
||||
break
|
||||
end
|
||||
end
|
||||
@@ -2831,6 +2853,19 @@ local function main()
|
||||
print("[WARN] No modem found for client sync")
|
||||
end
|
||||
|
||||
-- Detect crafting turtle on network
|
||||
for _, name in ipairs(peripheral.getNames()) do
|
||||
if name:match("^turtle_") then
|
||||
craftTurtleName = name
|
||||
break
|
||||
end
|
||||
end
|
||||
if craftTurtleName then
|
||||
print("[OK] Crafting turtle: " .. craftTurtleName)
|
||||
else
|
||||
print("[WARN] No crafting turtle found")
|
||||
end
|
||||
|
||||
-- Load recipe toggles from disk
|
||||
loadDisabledRecipes()
|
||||
if smeltingPaused then
|
||||
@@ -3132,6 +3167,17 @@ local function main()
|
||||
saveDisabledRecipes()
|
||||
smelterNeedsRedraw = true
|
||||
pcall(broadcastState)
|
||||
elseif message.type == "craft" and message.recipeIdx then
|
||||
print(string.format("[NET] Craft request: recipe #%d", message.recipeIdx))
|
||||
local ok, err = craftItem(message.recipeIdx)
|
||||
networkModem.transmit(replyChannel, ORDER_CHANNEL, {
|
||||
type = "craft_result",
|
||||
success = ok,
|
||||
error = err,
|
||||
})
|
||||
smelterNeedsRedraw = true
|
||||
needsRedraw = true
|
||||
pcall(broadcastState)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user