Allow craft program to craft unlimited items (#807)
This commit is contained in:
parent
0f6db63020
commit
f8074636bc
|
@ -1,5 +1,5 @@
|
||||||
craft is a program for Crafty Turtles. Craft will craft a stack of items using the current inventory.
|
craft is a program for Crafty Turtles. Craft will craft a stack of items using the current inventory.
|
||||||
|
|
||||||
ex:
|
ex:
|
||||||
"craft" will craft as many items as possible
|
"craft all" will craft as many items as possible
|
||||||
"craft 5" will craft at most 5 times
|
"craft 5" will craft at most 5 times
|
||||||
|
|
|
@ -9,20 +9,19 @@ if not turtle.craft then
|
||||||
end
|
end
|
||||||
|
|
||||||
local tArgs = { ... }
|
local tArgs = { ... }
|
||||||
local nLimit = nil
|
local nLimit = tonumber(tArgs[1])
|
||||||
if #tArgs < 1 then
|
|
||||||
|
if not nLimit and tArgs[1] ~= "all" then
|
||||||
local programName = arg[0] or fs.getName(shell.getRunningProgram())
|
local programName = arg[0] or fs.getName(shell.getRunningProgram())
|
||||||
print("Usage: " .. programName .. " [number]")
|
print("Usage: " .. programName .. " all|<number>")
|
||||||
return
|
return
|
||||||
else
|
|
||||||
nLimit = tonumber(tArgs[1])
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local nCrafted = 0
|
local nCrafted = 0
|
||||||
local nOldCount = turtle.getItemCount(turtle.getSelectedSlot())
|
local nOldCount = turtle.getItemCount(turtle.getSelectedSlot())
|
||||||
if turtle.craft(nLimit) then
|
if turtle.craft(nLimit) then
|
||||||
local nNewCount = turtle.getItemCount(turtle.getSelectedSlot())
|
local nNewCount = turtle.getItemCount(turtle.getSelectedSlot())
|
||||||
if nOldCount <= nLimit then
|
if not nLimit or nOldCount <= nLimit then
|
||||||
nCrafted = nNewCount
|
nCrafted = nNewCount
|
||||||
else
|
else
|
||||||
nCrafted = nOldCount - nNewCount
|
nCrafted = nOldCount - nNewCount
|
||||||
|
|
|
@ -19,7 +19,14 @@ describe("The craft program", function()
|
||||||
stub(_G, "turtle", { craft = function() end })
|
stub(_G, "turtle", { craft = function() end })
|
||||||
|
|
||||||
expect(capture(stub, "/rom/programs/turtle/craft.lua"))
|
expect(capture(stub, "/rom/programs/turtle/craft.lua"))
|
||||||
:matches { ok = true, output = "Usage: /rom/programs/turtle/craft.lua [number]\n", error = "" }
|
:matches { ok = true, output = "Usage: /rom/programs/turtle/craft.lua all|<number>\n", error = "" }
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("displays its usage when given incorrect arguments", function()
|
||||||
|
stub(_G, "turtle", { craft = function() end })
|
||||||
|
|
||||||
|
expect(capture(stub, "/rom/programs/turtle/craft.lua a"))
|
||||||
|
:matches { ok = true, output = "Usage: /rom/programs/turtle/craft.lua all|<number>\n", error = "" }
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("crafts multiple items", function()
|
it("crafts multiple items", function()
|
||||||
|
@ -52,7 +59,7 @@ describe("The craft program", function()
|
||||||
:matches { ok = true, output = "1 item crafted\n", error = "" }
|
:matches { ok = true, output = "1 item crafted\n", error = "" }
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("crafts no items", function()
|
it("crafts no items", function()
|
||||||
local item_count = 2
|
local item_count = 2
|
||||||
stub(_G, "turtle", {
|
stub(_G, "turtle", {
|
||||||
craft = function()
|
craft = function()
|
||||||
|
@ -66,4 +73,17 @@ describe("The craft program", function()
|
||||||
expect(capture(stub, "/rom/programs/turtle/craft.lua 1"))
|
expect(capture(stub, "/rom/programs/turtle/craft.lua 1"))
|
||||||
:matches { ok = true, output = "No items crafted\n", error = "" }
|
:matches { ok = true, output = "No items crafted\n", error = "" }
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("crafts all items", function()
|
||||||
|
stub(_G, "turtle", {
|
||||||
|
craft = function()
|
||||||
|
return true
|
||||||
|
end,
|
||||||
|
getItemCount = function() return 17 end,
|
||||||
|
getSelectedSlot = function() return 1 end,
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(capture(stub, "/rom/programs/turtle/craft.lua all"))
|
||||||
|
:matches { ok = true, output = "17 items crafted\n", error = "" }
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
Loading…
Reference in New Issue