From f8074636bccb10bbddd95fc16b5ad32efd38b125 Mon Sep 17 00:00:00 2001 From: Matthew Wilbern <39929480+fatboychummy@users.noreply.github.com> Date: Sat, 5 Jun 2021 23:52:01 -0600 Subject: [PATCH] Allow craft program to craft unlimited items (#807) --- .../data/computercraft/lua/rom/help/craft.txt | 2 +- .../lua/rom/programs/turtle/craft.lua | 11 ++++----- .../spec/programs/turtle/craft_spec.lua | 24 +++++++++++++++++-- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/main/resources/data/computercraft/lua/rom/help/craft.txt b/src/main/resources/data/computercraft/lua/rom/help/craft.txt index de6f6eb60..9f0bd6cf5 100644 --- a/src/main/resources/data/computercraft/lua/rom/help/craft.txt +++ b/src/main/resources/data/computercraft/lua/rom/help/craft.txt @@ -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 diff --git a/src/main/resources/data/computercraft/lua/rom/programs/turtle/craft.lua b/src/main/resources/data/computercraft/lua/rom/programs/turtle/craft.lua index ce9e84dc7..19ab82f45 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/turtle/craft.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/turtle/craft.lua @@ -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|") 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 diff --git a/src/test/resources/test-rom/spec/programs/turtle/craft_spec.lua b/src/test/resources/test-rom/spec/programs/turtle/craft_spec.lua index 8e1f12bb1..93e4c1bb9 100644 --- a/src/test/resources/test-rom/spec/programs/turtle/craft_spec.lua +++ b/src/test/resources/test-rom/spec/programs/turtle/craft_spec.lua @@ -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|\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|\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)