Allow craft program to craft unlimited items (#807)

This commit is contained in:
Matthew Wilbern 2021-06-05 23:52:01 -06:00 committed by GitHub
parent 0f6db63020
commit f8074636bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 9 deletions

View File

@ -1,5 +1,5 @@
craft is a program for Crafty Turtles. Craft will craft a stack of items using the current inventory.
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

View File

@ -9,20 +9,19 @@ if not turtle.craft then
end
local tArgs = { ... }
local nLimit = nil
if #tArgs < 1 then
local nLimit = tonumber(tArgs[1])
if not nLimit and tArgs[1] ~= "all" then
local programName = arg[0] or fs.getName(shell.getRunningProgram())
print("Usage: " .. programName .. " [number]")
print("Usage: " .. programName .. " all|<number>")
return
else
nLimit = tonumber(tArgs[1])
end
local nCrafted = 0
local nOldCount = turtle.getItemCount(turtle.getSelectedSlot())
if turtle.craft(nLimit) then
local nNewCount = turtle.getItemCount(turtle.getSelectedSlot())
if nOldCount <= nLimit then
if not nLimit or nOldCount <= nLimit then
nCrafted = nNewCount
else
nCrafted = nOldCount - nNewCount

View File

@ -19,7 +19,14 @@ describe("The craft program", function()
stub(_G, "turtle", { craft = function() end })
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)
it("crafts multiple items", function()
@ -52,7 +59,7 @@ describe("The craft program", function()
:matches { ok = true, output = "1 item crafted\n", error = "" }
end)
it("crafts no items", function()
it("crafts no items", function()
local item_count = 2
stub(_G, "turtle", {
craft = function()
@ -66,4 +73,17 @@ describe("The craft program", function()
expect(capture(stub, "/rom/programs/turtle/craft.lua 1"))
:matches { ok = true, output = "No items crafted\n", error = "" }
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)